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