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