diff --git a/test/check-cli b/test/check-cli index b0ee1c41..173c0dde 100755 --- a/test/check-cli +++ b/test/check-cli @@ -1,5 +1,6 @@ #!/usr/bin/python3 +import tempfile import unittest import composertest @@ -30,15 +31,47 @@ class TestImages(composertest.ComposerTestCase): @unittest.skip('Nested KVM seems to be buggy and software emulation is too slow') class TestQcow2(composertest.ComposerTestCase): + def tearDown(self): + super().tearDownTestMachine() + def test_qcow2(self): self.runCliTest("/tests/cli/test_compose_qcow2.sh") + with tempfile.TemporaryDirectory(prefix="/var/tmp/lorax-test.") as tmpdir: + # Copy the resulting qcow2 image and shut down the VM + self.tearDownVirt(virt_dir="/var/tmp/test-results/*", local_dir=tmpdir) + + # Boot the image + self.setUpTestMachine(tmpdir + "/disk.qcow2", tmpdir + "/id_rsa") + + # Upload the contents of the ./tests/ directory to the machine + self.machine.upload(["../tests"], "/") + + # Run the test, on the booted image + self.runImageTest("/tests/cli/test_boot_qcow2.sh") + @unittest.skip('Nested KVM seems to be buggy and software emulation is too slow') class TestLiveIso(composertest.ComposerTestCase): + def tearDown(self): + super().tearDownTestMachine() + def test_live_iso(self): self.runCliTest("/tests/cli/test_compose_live-iso.sh") + with tempfile.TemporaryDirectory(prefix="/var/tmp/lorax-test.") as tmpdir: + # Copy the resulting iso and shut down the VM + self.tearDownVirt(virt_dir="/var/tmp/test-results/*", local_dir=tmpdir) + + # Boot the image + self.setUpTestMachine(tmpdir + "/live.iso") + + # Upload the contents of the ./tests/ directory to the machine + self.machine.upload(["../tests"], "/") + + # Run the test, on the booted image + self.runImageTest("/tests/cli/test_boot_live-iso.sh") + class TestTar(composertest.ComposerTestCase): def test_tar(self): diff --git a/test/composertest.py b/test/composertest.py index fae996e1..f065bf5e 100644 --- a/test/composertest.py +++ b/test/composertest.py @@ -23,15 +23,20 @@ def print_exception(etype, value, tb): traceback.print_exception(etype, value, tb, limit=limit) -class ComposerTestCase(unittest.TestCase): - image = testvm.DEFAULT_IMAGE +class VirtMachineTestCase(unittest.TestCase): sit = False + network = None + machine = None + ssh_command = None - def setUp(self): + def setUpTestMachine(self, image, identity_file=None): self.network = testvm.VirtNetwork(0) - self.machine = testvm.VirtMachine(self.image, networking=self.network.host(), memory_mb=2048) + if identity_file: + self.machine = testvm.VirtMachine(image, networking=self.network.host(), cpus=2, memory_mb=2048, identity_file=identity_file) + else: + self.machine = testvm.VirtMachine(image, networking=self.network.host(), cpus=2, memory_mb=2048) - print(f"Starting virtual machine '{self.image}'") + print("Starting virtual machine '{}'".format(image)) self.machine.start() self.machine.wait_boot() @@ -46,15 +51,7 @@ class ComposerTestCase(unittest.TestCase): print(" ".join(self.ssh_command)) print() - print("Waiting for lorax-composer to become ready...") - curl_command = ["curl", "--max-time", "360", - "--silent", - "--unix-socket", "/run/weldr/api.socket", - "http://localhost/api/status"] - r = subprocess.run(self.ssh_command + curl_command, stdout=subprocess.DEVNULL) - self.assertEqual(r.returncode, 0) - - def tearDown(self): + def tearDownTestMachine(self): if os.environ.get('TEST_ATTACHMENTS'): self.machine.download_dir('/var/log/tests', os.environ.get('TEST_ATTACHMENTS')) @@ -79,6 +76,35 @@ class ComposerTestCase(unittest.TestCase): """ return subprocess.run(self.ssh_command + command, **args) + +class ComposerTestCase(VirtMachineTestCase): + def setUp(self): + self.setUpTestMachine(testvm.DEFAULT_IMAGE) + + # Upload the contents of the ./tests/ directory to the machine (it must have beakerlib already installed) + self.machine.upload(["../tests"], "/") + + print("Waiting for lorax-composer to become ready...") + curl_command = ["curl", "--max-time", "360", + "--silent", + "--unix-socket", "/run/weldr/api.socket", + "http://localhost/api/status"] + r = subprocess.run(self.ssh_command + curl_command, stdout=subprocess.DEVNULL) + self.assertEqual(r.returncode, 0) + + def tearDown(self): + self.tearDownVirt() + + def tearDownVirt(self, virt_dir=None, local_dir=None): + if os.environ.get('TEST_ATTACHMENTS'): + self.machine.download_dir('/var/log/tests', os.environ.get('TEST_ATTACHMENTS')) + + if virt_dir and local_dir: + self.machine.download_dir(virt_dir, local_dir) + + self.tearDownTestMachine() + return local_dir + def runCliTest(self, script): extra_env = [] if self.sit: @@ -91,6 +117,16 @@ class ComposerTestCase(unittest.TestCase): "/tests/test_cli.sh", script]) self.assertEqual(r.returncode, 0) + def runImageTest(self, script): + extra_env = [] + if self.sit: + extra_env.append("COMPOSER_TEST_FAIL_FAST=1") + + r = self.execute(["TEST=" + self.id(), + *extra_env, + "/tests/test_image.sh", script]) + self.assertEqual(r.returncode, 0) + def print_tests(tests): for test in tests: