runqemu.py 8.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. #
  2. # Copyright (c) 2017 Wind River Systems, Inc.
  3. #
  4. import re
  5. import tempfile
  6. import time
  7. from oeqa.selftest.case import OESelftestTestCase
  8. from oeqa.utils.commands import bitbake, runqemu, get_bb_var, runCmd
  9. from oeqa.core.decorator.oeid import OETestID
  10. class RunqemuTests(OESelftestTestCase):
  11. """Runqemu test class"""
  12. image_is_ready = False
  13. deploy_dir_image = ''
  14. # We only want to print runqemu stdout/stderr if there is a test case failure
  15. buffer = True
  16. def setUpLocal(self):
  17. super(RunqemuTests, self).setUpLocal()
  18. self.recipe = 'core-image-minimal'
  19. self.machine = 'qemux86-64'
  20. self.fstypes = "ext4 iso hddimg wic.vmdk wic.qcow2 wic.vdi"
  21. self.cmd_common = "runqemu nographic"
  22. self.write_config(
  23. """
  24. MACHINE = "%s"
  25. IMAGE_FSTYPES = "%s"
  26. # 10 means 1 second
  27. SYSLINUX_TIMEOUT = "10"
  28. """
  29. % (self.machine, self.fstypes)
  30. )
  31. if not RunqemuTests.image_is_ready:
  32. RunqemuTests.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
  33. bitbake(self.recipe)
  34. RunqemuTests.image_is_ready = True
  35. @OETestID(2001)
  36. def test_boot_machine(self):
  37. """Test runqemu machine"""
  38. cmd = "%s %s" % (self.cmd_common, self.machine)
  39. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  40. self.assertTrue(qemu.runner.logged, "Failed: %s" % cmd)
  41. @OETestID(2002)
  42. def test_boot_machine_ext4(self):
  43. """Test runqemu machine ext4"""
  44. cmd = "%s %s ext4" % (self.cmd_common, self.machine)
  45. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  46. with open(qemu.qemurunnerlog) as f:
  47. self.assertTrue('rootfs.ext4' in f.read(), "Failed: %s" % cmd)
  48. @OETestID(2003)
  49. def test_boot_machine_iso(self):
  50. """Test runqemu machine iso"""
  51. cmd = "%s %s iso" % (self.cmd_common, self.machine)
  52. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  53. with open(qemu.qemurunnerlog) as f:
  54. self.assertTrue('media=cdrom' in f.read(), "Failed: %s" % cmd)
  55. @OETestID(2004)
  56. def test_boot_recipe_image(self):
  57. """Test runqemu recipe-image"""
  58. cmd = "%s %s" % (self.cmd_common, self.recipe)
  59. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  60. self.assertTrue(qemu.runner.logged, "Failed: %s" % cmd)
  61. @OETestID(2005)
  62. def test_boot_recipe_image_vmdk(self):
  63. """Test runqemu recipe-image vmdk"""
  64. cmd = "%s %s wic.vmdk" % (self.cmd_common, self.recipe)
  65. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  66. with open(qemu.qemurunnerlog) as f:
  67. self.assertTrue('format=vmdk' in f.read(), "Failed: %s" % cmd)
  68. @OETestID(2006)
  69. def test_boot_recipe_image_vdi(self):
  70. """Test runqemu recipe-image vdi"""
  71. cmd = "%s %s wic.vdi" % (self.cmd_common, self.recipe)
  72. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  73. with open(qemu.qemurunnerlog) as f:
  74. self.assertTrue('format=vdi' in f.read(), "Failed: %s" % cmd)
  75. @OETestID(2007)
  76. def test_boot_deploy(self):
  77. """Test runqemu deploy_dir_image"""
  78. cmd = "%s %s" % (self.cmd_common, self.deploy_dir_image)
  79. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  80. self.assertTrue(qemu.runner.logged, "Failed: %s" % cmd)
  81. @OETestID(2008)
  82. def test_boot_deploy_hddimg(self):
  83. """Test runqemu deploy_dir_image hddimg"""
  84. cmd = "%s %s hddimg" % (self.cmd_common, self.deploy_dir_image)
  85. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  86. with open(qemu.qemurunnerlog) as f:
  87. self.assertTrue(re.search('file=.*.hddimg', f.read()), "Failed: %s" % cmd)
  88. @OETestID(2009)
  89. def test_boot_machine_slirp(self):
  90. """Test runqemu machine slirp"""
  91. cmd = "%s slirp %s" % (self.cmd_common, self.machine)
  92. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  93. with open(qemu.qemurunnerlog) as f:
  94. self.assertTrue(' -netdev user' in f.read(), "Failed: %s" % cmd)
  95. @OETestID(2009)
  96. def test_boot_machine_slirp_qcow2(self):
  97. """Test runqemu machine slirp qcow2"""
  98. cmd = "%s slirp wic.qcow2 %s" % (self.cmd_common, self.machine)
  99. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  100. with open(qemu.qemurunnerlog) as f:
  101. self.assertTrue('format=qcow2' in f.read(), "Failed: %s" % cmd)
  102. @OETestID(2010)
  103. def test_boot_qemu_boot(self):
  104. """Test runqemu /path/to/image.qemuboot.conf"""
  105. qemuboot_conf = "%s-%s.qemuboot.conf" % (self.recipe, self.machine)
  106. qemuboot_conf = os.path.join(self.deploy_dir_image, qemuboot_conf)
  107. if not os.path.exists(qemuboot_conf):
  108. self.skipTest("%s not found" % qemuboot_conf)
  109. cmd = "%s %s" % (self.cmd_common, qemuboot_conf)
  110. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  111. self.assertTrue(qemu.runner.logged, "Failed: %s" % cmd)
  112. @OETestID(2011)
  113. def test_boot_rootfs(self):
  114. """Test runqemu /path/to/rootfs.ext4"""
  115. rootfs = "%s-%s.ext4" % (self.recipe, self.machine)
  116. rootfs = os.path.join(self.deploy_dir_image, rootfs)
  117. if not os.path.exists(rootfs):
  118. self.skipTest("%s not found" % rootfs)
  119. cmd = "%s %s" % (self.cmd_common, rootfs)
  120. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  121. self.assertTrue(qemu.runner.logged, "Failed: %s" % cmd)
  122. # This test was designed as a separate class to test that shutdown
  123. # command will shutdown qemu as expected on each qemu architecture
  124. # based on the MACHINE configuration inside the config file
  125. # (eg. local.conf).
  126. #
  127. # This was different compared to RunqemuTests, where RunqemuTests was
  128. # dedicated for MACHINE=qemux86-64 where it test that qemux86-64 will
  129. # bootup various filesystem types, including live image(iso and hddimg)
  130. # where live image was not supported on all qemu architecture.
  131. class QemuTest(OESelftestTestCase):
  132. @classmethod
  133. def setUpClass(cls):
  134. super(QemuTest, cls).setUpClass()
  135. cls.recipe = 'core-image-minimal'
  136. cls.machine = get_bb_var('MACHINE')
  137. cls.deploy_dir_image = get_bb_var('DEPLOY_DIR_IMAGE')
  138. cls.cmd_common = "runqemu nographic"
  139. cls.qemuboot_conf = "%s-%s.qemuboot.conf" % (cls.recipe, cls.machine)
  140. cls.qemuboot_conf = os.path.join(cls.deploy_dir_image, cls.qemuboot_conf)
  141. bitbake(cls.recipe)
  142. def _start_qemu_shutdown_check_if_shutdown_succeeded(self, qemu, timeout):
  143. qemu.run_serial("shutdown -h now")
  144. # Stop thread will stop the LoggingThread instance used for logging
  145. # qemu through serial console, stop thread will prevent this code
  146. # from facing exception (Console connection closed unexpectedly)
  147. # when qemu was shutdown by the above shutdown command
  148. qemu.runner.stop_thread()
  149. time_track = 0
  150. while True:
  151. is_alive = qemu.check()
  152. if not is_alive:
  153. return True
  154. if time_track > timeout:
  155. return False
  156. time.sleep(1)
  157. time_track += 1
  158. def test_qemu_can_shutdown(self):
  159. self.assertExists(self.qemuboot_conf)
  160. cmd = "%s %s" % (self.cmd_common, self.qemuboot_conf)
  161. shutdown_timeout = 120
  162. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  163. qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
  164. self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))
  165. # Need to have portmap/rpcbind running to allow this test to work and
  166. # current autobuilder setup does not have this.
  167. def disabled_test_qemu_can_boot_nfs_and_shutdown(self):
  168. self.assertExists(self.qemuboot_conf)
  169. bitbake('meta-ide-support')
  170. rootfs_tar = "%s-%s.tar.bz2" % (self.recipe, self.machine)
  171. rootfs_tar = os.path.join(self.deploy_dir_image, rootfs_tar)
  172. self.assertExists(rootfs_tar)
  173. tmpdir = tempfile.mkdtemp(prefix='qemu_nfs')
  174. tmpdir_nfs = os.path.join(tmpdir, 'nfs')
  175. cmd_extract_nfs = 'runqemu-extract-sdk %s %s' % (rootfs_tar, tmpdir_nfs)
  176. result = runCmd(cmd_extract_nfs)
  177. self.assertEqual(0, result.status, "runqemu-extract-sdk didn't run as expected. %s" % result.output)
  178. cmd = "%s nfs %s %s" % (self.cmd_common, self.qemuboot_conf, tmpdir_nfs)
  179. shutdown_timeout = 120
  180. with runqemu(self.recipe, ssh=False, launch_cmd=cmd) as qemu:
  181. qemu_shutdown_succeeded = self._start_qemu_shutdown_check_if_shutdown_succeeded(qemu, shutdown_timeout)
  182. self.assertTrue(qemu_shutdown_succeeded, 'Failed: %s does not shutdown within timeout(%s)' % (self.machine, shutdown_timeout))
  183. runCmd('rm -rf %s' % tmpdir)