From f68297097e5f043191b52302fed17f7ff87ce972 Mon Sep 17 00:00:00 2001 From: Nikolay Edigaryev Date: Wed, 16 Aug 2023 12:04:52 +0400 Subject: [PATCH] Add a simple integration test for "tart run" (#587) * Add a simple integration test for "tart run" * Integration tests: only "tart list" local VM images --- integration-tests/requirements.txt | 1 + integration-tests/tart.py | 13 +++++++++---- integration-tests/test_clone.py | 2 +- integration-tests/test_create.py | 4 ++-- integration-tests/test_delete.py | 4 ++-- integration-tests/test_rename.py | 2 +- integration-tests/test_run.py | 30 ++++++++++++++++++++++++++++++ 7 files changed, 46 insertions(+), 10 deletions(-) create mode 100644 integration-tests/test_run.py diff --git a/integration-tests/requirements.txt b/integration-tests/requirements.txt index 7e6a7dc..c04db34 100644 --- a/integration-tests/requirements.txt +++ b/integration-tests/requirements.txt @@ -3,3 +3,4 @@ testcontainers requests bitmath pytest-dependency +paramiko diff --git a/integration-tests/tart.py b/integration-tests/tart.py index b26a263..752b33a 100644 --- a/integration-tests/tart.py +++ b/integration-tests/tart.py @@ -7,10 +7,9 @@ class Tart: def __init__(self): self.tmp_dir = tempfile.TemporaryDirectory(dir=os.environ.get("CIRRUS_WORKING_DIR")) - # Link to the users IPSW cache to make things faster - src = os.path.join(os.path.expanduser("~"), ".tart", "cache", "IPSWs") - dst = os.path.join(self.tmp_dir.name, "cache", "IPSWs") - os.makedirs(os.path.join(self.tmp_dir.name, "cache")) + # Link to the users cache to make things faster + src = os.path.join(os.path.expanduser("~"), ".tart", "cache") + dst = os.path.join(self.tmp_dir.name, "cache") os.symlink(src, dst) def __enter__(self): @@ -31,3 +30,9 @@ class Tart: completed_process.check_returncode() return completed_process.stdout.decode("utf-8"), completed_process.stderr.decode("utf-8") + + def run_async(self, args) -> subprocess.Popen: + env = os.environ.copy() + env.update({"TART_HOME": self.tmp_dir.name}) + + return subprocess.Popen(["tart"] + args, env=env) diff --git a/integration-tests/test_clone.py b/integration-tests/test_clone.py index 2519bc7..62ad9d4 100644 --- a/integration-tests/test_clone.py +++ b/integration-tests/test_clone.py @@ -6,5 +6,5 @@ def test_clone(tart): tart.run(["clone", "debian", "ubuntu"]) # Ensure that we have now 2 VMs - stdout, _, = tart.run(["list", "--quiet"]) + stdout, _, = tart.run(["list", "--source", "local", "--quiet"]) assert stdout == "debian\nubuntu\n" diff --git a/integration-tests/test_create.py b/integration-tests/test_create.py index 311e80b..866b7b6 100644 --- a/integration-tests/test_create.py +++ b/integration-tests/test_create.py @@ -3,7 +3,7 @@ def test_create_macos(tart): tart.run(["create", "--from-ipsw", "latest", "macos-vm"]) # Ensure that the VM was created - stdout, _ = tart.run(["list", "--quiet"]) + stdout, _ = tart.run(["list", "--source", "local", "--quiet"]) assert stdout == "macos-vm\n" @@ -12,5 +12,5 @@ def test_create_linux(tart): tart.run(["create", "--linux", "linux-vm"]) # Ensure that the VM was created - stdout, _ = tart.run(["list", "--quiet"]) + stdout, _ = tart.run(["list", "--source", "local", "--quiet"]) assert stdout == "linux-vm\n" diff --git a/integration-tests/test_delete.py b/integration-tests/test_delete.py index 760d292..1a8ccb3 100644 --- a/integration-tests/test_delete.py +++ b/integration-tests/test_delete.py @@ -3,12 +3,12 @@ def test_delete(tart): tart.run(["create", "--linux", "debian"]) # Ensure that the VM exists - stdout, _, = tart.run(["list", "--quiet"]) + stdout, _, = tart.run(["list", "--source", "local", "--quiet"]) assert stdout == "debian\n" # Delete the VM tart.run(["delete", "debian"]) # Ensure that the VM was removed - stdout, _, = tart.run(["list", "--quiet"]) + stdout, _, = tart.run(["list", "--source", "local", "--quiet"]) assert stdout == "" diff --git a/integration-tests/test_rename.py b/integration-tests/test_rename.py index 6eb709f..1ce8ffd 100644 --- a/integration-tests/test_rename.py +++ b/integration-tests/test_rename.py @@ -6,5 +6,5 @@ def test_rename(tart): tart.run(["rename", "debian", "ubuntu"]) # Ensure that the VM is now named "ubuntu" - stdout, _, = tart.run(["list", "--quiet"]) + stdout, _, = tart.run(["list", "--source", "local", "--quiet"]) assert stdout == "ubuntu\n" diff --git a/integration-tests/test_run.py b/integration-tests/test_run.py new file mode 100644 index 0000000..3a259c2 --- /dev/null +++ b/integration-tests/test_run.py @@ -0,0 +1,30 @@ +import uuid + +from paramiko.client import SSHClient, AutoAddPolicy + + +def test_run(tart): + vm_name = f"integration-test-run-{uuid.uuid4()}" + + # Instantiate a VM with admin:admin SSH access + tart.run(["clone", "ghcr.io/cirruslabs/macos-ventura-base:latest", vm_name]) + + # Run the VM asynchronously + tart_run_process = tart.run_async(["run", vm_name]) + + # Obtain the VM's IP + stdout, _ = tart.run(["ip", vm_name, "--wait", "120"]) + ip = stdout.strip() + + # Connect to the VM over SSH and shutdown it + client = SSHClient() + client.set_missing_host_key_policy(AutoAddPolicy) + client.connect(ip, username="admin", password="admin") + client.exec_command("sudo shutdown -h now") + + # Wait for the "tart run" to finish successfully + tart_run_process.wait() + assert tart_run_process.returncode == 0 + + # Delete the VM + _, _ = tart.run(["delete", vm_name])