xref: /openbmc/qemu/tests/qemu-iotests/207 (revision 40cf6a54)
1#!/usr/bin/env python
2#
3# Test ssh image creation
4#
5# Copyright (C) 2018 Red Hat, Inc.
6#
7# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22
23import iotests
24import subprocess
25import re
26
27iotests.verify_image_format(supported_fmts=['raw'])
28iotests.verify_protocol(supported=['ssh'])
29
30def filter_hash(msg):
31    return re.sub('"hash": "[0-9a-f]+"', '"hash": HASH', msg)
32
33def blockdev_create(vm, options):
34    result = vm.qmp_log('blockdev-create', job_id='job0', options=options,
35                        filters=[iotests.filter_testfiles, filter_hash])
36
37    if 'return' in result:
38        assert result['return'] == {}
39        vm.run_job('job0')
40    iotests.log("")
41
42with iotests.FilePath('t.img') as disk_path, \
43     iotests.VM() as vm:
44
45    remote_path = iotests.remote_filename(disk_path)
46
47    #
48    # Successful image creation (defaults)
49    #
50    iotests.log("=== Successful image creation (defaults) ===")
51    iotests.log("")
52
53    vm.launch()
54    blockdev_create(vm, { 'driver': 'ssh',
55                          'location': {
56                              'path': disk_path,
57                              'server': {
58                                  'host': '127.0.0.1',
59                                  'port': '22'
60                              }
61                          },
62                          'size': 4194304 })
63    vm.shutdown()
64
65    iotests.img_info_log(remote_path, filter_path=disk_path)
66    iotests.log("")
67    iotests.img_info_log(disk_path)
68
69    #
70    # Test host-key-check options
71    #
72    iotests.log("=== Test host-key-check options ===")
73    iotests.log("")
74
75    vm.launch()
76    blockdev_create(vm, { 'driver': 'ssh',
77                          'location': {
78                              'path': disk_path,
79                              'server': {
80                                  'host': '127.0.0.1',
81                                  'port': '22'
82                              },
83                              'host-key-check': {
84                                  'mode': 'none'
85                              }
86                          },
87                          'size': 8388608 })
88    vm.shutdown()
89
90    iotests.img_info_log(remote_path, filter_path=disk_path)
91
92    vm.launch()
93    blockdev_create(vm, { 'driver': 'ssh',
94                          'location': {
95                              'path': disk_path,
96                              'server': {
97                                  'host': '127.0.0.1',
98                                  'port': '22'
99                              },
100                              'host-key-check': {
101                                  'mode': 'known_hosts'
102                              }
103                          },
104                          'size': 4194304 })
105    vm.shutdown()
106
107    iotests.img_info_log(remote_path, filter_path=disk_path)
108
109    md5_key = subprocess.check_output(
110        'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' +
111        'cut -d" " -f3 | base64 -d | md5sum -b | cut -d" " -f1',
112        shell=True).rstrip().decode('ascii')
113
114    vm.launch()
115    blockdev_create(vm, { 'driver': 'ssh',
116                          'location': {
117                              'path': disk_path,
118                              'server': {
119                                  'host': '127.0.0.1',
120                                  'port': '22'
121                              },
122                              'host-key-check': {
123                                  'mode': 'hash',
124                                  'type': 'md5',
125                                  'hash': 'wrong',
126                              }
127                          },
128                          'size': 2097152 })
129    blockdev_create(vm, { 'driver': 'ssh',
130                          'location': {
131                              'path': disk_path,
132                              'server': {
133                                  'host': '127.0.0.1',
134                                  'port': '22'
135                              },
136                              'host-key-check': {
137                                  'mode': 'hash',
138                                  'type': 'md5',
139                                  'hash': md5_key,
140                              }
141                          },
142                          'size': 8388608 })
143    vm.shutdown()
144
145    iotests.img_info_log(remote_path, filter_path=disk_path)
146
147    sha1_key = subprocess.check_output(
148        'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' +
149        'cut -d" " -f3 | base64 -d | sha1sum -b | cut -d" " -f1',
150        shell=True).rstrip().decode('ascii')
151
152    vm.launch()
153    blockdev_create(vm, { 'driver': 'ssh',
154                          'location': {
155                              'path': disk_path,
156                              'server': {
157                                  'host': '127.0.0.1',
158                                  'port': '22'
159                              },
160                              'host-key-check': {
161                                  'mode': 'hash',
162                                  'type': 'sha1',
163                                  'hash': 'wrong',
164                              }
165                          },
166                          'size': 2097152 })
167    blockdev_create(vm, { 'driver': 'ssh',
168                          'location': {
169                              'path': disk_path,
170                              'server': {
171                                  'host': '127.0.0.1',
172                                  'port': '22'
173                              },
174                              'host-key-check': {
175                                  'mode': 'hash',
176                                  'type': 'sha1',
177                                  'hash': sha1_key,
178                              }
179                          },
180                          'size': 4194304 })
181    vm.shutdown()
182
183    iotests.img_info_log(remote_path, filter_path=disk_path)
184
185    #
186    # Invalid path and user
187    #
188    iotests.log("=== Invalid path and user ===")
189    iotests.log("")
190
191    vm.launch()
192    blockdev_create(vm, { 'driver': 'ssh',
193                          'location': {
194                              'path': '/this/is/not/an/existing/path',
195                              'server': {
196                                  'host': '127.0.0.1',
197                                  'port': '22'
198                              },
199                              'host-key-check': {
200                                  'mode': 'none'
201                              }
202                          },
203                          'size': 4194304 })
204    blockdev_create(vm, { 'driver': 'ssh',
205                          'location': {
206                              'path': disk_path,
207                              'user': 'invalid user',
208                              'server': {
209                                  'host': '127.0.0.1',
210                                  'port': '22'
211                              },
212                              'host-key-check': {
213                                  'mode': 'none'
214                              }
215                          },
216                          'size': 4194304 })
217    vm.shutdown()
218