blob: 58a4526df64dc5764662aaa1a2c81d544286c7f2 [file] [log] [blame]
Brad Bishopd7bf8c12018-02-25 22:55:05 -05001#
2# Copyright (c) 2017 Wind River Systems, Inc.
3#
Brad Bishopc342db32019-05-15 21:57:59 -04004# SPDX-License-Identifier: MIT
5#
Brad Bishopd7bf8c12018-02-25 22:55:05 -05006
7import re
Brad Bishop316dfdd2018-06-25 12:45:53 -04008import tempfile
9import time
Brad Bishop977dc1a2019-02-06 16:01:43 -050010import oe.types
Brad Bishop79641f22019-09-10 07:20:22 -040011from oeqa.core.decorator import OETestTag
Patrick Williams7784c422022-11-17 07:29:11 -060012from oeqa.core.decorator.data import skipIfNotArch, skipIfNotMachine
Brad Bishopd7bf8c12018-02-25 22:55:05 -050013from oeqa.selftest.case import OESelftestTestCase
Brad Bishop316dfdd2018-06-25 12:45:53 -040014from oeqa.utils.commands import bitbake, runqemu, get_bb_var, runCmd
Brad Bishopd7bf8c12018-02-25 22:55:05 -050015
Patrick Williams45852732022-04-02 08:58:32 -050016@OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050017class RunqemuTests(OESelftestTestCase):
18 """Runqemu test class"""
19
20 image_is_ready = False
21 deploy_dir_image = ''
Brad Bishopd7bf8c12018-02-25 22:55:05 -050022
23 def setUpLocal(self):
24 super(RunqemuTests, self).setUpLocal()
25 self.recipe = 'core-image-minimal'
Patrick Williams7784c422022-11-17 07:29:11 -060026 self.machine = self.td['MACHINE']
Brad Bishopd7bf8c12018-02-25 22:55:05 -050027
Patrick Williams7784c422022-11-17 07:29:11 -060028 self.fstypes = "ext4"
29 if self.td["HOST_ARCH"] in ('i586', 'i686', 'x86_64'):
30 self.fstypes += " iso hddimg"
31 if self.machine == "qemux86-64":
32 self.fstypes += " wic.vmdk wic.qcow2 wic.vdi"
33
34 self.cmd_common = "runqemu nographic"
35 kvm = oe.types.qemu_use_kvm(get_bb_var('QEMU_USE_KVM'), self.td["TARGET_ARCH"])
Brad Bishop977dc1a2019-02-06 16:01:43 -050036 if kvm:
37 self.cmd_common += " kvm"
38
Brad Bishopd7bf8c12018-02-25 22:55:05 -050039 self.write_config(
40"""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050041IMAGE_FSTYPES = "%s"
42# 10 means 1 second
43SYSLINUX_TIMEOUT = "10"
Patrick Williams7784c422022-11-17 07:29:11 -060044""" % self.fstypes)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050045
46 if not RunqemuTests.image_is_ready:
47 RunqemuTests.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
48 bitbake(self.recipe)
49 RunqemuTests.image_is_ready = True
50
Brad Bishopd7bf8c12018-02-25 22:55:05 -050051 def test_boot_machine(self):
52 """Test runqemu machine"""
53 cmd = "%s %s" % (self.cmd_common, self.machine)
54 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080055 with open(qemu.qemurunnerlog) as f:
56 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050057
Brad Bishopd7bf8c12018-02-25 22:55:05 -050058 def test_boot_machine_ext4(self):
59 """Test runqemu machine ext4"""
60 cmd = "%s %s ext4" % (self.cmd_common, self.machine)
61 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
62 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080063 self.assertIn('rootfs.ext4', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050064
Patrick Williams7784c422022-11-17 07:29:11 -060065 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -050066 def test_boot_machine_iso(self):
67 """Test runqemu machine iso"""
68 cmd = "%s %s iso" % (self.cmd_common, self.machine)
69 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
70 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080071 self.assertIn('media=cdrom', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050072
Brad Bishopd7bf8c12018-02-25 22:55:05 -050073 def test_boot_recipe_image(self):
74 """Test runqemu recipe-image"""
75 cmd = "%s %s" % (self.cmd_common, self.recipe)
76 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080077 with open(qemu.qemurunnerlog) as f:
78 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
79
Patrick Williams7784c422022-11-17 07:29:11 -060080 # https://bugzilla.yoctoproject.org/show_bug.cgi?id=14963
81 @skipIfNotMachine("qemux86-64", "tests are qemux86-64 specific currently")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050082 def test_boot_recipe_image_vmdk(self):
83 """Test runqemu recipe-image vmdk"""
84 cmd = "%s %s wic.vmdk" % (self.cmd_common, self.recipe)
85 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
86 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080087 self.assertIn('format=vmdk', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050088
Patrick Williams7784c422022-11-17 07:29:11 -060089 @skipIfNotMachine("qemux86-64", "tests are qemux86-64 specific currently")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090 def test_boot_recipe_image_vdi(self):
91 """Test runqemu recipe-image vdi"""
92 cmd = "%s %s wic.vdi" % (self.cmd_common, self.recipe)
93 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
94 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080095 self.assertIn('format=vdi', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050096
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097 def test_boot_deploy(self):
98 """Test runqemu deploy_dir_image"""
99 cmd = "%s %s" % (self.cmd_common, self.deploy_dir_image)
100 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800101 with open(qemu.qemurunnerlog) as f:
102 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
103
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500104
Patrick Williams7784c422022-11-17 07:29:11 -0600105 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500106 def test_boot_deploy_hddimg(self):
107 """Test runqemu deploy_dir_image hddimg"""
108 cmd = "%s %s hddimg" % (self.cmd_common, self.deploy_dir_image)
109 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
110 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800111 self.assertTrue(re.search('file=.*.hddimg', f.read()), "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500113 def test_boot_machine_slirp(self):
114 """Test runqemu machine slirp"""
115 cmd = "%s slirp %s" % (self.cmd_common, self.machine)
116 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
117 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800118 self.assertIn(' -netdev user', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119
Patrick Williams7784c422022-11-17 07:29:11 -0600120 @skipIfNotMachine("qemux86-64", "tests are qemux86-64 specific currently")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500121 def test_boot_machine_slirp_qcow2(self):
122 """Test runqemu machine slirp qcow2"""
123 cmd = "%s slirp wic.qcow2 %s" % (self.cmd_common, self.machine)
124 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
125 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800126 self.assertIn('format=qcow2', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500127
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128 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 Bishopf86d0552018-12-04 14:18:15 -0800136 with open(qemu.qemurunnerlog) as f:
137 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500138
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500139 def test_boot_rootfs(self):
140 """Test runqemu /path/to/rootfs.ext4"""
141 rootfs = "%s-%s.ext4" % (self.recipe, self.machine)
142 rootfs = os.path.join(self.deploy_dir_image, rootfs)
143 if not os.path.exists(rootfs):
144 self.skipTest("%s not found" % rootfs)
145 cmd = "%s %s" % (self.cmd_common, rootfs)
146 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800147 with open(qemu.qemurunnerlog) as f:
148 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
149
Brad Bishop316dfdd2018-06-25 12:45:53 -0400150
151# This test was designed as a separate class to test that shutdown
152# command will shutdown qemu as expected on each qemu architecture
153# based on the MACHINE configuration inside the config file
154# (eg. local.conf).
155#
156# This was different compared to RunqemuTests, where RunqemuTests was
157# dedicated for MACHINE=qemux86-64 where it test that qemux86-64 will
158# bootup various filesystem types, including live image(iso and hddimg)
159# where live image was not supported on all qemu architecture.
Brad Bishop79641f22019-09-10 07:20:22 -0400160@OETestTag("machine")
Patrick Williams45852732022-04-02 08:58:32 -0500161@OETestTag("runqemu")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400162class QemuTest(OESelftestTestCase):
163
164 @classmethod
165 def setUpClass(cls):
166 super(QemuTest, cls).setUpClass()
167 cls.recipe = 'core-image-minimal'
168 cls.machine = get_bb_var('MACHINE')
169 cls.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
170 cls.cmd_common = "runqemu nographic"
171 cls.qemuboot_conf = "%s-%s.qemuboot.conf" % (cls.recipe, cls.machine)
172 cls.qemuboot_conf = os.path.join(cls.deploy_dir_image, cls.qemuboot_conf)
173 bitbake(cls.recipe)
174
175 def _start_qemu_shutdown_check_if_shutdown_succeeded(self, qemu, timeout):
Andrew Geisslerc926e172021-05-07 16:11:35 -0500176 # Allow the runner's LoggingThread instance to exit without errors
177 # (such as the exception "Console connection closed unexpectedly")
178 # as qemu will disappear when we shut it down
179 qemu.runner.allowexit()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400180 qemu.run_serial("shutdown -h now")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400181 time_track = 0
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800182 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 Bishop316dfdd2018-06-25 12:45:53 -0400193
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)