1*bbbd9b6eSWillian Rampazzo# Functional test that boots a VM and run commands via a SSH session 2*bbbd9b6eSWillian Rampazzo# 3*bbbd9b6eSWillian Rampazzo# Copyright (c) Philippe Mathieu-Daudé <f4bug@amsat.org> 4*bbbd9b6eSWillian Rampazzo# 5*bbbd9b6eSWillian Rampazzo# This work is licensed under the terms of the GNU GPL, version 2 or 6*bbbd9b6eSWillian Rampazzo# later. See the COPYING file in the top-level directory. 7*bbbd9b6eSWillian Rampazzo 8*bbbd9b6eSWillian Rampazzoimport os 9*bbbd9b6eSWillian Rampazzoimport re 10*bbbd9b6eSWillian Rampazzoimport base64 11*bbbd9b6eSWillian Rampazzoimport logging 12*bbbd9b6eSWillian Rampazzoimport time 13*bbbd9b6eSWillian Rampazzo 14*bbbd9b6eSWillian Rampazzofrom avocado import skipUnless 15*bbbd9b6eSWillian Rampazzofrom avocado_qemu import Test, LinuxSSHMixIn 16*bbbd9b6eSWillian Rampazzofrom avocado_qemu import wait_for_console_pattern 17*bbbd9b6eSWillian Rampazzofrom avocado.utils import process 18*bbbd9b6eSWillian Rampazzofrom avocado.utils import archive 19*bbbd9b6eSWillian Rampazzofrom avocado.utils import ssh 20*bbbd9b6eSWillian Rampazzo 21*bbbd9b6eSWillian Rampazzo 22*bbbd9b6eSWillian Rampazzo@skipUnless(os.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout') 23*bbbd9b6eSWillian Rampazzo@skipUnless(ssh.SSH_CLIENT_BINARY, 'No SSH client available') 24*bbbd9b6eSWillian Rampazzoclass LinuxSSH(Test, LinuxSSHMixIn): 25*bbbd9b6eSWillian Rampazzo 26*bbbd9b6eSWillian Rampazzo timeout = 150 # Not for 'configure --enable-debug --enable-debug-tcg' 27*bbbd9b6eSWillian Rampazzo 28*bbbd9b6eSWillian Rampazzo KERNEL_COMMON_COMMAND_LINE = 'printk.time=0 ' 29*bbbd9b6eSWillian Rampazzo VM_IP = '127.0.0.1' 30*bbbd9b6eSWillian Rampazzo 31*bbbd9b6eSWillian Rampazzo BASE_URL = 'https://people.debian.org/~aurel32/qemu/' 32*bbbd9b6eSWillian Rampazzo IMAGE_INFO = { 33*bbbd9b6eSWillian Rampazzo 'be': {'base_url': 'mips', 34*bbbd9b6eSWillian Rampazzo 'image_name': 'debian_wheezy_mips_standard.qcow2', 35*bbbd9b6eSWillian Rampazzo 'image_hash': '8987a63270df67345b2135a6b7a4885a35e392d5', 36*bbbd9b6eSWillian Rampazzo 'kernel_hash': { 37*bbbd9b6eSWillian Rampazzo 32: '592e384a4edc16dade52a6cd5c785c637bcbc9ad', 38*bbbd9b6eSWillian Rampazzo 64: 'db6eea7de35d36c77d8c165b6bcb222e16eb91db'} 39*bbbd9b6eSWillian Rampazzo }, 40*bbbd9b6eSWillian Rampazzo 'le': {'base_url': 'mipsel', 41*bbbd9b6eSWillian Rampazzo 'image_name': 'debian_wheezy_mipsel_standard.qcow2', 42*bbbd9b6eSWillian Rampazzo 'image_hash': '7866764d9de3ef536ffca24c9fb9f04ffdb45802', 43*bbbd9b6eSWillian Rampazzo 'kernel_hash': { 44*bbbd9b6eSWillian Rampazzo 32: 'a66bea5a8adaa2cb3d36a1d4e0ccdb01be8f6c2a', 45*bbbd9b6eSWillian Rampazzo 64: '6a7f77245acf231415a0e8b725d91ed2f3487794'} 46*bbbd9b6eSWillian Rampazzo } 47*bbbd9b6eSWillian Rampazzo } 48*bbbd9b6eSWillian Rampazzo CPU_INFO = { 49*bbbd9b6eSWillian Rampazzo 32: {'cpu': 'MIPS 24Kc', 'kernel_release': '3.2.0-4-4kc-malta'}, 50*bbbd9b6eSWillian Rampazzo 64: {'cpu': 'MIPS 20Kc', 'kernel_release': '3.2.0-4-5kc-malta'} 51*bbbd9b6eSWillian Rampazzo } 52*bbbd9b6eSWillian Rampazzo 53*bbbd9b6eSWillian Rampazzo def get_url(self, endianess, path=''): 54*bbbd9b6eSWillian Rampazzo qkey = {'le': 'el', 'be': ''} 55*bbbd9b6eSWillian Rampazzo return '%s/mips%s/%s' % (self.BASE_URL, qkey[endianess], path) 56*bbbd9b6eSWillian Rampazzo 57*bbbd9b6eSWillian Rampazzo def get_image_info(self, endianess): 58*bbbd9b6eSWillian Rampazzo dinfo = self.IMAGE_INFO[endianess] 59*bbbd9b6eSWillian Rampazzo image_url = self.get_url(endianess, dinfo['image_name']) 60*bbbd9b6eSWillian Rampazzo image_hash = dinfo['image_hash'] 61*bbbd9b6eSWillian Rampazzo return (image_url, image_hash) 62*bbbd9b6eSWillian Rampazzo 63*bbbd9b6eSWillian Rampazzo def get_kernel_info(self, endianess, wordsize): 64*bbbd9b6eSWillian Rampazzo minfo = self.CPU_INFO[wordsize] 65*bbbd9b6eSWillian Rampazzo kernel_url = self.get_url(endianess, 66*bbbd9b6eSWillian Rampazzo 'vmlinux-%s' % minfo['kernel_release']) 67*bbbd9b6eSWillian Rampazzo kernel_hash = self.IMAGE_INFO[endianess]['kernel_hash'][wordsize] 68*bbbd9b6eSWillian Rampazzo return kernel_url, kernel_hash 69*bbbd9b6eSWillian Rampazzo 70*bbbd9b6eSWillian Rampazzo def ssh_disconnect_vm(self): 71*bbbd9b6eSWillian Rampazzo self.ssh_session.quit() 72*bbbd9b6eSWillian Rampazzo 73*bbbd9b6eSWillian Rampazzo def boot_debian_wheezy_image_and_ssh_login(self, endianess, kernel_path): 74*bbbd9b6eSWillian Rampazzo image_url, image_hash = self.get_image_info(endianess) 75*bbbd9b6eSWillian Rampazzo image_path = self.fetch_asset(image_url, asset_hash=image_hash) 76*bbbd9b6eSWillian Rampazzo 77*bbbd9b6eSWillian Rampazzo self.vm.set_console() 78*bbbd9b6eSWillian Rampazzo kernel_command_line = (self.KERNEL_COMMON_COMMAND_LINE 79*bbbd9b6eSWillian Rampazzo + 'console=ttyS0 root=/dev/sda1') 80*bbbd9b6eSWillian Rampazzo self.vm.add_args('-no-reboot', 81*bbbd9b6eSWillian Rampazzo '-kernel', kernel_path, 82*bbbd9b6eSWillian Rampazzo '-append', kernel_command_line, 83*bbbd9b6eSWillian Rampazzo '-drive', 'file=%s,snapshot=on' % image_path, 84*bbbd9b6eSWillian Rampazzo '-netdev', 'user,id=vnet,hostfwd=:127.0.0.1:0-:22', 85*bbbd9b6eSWillian Rampazzo '-device', 'pcnet,netdev=vnet') 86*bbbd9b6eSWillian Rampazzo self.vm.launch() 87*bbbd9b6eSWillian Rampazzo 88*bbbd9b6eSWillian Rampazzo self.log.info('VM launched, waiting for sshd') 89*bbbd9b6eSWillian Rampazzo console_pattern = 'Starting OpenBSD Secure Shell server: sshd' 90*bbbd9b6eSWillian Rampazzo wait_for_console_pattern(self, console_pattern, 'Oops') 91*bbbd9b6eSWillian Rampazzo self.log.info('sshd ready') 92*bbbd9b6eSWillian Rampazzo 93*bbbd9b6eSWillian Rampazzo self.ssh_connect('root', 'root', False) 94*bbbd9b6eSWillian Rampazzo 95*bbbd9b6eSWillian Rampazzo def shutdown_via_ssh(self): 96*bbbd9b6eSWillian Rampazzo self.ssh_command('poweroff') 97*bbbd9b6eSWillian Rampazzo self.ssh_disconnect_vm() 98*bbbd9b6eSWillian Rampazzo wait_for_console_pattern(self, 'Power down', 'Oops') 99*bbbd9b6eSWillian Rampazzo 100*bbbd9b6eSWillian Rampazzo def ssh_command_output_contains(self, cmd, exp): 101*bbbd9b6eSWillian Rampazzo stdout, _ = self.ssh_command(cmd) 102*bbbd9b6eSWillian Rampazzo for line in stdout: 103*bbbd9b6eSWillian Rampazzo if exp in line: 104*bbbd9b6eSWillian Rampazzo break 105*bbbd9b6eSWillian Rampazzo else: 106*bbbd9b6eSWillian Rampazzo self.fail('"%s" output does not contain "%s"' % (cmd, exp)) 107*bbbd9b6eSWillian Rampazzo 108*bbbd9b6eSWillian Rampazzo def run_common_commands(self, wordsize): 109*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 110*bbbd9b6eSWillian Rampazzo 'cat /proc/cpuinfo', 111*bbbd9b6eSWillian Rampazzo self.CPU_INFO[wordsize]['cpu']) 112*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 113*bbbd9b6eSWillian Rampazzo 'uname -m', 114*bbbd9b6eSWillian Rampazzo 'mips') 115*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 116*bbbd9b6eSWillian Rampazzo 'uname -r', 117*bbbd9b6eSWillian Rampazzo self.CPU_INFO[wordsize]['kernel_release']) 118*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 119*bbbd9b6eSWillian Rampazzo 'cat /proc/interrupts', 120*bbbd9b6eSWillian Rampazzo 'XT-PIC timer') 121*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 122*bbbd9b6eSWillian Rampazzo 'cat /proc/interrupts', 123*bbbd9b6eSWillian Rampazzo 'XT-PIC i8042') 124*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 125*bbbd9b6eSWillian Rampazzo 'cat /proc/interrupts', 126*bbbd9b6eSWillian Rampazzo 'XT-PIC serial') 127*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 128*bbbd9b6eSWillian Rampazzo 'cat /proc/interrupts', 129*bbbd9b6eSWillian Rampazzo 'XT-PIC ata_piix') 130*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 131*bbbd9b6eSWillian Rampazzo 'cat /proc/interrupts', 132*bbbd9b6eSWillian Rampazzo 'XT-PIC eth0') 133*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 134*bbbd9b6eSWillian Rampazzo 'cat /proc/devices', 135*bbbd9b6eSWillian Rampazzo 'input') 136*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 137*bbbd9b6eSWillian Rampazzo 'cat /proc/devices', 138*bbbd9b6eSWillian Rampazzo 'usb') 139*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 140*bbbd9b6eSWillian Rampazzo 'cat /proc/devices', 141*bbbd9b6eSWillian Rampazzo 'fb') 142*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 143*bbbd9b6eSWillian Rampazzo 'cat /proc/ioports', 144*bbbd9b6eSWillian Rampazzo ' : serial') 145*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 146*bbbd9b6eSWillian Rampazzo 'cat /proc/ioports', 147*bbbd9b6eSWillian Rampazzo ' : ata_piix') 148*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 149*bbbd9b6eSWillian Rampazzo 'cat /proc/ioports', 150*bbbd9b6eSWillian Rampazzo ' : piix4_smbus') 151*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 152*bbbd9b6eSWillian Rampazzo 'lspci -d 11ab:4620', 153*bbbd9b6eSWillian Rampazzo 'GT-64120') 154*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 155*bbbd9b6eSWillian Rampazzo 'cat /sys/bus/i2c/devices/i2c-0/name', 156*bbbd9b6eSWillian Rampazzo 'SMBus PIIX4 adapter') 157*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 158*bbbd9b6eSWillian Rampazzo 'cat /proc/mtd', 159*bbbd9b6eSWillian Rampazzo 'YAMON') 160*bbbd9b6eSWillian Rampazzo # Empty 'Board Config' (64KB) 161*bbbd9b6eSWillian Rampazzo self.ssh_command_output_contains( 162*bbbd9b6eSWillian Rampazzo 'md5sum /dev/mtd2ro', 163*bbbd9b6eSWillian Rampazzo '0dfbe8aa4c20b52e1b8bf3cb6cbdf193') 164*bbbd9b6eSWillian Rampazzo 165*bbbd9b6eSWillian Rampazzo def check_mips_malta(self, uname_m, endianess): 166*bbbd9b6eSWillian Rampazzo wordsize = 64 if '64' in uname_m else 32 167*bbbd9b6eSWillian Rampazzo kernel_url, kernel_hash = self.get_kernel_info(endianess, wordsize) 168*bbbd9b6eSWillian Rampazzo kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash) 169*bbbd9b6eSWillian Rampazzo self.boot_debian_wheezy_image_and_ssh_login(endianess, kernel_path) 170*bbbd9b6eSWillian Rampazzo 171*bbbd9b6eSWillian Rampazzo stdout, _ = self.ssh_command('uname -a') 172*bbbd9b6eSWillian Rampazzo self.assertIn(True, [uname_m + " GNU/Linux" in line for line in stdout]) 173*bbbd9b6eSWillian Rampazzo 174*bbbd9b6eSWillian Rampazzo self.run_common_commands(wordsize) 175*bbbd9b6eSWillian Rampazzo self.shutdown_via_ssh() 176*bbbd9b6eSWillian Rampazzo # Wait for VM to shut down gracefully 177*bbbd9b6eSWillian Rampazzo self.vm.wait() 178*bbbd9b6eSWillian Rampazzo 179*bbbd9b6eSWillian Rampazzo def test_mips_malta32eb_kernel3_2_0(self): 180*bbbd9b6eSWillian Rampazzo """ 181*bbbd9b6eSWillian Rampazzo :avocado: tags=arch:mips 182*bbbd9b6eSWillian Rampazzo :avocado: tags=endian:big 183*bbbd9b6eSWillian Rampazzo :avocado: tags=device:pcnet32 184*bbbd9b6eSWillian Rampazzo """ 185*bbbd9b6eSWillian Rampazzo self.check_mips_malta('mips', 'be') 186*bbbd9b6eSWillian Rampazzo 187*bbbd9b6eSWillian Rampazzo def test_mips_malta32el_kernel3_2_0(self): 188*bbbd9b6eSWillian Rampazzo """ 189*bbbd9b6eSWillian Rampazzo :avocado: tags=arch:mipsel 190*bbbd9b6eSWillian Rampazzo :avocado: tags=endian:little 191*bbbd9b6eSWillian Rampazzo :avocado: tags=device:pcnet32 192*bbbd9b6eSWillian Rampazzo """ 193*bbbd9b6eSWillian Rampazzo self.check_mips_malta('mips', 'le') 194*bbbd9b6eSWillian Rampazzo 195*bbbd9b6eSWillian Rampazzo def test_mips_malta64eb_kernel3_2_0(self): 196*bbbd9b6eSWillian Rampazzo """ 197*bbbd9b6eSWillian Rampazzo :avocado: tags=arch:mips64 198*bbbd9b6eSWillian Rampazzo :avocado: tags=endian:big 199*bbbd9b6eSWillian Rampazzo :avocado: tags=device:pcnet32 200*bbbd9b6eSWillian Rampazzo """ 201*bbbd9b6eSWillian Rampazzo self.check_mips_malta('mips64', 'be') 202*bbbd9b6eSWillian Rampazzo 203*bbbd9b6eSWillian Rampazzo def test_mips_malta64el_kernel3_2_0(self): 204*bbbd9b6eSWillian Rampazzo """ 205*bbbd9b6eSWillian Rampazzo :avocado: tags=arch:mips64el 206*bbbd9b6eSWillian Rampazzo :avocado: tags=endian:little 207*bbbd9b6eSWillian Rampazzo :avocado: tags=device:pcnet32 208*bbbd9b6eSWillian Rampazzo """ 209*bbbd9b6eSWillian Rampazzo self.check_mips_malta('mips64', 'le') 210