xref: /openbmc/qemu/tests/qemu-iotests/207 (revision 24496b8d)
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(qmsg):
31    def _filter(key, value):
32        if key == 'hash' and re.match('[0-9a-f]+', value):
33            return 'HASH'
34        return value
35    return iotests.filter_qmp(qmsg, _filter)
36
37def blockdev_create(vm, options):
38    result = vm.qmp_log('blockdev-create', job_id='job0', options=options,
39                        filters=[iotests.filter_qmp_testfiles, filter_hash])
40
41    if 'return' in result:
42        assert result['return'] == {}
43        vm.run_job('job0')
44    iotests.log("")
45
46with iotests.FilePath('t.img') as disk_path, \
47     iotests.VM() as vm:
48
49    remote_path = iotests.remote_filename(disk_path)
50
51    #
52    # Successful image creation (defaults)
53    #
54    iotests.log("=== Successful image creation (defaults) ===")
55    iotests.log("")
56
57    vm.launch()
58    blockdev_create(vm, { 'driver': 'ssh',
59                          'location': {
60                              'path': disk_path,
61                              'server': {
62                                  'host': '127.0.0.1',
63                                  'port': '22'
64                              }
65                          },
66                          'size': 4194304 })
67    vm.shutdown()
68
69    iotests.img_info_log(remote_path, filter_path=disk_path)
70    iotests.log("")
71    iotests.img_info_log(disk_path)
72
73    #
74    # Test host-key-check options
75    #
76    iotests.log("=== Test host-key-check options ===")
77    iotests.log("")
78
79    vm.launch()
80    blockdev_create(vm, { 'driver': 'ssh',
81                          'location': {
82                              'path': disk_path,
83                              'server': {
84                                  'host': '127.0.0.1',
85                                  'port': '22'
86                              },
87                              'host-key-check': {
88                                  'mode': 'none'
89                              }
90                          },
91                          'size': 8388608 })
92    vm.shutdown()
93
94    iotests.img_info_log(remote_path, filter_path=disk_path)
95
96    vm.launch()
97    blockdev_create(vm, { 'driver': 'ssh',
98                          'location': {
99                              'path': disk_path,
100                              'server': {
101                                  'host': '127.0.0.1',
102                                  'port': '22'
103                              },
104                              'host-key-check': {
105                                  'mode': 'known_hosts'
106                              }
107                          },
108                          'size': 4194304 })
109    vm.shutdown()
110
111    iotests.img_info_log(remote_path, filter_path=disk_path)
112
113    md5_key = subprocess.check_output(
114        'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' +
115        'cut -d" " -f3 | base64 -d | md5sum -b | cut -d" " -f1',
116        shell=True).rstrip().decode('ascii')
117
118    vm.launch()
119    blockdev_create(vm, { 'driver': 'ssh',
120                          'location': {
121                              'path': disk_path,
122                              'server': {
123                                  'host': '127.0.0.1',
124                                  'port': '22'
125                              },
126                              'host-key-check': {
127                                  'mode': 'hash',
128                                  'type': 'md5',
129                                  'hash': 'wrong',
130                              }
131                          },
132                          'size': 2097152 })
133    blockdev_create(vm, { 'driver': 'ssh',
134                          'location': {
135                              'path': disk_path,
136                              'server': {
137                                  'host': '127.0.0.1',
138                                  'port': '22'
139                              },
140                              'host-key-check': {
141                                  'mode': 'hash',
142                                  'type': 'md5',
143                                  'hash': md5_key,
144                              }
145                          },
146                          'size': 8388608 })
147    vm.shutdown()
148
149    iotests.img_info_log(remote_path, filter_path=disk_path)
150
151    sha1_key = subprocess.check_output(
152        'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' +
153        'cut -d" " -f3 | base64 -d | sha1sum -b | cut -d" " -f1',
154        shell=True).rstrip().decode('ascii')
155
156    vm.launch()
157    blockdev_create(vm, { 'driver': 'ssh',
158                          'location': {
159                              'path': disk_path,
160                              'server': {
161                                  'host': '127.0.0.1',
162                                  'port': '22'
163                              },
164                              'host-key-check': {
165                                  'mode': 'hash',
166                                  'type': 'sha1',
167                                  'hash': 'wrong',
168                              }
169                          },
170                          'size': 2097152 })
171    blockdev_create(vm, { 'driver': 'ssh',
172                          'location': {
173                              'path': disk_path,
174                              'server': {
175                                  'host': '127.0.0.1',
176                                  'port': '22'
177                              },
178                              'host-key-check': {
179                                  'mode': 'hash',
180                                  'type': 'sha1',
181                                  'hash': sha1_key,
182                              }
183                          },
184                          'size': 4194304 })
185    vm.shutdown()
186
187    iotests.img_info_log(remote_path, filter_path=disk_path)
188
189    #
190    # Invalid path and user
191    #
192    iotests.log("=== Invalid path and user ===")
193    iotests.log("")
194
195    vm.launch()
196    blockdev_create(vm, { 'driver': 'ssh',
197                          'location': {
198                              'path': '/this/is/not/an/existing/path',
199                              'server': {
200                                  'host': '127.0.0.1',
201                                  'port': '22'
202                              },
203                              'host-key-check': {
204                                  'mode': 'none'
205                              }
206                          },
207                          'size': 4194304 })
208    blockdev_create(vm, { 'driver': 'ssh',
209                          'location': {
210                              'path': disk_path,
211                              'user': 'invalid user',
212                              'server': {
213                                  'host': '127.0.0.1',
214                                  'port': '22'
215                              },
216                              'host-key-check': {
217                                  'mode': 'none'
218                              }
219                          },
220                          'size': 4194304 })
221    vm.shutdown()
222