18720471eSThomas Huth# Test class for testing the boot process of a Linux kernel
28720471eSThomas Huth#
38720471eSThomas Huth# This work is licensed under the terms of the GNU GPL, version 2 or
48720471eSThomas Huth# later.  See the COPYING file in the top-level directory.
58720471eSThomas Huth
68720471eSThomas Huthimport os
78720471eSThomas Huth
88720471eSThomas Huthfrom .testcase import QemuSystemTest
98720471eSThomas Huthfrom .cmd import run_cmd, wait_for_console_pattern
108720471eSThomas Huthfrom .utils import archive_extract
118720471eSThomas Huth
128720471eSThomas Huthclass LinuxKernelTest(QemuSystemTest):
138720471eSThomas Huth    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
148720471eSThomas Huth
158720471eSThomas Huth    def wait_for_console_pattern(self, success_message, vm=None):
168720471eSThomas Huth        wait_for_console_pattern(self, success_message,
178720471eSThomas Huth                                 failure_message='Kernel panic - not syncing',
188720471eSThomas Huth                                 vm=vm)
198720471eSThomas Huth
20*d2a500ceSThomas Huth    def launch_kernel(self, kernel, initrd=None, dtb=None, console_index=0,
21*d2a500ceSThomas Huth                      wait_for=None):
22*d2a500ceSThomas Huth        self.vm.set_console(console_index=console_index)
23*d2a500ceSThomas Huth        self.vm.add_args('-kernel', kernel)
24*d2a500ceSThomas Huth        if initrd:
25*d2a500ceSThomas Huth                self.vm.add_args('-initrd', initrd)
26*d2a500ceSThomas Huth        if dtb:
27*d2a500ceSThomas Huth                self.vm.add_args('-dtb', dtb)
28*d2a500ceSThomas Huth        self.vm.launch()
29*d2a500ceSThomas Huth        if wait_for:
30*d2a500ceSThomas Huth                self.wait_for_console_pattern(wait_for)
31*d2a500ceSThomas Huth
328720471eSThomas Huth    def extract_from_deb(self, deb_path, path):
338720471eSThomas Huth        """
348720471eSThomas Huth        Extracts a file from a deb package into the test workdir
358720471eSThomas Huth
368720471eSThomas Huth        :param deb_path: path to the deb archive
378720471eSThomas Huth        :param path: path within the deb archive of the file to be extracted
388720471eSThomas Huth        :returns: path of the extracted file
398720471eSThomas Huth        """
408720471eSThomas Huth        cwd = os.getcwd()
418720471eSThomas Huth        os.chdir(self.workdir)
428720471eSThomas Huth        (stdout, stderr, ret) = run_cmd(['ar', 't', deb_path])
438720471eSThomas Huth        file_path = stdout.split()[2]
448720471eSThomas Huth        run_cmd(['ar', 'x', deb_path, file_path])
458720471eSThomas Huth        archive_extract(file_path, self.workdir)
468720471eSThomas Huth        os.chdir(cwd)
478720471eSThomas Huth        # Return complete path to extracted file.  Because callers to
488720471eSThomas Huth        # extract_from_deb() specify 'path' with a leading slash, it is
498720471eSThomas Huth        # necessary to use os.path.relpath() as otherwise os.path.join()
508720471eSThomas Huth        # interprets it as an absolute path and drops the self.workdir part.
518720471eSThomas Huth        return os.path.normpath(os.path.join(self.workdir,
528720471eSThomas Huth                                             os.path.relpath(path, '/')))
538720471eSThomas Huth
54