100af1935SKevin Wolf#!/usr/bin/env python 256ea7450SKevin Wolf# 356ea7450SKevin Wolf# Test ssh image creation 456ea7450SKevin Wolf# 556ea7450SKevin Wolf# Copyright (C) 2018 Red Hat, Inc. 656ea7450SKevin Wolf# 700af1935SKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 800af1935SKevin Wolf# 956ea7450SKevin Wolf# This program is free software; you can redistribute it and/or modify 1056ea7450SKevin Wolf# it under the terms of the GNU General Public License as published by 1156ea7450SKevin Wolf# the Free Software Foundation; either version 2 of the License, or 1256ea7450SKevin Wolf# (at your option) any later version. 1356ea7450SKevin Wolf# 1456ea7450SKevin Wolf# This program is distributed in the hope that it will be useful, 1556ea7450SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 1656ea7450SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1756ea7450SKevin Wolf# GNU General Public License for more details. 1856ea7450SKevin Wolf# 1956ea7450SKevin Wolf# You should have received a copy of the GNU General Public License 2056ea7450SKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 2156ea7450SKevin Wolf# 2256ea7450SKevin Wolf 2300af1935SKevin Wolfimport iotests 2400af1935SKevin Wolfimport subprocess 2500af1935SKevin Wolfimport re 2656ea7450SKevin Wolf 2700af1935SKevin Wolfiotests.verify_image_format(supported_fmts=['raw']) 2800af1935SKevin Wolfiotests.verify_protocol(supported=['ssh']) 2956ea7450SKevin Wolf 30*9ac10f2eSMax Reitzdef filter_hash(qmsg): 31*9ac10f2eSMax Reitz def _filter(key, value): 32*9ac10f2eSMax Reitz if key == 'hash' and re.match('[0-9a-f]+', value): 33*9ac10f2eSMax Reitz return 'HASH' 34*9ac10f2eSMax Reitz return value 35*9ac10f2eSMax Reitz return iotests.filter_qmp(qmsg, _filter) 3656ea7450SKevin Wolf 3700af1935SKevin Wolfdef blockdev_create(vm, options): 383fb588a0SKevin Wolf result = vm.qmp_log('blockdev-create', job_id='job0', options=options, 39*9ac10f2eSMax Reitz filters=[iotests.filter_qmp_testfiles, filter_hash]) 4056ea7450SKevin Wolf 4100af1935SKevin Wolf if 'return' in result: 4200af1935SKevin Wolf assert result['return'] == {} 4300af1935SKevin Wolf vm.run_job('job0') 4400af1935SKevin Wolf iotests.log("") 4556ea7450SKevin Wolf 4600af1935SKevin Wolfwith iotests.FilePath('t.img') as disk_path, \ 4700af1935SKevin Wolf iotests.VM() as vm: 4856ea7450SKevin Wolf 4900af1935SKevin Wolf remote_path = iotests.remote_filename(disk_path) 5056ea7450SKevin Wolf 5100af1935SKevin Wolf # 5200af1935SKevin Wolf # Successful image creation (defaults) 5300af1935SKevin Wolf # 5400af1935SKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 5500af1935SKevin Wolf iotests.log("") 5656ea7450SKevin Wolf 5700af1935SKevin Wolf vm.launch() 5800af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 5900af1935SKevin Wolf 'location': { 6000af1935SKevin Wolf 'path': disk_path, 6100af1935SKevin Wolf 'server': { 6200af1935SKevin Wolf 'host': '127.0.0.1', 6300af1935SKevin Wolf 'port': '22' 6456ea7450SKevin Wolf } 6556ea7450SKevin Wolf }, 6600af1935SKevin Wolf 'size': 4194304 }) 6700af1935SKevin Wolf vm.shutdown() 6856ea7450SKevin Wolf 6900af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 7000af1935SKevin Wolf iotests.log("") 7100af1935SKevin Wolf iotests.img_info_log(disk_path) 7256ea7450SKevin Wolf 7300af1935SKevin Wolf # 7400af1935SKevin Wolf # Test host-key-check options 7500af1935SKevin Wolf # 7600af1935SKevin Wolf iotests.log("=== Test host-key-check options ===") 7700af1935SKevin Wolf iotests.log("") 7856ea7450SKevin Wolf 7900af1935SKevin Wolf vm.launch() 8000af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 8100af1935SKevin Wolf 'location': { 8200af1935SKevin Wolf 'path': disk_path, 8300af1935SKevin Wolf 'server': { 8400af1935SKevin Wolf 'host': '127.0.0.1', 8500af1935SKevin Wolf 'port': '22' 8656ea7450SKevin Wolf }, 8700af1935SKevin Wolf 'host-key-check': { 8800af1935SKevin Wolf 'mode': 'none' 8956ea7450SKevin Wolf } 9056ea7450SKevin Wolf }, 9100af1935SKevin Wolf 'size': 8388608 }) 9200af1935SKevin Wolf vm.shutdown() 9356ea7450SKevin Wolf 9400af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 9556ea7450SKevin Wolf 9600af1935SKevin Wolf vm.launch() 9700af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 9800af1935SKevin Wolf 'location': { 9900af1935SKevin Wolf 'path': disk_path, 10000af1935SKevin Wolf 'server': { 10100af1935SKevin Wolf 'host': '127.0.0.1', 10200af1935SKevin Wolf 'port': '22' 10356ea7450SKevin Wolf }, 10400af1935SKevin Wolf 'host-key-check': { 10500af1935SKevin Wolf 'mode': 'known_hosts' 10656ea7450SKevin Wolf } 10756ea7450SKevin Wolf }, 10800af1935SKevin Wolf 'size': 4194304 }) 10900af1935SKevin Wolf vm.shutdown() 11056ea7450SKevin Wolf 11100af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 11256ea7450SKevin Wolf 11300af1935SKevin Wolf md5_key = subprocess.check_output( 11400af1935SKevin Wolf 'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + 11500af1935SKevin Wolf 'cut -d" " -f3 | base64 -d | md5sum -b | cut -d" " -f1', 1168eb5e674SMax Reitz shell=True).rstrip().decode('ascii') 11756ea7450SKevin Wolf 11800af1935SKevin Wolf vm.launch() 11900af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 12000af1935SKevin Wolf 'location': { 12100af1935SKevin Wolf 'path': disk_path, 12200af1935SKevin Wolf 'server': { 12300af1935SKevin Wolf 'host': '127.0.0.1', 12400af1935SKevin Wolf 'port': '22' 12556ea7450SKevin Wolf }, 12600af1935SKevin Wolf 'host-key-check': { 12700af1935SKevin Wolf 'mode': 'hash', 12800af1935SKevin Wolf 'type': 'md5', 12900af1935SKevin Wolf 'hash': 'wrong', 13056ea7450SKevin Wolf } 13156ea7450SKevin Wolf }, 13200af1935SKevin Wolf 'size': 2097152 }) 13300af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 13400af1935SKevin Wolf 'location': { 13500af1935SKevin Wolf 'path': disk_path, 13600af1935SKevin Wolf 'server': { 13700af1935SKevin Wolf 'host': '127.0.0.1', 13800af1935SKevin Wolf 'port': '22' 13956ea7450SKevin Wolf }, 14000af1935SKevin Wolf 'host-key-check': { 14100af1935SKevin Wolf 'mode': 'hash', 14200af1935SKevin Wolf 'type': 'md5', 14300af1935SKevin Wolf 'hash': md5_key, 14456ea7450SKevin Wolf } 14556ea7450SKevin Wolf }, 14600af1935SKevin Wolf 'size': 8388608 }) 14700af1935SKevin Wolf vm.shutdown() 14856ea7450SKevin Wolf 14900af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 15056ea7450SKevin Wolf 15100af1935SKevin Wolf sha1_key = subprocess.check_output( 15200af1935SKevin Wolf 'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + 15300af1935SKevin Wolf 'cut -d" " -f3 | base64 -d | sha1sum -b | cut -d" " -f1', 1548eb5e674SMax Reitz shell=True).rstrip().decode('ascii') 15556ea7450SKevin Wolf 15600af1935SKevin Wolf vm.launch() 15700af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 15800af1935SKevin Wolf 'location': { 15900af1935SKevin Wolf 'path': disk_path, 16000af1935SKevin Wolf 'server': { 16100af1935SKevin Wolf 'host': '127.0.0.1', 16200af1935SKevin Wolf 'port': '22' 16356ea7450SKevin Wolf }, 16400af1935SKevin Wolf 'host-key-check': { 16500af1935SKevin Wolf 'mode': 'hash', 16600af1935SKevin Wolf 'type': 'sha1', 16700af1935SKevin Wolf 'hash': 'wrong', 16856ea7450SKevin Wolf } 16956ea7450SKevin Wolf }, 17000af1935SKevin Wolf 'size': 2097152 }) 17100af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 17200af1935SKevin Wolf 'location': { 17300af1935SKevin Wolf 'path': disk_path, 17400af1935SKevin Wolf 'server': { 17500af1935SKevin Wolf 'host': '127.0.0.1', 17600af1935SKevin Wolf 'port': '22' 17756ea7450SKevin Wolf }, 17800af1935SKevin Wolf 'host-key-check': { 17900af1935SKevin Wolf 'mode': 'hash', 18000af1935SKevin Wolf 'type': 'sha1', 18100af1935SKevin Wolf 'hash': sha1_key, 18256ea7450SKevin Wolf } 18356ea7450SKevin Wolf }, 18400af1935SKevin Wolf 'size': 4194304 }) 18500af1935SKevin Wolf vm.shutdown() 18656ea7450SKevin Wolf 18700af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 18856ea7450SKevin Wolf 18900af1935SKevin Wolf # 19000af1935SKevin Wolf # Invalid path and user 19100af1935SKevin Wolf # 19200af1935SKevin Wolf iotests.log("=== Invalid path and user ===") 19300af1935SKevin Wolf iotests.log("") 19456ea7450SKevin Wolf 19500af1935SKevin Wolf vm.launch() 19600af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 19700af1935SKevin Wolf 'location': { 19800af1935SKevin Wolf 'path': '/this/is/not/an/existing/path', 19900af1935SKevin Wolf 'server': { 20000af1935SKevin Wolf 'host': '127.0.0.1', 20100af1935SKevin Wolf 'port': '22' 20200af1935SKevin Wolf }, 20300af1935SKevin Wolf 'host-key-check': { 20400af1935SKevin Wolf 'mode': 'none' 20556ea7450SKevin Wolf } 20656ea7450SKevin Wolf }, 20700af1935SKevin Wolf 'size': 4194304 }) 20800af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 20900af1935SKevin Wolf 'location': { 21000af1935SKevin Wolf 'path': disk_path, 21100af1935SKevin Wolf 'user': 'invalid user', 21200af1935SKevin Wolf 'server': { 21300af1935SKevin Wolf 'host': '127.0.0.1', 21400af1935SKevin Wolf 'port': '22' 21500af1935SKevin Wolf }, 21600af1935SKevin Wolf 'host-key-check': { 21700af1935SKevin Wolf 'mode': 'none' 21856ea7450SKevin Wolf } 21956ea7450SKevin Wolf }, 22000af1935SKevin Wolf 'size': 4194304 }) 22100af1935SKevin Wolf vm.shutdown() 222