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.
15 from avocado
import skipUnless
16 from avocado_qemu
import Test
17 from avocado
.utils
import process
18 from avocado
.utils
import archive
23 timeout
= 150 # Not for 'configure --enable-debug --enable-debug-tcg'
25 KERNEL_COMMON_COMMAND_LINE
= 'printk.time=0 '
30 'image_url': 'https://people.debian.org/~aurel32/qemu/mips/'
31 'debian_wheezy_mips_standard.qcow2',
32 'image_hash': '8987a63270df67345b2135a6b7a4885a35e392d5',
33 'rsa_hostkey': b
'AAAAB3NzaC1yc2EAAAADAQABAAABAQCca1VitiyLAdQOld'
34 b
'zT43IOEVJZ0wHD78GJi8wDAjMiYWUzNSSn0rXGQsINHuH5'
35 b
'IlF+kBZsHinb/FtKCAyS9a8uCHhQI4SuB4QhAb0+39MlUw'
36 b
'Mm0CLkctgM2eUUZ6MQMQvDlqnue6CCkxN62EZYbaxmby7j'
37 b
'CQa1125o1HRKBvdGm2zrJWxXAfA+f1v6jHLyE8Jnu83eQ+'
38 b
'BFY25G+Vzx1PVc3zQBwJ8r0NGTRqy2//oWQP0h+bMsgeFe'
39 b
'KH/J3RJM22vg6+I4JAdBFcxnK+l781h1FuRxOn4O/Xslbg'
40 b
'go6WtB4V4TOsw2E/KfxI5IZ/icxF+swVcnvF46Hf3uQc/0'
44 'image_url': 'https://people.debian.org/~aurel32/qemu/mipsel/'
45 'debian_wheezy_mipsel_standard.qcow2',
46 'image_hash': '7866764d9de3ef536ffca24c9fb9f04ffdb45802',
47 'rsa_hostkey': b
'AAAAB3NzaC1yc2EAAAADAQABAAABAQClXJlBT71HL5yKvv'
48 b
'gfC7jmxSWx5zSBCzET6CLZczwAafSIs7YKfNOy/dQTxhuk'
49 b
'yIGFUugZFoF3E9PzdhunuyvyTd56MPoNIqFbb5rGokwU5I'
50 b
'TOx3dBHZR0mClypL6MVrwe0bsiIb8GhF1zioNwcsaAZnAi'
51 b
'KfXStVDtXvn/kLLq+xLABYt48CC5KYWoFaCoICskLAY+qo'
52 b
'L+LWyAnQisj4jAH8VSaSKIImFpfkHWEXPhHcC4ZBlDKtnH'
53 b
'po9vhfCHgnfW3Pzrqmk8BI4HysqPFVmJWkJGlGUL+sGeg3'
54 b
'ZZolAYuDXGuBrw8ooPJq2v2dOH+z6dyD2q/ypmAbyPqj5C'
59 def wait_for_console_pattern(self
, success_message
,
60 failure_message
='Oops'):
61 console
= self
.vm
.console_socket
.makefile()
62 console_logger
= logging
.getLogger('console')
64 msg
= console
.readline()
65 console_logger
.debug(msg
.strip())
66 if success_message
in msg
:
68 if failure_message
in msg
:
69 fail
= 'Failure message found in console: %s' % failure_message
72 def get_portfwd(self
):
73 res
= self
.vm
.command('human-monitor-command',
74 command_line
='info usernet')
75 line
= res
.split('\r\n')[2]
76 port
= re
.split(r
'.*TCP.HOST_FORWARD.*127\.0\.0\.1 (\d+)\s+10\..*',
78 self
.log
.debug("sshd listening on port:" + port
)
81 def ssh_connect(self
, username
, password
, rsa_hostkey_b64
=None):
82 self
.ssh_logger
= logging
.getLogger('ssh')
83 self
.ssh_username
= username
84 self
.ssh_ps1
= '# ' if username
is 'root' else '$ '
85 self
.ssh_client
= paramiko
.SSHClient()
86 port
= self
.get_portfwd()
88 rsa_hostkey_bin
= base64
.b64decode(rsa_hostkey_b64
)
89 rsa_hostkey
= paramiko
.RSAKey(data
= rsa_hostkey_bin
)
90 ipport
= '[%s]:%s' % (self
.VM_IP
, port
)
91 self
.ssh_logger
.debug('ipport ' + ipport
)
92 self
.ssh_client
.get_host_keys().add(ipport
, 'ssh-rsa', rsa_hostkey
)
95 self
.ssh_client
.connect(self
.VM_IP
, int(port
),
96 username
, password
, banner_timeout
=90)
97 self
.ssh_logger
.info("Entering interactive session.")
102 self
.fail("sshd timeout")
104 def ssh_disconnect_vm(self
):
105 self
.ssh_client
.close()
107 def ssh_command(self
, command
, is_root
=True):
108 self
.ssh_logger
.info(self
.ssh_ps1
+ command
)
109 stdin
, stdout
, stderr
= self
.ssh_client
.exec_command(command
)
110 stdout_lines
= [line
.strip('\n') for line
in stdout
]
111 for line
in stdout_lines
:
112 self
.ssh_logger
.info(line
)
113 stderr_lines
= [line
.strip('\n') for line
in stderr
]
114 for line
in stderr_lines
:
115 self
.ssh_logger
.warning(line
)
116 return stdout_lines
, stderr_lines
118 def boot_debian_wheezy_image_and_ssh_login(self
, endianess
, kernel_path
):
119 image_url
= self
.IMAGE_INFO
[endianess
]['image_url']
120 image_hash
= self
.IMAGE_INFO
[endianess
]['image_hash']
121 image_path
= self
.fetch_asset(image_url
, asset_hash
=image_hash
)
122 rsa_hostkey_b64
= self
.IMAGE_INFO
[endianess
]['rsa_hostkey']
124 self
.vm
.set_machine('malta')
125 self
.vm
.set_console()
126 kernel_command_line
= (self
.KERNEL_COMMON_COMMAND_LINE
127 + 'console=ttyS0 root=/dev/sda1')
128 self
.vm
.add_args('-no-reboot',
129 '-kernel', kernel_path
,
130 '-append', kernel_command_line
,
132 '-netdev', 'user,id=vnet,hostfwd=:127.0.0.1:0-:22',
133 '-device', 'pcnet,netdev=vnet')
136 self
.log
.info('VM launched, waiting for sshd')
137 console_pattern
= 'Starting OpenBSD Secure Shell server: sshd'
138 self
.wait_for_console_pattern(console_pattern
)
139 self
.log
.info('sshd ready')
141 self
.ssh_connect('root', 'root', rsa_hostkey_b64
=rsa_hostkey_b64
)
143 def shutdown_via_ssh(self
):
144 self
.ssh_command('poweroff')
145 self
.ssh_disconnect_vm()
146 self
.wait_for_console_pattern('Power down')
148 def ssh_command_output_contains(self
, cmd
, exp
):
149 stdout
, _
= self
.ssh_command(cmd
)
154 self
.fail('"%s" output does not contain "%s"' % (cmd
, exp
))
156 def run_common_commands(self
):
157 self
.ssh_command_output_contains(
160 self
.ssh_command_output_contains(
163 self
.ssh_command_output_contains(
166 self
.ssh_command_output_contains(
167 'cat /proc/interrupts',
169 self
.ssh_command_output_contains(
170 'cat /proc/interrupts',
172 self
.ssh_command_output_contains(
173 'cat /proc/interrupts',
175 self
.ssh_command_output_contains(
176 'cat /proc/interrupts',
178 self
.ssh_command_output_contains(
179 'cat /proc/interrupts',
181 self
.ssh_command_output_contains(
182 'cat /proc/interrupts',
184 self
.ssh_command_output_contains(
187 self
.ssh_command_output_contains(
190 self
.ssh_command_output_contains(
193 self
.ssh_command_output_contains(
196 self
.ssh_command_output_contains(
199 self
.ssh_command_output_contains(
202 self
.ssh_command_output_contains(
203 'lspci -d 11ab:4620',
205 self
.ssh_command_output_contains(
206 'cat /sys/bus/i2c/devices/i2c-0/name',
207 'SMBus PIIX4 adapter')
208 self
.ssh_command_output_contains(
211 # Empty 'Board Config'
212 self
.ssh_command_output_contains(
213 'md5sum /dev/mtd2ro',
214 '0dfbe8aa4c20b52e1b8bf3cb6cbdf193')
216 def check_mips_malta(self
, endianess
, kernel_path
, uname_m
):
217 self
.boot_debian_wheezy_image_and_ssh_login(endianess
, kernel_path
)
219 stdout
, _
= self
.ssh_command('uname -a')
220 self
.assertIn(True, [uname_m
+ " GNU/Linux" in line
for line
in stdout
])
222 self
.run_common_commands()
223 self
.shutdown_via_ssh()
225 @skipUnless(os
.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
226 def test_mips_malta32eb_kernel3_2_0(self
):
228 :avocado: tags=arch:mips
229 :avocado: tags=machine:malta
230 :avocado: tags=endian:big
231 :avocado: tags=device:pcnet32
233 kernel_url
= ('https://people.debian.org/~aurel32/qemu/mips/'
234 'vmlinux-3.2.0-4-4kc-malta')
235 kernel_hash
= '592e384a4edc16dade52a6cd5c785c637bcbc9ad'
236 kernel_path
= self
.fetch_asset(kernel_url
, asset_hash
=kernel_hash
)
238 self
.check_mips_malta('be', kernel_path
, 'mips')
240 @skipUnless(os
.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
241 def test_mips_malta32el_kernel3_2_0(self
):
243 :avocado: tags=arch:mipsel
244 :avocado: tags=machine:malta
245 :avocado: tags=endian:little
246 :avocado: tags=device:pcnet32
248 kernel_url
= ('https://people.debian.org/~aurel32/qemu/mipsel/'
249 'vmlinux-3.2.0-4-4kc-malta')
250 kernel_hash
= 'a66bea5a8adaa2cb3d36a1d4e0ccdb01be8f6c2a'
251 kernel_path
= self
.fetch_asset(kernel_url
, asset_hash
=kernel_hash
)
253 self
.check_mips_malta('le', kernel_path
, 'mips')
255 @skipUnless(os
.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
256 def test_mips_malta64eb_kernel3_2_0(self
):
258 :avocado: tags=arch:mips64
259 :avocado: tags=machine:malta
260 :avocado: tags=endian:big
261 :avocado: tags=device:pcnet32
263 kernel_url
= ('https://people.debian.org/~aurel32/qemu/mips/'
264 'vmlinux-3.2.0-4-5kc-malta')
265 kernel_hash
= 'db6eea7de35d36c77d8c165b6bcb222e16eb91db'
266 kernel_path
= self
.fetch_asset(kernel_url
, asset_hash
=kernel_hash
)
267 self
.check_mips_malta('be', kernel_path
, 'mips64')
269 @skipUnless(os
.getenv('AVOCADO_TIMEOUT_EXPECTED'), 'Test might timeout')
270 def test_mips_malta64el_kernel3_2_0(self
):
272 :avocado: tags=arch:mips64el
273 :avocado: tags=machine:malta
274 :avocado: tags=endian:little
275 :avocado: tags=device:pcnet32
277 kernel_url
= ('https://people.debian.org/~aurel32/qemu/mipsel/'
278 'vmlinux-3.2.0-4-5kc-malta')
279 kernel_hash
= '6a7f77245acf231415a0e8b725d91ed2f3487794'
280 kernel_path
= self
.fetch_asset(kernel_url
, asset_hash
=kernel_hash
)
281 self
.check_mips_malta('le', kernel_path
, 'mips64')