blob: c1d277a095a4056d92a3c142fcfdc8c6ffab33c0 [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
Brad Bishopd7bf8c12018-02-25 22:55:05 -050012from oeqa.selftest.case import OESelftestTestCase
Brad Bishop316dfdd2018-06-25 12:45:53 -040013from oeqa.utils.commands import bitbake, runqemu, get_bb_var, runCmd
Brad Bishopd7bf8c12018-02-25 22:55:05 -050014
Patrick Williams45852732022-04-02 08:58:32 -050015@OETestTag("runqemu")
Brad Bishopd7bf8c12018-02-25 22:55:05 -050016class RunqemuTests(OESelftestTestCase):
17 """Runqemu test class"""
18
19 image_is_ready = False
20 deploy_dir_image = ''
Brad Bishopd7bf8c12018-02-25 22:55:05 -050021
22 def setUpLocal(self):
23 super(RunqemuTests, self).setUpLocal()
24 self.recipe = 'core-image-minimal'
25 self.machine = 'qemux86-64'
26 self.fstypes = "ext4 iso hddimg wic.vmdk wic.qcow2 wic.vdi"
27 self.cmd_common = "runqemu nographic"
28
Brad Bishop977dc1a2019-02-06 16:01:43 -050029 kvm = oe.types.qemu_use_kvm(get_bb_var('QEMU_USE_KVM'), 'x86_64')
30 if kvm:
31 self.cmd_common += " kvm"
32
Brad Bishopd7bf8c12018-02-25 22:55:05 -050033 self.write_config(
34"""
35MACHINE = "%s"
36IMAGE_FSTYPES = "%s"
37# 10 means 1 second
38SYSLINUX_TIMEOUT = "10"
39"""
40% (self.machine, self.fstypes)
41 )
42
43 if not RunqemuTests.image_is_ready:
44 RunqemuTests.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
45 bitbake(self.recipe)
46 RunqemuTests.image_is_ready = True
47
Brad Bishopd7bf8c12018-02-25 22:55:05 -050048 def test_boot_machine(self):
49 """Test runqemu machine"""
50 cmd = "%s %s" % (self.cmd_common, self.machine)
51 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080052 with open(qemu.qemurunnerlog) as f:
53 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054
Brad Bishopd7bf8c12018-02-25 22:55:05 -050055 def test_boot_machine_ext4(self):
56 """Test runqemu machine ext4"""
57 cmd = "%s %s ext4" % (self.cmd_common, self.machine)
58 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
59 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080060 self.assertIn('rootfs.ext4', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061
Brad Bishopd7bf8c12018-02-25 22:55:05 -050062 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 Bishopf86d0552018-12-04 14:18:15 -080067 self.assertIn('media=cdrom', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050068
Brad Bishopd7bf8c12018-02-25 22:55:05 -050069 def test_boot_recipe_image(self):
70 """Test runqemu recipe-image"""
71 cmd = "%s %s" % (self.cmd_common, self.recipe)
72 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080073 with open(qemu.qemurunnerlog) as f:
74 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
75
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076
Brad Bishopd7bf8c12018-02-25 22:55:05 -050077 def test_boot_recipe_image_vmdk(self):
78 """Test runqemu recipe-image vmdk"""
79 cmd = "%s %s wic.vmdk" % (self.cmd_common, self.recipe)
80 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
81 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080082 self.assertIn('format=vmdk', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050083
Brad Bishopd7bf8c12018-02-25 22:55:05 -050084 def test_boot_recipe_image_vdi(self):
85 """Test runqemu recipe-image vdi"""
86 cmd = "%s %s wic.vdi" % (self.cmd_common, self.recipe)
87 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
88 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080089 self.assertIn('format=vdi', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090
Brad Bishopd7bf8c12018-02-25 22:55:05 -050091 def test_boot_deploy(self):
92 """Test runqemu deploy_dir_image"""
93 cmd = "%s %s" % (self.cmd_common, self.deploy_dir_image)
94 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080095 with open(qemu.qemurunnerlog) as f:
96 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
97
Brad Bishopd7bf8c12018-02-25 22:55:05 -050098
Brad Bishopd7bf8c12018-02-25 22:55:05 -050099 def test_boot_deploy_hddimg(self):
100 """Test runqemu deploy_dir_image hddimg"""
101 cmd = "%s %s hddimg" % (self.cmd_common, self.deploy_dir_image)
102 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
103 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800104 self.assertTrue(re.search('file=.*.hddimg', f.read()), "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500106 def test_boot_machine_slirp(self):
107 """Test runqemu machine slirp"""
108 cmd = "%s slirp %s" % (self.cmd_common, self.machine)
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.assertIn(' -netdev user', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500113 def test_boot_machine_slirp_qcow2(self):
114 """Test runqemu machine slirp qcow2"""
115 cmd = "%s slirp wic.qcow2 %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('format=qcow2', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500120 def test_boot_qemu_boot(self):
121 """Test runqemu /path/to/image.qemuboot.conf"""
122 qemuboot_conf = "%s-%s.qemuboot.conf" % (self.recipe, self.machine)
123 qemuboot_conf = os.path.join(self.deploy_dir_image, qemuboot_conf)
124 if not os.path.exists(qemuboot_conf):
125 self.skipTest("%s not found" % qemuboot_conf)
126 cmd = "%s %s" % (self.cmd_common, qemuboot_conf)
127 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800128 with open(qemu.qemurunnerlog) as f:
129 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500130
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500131 def test_boot_rootfs(self):
132 """Test runqemu /path/to/rootfs.ext4"""
133 rootfs = "%s-%s.ext4" % (self.recipe, self.machine)
134 rootfs = os.path.join(self.deploy_dir_image, rootfs)
135 if not os.path.exists(rootfs):
136 self.skipTest("%s not found" % rootfs)
137 cmd = "%s %s" % (self.cmd_common, rootfs)
138 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800139 with open(qemu.qemurunnerlog) as f:
140 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
141
Brad Bishop316dfdd2018-06-25 12:45:53 -0400142
143# This test was designed as a separate class to test that shutdown
144# command will shutdown qemu as expected on each qemu architecture
145# based on the MACHINE configuration inside the config file
146# (eg. local.conf).
147#
148# This was different compared to RunqemuTests, where RunqemuTests was
149# dedicated for MACHINE=qemux86-64 where it test that qemux86-64 will
150# bootup various filesystem types, including live image(iso and hddimg)
151# where live image was not supported on all qemu architecture.
Brad Bishop79641f22019-09-10 07:20:22 -0400152@OETestTag("machine")
Patrick Williams45852732022-04-02 08:58:32 -0500153@OETestTag("runqemu")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400154class QemuTest(OESelftestTestCase):
155
156 @classmethod
157 def setUpClass(cls):
158 super(QemuTest, cls).setUpClass()
159 cls.recipe = 'core-image-minimal'
160 cls.machine = get_bb_var('MACHINE')
161 cls.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
162 cls.cmd_common = "runqemu nographic"
163 cls.qemuboot_conf = "%s-%s.qemuboot.conf" % (cls.recipe, cls.machine)
164 cls.qemuboot_conf = os.path.join(cls.deploy_dir_image, cls.qemuboot_conf)
165 bitbake(cls.recipe)
166
167 def _start_qemu_shutdown_check_if_shutdown_succeeded(self, qemu, timeout):
Andrew Geisslerc926e172021-05-07 16:11:35 -0500168 # Allow the runner's LoggingThread instance to exit without errors
169 # (such as the exception "Console connection closed unexpectedly")
170 # as qemu will disappear when we shut it down
171 qemu.runner.allowexit()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400172 qemu.run_serial("shutdown -h now")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400173 time_track = 0
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800174 try:
175 while True:
176 is_alive = qemu.check()
177 if not is_alive:
178 return True
179 if time_track > timeout:
180 return False
181 time.sleep(1)
182 time_track += 1
183 except SystemExit:
184 return True
Brad Bishop316dfdd2018-06-25 12:45:53 -0400185
186 def test_qemu_can_shutdown(self):
187 self.assertExists(self.qemuboot_conf)
188 cmd = "%s %s" % (self.cmd_common, self.qemuboot_conf)
189 shutdown_timeout = 120
190 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
191 qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
192 self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))
193
194 # Need to have portmap/rpcbind running to allow this test to work and
195 # current autobuilder setup does not have this.
196 def disabled_test_qemu_can_boot_nfs_and_shutdown(self):
197 self.assertExists(self.qemuboot_conf)
198 bitbake('meta-ide-support')
199 rootfs_tar = "%s-%s.tar.bz2" % (self.recipe, self.machine)
200 rootfs_tar = os.path.join(self.deploy_dir_image, rootfs_tar)
201 self.assertExists(rootfs_tar)
202 tmpdir = tempfile.mkdtemp(prefix='qemu_nfs')
203 tmpdir_nfs = os.path.join(tmpdir, 'nfs')
204 cmd_extract_nfs = 'runqemu-extract-sdk %s %s' % (rootfs_tar, tmpdir_nfs)
205 result = runCmd(cmd_extract_nfs)
206 self.assertEqual(0, result.status, "runqemu-extract-sdk didn't run as expected. %s" % result.output)
207 cmd = "%s nfs %s %s" % (self.cmd_common, self.qemuboot_conf, tmpdir_nfs)
208 shutdown_timeout = 120
209 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
210 qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
211 self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))
212 runCmd('rm -rf %s' % tmpdir)