1 # Functional test that boots a VM and run commands via a SSH session
3 # Copyright (c) Philippe Mathieu-Daudé <f4bug@amsat.org>
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.
14 from avocado
import skipUnless
15 from avocado_qemu
import Test
16 from avocado_qemu
import wait_for_console_pattern
17 from avocado
.utils
import process
18 from avocado
.utils
import archive
19 from avocado
.utils
import ssh
24 timeout
= 150 # Not for 'configure --enable-debug --enable-debug-tcg'
26 KERNEL_COMMON_COMMAND_LINE
= 'printk.time=0 '
29 BASE_URL
= 'https://people.debian.org/~aurel32/qemu/'
31 'be': {'base_url': 'mips',
32 'image_name': 'debian_wheezy_mips_standard.qcow2',
33 'image_hash': '8987a63270df67345b2135a6b7a4885a35e392d5',
35 32: '592e384a4edc16dade52a6cd5c785c637bcbc9ad',
36 64: 'db6eea7de35d36c77d8c165b6bcb222e16eb91db'}
38 'le': {'base_url': 'mipsel',
39 'image_name': 'debian_wheezy_mipsel_standard.qcow2',
40 'image_hash': '7866764d9de3ef536ffca24c9fb9f04ffdb45802',
42 32: 'a66bea5a8adaa2cb3d36a1d4e0ccdb01be8f6c2a',
43 64: '6a7f77245acf231415a0e8b725d91ed2f3487794'}
47 32: {'cpu': 'MIPS 24Kc', 'kernel_release': '3.2.0-4-4kc-malta'},
48 64: {'cpu': 'MIPS 20Kc', 'kernel_release': '3.2.0-4-5kc-malta'}
51 def get_url(self
, endianess
, path
=''):
52 qkey
= {'le': 'el', 'be': ''}
53 return '%s/mips%s/%s' % (self
.BASE_URL
, qkey
[endianess
], path
)
55 def get_image_info(self
, endianess
):
56 dinfo
= self
.IMAGE_INFO
[endianess
]
57 image_url
= self
.get_url(endianess
, dinfo
['image_name'])
58 image_hash
= dinfo
['image_hash']
59 return (image_url
, image_hash
)
61 def get_kernel_info(self
, endianess
, wordsize
):
62 minfo
= self
.CPU_INFO
[wordsize
]
63 kernel_url
= self
.get_url(endianess
,
64 'vmlinux-%s' % minfo
['kernel_release'])
65 kernel_hash
= self
.IMAGE_INFO
[endianess
]['kernel_hash'][wordsize
]
66 return kernel_url
, kernel_hash
68 @skipUnless(ssh
.SSH_CLIENT_BINARY
, 'No SSH client available')
69 @skipUnless(os
.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
71 super(LinuxSSH
, self
).setUp()
73 def get_portfwd(self
):
74 res
= self
.vm
.command('human-monitor-command',
75 command_line
='info usernet')
76 line
= res
.split('\r\n')[2]
77 port
= re
.split(r
'.*TCP.HOST_FORWARD.*127\.0\.0\.1 (\d+)\s+10\..*',
79 self
.log
.debug("sshd listening on port:" + port
)
82 def ssh_connect(self
, username
, password
):
83 self
.ssh_logger
= logging
.getLogger('ssh')
84 port
= self
.get_portfwd()
85 self
.ssh_session
= ssh
.Session(self
.VM_IP
, port
=int(port
),
86 user
=username
, password
=password
)
89 self
.ssh_session
.connect()
94 self
.fail("sshd timeout")
96 def ssh_disconnect_vm(self
):
97 self
.ssh_session
.quit()
99 def ssh_command(self
, command
, is_root
=True):
100 self
.ssh_logger
.info(command
)
101 result
= self
.ssh_session
.cmd(command
)
102 stdout_lines
= [line
.rstrip() for line
in result
.stdout_text
.splitlines()]
103 for line
in stdout_lines
:
104 self
.ssh_logger
.info(line
)
105 stderr_lines
= [line
.rstrip() for line
in result
.stderr_text
.splitlines()]
106 for line
in stderr_lines
:
107 self
.ssh_logger
.warning(line
)
108 return stdout_lines
, stderr_lines
110 def boot_debian_wheezy_image_and_ssh_login(self
, endianess
, kernel_path
):
111 image_url
, image_hash
= self
.get_image_info(endianess
)
112 image_path
= self
.fetch_asset(image_url
, asset_hash
=image_hash
)
114 self
.vm
.set_console()
115 kernel_command_line
= (self
.KERNEL_COMMON_COMMAND_LINE
116 + 'console=ttyS0 root=/dev/sda1')
117 self
.vm
.add_args('-no-reboot',
118 '-kernel', kernel_path
,
119 '-append', kernel_command_line
,
120 '-drive', 'file=%s,snapshot=on' % image_path
,
121 '-netdev', 'user,id=vnet,hostfwd=:127.0.0.1:0-:22',
122 '-device', 'pcnet,netdev=vnet')
125 self
.log
.info('VM launched, waiting for sshd')
126 console_pattern
= 'Starting OpenBSD Secure Shell server: sshd'
127 wait_for_console_pattern(self
, console_pattern
, 'Oops')
128 self
.log
.info('sshd ready')
130 self
.ssh_connect('root', 'root')
132 def shutdown_via_ssh(self
):
133 self
.ssh_command('poweroff')
134 self
.ssh_disconnect_vm()
135 wait_for_console_pattern(self
, 'Power down', 'Oops')
137 def ssh_command_output_contains(self
, cmd
, exp
):
138 stdout
, _
= self
.ssh_command(cmd
)
143 self
.fail('"%s" output does not contain "%s"' % (cmd
, exp
))
145 def run_common_commands(self
, wordsize
):
146 self
.ssh_command_output_contains(
148 self
.CPU_INFO
[wordsize
]['cpu'])
149 self
.ssh_command_output_contains(
152 self
.ssh_command_output_contains(
154 self
.CPU_INFO
[wordsize
]['kernel_release'])
155 self
.ssh_command_output_contains(
156 'cat /proc/interrupts',
158 self
.ssh_command_output_contains(
159 'cat /proc/interrupts',
161 self
.ssh_command_output_contains(
162 'cat /proc/interrupts',
164 self
.ssh_command_output_contains(
165 'cat /proc/interrupts',
167 self
.ssh_command_output_contains(
168 'cat /proc/interrupts',
170 self
.ssh_command_output_contains(
173 self
.ssh_command_output_contains(
176 self
.ssh_command_output_contains(
179 self
.ssh_command_output_contains(
182 self
.ssh_command_output_contains(
185 self
.ssh_command_output_contains(
188 self
.ssh_command_output_contains(
189 'lspci -d 11ab:4620',
191 self
.ssh_command_output_contains(
192 'cat /sys/bus/i2c/devices/i2c-0/name',
193 'SMBus PIIX4 adapter')
194 self
.ssh_command_output_contains(
197 # Empty 'Board Config' (64KB)
198 self
.ssh_command_output_contains(
199 'md5sum /dev/mtd2ro',
200 '0dfbe8aa4c20b52e1b8bf3cb6cbdf193')
202 def check_mips_malta(self
, uname_m
, endianess
):
203 wordsize
= 64 if '64' in uname_m
else 32
204 kernel_url
, kernel_hash
= self
.get_kernel_info(endianess
, wordsize
)
205 kernel_path
= self
.fetch_asset(kernel_url
, asset_hash
=kernel_hash
)
206 self
.boot_debian_wheezy_image_and_ssh_login(endianess
, kernel_path
)
208 stdout
, _
= self
.ssh_command('uname -a')
209 self
.assertIn(True, [uname_m
+ " GNU/Linux" in line
for line
in stdout
])
211 self
.run_common_commands(wordsize
)
212 self
.shutdown_via_ssh()
214 def test_mips_malta32eb_kernel3_2_0(self
):
216 :avocado: tags=arch:mips
217 :avocado: tags=endian:big
218 :avocado: tags=device:pcnet32
220 self
.check_mips_malta('mips', 'be')
222 def test_mips_malta32el_kernel3_2_0(self
):
224 :avocado: tags=arch:mipsel
225 :avocado: tags=endian:little
226 :avocado: tags=device:pcnet32
228 self
.check_mips_malta('mips', 'le')
230 def test_mips_malta64eb_kernel3_2_0(self
):
232 :avocado: tags=arch:mips64
233 :avocado: tags=endian:big
234 :avocado: tags=device:pcnet32
236 self
.check_mips_malta('mips64', 'be')
238 def test_mips_malta64el_kernel3_2_0(self
):
240 :avocado: tags=arch:mips64el
241 :avocado: tags=endian:little
242 :avocado: tags=device:pcnet32
244 self
.check_mips_malta('mips64', 'le')