From c1609d7ffe0d3d97f60bb2a94109950069f682b8 Mon Sep 17 00:00:00 2001 From: "Brian C. Lane" Date: Tue, 15 Oct 2019 16:46:50 -0700 Subject: [PATCH] test: Split up the test class to allow booting other images This splits out the lorax-composer specific execution so that the built image can be downloaded from the build vm and booted by the host instead of using nested virt to try and boot it inside the build vm. Also adds copying the ssh key from the build vm so that it can log into the image and run the test_boot_* scripts. Cherry-picked from df7a018ee247bfef8046269a37411951a448a5a8 Related: rhbz#1770193 --- test/check-cli | 33 +++++++++++++++++++++++ test/composertest.py | 64 ++++++++++++++++++++++++++++++++++---------- 2 files changed, 83 insertions(+), 14 deletions(-) diff --git a/test/check-cli b/test/check-cli index 30b387ff..5b01f66f 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('Disabled due to issues with nested KVM') 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('Disabled due to issues with nested KVM') 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 df34dbce..b96cada4 100755 --- a/test/composertest.py +++ b/test/composertest.py @@ -22,15 +22,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=3072) + 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("Starting virtual machine '{}'".format(self.image)) + print("Starting virtual machine '{}'".format(image)) self.machine.start() self.machine.wait_boot() @@ -45,15 +50,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')) @@ -78,6 +75,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: @@ -90,6 +116,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) + class ComposerTestResult(unittest.TestResult): def name(self, test):