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