tuxruntest.py 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148
  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 stat
  13. from subprocess import check_call, DEVNULL
  14. from qemu_test import QemuSystemTest
  15. from qemu_test import exec_command_and_wait_for_pattern
  16. from qemu_test import wait_for_console_pattern
  17. from qemu_test import which, get_qemu_img
  18. class TuxRunBaselineTest(QemuSystemTest):
  19. KERNEL_COMMON_COMMAND_LINE = 'printk.time=0'
  20. # Tests are ~10-40s, allow for --debug/--enable-gcov overhead
  21. timeout = 100
  22. def get_tag(self, tagname, default=None):
  23. """
  24. Get the metadata tag or return the default.
  25. """
  26. utag = self._get_unique_tag_val(tagname)
  27. print(f"{tagname}/{default} -> {utag}")
  28. if utag:
  29. return utag
  30. return default
  31. def setUp(self):
  32. super().setUp()
  33. # We need zstd for all the tuxrun tests
  34. if which('zstd') is None:
  35. self.skipTest("zstd not found in $PATH")
  36. # Pre-init TuxRun specific settings: Most machines work with
  37. # reasonable defaults but we sometimes need to tweak the
  38. # config. To avoid open coding everything we store all these
  39. # details in the metadata for each test.
  40. # The tuxboot tag matches the root directory
  41. self.tuxboot = self.arch
  42. # Most Linux's use ttyS0 for their serial port
  43. self.console = "ttyS0"
  44. # Does the machine shutdown QEMU nicely on "halt"
  45. self.wait_for_shutdown = True
  46. self.root = "vda"
  47. # Occasionally we need extra devices to hook things up
  48. self.extradev = None
  49. self.qemu_img = get_qemu_img(self)
  50. def wait_for_console_pattern(self, success_message, vm=None):
  51. wait_for_console_pattern(self, success_message,
  52. failure_message='Kernel panic - not syncing',
  53. vm=vm)
  54. def fetch_tuxrun_assets(self, kernel_asset, rootfs_asset, dtb_asset=None):
  55. """
  56. Fetch the TuxBoot assets.
  57. """
  58. kernel_image = kernel_asset.fetch()
  59. disk_image = self.uncompress(rootfs_asset)
  60. dtb = dtb_asset.fetch() if dtb_asset is not None else None
  61. return (kernel_image, disk_image, dtb)
  62. def prepare_run(self, kernel, disk, drive, dtb=None, console_index=0):
  63. """
  64. Setup to run and add the common parameters to the system
  65. """
  66. self.vm.set_console(console_index=console_index)
  67. # all block devices are raw ext4's
  68. blockdev = "driver=raw,file.driver=file," \
  69. + f"file.filename={disk},node-name=hd0"
  70. kcmd_line = self.KERNEL_COMMON_COMMAND_LINE
  71. kcmd_line += f" root=/dev/{self.root}"
  72. kcmd_line += f" console={self.console}"
  73. self.vm.add_args('-kernel', kernel,
  74. '-append', kcmd_line,
  75. '-blockdev', blockdev)
  76. # Sometimes we need extra devices attached
  77. if self.extradev:
  78. self.vm.add_args('-device', self.extradev)
  79. self.vm.add_args('-device',
  80. f"{drive},drive=hd0")
  81. # Some machines need an explicit DTB
  82. if dtb:
  83. self.vm.add_args('-dtb', dtb)
  84. def run_tuxtest_tests(self, haltmsg):
  85. """
  86. Wait for the system to boot up, wait for the login prompt and
  87. then do a few things on the console. Trigger a shutdown and
  88. wait to exit cleanly.
  89. """
  90. ps1='root@tuxtest:~#'
  91. self.wait_for_console_pattern('tuxtest login:')
  92. exec_command_and_wait_for_pattern(self, 'root', ps1)
  93. exec_command_and_wait_for_pattern(self, 'cat /proc/interrupts', ps1)
  94. exec_command_and_wait_for_pattern(self, 'cat /proc/self/maps', ps1)
  95. exec_command_and_wait_for_pattern(self, 'uname -a', ps1)
  96. exec_command_and_wait_for_pattern(self, 'halt', haltmsg)
  97. # Wait for VM to shut down gracefully if it can
  98. if self.wait_for_shutdown:
  99. self.vm.wait()
  100. else:
  101. self.vm.shutdown()
  102. def common_tuxrun(self,
  103. kernel_asset,
  104. rootfs_asset,
  105. dtb_asset=None,
  106. drive="virtio-blk-device",
  107. haltmsg="reboot: System halted",
  108. console_index=0):
  109. """
  110. Common path for LKFT tests. Unless we need to do something
  111. special with the command line we can process most things using
  112. the tag metadata.
  113. """
  114. (kernel, disk, dtb) = self.fetch_tuxrun_assets(kernel_asset, rootfs_asset,
  115. dtb_asset)
  116. self.prepare_run(kernel, disk, drive, dtb, console_index)
  117. self.vm.launch()
  118. self.run_tuxtest_tests(haltmsg)
  119. os.remove(disk)