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