blob: f01e1eec667b3f4d931b38d7b42f6057936136ce [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
Patrick Williams8e7b46e2023-05-01 14:19:06 -05007import os
Brad Bishopd7bf8c12018-02-25 22:55:05 -05008import re
Brad Bishop316dfdd2018-06-25 12:45:53 -04009import 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
Patrick Williams8e7b46e2023-05-01 14:19:06 -050014from oeqa.utils.commands import bitbake, runqemu, get_bb_var
15
Brad Bishopd7bf8c12018-02-25 22:55:05 -050016
Patrick Williams45852732022-04-02 08:58:32 -050017@OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050018class RunqemuTests(OESelftestTestCase):
19 """Runqemu test class"""
20
21 image_is_ready = False
22 deploy_dir_image = ''
Brad Bishopd7bf8c12018-02-25 22:55:05 -050023
24 def setUpLocal(self):
25 super(RunqemuTests, self).setUpLocal()
26 self.recipe = 'core-image-minimal'
Patrick Williams8e7b46e2023-05-01 14:19:06 -050027 self.machine = self.td['MACHINE']
28 self.image_link_name = get_bb_var('IMAGE_LINK_NAME', self.recipe)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050029
Patrick Williams7784c422022-11-17 07:29:11 -060030 self.fstypes = "ext4"
31 if self.td["HOST_ARCH"] in ('i586', 'i686', 'x86_64'):
32 self.fstypes += " iso hddimg"
33 if self.machine == "qemux86-64":
34 self.fstypes += " wic.vmdk wic.qcow2 wic.vdi"
35
36 self.cmd_common = "runqemu nographic"
37 kvm = oe.types.qemu_use_kvm(get_bb_var('QEMU_USE_KVM'), self.td["TARGET_ARCH"])
Brad Bishop977dc1a2019-02-06 16:01:43 -050038 if kvm:
39 self.cmd_common += " kvm"
40
Brad Bishopd7bf8c12018-02-25 22:55:05 -050041 self.write_config(
42"""
Brad Bishopd7bf8c12018-02-25 22:55:05 -050043IMAGE_FSTYPES = "%s"
44# 10 means 1 second
45SYSLINUX_TIMEOUT = "10"
Patrick Williams7784c422022-11-17 07:29:11 -060046""" % self.fstypes)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050047
48 if not RunqemuTests.image_is_ready:
49 RunqemuTests.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
50 bitbake(self.recipe)
51 RunqemuTests.image_is_ready = True
52
Brad Bishopd7bf8c12018-02-25 22:55:05 -050053 def test_boot_machine(self):
54 """Test runqemu machine"""
55 cmd = "%s %s" % (self.cmd_common, self.machine)
56 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080057 with open(qemu.qemurunnerlog) as f:
58 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050059
Brad Bishopd7bf8c12018-02-25 22:55:05 -050060 def test_boot_machine_ext4(self):
61 """Test runqemu machine ext4"""
62 cmd = "%s %s ext4" % (self.cmd_common, self.machine)
63 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
64 with open(qemu.qemurunnerlog) as f:
Patrick Williams8e7b46e2023-05-01 14:19:06 -050065 regexp = r'\nROOTFS: .*\.ext4]\n'
66 self.assertRegex(f.read(), regexp, "Failed to find '%s' in '%s' after running '%s'" % (regexp, qemu.qemurunnerlog, cmd))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050067
Patrick Williams7784c422022-11-17 07:29:11 -060068 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069 def test_boot_machine_iso(self):
70 """Test runqemu machine iso"""
71 cmd = "%s %s iso" % (self.cmd_common, self.machine)
72 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
73 with open(qemu.qemurunnerlog) as f:
Patrick Williams8e7b46e2023-05-01 14:19:06 -050074 text_in = 'media=cdrom'
75 self.assertIn(text_in, f.read(), "Failed to find '%s' in '%s' after running '%s'" % (text_in, qemu.qemurunnerlog, cmd))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 def test_boot_recipe_image(self):
78 """Test runqemu recipe-image"""
79 cmd = "%s %s" % (self.cmd_common, self.recipe)
80 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080081 with open(qemu.qemurunnerlog) as f:
82 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
83
Patrick Williams7784c422022-11-17 07:29:11 -060084 # https://bugzilla.yoctoproject.org/show_bug.cgi?id=14963
85 @skipIfNotMachine("qemux86-64", "tests are qemux86-64 specific currently")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050086 def test_boot_recipe_image_vmdk(self):
87 """Test runqemu recipe-image vmdk"""
88 cmd = "%s %s wic.vmdk" % (self.cmd_common, self.recipe)
89 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
90 with open(qemu.qemurunnerlog) as f:
Patrick Williams8e7b46e2023-05-01 14:19:06 -050091 text_in = 'format=vmdk'
92 self.assertIn(text_in, f.read(), "Failed to find '%s' in '%s' after running '%s'" % (text_in, qemu.qemurunnerlog, cmd))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050093
Patrick Williams7784c422022-11-17 07:29:11 -060094 @skipIfNotMachine("qemux86-64", "tests are qemux86-64 specific currently")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050095 def test_boot_recipe_image_vdi(self):
96 """Test runqemu recipe-image vdi"""
97 cmd = "%s %s wic.vdi" % (self.cmd_common, self.recipe)
98 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
99 with open(qemu.qemurunnerlog) as f:
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500100 text_in = 'format=vdi'
101 self.assertIn(text_in, f.read(), "Failed to find '%s' in '%s' after running '%s'" % (text_in, qemu.qemurunnerlog, cmd))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500102
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500103 def test_boot_deploy(self):
104 """Test runqemu deploy_dir_image"""
105 cmd = "%s %s" % (self.cmd_common, self.deploy_dir_image)
106 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800107 with open(qemu.qemurunnerlog) as f:
108 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
109
Patrick Williams7784c422022-11-17 07:29:11 -0600110 @skipIfNotArch(['i586', 'i686', 'x86_64'])
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111 def test_boot_deploy_hddimg(self):
112 """Test runqemu deploy_dir_image hddimg"""
113 cmd = "%s %s hddimg" % (self.cmd_common, self.deploy_dir_image)
114 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
115 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800116 self.assertTrue(re.search('file=.*.hddimg', f.read()), "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500117
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118 def test_boot_machine_slirp(self):
119 """Test runqemu machine slirp"""
120 cmd = "%s slirp %s" % (self.cmd_common, self.machine)
121 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
122 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800123 self.assertIn(' -netdev user', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500124
Patrick Williams7784c422022-11-17 07:29:11 -0600125 @skipIfNotMachine("qemux86-64", "tests are qemux86-64 specific currently")
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500126 def test_boot_machine_slirp_qcow2(self):
127 """Test runqemu machine slirp qcow2"""
128 cmd = "%s slirp wic.qcow2 %s" % (self.cmd_common, self.machine)
129 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
130 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800131 self.assertIn('format=qcow2', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500132
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500133 def test_boot_qemu_boot(self):
134 """Test runqemu /path/to/image.qemuboot.conf"""
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500135 qemuboot_conf = "%s.qemuboot.conf" % (self.image_link_name)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500136 qemuboot_conf = os.path.join(self.deploy_dir_image, qemuboot_conf)
137 if not os.path.exists(qemuboot_conf):
138 self.skipTest("%s not found" % qemuboot_conf)
139 cmd = "%s %s" % (self.cmd_common, qemuboot_conf)
140 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800141 with open(qemu.qemurunnerlog) as f:
142 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500143
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500144 def test_boot_rootfs(self):
145 """Test runqemu /path/to/rootfs.ext4"""
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500146 rootfs = "%s.ext4" % (self.image_link_name)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500147 rootfs = os.path.join(self.deploy_dir_image, rootfs)
148 if not os.path.exists(rootfs):
149 self.skipTest("%s not found" % rootfs)
150 cmd = "%s %s" % (self.cmd_common, rootfs)
151 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800152 with open(qemu.qemurunnerlog) as f:
153 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
154
Brad Bishop316dfdd2018-06-25 12:45:53 -0400155
156# This test was designed as a separate class to test that shutdown
157# command will shutdown qemu as expected on each qemu architecture
158# based on the MACHINE configuration inside the config file
159# (eg. local.conf).
160#
161# This was different compared to RunqemuTests, where RunqemuTests was
162# dedicated for MACHINE=qemux86-64 where it test that qemux86-64 will
163# bootup various filesystem types, including live image(iso and hddimg)
164# where live image was not supported on all qemu architecture.
Brad Bishop79641f22019-09-10 07:20:22 -0400165@OETestTag("machine")
Patrick Williams45852732022-04-02 08:58:32 -0500166@OETestTag("runqemu")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400167class QemuTest(OESelftestTestCase):
168
169 @classmethod
170 def setUpClass(cls):
171 super(QemuTest, cls).setUpClass()
172 cls.recipe = 'core-image-minimal'
Patrick Williams8e7b46e2023-05-01 14:19:06 -0500173 cls.machine = get_bb_var('MACHINE')
174 cls.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
175 cls.image_link_name = get_bb_var('IMAGE_LINK_NAME', cls.recipe)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400176 cls.cmd_common = "runqemu nographic"
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500177 cls.qemuboot_conf = "%s.qemuboot.conf" % (cls.image_link_name)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400178 cls.qemuboot_conf = os.path.join(cls.deploy_dir_image, cls.qemuboot_conf)
179 bitbake(cls.recipe)
180
181 def _start_qemu_shutdown_check_if_shutdown_succeeded(self, qemu, timeout):
Andrew Geisslerc926e172021-05-07 16:11:35 -0500182 # Allow the runner's LoggingThread instance to exit without errors
183 # (such as the exception "Console connection closed unexpectedly")
184 # as qemu will disappear when we shut it down
185 qemu.runner.allowexit()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400186 qemu.run_serial("shutdown -h now")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400187 time_track = 0
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800188 try:
189 while True:
190 is_alive = qemu.check()
191 if not is_alive:
192 return True
193 if time_track > timeout:
194 return False
195 time.sleep(1)
196 time_track += 1
197 except SystemExit:
198 return True
Brad Bishop316dfdd2018-06-25 12:45:53 -0400199
200 def test_qemu_can_shutdown(self):
201 self.assertExists(self.qemuboot_conf)
202 cmd = "%s %s" % (self.cmd_common, self.qemuboot_conf)
203 shutdown_timeout = 120
204 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
205 qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
206 self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))
207
Andrew Geissler517393d2023-01-13 08:55:19 -0600208 def test_qemu_can_boot_nfs_and_shutdown(self):
Andrew Geisslerfc113ea2023-03-31 09:59:46 -0500209 rootfs_tar = "%s.tar.bz2" % (self.image_link_name)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400210 rootfs_tar = os.path.join(self.deploy_dir_image, rootfs_tar)
211 self.assertExists(rootfs_tar)
Andrew Geissler517393d2023-01-13 08:55:19 -0600212 cmd = "%s %s" % (self.cmd_common, rootfs_tar)
Brad Bishop316dfdd2018-06-25 12:45:53 -0400213 shutdown_timeout = 120
214 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
215 qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
216 self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))