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