xref: /openbmc/qemu/tests/functional/qemu_test/tuxruntest.py (revision f8c54844175922d77faa8b586f451e379d53a191)
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        self.kcmd_line = self.KERNEL_COMMON_COMMAND_LINE
81        self.kcmd_line += f" root=/dev/{self.root}"
82        self.kcmd_line += f" console={self.console}"
83
84        self.vm.add_args('-kernel', kernel,
85                         '-append', self.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(self.kcmd_line)
107        self.wait_for_console_pattern('tuxtest login:')
108        exec_command_and_wait_for_pattern(self, 'root', ps1)
109        exec_command_and_wait_for_pattern(self, 'cat /proc/interrupts', ps1)
110        exec_command_and_wait_for_pattern(self, 'cat /proc/self/maps', ps1)
111        exec_command_and_wait_for_pattern(self, 'uname -a', ps1)
112        exec_command_and_wait_for_pattern(self, 'halt', haltmsg)
113
114        # Wait for VM to shut down gracefully if it can
115        if self.wait_for_shutdown:
116            self.vm.wait()
117        else:
118            self.vm.shutdown()
119
120    def common_tuxrun(self,
121                      kernel_asset,
122                      rootfs_asset,
123                      dtb_asset=None,
124                      drive="virtio-blk-device",
125                      haltmsg="reboot: System halted",
126                      console_index=0):
127        """
128        Common path for LKFT tests. Unless we need to do something
129        special with the command line we can process most things using
130        the tag metadata.
131        """
132        (kernel, disk, dtb) = self.fetch_tuxrun_assets(kernel_asset, rootfs_asset,
133                                                       dtb_asset)
134
135        self.prepare_run(kernel, disk, drive, dtb, console_index)
136        self.vm.launch()
137        self.run_tuxtest_tests(haltmsg)
138        os.remove(disk)
139