xref: /qemu/tests/functional/qemu_test/tuxruntest.py (revision bc65ae696104c15e52a5e26f225b689e2c2cdba6)
1c1b24f0fSThomas Huth# Functional test that boots known good tuxboot images the same way
2c1b24f0fSThomas Huth# that tuxrun (www.tuxrun.org) does. This tool is used by things like
3c1b24f0fSThomas Huth# the LKFT project to run regression tests on kernels.
4c1b24f0fSThomas Huth#
5c1b24f0fSThomas Huth# Copyright (c) 2023 Linaro Ltd.
6c1b24f0fSThomas Huth#
7c1b24f0fSThomas Huth# Author:
8c1b24f0fSThomas Huth#  Alex Bennée <alex.bennee@linaro.org>
9c1b24f0fSThomas Huth#
10c1b24f0fSThomas Huth# SPDX-License-Identifier: GPL-2.0-or-later
11c1b24f0fSThomas Huth
12c1b24f0fSThomas Huthimport os
13c9daf680SDaniel P. Berrangéimport stat
1437e9b19cSDaniel P. Berrangéfrom subprocess import check_call, DEVNULL
15c1b24f0fSThomas Huth
16c1b24f0fSThomas Huthfrom qemu_test import QemuSystemTest
178a6253a4SDaniel P. Berrangéfrom qemu_test import exec_command_and_wait_for_pattern
18c1b24f0fSThomas Huthfrom qemu_test import wait_for_console_pattern
1937e9b19cSDaniel P. Berrangéfrom qemu_test import which, get_qemu_img
20c1b24f0fSThomas Huth
21c1b24f0fSThomas Huthclass TuxRunBaselineTest(QemuSystemTest):
22c1b24f0fSThomas Huth
23c1b24f0fSThomas Huth    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0'
24c1b24f0fSThomas Huth    # Tests are ~10-40s, allow for --debug/--enable-gcov overhead
25c1b24f0fSThomas Huth    timeout = 100
26c1b24f0fSThomas Huth
27c1b24f0fSThomas Huth    def setUp(self):
28c1b24f0fSThomas Huth        super().setUp()
29c1b24f0fSThomas Huth
30c1b24f0fSThomas Huth        # We need zstd for all the tuxrun tests
319132fff8SDaniel P. Berrangé        if which('zstd') is None:
329132fff8SDaniel P. Berrangé            self.skipTest("zstd not found in $PATH")
33c1b24f0fSThomas Huth
34c1b24f0fSThomas Huth        # Pre-init TuxRun specific settings: Most machines work with
35c1b24f0fSThomas Huth        # reasonable defaults but we sometimes need to tweak the
36c1b24f0fSThomas Huth        # config. To avoid open coding everything we store all these
37c1b24f0fSThomas Huth        # details in the metadata for each test.
38c1b24f0fSThomas Huth
39c1b24f0fSThomas Huth        # The tuxboot tag matches the root directory
40c1b24f0fSThomas Huth        self.tuxboot = self.arch
41c1b24f0fSThomas Huth
42c1b24f0fSThomas Huth        # Most Linux's use ttyS0 for their serial port
43c1b24f0fSThomas Huth        self.console = "ttyS0"
44c1b24f0fSThomas Huth
45c1b24f0fSThomas Huth        # Does the machine shutdown QEMU nicely on "halt"
46c1b24f0fSThomas Huth        self.wait_for_shutdown = True
47c1b24f0fSThomas Huth
48c1b24f0fSThomas Huth        self.root = "vda"
49c1b24f0fSThomas Huth
50c1b24f0fSThomas Huth        # Occasionally we need extra devices to hook things up
51c1b24f0fSThomas Huth        self.extradev = None
52c1b24f0fSThomas Huth
53c1b24f0fSThomas Huth        self.qemu_img = get_qemu_img(self)
54c1b24f0fSThomas Huth
55c1b24f0fSThomas Huth    def wait_for_console_pattern(self, success_message, vm=None):
56c1b24f0fSThomas Huth        wait_for_console_pattern(self, success_message,
57c1b24f0fSThomas Huth                                 failure_message='Kernel panic - not syncing',
58c1b24f0fSThomas Huth                                 vm=vm)
59c1b24f0fSThomas Huth
60c1b24f0fSThomas Huth    def fetch_tuxrun_assets(self, kernel_asset, rootfs_asset, dtb_asset=None):
61c1b24f0fSThomas Huth        """
62c1b24f0fSThomas Huth        Fetch the TuxBoot assets.
63c1b24f0fSThomas Huth        """
64c1b24f0fSThomas Huth        kernel_image =  kernel_asset.fetch()
653b9ec25eSAlex Bennée        disk_image = self.uncompress(rootfs_asset)
66c1b24f0fSThomas Huth        dtb = dtb_asset.fetch() if dtb_asset is not None else None
67c1b24f0fSThomas Huth
68c9daf680SDaniel P. Berrangé        return (kernel_image, disk_image, dtb)
69c1b24f0fSThomas Huth
70c1b24f0fSThomas Huth    def prepare_run(self, kernel, disk, drive, dtb=None, console_index=0):
71c1b24f0fSThomas Huth        """
72c1b24f0fSThomas Huth        Setup to run and add the common parameters to the system
73c1b24f0fSThomas Huth        """
74c1b24f0fSThomas Huth        self.vm.set_console(console_index=console_index)
75c1b24f0fSThomas Huth
76c1b24f0fSThomas Huth        # all block devices are raw ext4's
77c1b24f0fSThomas Huth        blockdev = "driver=raw,file.driver=file," \
78c1b24f0fSThomas Huth            + f"file.filename={disk},node-name=hd0"
79c1b24f0fSThomas Huth
80*bc65ae69SThomas Huth        self.kcmd_line = self.KERNEL_COMMON_COMMAND_LINE
81*bc65ae69SThomas Huth        self.kcmd_line += f" root=/dev/{self.root}"
82*bc65ae69SThomas Huth        self.kcmd_line += f" console={self.console}"
83c1b24f0fSThomas Huth
84c1b24f0fSThomas Huth        self.vm.add_args('-kernel', kernel,
85*bc65ae69SThomas Huth                         '-append', self.kcmd_line,
86c1b24f0fSThomas Huth                         '-blockdev', blockdev)
87c1b24f0fSThomas Huth
88c1b24f0fSThomas Huth        # Sometimes we need extra devices attached
89c1b24f0fSThomas Huth        if self.extradev:
90c1b24f0fSThomas Huth            self.vm.add_args('-device', self.extradev)
91c1b24f0fSThomas Huth
92c1b24f0fSThomas Huth        self.vm.add_args('-device',
93c1b24f0fSThomas Huth                         f"{drive},drive=hd0")
94c1b24f0fSThomas Huth
95c1b24f0fSThomas Huth        # Some machines need an explicit DTB
96c1b24f0fSThomas Huth        if dtb:
97c1b24f0fSThomas Huth            self.vm.add_args('-dtb', dtb)
98c1b24f0fSThomas Huth
99c1b24f0fSThomas Huth    def run_tuxtest_tests(self, haltmsg):
100c1b24f0fSThomas Huth        """
101c1b24f0fSThomas Huth        Wait for the system to boot up, wait for the login prompt and
102c1b24f0fSThomas Huth        then do a few things on the console. Trigger a shutdown and
103c1b24f0fSThomas Huth        wait to exit cleanly.
104c1b24f0fSThomas Huth        """
10597d79319SDaniel P. Berrangé        ps1='root@tuxtest:~#'
106*bc65ae69SThomas Huth        self.wait_for_console_pattern(self.kcmd_line)
10797d79319SDaniel P. Berrangé        self.wait_for_console_pattern('tuxtest login:')
10897d79319SDaniel P. Berrangé        exec_command_and_wait_for_pattern(self, 'root', ps1)
10997d79319SDaniel P. Berrangé        exec_command_and_wait_for_pattern(self, 'cat /proc/interrupts', ps1)
11097d79319SDaniel P. Berrangé        exec_command_and_wait_for_pattern(self, 'cat /proc/self/maps', ps1)
11197d79319SDaniel P. Berrangé        exec_command_and_wait_for_pattern(self, 'uname -a', ps1)
112c1b24f0fSThomas Huth        exec_command_and_wait_for_pattern(self, 'halt', haltmsg)
113c1b24f0fSThomas Huth
114c1b24f0fSThomas Huth        # Wait for VM to shut down gracefully if it can
115c1b24f0fSThomas Huth        if self.wait_for_shutdown:
116c1b24f0fSThomas Huth            self.vm.wait()
117c1b24f0fSThomas Huth        else:
118c1b24f0fSThomas Huth            self.vm.shutdown()
119c1b24f0fSThomas Huth
120c1b24f0fSThomas Huth    def common_tuxrun(self,
121c1b24f0fSThomas Huth                      kernel_asset,
122c1b24f0fSThomas Huth                      rootfs_asset,
123c1b24f0fSThomas Huth                      dtb_asset=None,
124c1b24f0fSThomas Huth                      drive="virtio-blk-device",
125c1b24f0fSThomas Huth                      haltmsg="reboot: System halted",
126c1b24f0fSThomas Huth                      console_index=0):
127c1b24f0fSThomas Huth        """
128c1b24f0fSThomas Huth        Common path for LKFT tests. Unless we need to do something
129c1b24f0fSThomas Huth        special with the command line we can process most things using
130c1b24f0fSThomas Huth        the tag metadata.
131c1b24f0fSThomas Huth        """
132c1b24f0fSThomas Huth        (kernel, disk, dtb) = self.fetch_tuxrun_assets(kernel_asset, rootfs_asset,
133c1b24f0fSThomas Huth                                                       dtb_asset)
134c1b24f0fSThomas Huth
135c1b24f0fSThomas Huth        self.prepare_run(kernel, disk, drive, dtb, console_index)
136c1b24f0fSThomas Huth        self.vm.launch()
137c1b24f0fSThomas Huth        self.run_tuxtest_tests(haltmsg)
138c1b24f0fSThomas Huth        os.remove(disk)
139