blob: da22f77b2767e4c2e7f07cc5dfc11feb69e51a6d [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
15class RunqemuTests(OESelftestTestCase):
16 """Runqemu test class"""
17
18 image_is_ready = False
19 deploy_dir_image = ''
Brad Bishopd7bf8c12018-02-25 22:55:05 -050020
21 def setUpLocal(self):
22 super(RunqemuTests, self).setUpLocal()
23 self.recipe = 'core-image-minimal'
24 self.machine = 'qemux86-64'
25 self.fstypes = "ext4 iso hddimg wic.vmdk wic.qcow2 wic.vdi"
26 self.cmd_common = "runqemu nographic"
27
Brad Bishop977dc1a2019-02-06 16:01:43 -050028 kvm = oe.types.qemu_use_kvm(get_bb_var('QEMU_USE_KVM'), 'x86_64')
29 if kvm:
30 self.cmd_common += " kvm"
31
Brad Bishopd7bf8c12018-02-25 22:55:05 -050032 self.write_config(
33"""
34MACHINE = "%s"
35IMAGE_FSTYPES = "%s"
36# 10 means 1 second
37SYSLINUX_TIMEOUT = "10"
38"""
39% (self.machine, self.fstypes)
40 )
41
42 if not RunqemuTests.image_is_ready:
43 RunqemuTests.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
44 bitbake(self.recipe)
45 RunqemuTests.image_is_ready = True
46
Brad Bishopd7bf8c12018-02-25 22:55:05 -050047 def test_boot_machine(self):
48 """Test runqemu machine"""
49 cmd = "%s %s" % (self.cmd_common, self.machine)
50 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080051 with open(qemu.qemurunnerlog) as f:
52 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -050053
Brad Bishopd7bf8c12018-02-25 22:55:05 -050054 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 Bishopf86d0552018-12-04 14:18:15 -080059 self.assertIn('rootfs.ext4', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050060
Brad Bishopd7bf8c12018-02-25 22:55:05 -050061 def test_boot_machine_iso(self):
62 """Test runqemu machine iso"""
63 cmd = "%s %s iso" % (self.cmd_common, self.machine)
64 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
65 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080066 self.assertIn('media=cdrom', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050067
Brad Bishopd7bf8c12018-02-25 22:55:05 -050068 def test_boot_recipe_image(self):
69 """Test runqemu recipe-image"""
70 cmd = "%s %s" % (self.cmd_common, self.recipe)
71 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080072 with open(qemu.qemurunnerlog) as f:
73 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
74
Brad Bishopd7bf8c12018-02-25 22:55:05 -050075
Brad Bishopd7bf8c12018-02-25 22:55:05 -050076 def test_boot_recipe_image_vmdk(self):
77 """Test runqemu recipe-image vmdk"""
78 cmd = "%s %s wic.vmdk" % (self.cmd_common, self.recipe)
79 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
80 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -080081 self.assertIn('format=vmdk', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050082
Brad Bishopd7bf8c12018-02-25 22:55:05 -050083 def test_boot_recipe_image_vdi(self):
84 """Test runqemu recipe-image vdi"""
85 cmd = "%s %s wic.vdi" % (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=vdi', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -050089
Brad Bishopd7bf8c12018-02-25 22:55:05 -050090 def test_boot_deploy(self):
91 """Test runqemu deploy_dir_image"""
92 cmd = "%s %s" % (self.cmd_common, self.deploy_dir_image)
93 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -080094 with open(qemu.qemurunnerlog) as f:
95 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
96
Brad Bishopd7bf8c12018-02-25 22:55:05 -050097
Brad Bishopd7bf8c12018-02-25 22:55:05 -050098 def test_boot_deploy_hddimg(self):
99 """Test runqemu deploy_dir_image hddimg"""
100 cmd = "%s %s hddimg" % (self.cmd_common, self.deploy_dir_image)
101 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
102 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800103 self.assertTrue(re.search('file=.*.hddimg', f.read()), "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500104
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500105 def test_boot_machine_slirp(self):
106 """Test runqemu machine slirp"""
107 cmd = "%s slirp %s" % (self.cmd_common, self.machine)
108 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
109 with open(qemu.qemurunnerlog) as f:
Brad Bishopf86d0552018-12-04 14:18:15 -0800110 self.assertIn(' -netdev user', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500111
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500112 def test_boot_machine_slirp_qcow2(self):
113 """Test runqemu machine slirp qcow2"""
114 cmd = "%s slirp wic.qcow2 %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 Bishopf86d0552018-12-04 14:18:15 -0800117 self.assertIn('format=qcow2', f.read(), "Failed: %s" % cmd)
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500118
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500119 def test_boot_qemu_boot(self):
120 """Test runqemu /path/to/image.qemuboot.conf"""
121 qemuboot_conf = "%s-%s.qemuboot.conf" % (self.recipe, self.machine)
122 qemuboot_conf = os.path.join(self.deploy_dir_image, qemuboot_conf)
123 if not os.path.exists(qemuboot_conf):
124 self.skipTest("%s not found" % qemuboot_conf)
125 cmd = "%s %s" % (self.cmd_common, qemuboot_conf)
126 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800127 with open(qemu.qemurunnerlog) as f:
128 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500129
Brad Bishopd7bf8c12018-02-25 22:55:05 -0500130 def test_boot_rootfs(self):
131 """Test runqemu /path/to/rootfs.ext4"""
132 rootfs = "%s-%s.ext4" % (self.recipe, self.machine)
133 rootfs = os.path.join(self.deploy_dir_image, rootfs)
134 if not os.path.exists(rootfs):
135 self.skipTest("%s not found" % rootfs)
136 cmd = "%s %s" % (self.cmd_common, rootfs)
137 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
Brad Bishopf86d0552018-12-04 14:18:15 -0800138 with open(qemu.qemurunnerlog) as f:
139 self.assertTrue(qemu.runner.logged, "Failed: %s, %s" % (cmd, f.read()))
140
Brad Bishop316dfdd2018-06-25 12:45:53 -0400141
142# This test was designed as a separate class to test that shutdown
143# command will shutdown qemu as expected on each qemu architecture
144# based on the MACHINE configuration inside the config file
145# (eg. local.conf).
146#
147# This was different compared to RunqemuTests, where RunqemuTests was
148# dedicated for MACHINE=qemux86-64 where it test that qemux86-64 will
149# bootup various filesystem types, including live image(iso and hddimg)
150# where live image was not supported on all qemu architecture.
Brad Bishop79641f22019-09-10 07:20:22 -0400151@OETestTag("machine")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400152class QemuTest(OESelftestTestCase):
153
154 @classmethod
155 def setUpClass(cls):
156 super(QemuTest, cls).setUpClass()
157 cls.recipe = 'core-image-minimal'
158 cls.machine = get_bb_var('MACHINE')
159 cls.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
160 cls.cmd_common = "runqemu nographic"
161 cls.qemuboot_conf = "%s-%s.qemuboot.conf" % (cls.recipe, cls.machine)
162 cls.qemuboot_conf = os.path.join(cls.deploy_dir_image, cls.qemuboot_conf)
163 bitbake(cls.recipe)
164
165 def _start_qemu_shutdown_check_if_shutdown_succeeded(self, qemu, timeout):
Andrew Geisslerc926e172021-05-07 16:11:35 -0500166 # Allow the runner's LoggingThread instance to exit without errors
167 # (such as the exception "Console connection closed unexpectedly")
168 # as qemu will disappear when we shut it down
169 qemu.runner.allowexit()
Brad Bishop316dfdd2018-06-25 12:45:53 -0400170 qemu.run_serial("shutdown -h now")
Brad Bishop316dfdd2018-06-25 12:45:53 -0400171 time_track = 0
Brad Bishop1a4b7ee2018-12-16 17:11:34 -0800172 try:
173 while True:
174 is_alive = qemu.check()
175 if not is_alive:
176 return True
177 if time_track > timeout:
178 return False
179 time.sleep(1)
180 time_track += 1
181 except SystemExit:
182 return True
Brad Bishop316dfdd2018-06-25 12:45:53 -0400183
184 def test_qemu_can_shutdown(self):
185 self.assertExists(self.qemuboot_conf)
186 cmd = "%s %s" % (self.cmd_common, self.qemuboot_conf)
187 shutdown_timeout = 120
188 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
189 qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
190 self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))
191
192 # Need to have portmap/rpcbind running to allow this test to work and
193 # current autobuilder setup does not have this.
194 def disabled_test_qemu_can_boot_nfs_and_shutdown(self):
195 self.assertExists(self.qemuboot_conf)
196 bitbake('meta-ide-support')
197 rootfs_tar = "%s-%s.tar.bz2" % (self.recipe, self.machine)
198 rootfs_tar = os.path.join(self.deploy_dir_image, rootfs_tar)
199 self.assertExists(rootfs_tar)
200 tmpdir = tempfile.mkdtemp(prefix='qemu_nfs')
201 tmpdir_nfs = os.path.join(tmpdir, 'nfs')
202 cmd_extract_nfs = 'runqemu-extract-sdk %s %s' % (rootfs_tar, tmpdir_nfs)
203 result = runCmd(cmd_extract_nfs)
204 self.assertEqual(0, result.status, "runqemu-extract-sdk didn't run as expected. %s" % result.output)
205 cmd = "%s nfs %s %s" % (self.cmd_common, self.qemuboot_conf, tmpdir_nfs)
206 shutdown_timeout = 120
207 with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
208 qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
209 self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))
210 runCmd('rm -rf %s' % tmpdir)