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 = self.uncompress(rootfs_asset) 77 dtb = dtb_asset.fetch() if dtb_asset is not None else None 78 79 return (kernel_image, disk_image, dtb) 80 81 def prepare_run(self, kernel, disk, drive, dtb=None, console_index=0): 82 """ 83 Setup to run and add the common parameters to the system 84 """ 85 self.vm.set_console(console_index=console_index) 86 87 # all block devices are raw ext4's 88 blockdev = "driver=raw,file.driver=file," \ 89 + f"file.filename={disk},node-name=hd0" 90 91 kcmd_line = self.KERNEL_COMMON_COMMAND_LINE 92 kcmd_line += f" root=/dev/{self.root}" 93 kcmd_line += f" console={self.console}" 94 95 self.vm.add_args('-kernel', kernel, 96 '-append', kcmd_line, 97 '-blockdev', blockdev) 98 99 # Sometimes we need extra devices attached 100 if self.extradev: 101 self.vm.add_args('-device', self.extradev) 102 103 self.vm.add_args('-device', 104 f"{drive},drive=hd0") 105 106 # Some machines need an explicit DTB 107 if dtb: 108 self.vm.add_args('-dtb', dtb) 109 110 def run_tuxtest_tests(self, haltmsg): 111 """ 112 Wait for the system to boot up, wait for the login prompt and 113 then do a few things on the console. Trigger a shutdown and 114 wait to exit cleanly. 115 """ 116 ps1='root@tuxtest:~#' 117 self.wait_for_console_pattern('tuxtest login:') 118 exec_command_and_wait_for_pattern(self, 'root', ps1) 119 exec_command_and_wait_for_pattern(self, 'cat /proc/interrupts', ps1) 120 exec_command_and_wait_for_pattern(self, 'cat /proc/self/maps', ps1) 121 exec_command_and_wait_for_pattern(self, 'uname -a', ps1) 122 exec_command_and_wait_for_pattern(self, 'halt', haltmsg) 123 124 # Wait for VM to shut down gracefully if it can 125 if self.wait_for_shutdown: 126 self.vm.wait() 127 else: 128 self.vm.shutdown() 129 130 def common_tuxrun(self, 131 kernel_asset, 132 rootfs_asset, 133 dtb_asset=None, 134 drive="virtio-blk-device", 135 haltmsg="reboot: System halted", 136 console_index=0): 137 """ 138 Common path for LKFT tests. Unless we need to do something 139 special with the command line we can process most things using 140 the tag metadata. 141 """ 142 (kernel, disk, dtb) = self.fetch_tuxrun_assets(kernel_asset, rootfs_asset, 143 dtb_asset) 144 145 self.prepare_run(kernel, disk, drive, dtb, console_index) 146 self.vm.launch() 147 self.run_tuxtest_tests(haltmsg) 148 os.remove(disk) 149