xref: /qemu/tests/functional/qemu_test/linuxkernel.py (revision 465a4b80e2afd93f2eb4c9f55233788ebfb47ac0)
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 hashlib
7import urllib.request
8
9from .cmd import wait_for_console_pattern, exec_command_and_wait_for_pattern
10from .testcase import QemuSystemTest
11from .utils import get_usernet_hostfwd_port
12
13
14class LinuxKernelTest(QemuSystemTest):
15    KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 '
16
17    def wait_for_console_pattern(self, success_message, vm=None):
18        wait_for_console_pattern(self, success_message,
19                                 failure_message='Kernel panic - not syncing',
20                                 vm=vm)
21
22    def launch_kernel(self, kernel, initrd=None, dtb=None, console_index=0,
23                      wait_for=None):
24        self.vm.set_console(console_index=console_index)
25        self.vm.add_args('-kernel', kernel)
26        if initrd:
27                self.vm.add_args('-initrd', initrd)
28        if dtb:
29                self.vm.add_args('-dtb', dtb)
30        self.vm.launch()
31        if wait_for:
32                self.wait_for_console_pattern(wait_for)
33
34    def check_http_download(self, filename, hashsum, guestport=8080,
35                            pythoncmd='python3 -m http.server'):
36        exec_command_and_wait_for_pattern(self,
37                        f'{pythoncmd} {guestport} & sleep 1',
38                        f'Serving HTTP on 0.0.0.0 port {guestport}')
39        hl = hashlib.sha256()
40        hostport = get_usernet_hostfwd_port(self.vm)
41        url = f'http://localhost:{hostport}{filename}'
42        self.log.info(f'Downloading {url} ...')
43        with urllib.request.urlopen(url) as response:
44            while True:
45                chunk = response.read(1 << 20)
46                if not chunk:
47                    break
48                hl.update(chunk)
49
50        digest = hl.hexdigest()
51        self.log.info(f'sha256sum of download is {digest}.')
52        self.assertEqual(digest, hashsum)
53