1# Test class for testing the boot process of a Linux kernel
2#
3# This work is licensed under the terms of the GNU GPL, version 2 or
4# later.  See the COPYING file in the top-level directory.
5
6import os
7
8from .testcase import QemuSystemTest
9from .cmd import run_cmd, wait_for_console_pattern
10from .utils import archive_extract
11
12class LinuxKernelTest(QemuSystemTest):
13    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
14
15    def wait_for_console_pattern(self, success_message, vm=None):
16        wait_for_console_pattern(self, success_message,
17                                 failure_message='Kernel panic - not syncing',
18                                 vm=vm)
19
20    def launch_kernel(self, kernel, initrd=None, dtb=None, console_index=0,
21                      wait_for=None):
22        self.vm.set_console(console_index=console_index)
23        self.vm.add_args('-kernel', kernel)
24        if initrd:
25                self.vm.add_args('-initrd', initrd)
26        if dtb:
27                self.vm.add_args('-dtb', dtb)
28        self.vm.launch()
29        if wait_for:
30                self.wait_for_console_pattern(wait_for)
31
32    def extract_from_deb(self, deb_path, path):
33        """
34        Extracts a file from a deb package into the test workdir
35
36        :param deb_path: path to the deb archive
37        :param path: path within the deb archive of the file to be extracted
38        :returns: path of the extracted file
39        """
40        cwd = os.getcwd()
41        os.chdir(self.workdir)
42        (stdout, stderr, ret) = run_cmd(['ar', 't', deb_path])
43        file_path = stdout.split()[2]
44        run_cmd(['ar', 'x', deb_path, file_path])
45        archive_extract(file_path, self.workdir)
46        os.chdir(cwd)
47        # Return complete path to extracted file.  Because callers to
48        # extract_from_deb() specify 'path' with a leading slash, it is
49        # necessary to use os.path.relpath() as otherwise os.path.join()
50        # interprets it as an absolute path and drops the self.workdir part.
51        return os.path.normpath(os.path.join(self.workdir,
52                                             os.path.relpath(path, '/')))
53
54