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