blob: e72ff529c43041159761bacb1231cc01717a65c7 [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']
Andrew Geisslerfc113ea2023-03-31 09:59:46 -050027 self.image_link_name = get_bb_var('IMAGE_LINK_NAME', self.recipe)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050028
Patrick Williams7784c422022-11-17 07:29:11 -060029 self.fstypes = "ext4"
30 if self.td["HOST_ARCH"] in ('i586', 'i686', 'x86_64'):
31 self.fstypes += " iso hddimg"
32 if self.machine == "qemux86-64":
33 self.fstypes += " wic.vmdk wic.qcow2 wic.vdi"
34
35 self.cmd_common = "runqemu nographic"
36 kvm = oe.types.qemu_use_kvm(get_bb_var('QEMU_USE_KVM'), self.td["TARGET_ARCH"])
Brad Bishop977dc1a2019-02-06 16:01:43 -050037 if kvm:
38 self.cmd_common += " kvm"
39
Brad Bishopd7bf8c12018-02-25 22:55:05 -050040 self.write_config(
41"""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050042IMAGE_FSTYPES = "%s"
43# 10 means 1 second
44SYSLINUX_TIMEOUT = "10"
Patrick Williams7784c422022-11-17 07:29:11 -060045""" % self.fstypes)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050046
47 if not RunqemuTests.image_is_ready:
48 RunqemuTests.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
49 bitbake(self.recipe)
50 RunqemuTests.image_is_ready = True
51
Brad Bishopd7bf8c12018-02-25 22:55:05 -050052 def test_boot_machine(self):
53 """Test runqemu machine"""
54 cmd = "%s %s" % (self.cmd_common, self.machine)
55 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080056 with open(qemu.qemurunnerlog) as f:
57 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050058
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059 def test_boot_machine_ext4(self):
60 """Test runqemu machine ext4"""
61 cmd = "%s %s ext4" % (self.cmd_common, self.machine)
62 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
63 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080064 self.assertIn('rootfs.ext4', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050065
Patrick Williams7784c422022-11-17 07:29:11 -060066 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -050067 def test_boot_machine_iso(self):
68 """Test runqemu machine iso"""
69 cmd = "%s %s iso" % (self.cmd_common, self.machine)
70 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
71 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080072 self.assertIn('media=cdrom', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050073
Brad Bishopd7bf8c12018-02-25 22:55:05 -050074 def test_boot_recipe_image(self):
75 """Test runqemu recipe-image"""
76 cmd = "%s %s" % (self.cmd_common, self.recipe)
77 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080078 with open(qemu.qemurunnerlog) as f:
79 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
80
Patrick Williams7784c422022-11-17 07:29:11 -060081 # https://bugzilla.yoctoproject.org/show_bug.cgi?id=14963
82 @skipIfNotMachine("qemux86-64", "tests are qemux86-64 specific currently")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050083 def test_boot_recipe_image_vmdk(self):
84 """Test runqemu recipe-image vmdk"""
85 cmd = "%s %s wic.vmdk" % (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 Bishopf86d0552018-12-04 14:18:15 -080088 self.assertIn('format=vmdk', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050089
Patrick Williams7784c422022-11-17 07:29:11 -060090 @skipIfNotMachine("qemux86-64", "tests are qemux86-64 specific currently")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050091 def test_boot_recipe_image_vdi(self):
92 """Test runqemu recipe-image vdi"""
93 cmd = "%s %s wic.vdi" % (self.cmd_common, self.recipe)
94 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
95 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080096 self.assertIn('format=vdi', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097
Brad Bishopd7bf8c12018-02-25 22:55:05 -050098 def test_boot_deploy(self):
99 """Test runqemu deploy_dir_image"""
100 cmd = "%s %s" % (self.cmd_common, self.deploy_dir_image)
101 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800102 with open(qemu.qemurunnerlog) as f:
103 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
104
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105
Patrick Williams7784c422022-11-17 07:29:11 -0600106 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500107 def test_boot_deploy_hddimg(self):
108 """Test runqemu deploy_dir_image hddimg"""
109 cmd = "%s %s hddimg" % (self.cmd_common, self.deploy_dir_image)
110 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
111 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800112 self.assertTrue(re.search('file=.*.hddimg', f.read()), "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500113
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500114 def test_boot_machine_slirp(self):
115 """Test runqemu machine slirp"""
116 cmd = "%s slirp %s" % (self.cmd_common, self.machine)
117 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
118 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800119 self.assertIn(' -netdev user', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120
Patrick Williams7784c422022-11-17 07:29:11 -0600121 @skipIfNotMachine("qemux86-64", "tests are qemux86-64 specific currently")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500122 def test_boot_machine_slirp_qcow2(self):
123 """Test runqemu machine slirp qcow2"""
124 cmd = "%s slirp wic.qcow2 %s" % (self.cmd_common, self.machine)
125 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
126 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800127 self.assertIn('format=qcow2', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500128
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500129 def test_boot_qemu_boot(self):
130 """Test runqemu /path/to/image.qemuboot.conf"""
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500131 qemuboot_conf = "%s.qemuboot.conf" % (self.image_link_name)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132 qemuboot_conf = os.path.join(self.deploy_dir_image, qemuboot_conf)
133 if not os.path.exists(qemuboot_conf):
134 self.skipTest("%s not found" % qemuboot_conf)
135 cmd = "%s %s" % (self.cmd_common, qemuboot_conf)
136 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800137 with open(qemu.qemurunnerlog) as f:
138 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500139
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500140 def test_boot_rootfs(self):
141 """Test runqemu /path/to/rootfs.ext4"""
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500142 rootfs = "%s.ext4" % (self.image_link_name)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143 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 Bishopf86d0552018-12-04 14:18:15 -0800148 with open(qemu.qemurunnerlog) as f:
149 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
150
Brad Bishop316dfdd2018-06-25 12:45:53 -0400151
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.
Brad Bishop79641f22019-09-10 07:20:22 -0400161@OETestTag("machine")
Patrick Williams45852732022-04-02 08:58:32 -0500162@OETestTag("runqemu")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400163class QemuTest(OESelftestTestCase):
164
165 @classmethod
166 def setUpClass(cls):
167 super(QemuTest, cls).setUpClass()
168 cls.recipe = 'core-image-minimal'
169 cls.machine = get_bb_var('MACHINE')
170 cls.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500171 cls.image_link_name = get_bb_var('IMAGE_LINK_NAME', cls.recipe)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400172 cls.cmd_common = "runqemu nographic"
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500173 cls.qemuboot_conf = "%s.qemuboot.conf" % (cls.image_link_name)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400174 cls.qemuboot_conf = os.path.join(cls.deploy_dir_image, cls.qemuboot_conf)
175 bitbake(cls.recipe)
176
177 def _start_qemu_shutdown_check_if_shutdown_succeeded(self, qemu, timeout):
Andrew Geisslerc926e172021-05-07 16:11:35 -0500178 # Allow the runner's LoggingThread instance to exit without errors
179 # (such as the exception "Console connection closed unexpectedly")
180 # as qemu will disappear when we shut it down
181 qemu.runner.allowexit()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400182 qemu.run_serial("shutdown -h now")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400183 time_track = 0
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800184 try:
185 while True:
186 is_alive = qemu.check()
187 if not is_alive:
188 return True
189 if time_track > timeout:
190 return False
191 time.sleep(1)
192 time_track += 1
193 except SystemExit:
194 return True
Brad Bishop316dfdd2018-06-25 12:45:53 -0400195
196 def test_qemu_can_shutdown(self):
197 self.assertExists(self.qemuboot_conf)
198 cmd = "%s %s" % (self.cmd_common, self.qemuboot_conf)
199 shutdown_timeout = 120
200 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
201 qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
202 self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))
203
Andrew Geissler517393d2023-01-13 08:55:19 -0600204 def test_qemu_can_boot_nfs_and_shutdown(self):
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500205 rootfs_tar = "%s.tar.bz2" % (self.image_link_name)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400206 rootfs_tar = os.path.join(self.deploy_dir_image, rootfs_tar)
207 self.assertExists(rootfs_tar)
Andrew Geissler517393d2023-01-13 08:55:19 -0600208 cmd = "%s %s" % (self.cmd_common, rootfs_tar)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400209 shutdown_timeout = 120
210 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
211 qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
212 self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))