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 6*2c92ecb6SThomas Huthimport hashlib 7*2c92ecb6SThomas Huthimport urllib.request 8*2c92ecb6SThomas Huth 9*2c92ecb6SThomas Huthfrom .cmd import wait_for_console_pattern, exec_command_and_wait_for_pattern 108720471eSThomas Huthfrom .testcase import QemuSystemTest 11*2c92ecb6SThomas Huthfrom .utils import get_usernet_hostfwd_port 12512fe088SDaniel P. Berrangé 138720471eSThomas Huth 148720471eSThomas Huthclass LinuxKernelTest(QemuSystemTest): 158720471eSThomas Huth KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 ' 168720471eSThomas Huth 178720471eSThomas Huth def wait_for_console_pattern(self, success_message, vm=None): 188720471eSThomas Huth wait_for_console_pattern(self, success_message, 198720471eSThomas Huth failure_message='Kernel panic - not syncing', 208720471eSThomas Huth vm=vm) 218720471eSThomas Huth 22d2a500ceSThomas Huth def launch_kernel(self, kernel, initrd=None, dtb=None, console_index=0, 23d2a500ceSThomas Huth wait_for=None): 24d2a500ceSThomas Huth self.vm.set_console(console_index=console_index) 25d2a500ceSThomas Huth self.vm.add_args('-kernel', kernel) 26d2a500ceSThomas Huth if initrd: 27d2a500ceSThomas Huth self.vm.add_args('-initrd', initrd) 28d2a500ceSThomas Huth if dtb: 29d2a500ceSThomas Huth self.vm.add_args('-dtb', dtb) 30d2a500ceSThomas Huth self.vm.launch() 31d2a500ceSThomas Huth if wait_for: 32d2a500ceSThomas Huth self.wait_for_console_pattern(wait_for) 33*2c92ecb6SThomas Huth 34*2c92ecb6SThomas Huth def check_http_download(self, filename, hashsum, guestport=8080, 35*2c92ecb6SThomas Huth pythoncmd='python3 -m http.server'): 36*2c92ecb6SThomas Huth exec_command_and_wait_for_pattern(self, 37*2c92ecb6SThomas Huth f'{pythoncmd} {guestport} & sleep 1', 38*2c92ecb6SThomas Huth f'Serving HTTP on 0.0.0.0 port {guestport}') 39*2c92ecb6SThomas Huth hl = hashlib.sha256() 40*2c92ecb6SThomas Huth hostport = get_usernet_hostfwd_port(self.vm) 41*2c92ecb6SThomas Huth url = f'http://localhost:{hostport}{filename}' 42*2c92ecb6SThomas Huth self.log.info(f'Downloading {url} ...') 43*2c92ecb6SThomas Huth with urllib.request.urlopen(url) as response: 44*2c92ecb6SThomas Huth while True: 45*2c92ecb6SThomas Huth chunk = response.read(1 << 20) 46*2c92ecb6SThomas Huth if not chunk: 47*2c92ecb6SThomas Huth break 48*2c92ecb6SThomas Huth hl.update(chunk) 49*2c92ecb6SThomas Huth 50*2c92ecb6SThomas Huth digest = hl.hexdigest() 51*2c92ecb6SThomas Huth self.log.info(f'sha256sum of download is {digest}.') 52*2c92ecb6SThomas Huth self.assertEqual(digest, hashsum) 53