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