tuxruntest.py 5.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. # Functional test that boots known good tuxboot images the same way
  2. # that tuxrun (www.tuxrun.org) does. This tool is used by things like
  3. # the LKFT project to run regression tests on kernels.
  4. #
  5. # Copyright (c) 2023 Linaro Ltd.
  6. #
  7. # Author:
  8. # Alex Bennée <alex.bennee@linaro.org>
  9. #
  10. # SPDX-License-Identifier: GPL-2.0-or-later
  11. import os
  12. import time
  13. from qemu_test import QemuSystemTest
  14. from qemu_test import exec_command, exec_command_and_wait_for_pattern
  15. from qemu_test import wait_for_console_pattern
  16. from qemu_test import has_cmd, run_cmd, get_qemu_img
  17. class TuxRunBaselineTest(QemuSystemTest):
  18. KERNEL_COMMON_COMMAND_LINE = 'printk.time=0'
  19. # Tests are ~10-40s, allow for --debug/--enable-gcov overhead
  20. timeout = 100
  21. def get_tag(self, tagname, default=None):
  22. """
  23. Get the metadata tag or return the default.
  24. """
  25. utag = self._get_unique_tag_val(tagname)
  26. print(f"{tagname}/{default} -> {utag}")
  27. if utag:
  28. return utag
  29. return default
  30. def setUp(self):
  31. super().setUp()
  32. # We need zstd for all the tuxrun tests
  33. # See https://github.com/avocado-framework/avocado/issues/5609
  34. (has_zstd, msg) = has_cmd('zstd')
  35. if has_zstd is False:
  36. self.skipTest(msg)
  37. self.zstd = 'zstd'
  38. # Pre-init TuxRun specific settings: Most machines work with
  39. # reasonable defaults but we sometimes need to tweak the
  40. # config. To avoid open coding everything we store all these
  41. # details in the metadata for each test.
  42. # The tuxboot tag matches the root directory
  43. self.tuxboot = self.arch
  44. # Most Linux's use ttyS0 for their serial port
  45. self.console = "ttyS0"
  46. # Does the machine shutdown QEMU nicely on "halt"
  47. self.wait_for_shutdown = True
  48. self.root = "vda"
  49. # Occasionally we need extra devices to hook things up
  50. self.extradev = None
  51. self.qemu_img = get_qemu_img(self)
  52. def wait_for_console_pattern(self, success_message, vm=None):
  53. wait_for_console_pattern(self, success_message,
  54. failure_message='Kernel panic - not syncing',
  55. vm=vm)
  56. def fetch_tuxrun_assets(self, kernel_asset, rootfs_asset, dtb_asset=None):
  57. """
  58. Fetch the TuxBoot assets.
  59. """
  60. kernel_image = kernel_asset.fetch()
  61. disk_image_zst = rootfs_asset.fetch()
  62. run_cmd([self.zstd, "-f", "-d", disk_image_zst,
  63. "-o", self.workdir + "/rootfs.ext4"])
  64. dtb = dtb_asset.fetch() if dtb_asset is not None else None
  65. return (kernel_image, self.workdir + "/rootfs.ext4", dtb)
  66. def prepare_run(self, kernel, disk, drive, dtb=None, console_index=0):
  67. """
  68. Setup to run and add the common parameters to the system
  69. """
  70. self.vm.set_console(console_index=console_index)
  71. # all block devices are raw ext4's
  72. blockdev = "driver=raw,file.driver=file," \
  73. + f"file.filename={disk},node-name=hd0"
  74. kcmd_line = self.KERNEL_COMMON_COMMAND_LINE
  75. kcmd_line += f" root=/dev/{self.root}"
  76. kcmd_line += f" console={self.console}"
  77. self.vm.add_args('-kernel', kernel,
  78. '-append', kcmd_line,
  79. '-blockdev', blockdev)
  80. # Sometimes we need extra devices attached
  81. if self.extradev:
  82. self.vm.add_args('-device', self.extradev)
  83. self.vm.add_args('-device',
  84. f"{drive},drive=hd0")
  85. # Some machines need an explicit DTB
  86. if dtb:
  87. self.vm.add_args('-dtb', dtb)
  88. def run_tuxtest_tests(self, haltmsg):
  89. """
  90. Wait for the system to boot up, wait for the login prompt and
  91. then do a few things on the console. Trigger a shutdown and
  92. wait to exit cleanly.
  93. """
  94. self.wait_for_console_pattern("Welcome to TuxTest")
  95. time.sleep(0.2)
  96. exec_command(self, 'root')
  97. time.sleep(0.2)
  98. exec_command(self, 'cat /proc/interrupts')
  99. time.sleep(0.1)
  100. exec_command(self, 'cat /proc/self/maps')
  101. time.sleep(0.1)
  102. exec_command(self, 'uname -a')
  103. time.sleep(0.1)
  104. exec_command_and_wait_for_pattern(self, 'halt', haltmsg)
  105. # Wait for VM to shut down gracefully if it can
  106. if self.wait_for_shutdown:
  107. self.vm.wait()
  108. else:
  109. self.vm.shutdown()
  110. def common_tuxrun(self,
  111. kernel_asset,
  112. rootfs_asset,
  113. dtb_asset=None,
  114. drive="virtio-blk-device",
  115. haltmsg="reboot: System halted",
  116. console_index=0):
  117. """
  118. Common path for LKFT tests. Unless we need to do something
  119. special with the command line we can process most things using
  120. the tag metadata.
  121. """
  122. (kernel, disk, dtb) = self.fetch_tuxrun_assets(kernel_asset, rootfs_asset,
  123. dtb_asset)
  124. self.prepare_run(kernel, disk, drive, dtb, console_index)
  125. self.vm.launch()
  126. self.run_tuxtest_tests(haltmsg)
  127. os.remove(disk)