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 ssh_command_output_contains(self, cmd, exp):
105        stdout, _ = self.ssh_command(cmd)
106        for line in stdout:
107            if exp in line:
108                break
109        else:
110            self.fail('"%s" output does not contain "%s"' % (cmd, exp))
111
112    def run_common_commands(self, wordsize):
113        self.ssh_command_output_contains(
114            'cat /proc/cpuinfo',
115            self.CPU_INFO[wordsize]['cpu'])
116        self.ssh_command_output_contains(
117            'uname -m',
118            'mips')
119        self.ssh_command_output_contains(
120            'uname -r',
121            self.CPU_INFO[wordsize]['kernel_release'])
122        self.ssh_command_output_contains(
123            'cat /proc/interrupts',
124            'XT-PIC  timer')
125        self.ssh_command_output_contains(
126            'cat /proc/interrupts',
127            'XT-PIC  i8042')
128        self.ssh_command_output_contains(
129            'cat /proc/interrupts',
130            'XT-PIC  serial')
131        self.ssh_command_output_contains(
132            'cat /proc/interrupts',
133            'XT-PIC  ata_piix')
134        self.ssh_command_output_contains(
135            'cat /proc/interrupts',
136            'XT-PIC  eth0')
137        self.ssh_command_output_contains(
138            'cat /proc/devices',
139            'input')
140        self.ssh_command_output_contains(
141            'cat /proc/devices',
142            'usb')
143        self.ssh_command_output_contains(
144            'cat /proc/devices',
145            'fb')
146        self.ssh_command_output_contains(
147            'cat /proc/ioports',
148            ' : serial')
149        self.ssh_command_output_contains(
150            'cat /proc/ioports',
151            ' : ata_piix')
152        self.ssh_command_output_contains(
153            'cat /proc/ioports',
154            ' : piix4_smbus')
155        self.ssh_command_output_contains(
156            'lspci -d 11ab:4620',
157            'GT-64120')
158        self.ssh_command_output_contains(
159            'cat /sys/bus/i2c/devices/i2c-0/name',
160            'SMBus PIIX4 adapter')
161        self.ssh_command_output_contains(
162            'cat /proc/mtd',
163            'YAMON')
164        # Empty 'Board Config' (64KB)
165        self.ssh_command_output_contains(
166            'md5sum /dev/mtd2ro',
167            '0dfbe8aa4c20b52e1b8bf3cb6cbdf193')
168
169    def check_mips_malta(self, uname_m, endianess):
170        wordsize = 64 if '64' in uname_m else 32
171        kernel_url, kernel_hash = self.get_kernel_info(endianess, wordsize)
172        kernel_path = self.fetch_asset(kernel_url, asset_hash=kernel_hash)
173        self.boot_debian_wheezy_image_and_ssh_login(endianess, kernel_path)
174
175        stdout, _ = self.ssh_command('uname -a')
176        self.assertIn(True, [uname_m + " GNU/Linux" in line for line in stdout])
177
178        self.run_common_commands(wordsize)
179        self.shutdown_via_ssh()
180        # Wait for VM to shut down gracefully
181        self.vm.wait()
182
183    def test_mips_malta32eb_kernel3_2_0(self):
184        """
185        :avocado: tags=arch:mips
186        :avocado: tags=endian:big
187        :avocado: tags=device:pcnet32
188        """
189        self.check_mips_malta('mips', 'be')
190
191    def test_mips_malta32el_kernel3_2_0(self):
192        """
193        :avocado: tags=arch:mipsel
194        :avocado: tags=endian:little
195        :avocado: tags=device:pcnet32
196        """
197        self.check_mips_malta('mips', 'le')
198
199    def test_mips_malta64eb_kernel3_2_0(self):
200        """
201        :avocado: tags=arch:mips64
202        :avocado: tags=endian:big
203        :avocado: tags=device:pcnet32
204        """
205        self.check_mips_malta('mips64', 'be')
206
207    def test_mips_malta64el_kernel3_2_0(self):
208        """
209        :avocado: tags=arch:mips64el
210        :avocado: tags=endian:little
211        :avocado: tags=device:pcnet32
212        """
213        self.check_mips_malta('mips64', 'le')
214