Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 1 | # |
| 2 | # Copyright (c) 2017 Wind River Systems, Inc. |
| 3 | # |
Brad Bishop | c342db3 | 2019-05-15 21:57:59 -0400 | [diff] [blame] | 4 | # SPDX-License-Identifier: MIT |
| 5 | # |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 6 | |
| 7 | import re |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 8 | import tempfile |
| 9 | import time |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 10 | import oe.types |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 11 | from oeqa.core.decorator import OETestTag |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 12 | from oeqa.selftest.case import OESelftestTestCase |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 13 | from oeqa.utils.commands import bitbake, runqemu, get_bb_var, runCmd |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 14 | |
| 15 | class RunqemuTests(OESelftestTestCase): |
| 16 | """Runqemu test class""" |
| 17 | |
| 18 | image_is_ready = False |
| 19 | deploy_dir_image = '' |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 20 | |
| 21 | def setUpLocal(self): |
| 22 | super(RunqemuTests, self).setUpLocal() |
| 23 | self.recipe = 'core-image-minimal' |
| 24 | self.machine = 'qemux86-64' |
| 25 | self.fstypes = "ext4 iso hddimg wic.vmdk wic.qcow2 wic.vdi" |
| 26 | self.cmd_common = "runqemu nographic" |
| 27 | |
Brad Bishop | 977dc1a | 2019-02-06 16:01:43 -0500 | [diff] [blame] | 28 | kvm = oe.types.qemu_use_kvm(get_bb_var('QEMU_USE_KVM'), 'x86_64') |
| 29 | if kvm: |
| 30 | self.cmd_common += " kvm" |
| 31 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 32 | self.write_config( |
| 33 | """ |
| 34 | MACHINE = "%s" |
| 35 | IMAGE_FSTYPES = "%s" |
| 36 | # 10 means 1 second |
| 37 | SYSLINUX_TIMEOUT = "10" |
| 38 | """ |
| 39 | % (self.machine, self.fstypes) |
| 40 | ) |
| 41 | |
| 42 | if not RunqemuTests.image_is_ready: |
| 43 | RunqemuTests.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE') |
| 44 | bitbake(self.recipe) |
| 45 | RunqemuTests.image_is_ready = True |
| 46 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 47 | def test_boot_machine(self): |
| 48 | """Test runqemu machine""" |
| 49 | cmd = "%s %s" % (self.cmd_common, self.machine) |
| 50 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 51 | with open(qemu.qemurunnerlog) as f: |
| 52 | self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read())) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 53 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 54 | def test_boot_machine_ext4(self): |
| 55 | """Test runqemu machine ext4""" |
| 56 | cmd = "%s %s ext4" % (self.cmd_common, self.machine) |
| 57 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
| 58 | with open(qemu.qemurunnerlog) as f: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 59 | self.assertIn('rootfs.ext4', f.read(), "Failed: %s" % cmd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 60 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 61 | def test_boot_machine_iso(self): |
| 62 | """Test runqemu machine iso""" |
| 63 | cmd = "%s %s iso" % (self.cmd_common, self.machine) |
| 64 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
| 65 | with open(qemu.qemurunnerlog) as f: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 66 | self.assertIn('media=cdrom', f.read(), "Failed: %s" % cmd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 67 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 68 | def test_boot_recipe_image(self): |
| 69 | """Test runqemu recipe-image""" |
| 70 | cmd = "%s %s" % (self.cmd_common, self.recipe) |
| 71 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 72 | with open(qemu.qemurunnerlog) as f: |
| 73 | self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read())) |
| 74 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 75 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 76 | def test_boot_recipe_image_vmdk(self): |
| 77 | """Test runqemu recipe-image vmdk""" |
| 78 | cmd = "%s %s wic.vmdk" % (self.cmd_common, self.recipe) |
| 79 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
| 80 | with open(qemu.qemurunnerlog) as f: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 81 | self.assertIn('format=vmdk', f.read(), "Failed: %s" % cmd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 82 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 83 | def test_boot_recipe_image_vdi(self): |
| 84 | """Test runqemu recipe-image vdi""" |
| 85 | cmd = "%s %s wic.vdi" % (self.cmd_common, self.recipe) |
| 86 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
| 87 | with open(qemu.qemurunnerlog) as f: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 88 | self.assertIn('format=vdi', f.read(), "Failed: %s" % cmd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 89 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 90 | def test_boot_deploy(self): |
| 91 | """Test runqemu deploy_dir_image""" |
| 92 | cmd = "%s %s" % (self.cmd_common, self.deploy_dir_image) |
| 93 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 94 | with open(qemu.qemurunnerlog) as f: |
| 95 | self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read())) |
| 96 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 97 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 98 | def test_boot_deploy_hddimg(self): |
| 99 | """Test runqemu deploy_dir_image hddimg""" |
| 100 | cmd = "%s %s hddimg" % (self.cmd_common, self.deploy_dir_image) |
| 101 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
| 102 | with open(qemu.qemurunnerlog) as f: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 103 | self.assertTrue(re.search('file=.*.hddimg', f.read()), "Failed: %s, %s" % (cmd, f.read())) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 104 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 105 | def test_boot_machine_slirp(self): |
| 106 | """Test runqemu machine slirp""" |
| 107 | cmd = "%s slirp %s" % (self.cmd_common, self.machine) |
| 108 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
| 109 | with open(qemu.qemurunnerlog) as f: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 110 | self.assertIn(' -netdev user', f.read(), "Failed: %s" % cmd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 111 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 112 | def test_boot_machine_slirp_qcow2(self): |
| 113 | """Test runqemu machine slirp qcow2""" |
| 114 | cmd = "%s slirp wic.qcow2 %s" % (self.cmd_common, self.machine) |
| 115 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
| 116 | with open(qemu.qemurunnerlog) as f: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 117 | self.assertIn('format=qcow2', f.read(), "Failed: %s" % cmd) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 118 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 119 | def test_boot_qemu_boot(self): |
| 120 | """Test runqemu /path/to/image.qemuboot.conf""" |
| 121 | qemuboot_conf = "%s-%s.qemuboot.conf" % (self.recipe, self.machine) |
| 122 | qemuboot_conf = os.path.join(self.deploy_dir_image, qemuboot_conf) |
| 123 | if not os.path.exists(qemuboot_conf): |
| 124 | self.skipTest("%s not found" % qemuboot_conf) |
| 125 | cmd = "%s %s" % (self.cmd_common, qemuboot_conf) |
| 126 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 127 | with open(qemu.qemurunnerlog) as f: |
| 128 | self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read())) |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 129 | |
Brad Bishop | d7bf8c1 | 2018-02-25 22:55:05 -0500 | [diff] [blame] | 130 | def test_boot_rootfs(self): |
| 131 | """Test runqemu /path/to/rootfs.ext4""" |
| 132 | rootfs = "%s-%s.ext4" % (self.recipe, self.machine) |
| 133 | rootfs = os.path.join(self.deploy_dir_image, rootfs) |
| 134 | if not os.path.exists(rootfs): |
| 135 | self.skipTest("%s not found" % rootfs) |
| 136 | cmd = "%s %s" % (self.cmd_common, rootfs) |
| 137 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
Brad Bishop | f86d055 | 2018-12-04 14:18:15 -0800 | [diff] [blame] | 138 | with open(qemu.qemurunnerlog) as f: |
| 139 | self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read())) |
| 140 | |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 141 | |
| 142 | # This test was designed as a separate class to test that shutdown |
| 143 | # command will shutdown qemu as expected on each qemu architecture |
| 144 | # based on the MACHINE configuration inside the config file |
| 145 | # (eg. local.conf). |
| 146 | # |
| 147 | # This was different compared to RunqemuTests, where RunqemuTests was |
| 148 | # dedicated for MACHINE=qemux86-64 where it test that qemux86-64 will |
| 149 | # bootup various filesystem types, including live image(iso and hddimg) |
| 150 | # where live image was not supported on all qemu architecture. |
Brad Bishop | 79641f2 | 2019-09-10 07:20:22 -0400 | [diff] [blame] | 151 | @OETestTag("machine") |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 152 | class QemuTest(OESelftestTestCase): |
| 153 | |
| 154 | @classmethod |
| 155 | def setUpClass(cls): |
| 156 | super(QemuTest, cls).setUpClass() |
| 157 | cls.recipe = 'core-image-minimal' |
| 158 | cls.machine = get_bb_var('MACHINE') |
| 159 | cls.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE') |
| 160 | cls.cmd_common = "runqemu nographic" |
| 161 | cls.qemuboot_conf = "%s-%s.qemuboot.conf" % (cls.recipe, cls.machine) |
| 162 | cls.qemuboot_conf = os.path.join(cls.deploy_dir_image, cls.qemuboot_conf) |
| 163 | bitbake(cls.recipe) |
| 164 | |
| 165 | def _start_qemu_shutdown_check_if_shutdown_succeeded(self, qemu, timeout): |
| 166 | qemu.run_serial("shutdown -h now") |
| 167 | # Stop thread will stop the LoggingThread instance used for logging |
| 168 | # qemu through serial console, stop thread will prevent this code |
| 169 | # from facing exception (Console connection closed unexpectedly) |
| 170 | # when qemu was shutdown by the above shutdown command |
| 171 | qemu.runner.stop_thread() |
| 172 | time_track = 0 |
Brad Bishop | 1a4b7ee | 2018-12-16 17:11:34 -0800 | [diff] [blame] | 173 | try: |
| 174 | while True: |
| 175 | is_alive = qemu.check() |
| 176 | if not is_alive: |
| 177 | return True |
| 178 | if time_track > timeout: |
| 179 | return False |
| 180 | time.sleep(1) |
| 181 | time_track += 1 |
| 182 | except SystemExit: |
| 183 | return True |
Brad Bishop | 316dfdd | 2018-06-25 12:45:53 -0400 | [diff] [blame] | 184 | |
| 185 | def test_qemu_can_shutdown(self): |
| 186 | self.assertExists(self.qemuboot_conf) |
| 187 | cmd = "%s %s" % (self.cmd_common, self.qemuboot_conf) |
| 188 | shutdown_timeout = 120 |
| 189 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
| 190 | qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout) |
| 191 | self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout)) |
| 192 | |
| 193 | # Need to have portmap/rpcbind running to allow this test to work and |
| 194 | # current autobuilder setup does not have this. |
| 195 | def disabled_test_qemu_can_boot_nfs_and_shutdown(self): |
| 196 | self.assertExists(self.qemuboot_conf) |
| 197 | bitbake('meta-ide-support') |
| 198 | rootfs_tar = "%s-%s.tar.bz2" % (self.recipe, self.machine) |
| 199 | rootfs_tar = os.path.join(self.deploy_dir_image, rootfs_tar) |
| 200 | self.assertExists(rootfs_tar) |
| 201 | tmpdir = tempfile.mkdtemp(prefix='qemu_nfs') |
| 202 | tmpdir_nfs = os.path.join(tmpdir, 'nfs') |
| 203 | cmd_extract_nfs = 'runqemu-extract-sdk %s %s' % (rootfs_tar, tmpdir_nfs) |
| 204 | result = runCmd(cmd_extract_nfs) |
| 205 | self.assertEqual(0, result.status, "runqemu-extract-sdk didn't run as expected. %s" % result.output) |
| 206 | cmd = "%s nfs %s %s" % (self.cmd_common, self.qemuboot_conf, tmpdir_nfs) |
| 207 | shutdown_timeout = 120 |
| 208 | with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu: |
| 209 | qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout) |
| 210 | self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout)) |
| 211 | runCmd('rm -rf %s' % tmpdir) |