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 14 15from qemu_test import QemuSystemTest 16from qemu_test import exec_command_and_wait_for_pattern 17from qemu_test import wait_for_console_pattern 18from qemu_test import has_cmd, run_cmd, get_qemu_img 19 20class TuxRunBaselineTest(QemuSystemTest): 21 22 KERNEL_COMMON_COMMAND_LINE = 'printk.time=0' 23 # Tests are ~10-40s, allow for --debug/--enable-gcov overhead 24 timeout = 100 25 26 def get_tag(self, tagname, default=None): 27 """ 28 Get the metadata tag or return the default. 29 """ 30 utag = self._get_unique_tag_val(tagname) 31 print(f"{tagname}/{default} -> {utag}") 32 if utag: 33 return utag 34 35 return default 36 37 def setUp(self): 38 super().setUp() 39 40 # We need zstd for all the tuxrun tests 41 (has_zstd, msg) = has_cmd('zstd') 42 if has_zstd is False: 43 self.skipTest(msg) 44 self.zstd = 'zstd' 45 46 # Pre-init TuxRun specific settings: Most machines work with 47 # reasonable defaults but we sometimes need to tweak the 48 # config. To avoid open coding everything we store all these 49 # details in the metadata for each test. 50 51 # The tuxboot tag matches the root directory 52 self.tuxboot = self.arch 53 54 # Most Linux's use ttyS0 for their serial port 55 self.console = "ttyS0" 56 57 # Does the machine shutdown QEMU nicely on "halt" 58 self.wait_for_shutdown = True 59 60 self.root = "vda" 61 62 # Occasionally we need extra devices to hook things up 63 self.extradev = None 64 65 self.qemu_img = get_qemu_img(self) 66 67 def wait_for_console_pattern(self, success_message, vm=None): 68 wait_for_console_pattern(self, success_message, 69 failure_message='Kernel panic - not syncing', 70 vm=vm) 71 72 def fetch_tuxrun_assets(self, kernel_asset, rootfs_asset, dtb_asset=None): 73 """ 74 Fetch the TuxBoot assets. 75 """ 76 kernel_image = kernel_asset.fetch() 77 disk_image_zst = rootfs_asset.fetch() 78 79 disk_image = self.workdir + "/rootfs.ext4" 80 81 run_cmd([self.zstd, "-f", "-d", disk_image_zst, 82 "-o", disk_image]) 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