xref: /openbmc/qemu/tests/avocado/boot_linux_console.py (revision ba182a693fe15a4f6f2a04e8ecb865c2630e5a16)
1# Functional test that boots a Linux kernel and checks the console
2#
3# Copyright (c) 2018 Red Hat, Inc.
4#
5# Author:
6#  Cleber Rosa <crosa@redhat.com>
7#
8# This work is licensed under the terms of the GNU GPL, version 2 or
9# later.  See the COPYING file in the top-level directory.
10
11import os
12import lzma
13import gzip
14import shutil
15
16from avocado import skip
17from avocado import skipUnless
18from avocado import skipUnless
19from avocado_qemu import QemuSystemTest
20from avocado_qemu import exec_command
21from avocado_qemu import exec_command_and_wait_for_pattern
22from avocado_qemu import interrupt_interactive_console_until_pattern
23from avocado_qemu import wait_for_console_pattern
24from avocado.utils import process
25from avocado.utils import archive
26
27class LinuxKernelTest(QemuSystemTest):
28    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
29
30    def wait_for_console_pattern(self, success_message, vm=None):
31        wait_for_console_pattern(self, success_message,
32                                 failure_message='Kernel panic - not syncing',
33                                 vm=vm)
34
35    def extract_from_deb(self, deb, path):
36        """
37        Extracts a file from a deb package into the test workdir
38
39        :param deb: path to the deb archive
40        :param path: path within the deb archive of the file to be extracted
41        :returns: path of the extracted file
42        """
43        cwd = os.getcwd()
44        os.chdir(self.workdir)
45        file_path = process.run("ar t %s" % deb).stdout_text.split()[2]
46        process.run("ar x %s %s" % (deb, file_path))
47        archive.extract(file_path, self.workdir)
48        os.chdir(cwd)
49        # Return complete path to extracted file.  Because callers to
50        # extract_from_deb() specify 'path' with a leading slash, it is
51        # necessary to use os.path.relpath() as otherwise os.path.join()
52        # interprets it as an absolute path and drops the self.workdir part.
53        return os.path.normpath(os.path.join(self.workdir,
54                                             os.path.relpath(path, '/')))
55
56    def extract_from_rpm(self, rpm, path):
57        """
58        Extracts a file from an RPM package into the test workdir.
59
60        :param rpm: path to the rpm archive
61        :param path: path within the rpm archive of the file to be extracted
62                     needs to be a relative path (starting with './') because
63                     cpio(1), which is used to extract the file, expects that.
64        :returns: path of the extracted file
65        """
66        cwd = os.getcwd()
67        os.chdir(self.workdir)
68        process.run("rpm2cpio %s | cpio -id %s" % (rpm, path), shell=True)
69        os.chdir(cwd)
70        return os.path.normpath(os.path.join(self.workdir, path))
71
72class BootLinuxConsole(LinuxKernelTest):
73    """
74    Boots a Linux kernel and checks that the console is operational and the
75    kernel command line is properly passed from QEMU to the kernel
76    """
77    timeout = 90
78
79    def test_x86_64_pc(self):
80        """
81        :avocado: tags=arch:x86_64
82        :avocado: tags=machine:pc
83        """
84        kernel_url = ('https://archives.fedoraproject.org/pub/archive/fedora'
85                      '/linux/releases/29/Everything/x86_64/os/images/pxeboot'
86                      '/vmlinuz')
87        kernel_hash = '23bebd2680757891cf7adedb033532163a792495'
88        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
89
90        self.vm.set_console()
91        kernel_command_line = self.KERNEL_COMMON_COMMAND_LINE + 'console=ttyS0'
92        self.vm.add_args('-kernel', kernel_path,
93                         '-append', kernel_command_line)
94        self.vm.launch()
95        console_pattern = 'Kernel command line: %s' % kernel_command_line
96        self.wait_for_console_pattern(console_pattern)
97