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 3000af1935SKevin Wolfdef filter_hash(msg): 3100af1935SKevin Wolf return re.sub("'hash': '[0-9a-f]+'", "'hash': HASH", msg) 3256ea7450SKevin Wolf 3300af1935SKevin Wolfdef blockdev_create(vm, options): 34*3fb588a0SKevin Wolf result = vm.qmp_log('blockdev-create', job_id='job0', options=options, 3500af1935SKevin Wolf filters=[iotests.filter_testfiles, filter_hash]) 3656ea7450SKevin Wolf 3700af1935SKevin Wolf if 'return' in result: 3800af1935SKevin Wolf assert result['return'] == {} 3900af1935SKevin Wolf vm.run_job('job0') 4000af1935SKevin Wolf iotests.log("") 4156ea7450SKevin Wolf 4200af1935SKevin Wolfwith iotests.FilePath('t.img') as disk_path, \ 4300af1935SKevin Wolf iotests.VM() as vm: 4456ea7450SKevin Wolf 4500af1935SKevin Wolf remote_path = iotests.remote_filename(disk_path) 4656ea7450SKevin Wolf 4700af1935SKevin Wolf # 4800af1935SKevin Wolf # Successful image creation (defaults) 4900af1935SKevin Wolf # 5000af1935SKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 5100af1935SKevin Wolf iotests.log("") 5256ea7450SKevin Wolf 5300af1935SKevin Wolf vm.launch() 5400af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 5500af1935SKevin Wolf 'location': { 5600af1935SKevin Wolf 'path': disk_path, 5700af1935SKevin Wolf 'server': { 5800af1935SKevin Wolf 'host': '127.0.0.1', 5900af1935SKevin Wolf 'port': '22' 6056ea7450SKevin Wolf } 6156ea7450SKevin Wolf }, 6200af1935SKevin Wolf 'size': 4194304 }) 6300af1935SKevin Wolf vm.shutdown() 6456ea7450SKevin Wolf 6500af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 6600af1935SKevin Wolf iotests.log("") 6700af1935SKevin Wolf iotests.img_info_log(disk_path) 6856ea7450SKevin Wolf 6900af1935SKevin Wolf # 7000af1935SKevin Wolf # Test host-key-check options 7100af1935SKevin Wolf # 7200af1935SKevin Wolf iotests.log("=== Test host-key-check options ===") 7300af1935SKevin Wolf iotests.log("") 7456ea7450SKevin Wolf 7500af1935SKevin Wolf vm.launch() 7600af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 7700af1935SKevin Wolf 'location': { 7800af1935SKevin Wolf 'path': disk_path, 7900af1935SKevin Wolf 'server': { 8000af1935SKevin Wolf 'host': '127.0.0.1', 8100af1935SKevin Wolf 'port': '22' 8256ea7450SKevin Wolf }, 8300af1935SKevin Wolf 'host-key-check': { 8400af1935SKevin Wolf 'mode': 'none' 8556ea7450SKevin Wolf } 8656ea7450SKevin Wolf }, 8700af1935SKevin Wolf 'size': 8388608 }) 8800af1935SKevin Wolf vm.shutdown() 8956ea7450SKevin Wolf 9000af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 9156ea7450SKevin Wolf 9200af1935SKevin Wolf vm.launch() 9300af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 9400af1935SKevin Wolf 'location': { 9500af1935SKevin Wolf 'path': disk_path, 9600af1935SKevin Wolf 'server': { 9700af1935SKevin Wolf 'host': '127.0.0.1', 9800af1935SKevin Wolf 'port': '22' 9956ea7450SKevin Wolf }, 10000af1935SKevin Wolf 'host-key-check': { 10100af1935SKevin Wolf 'mode': 'known_hosts' 10256ea7450SKevin Wolf } 10356ea7450SKevin Wolf }, 10400af1935SKevin Wolf 'size': 4194304 }) 10500af1935SKevin Wolf vm.shutdown() 10656ea7450SKevin Wolf 10700af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 10856ea7450SKevin Wolf 10900af1935SKevin Wolf md5_key = subprocess.check_output( 11000af1935SKevin Wolf 'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + 11100af1935SKevin Wolf 'cut -d" " -f3 | base64 -d | md5sum -b | cut -d" " -f1', 11200af1935SKevin Wolf shell=True).rstrip() 11356ea7450SKevin Wolf 11400af1935SKevin Wolf vm.launch() 11500af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 11600af1935SKevin Wolf 'location': { 11700af1935SKevin Wolf 'path': disk_path, 11800af1935SKevin Wolf 'server': { 11900af1935SKevin Wolf 'host': '127.0.0.1', 12000af1935SKevin Wolf 'port': '22' 12156ea7450SKevin Wolf }, 12200af1935SKevin Wolf 'host-key-check': { 12300af1935SKevin Wolf 'mode': 'hash', 12400af1935SKevin Wolf 'type': 'md5', 12500af1935SKevin Wolf 'hash': 'wrong', 12656ea7450SKevin Wolf } 12756ea7450SKevin Wolf }, 12800af1935SKevin Wolf 'size': 2097152 }) 12900af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 13000af1935SKevin Wolf 'location': { 13100af1935SKevin Wolf 'path': disk_path, 13200af1935SKevin Wolf 'server': { 13300af1935SKevin Wolf 'host': '127.0.0.1', 13400af1935SKevin Wolf 'port': '22' 13556ea7450SKevin Wolf }, 13600af1935SKevin Wolf 'host-key-check': { 13700af1935SKevin Wolf 'mode': 'hash', 13800af1935SKevin Wolf 'type': 'md5', 13900af1935SKevin Wolf 'hash': md5_key, 14056ea7450SKevin Wolf } 14156ea7450SKevin Wolf }, 14200af1935SKevin Wolf 'size': 8388608 }) 14300af1935SKevin Wolf vm.shutdown() 14456ea7450SKevin Wolf 14500af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 14656ea7450SKevin Wolf 14700af1935SKevin Wolf sha1_key = subprocess.check_output( 14800af1935SKevin Wolf 'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + 14900af1935SKevin Wolf 'cut -d" " -f3 | base64 -d | sha1sum -b | cut -d" " -f1', 15000af1935SKevin Wolf shell=True).rstrip() 15156ea7450SKevin Wolf 15200af1935SKevin Wolf vm.launch() 15300af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 15400af1935SKevin Wolf 'location': { 15500af1935SKevin Wolf 'path': disk_path, 15600af1935SKevin Wolf 'server': { 15700af1935SKevin Wolf 'host': '127.0.0.1', 15800af1935SKevin Wolf 'port': '22' 15956ea7450SKevin Wolf }, 16000af1935SKevin Wolf 'host-key-check': { 16100af1935SKevin Wolf 'mode': 'hash', 16200af1935SKevin Wolf 'type': 'sha1', 16300af1935SKevin Wolf 'hash': 'wrong', 16456ea7450SKevin Wolf } 16556ea7450SKevin Wolf }, 16600af1935SKevin Wolf 'size': 2097152 }) 16700af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 16800af1935SKevin Wolf 'location': { 16900af1935SKevin Wolf 'path': disk_path, 17000af1935SKevin Wolf 'server': { 17100af1935SKevin Wolf 'host': '127.0.0.1', 17200af1935SKevin Wolf 'port': '22' 17356ea7450SKevin Wolf }, 17400af1935SKevin Wolf 'host-key-check': { 17500af1935SKevin Wolf 'mode': 'hash', 17600af1935SKevin Wolf 'type': 'sha1', 17700af1935SKevin Wolf 'hash': sha1_key, 17856ea7450SKevin Wolf } 17956ea7450SKevin Wolf }, 18000af1935SKevin Wolf 'size': 4194304 }) 18100af1935SKevin Wolf vm.shutdown() 18256ea7450SKevin Wolf 18300af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 18456ea7450SKevin Wolf 18500af1935SKevin Wolf # 18600af1935SKevin Wolf # Invalid path and user 18700af1935SKevin Wolf # 18800af1935SKevin Wolf iotests.log("=== Invalid path and user ===") 18900af1935SKevin Wolf iotests.log("") 19056ea7450SKevin Wolf 19100af1935SKevin Wolf vm.launch() 19200af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 19300af1935SKevin Wolf 'location': { 19400af1935SKevin Wolf 'path': '/this/is/not/an/existing/path', 19500af1935SKevin Wolf 'server': { 19600af1935SKevin Wolf 'host': '127.0.0.1', 19700af1935SKevin Wolf 'port': '22' 19800af1935SKevin Wolf }, 19900af1935SKevin Wolf 'host-key-check': { 20000af1935SKevin Wolf 'mode': 'none' 20156ea7450SKevin Wolf } 20256ea7450SKevin Wolf }, 20300af1935SKevin Wolf 'size': 4194304 }) 20400af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 20500af1935SKevin Wolf 'location': { 20600af1935SKevin Wolf 'path': disk_path, 20700af1935SKevin Wolf 'user': 'invalid user', 20800af1935SKevin Wolf 'server': { 20900af1935SKevin Wolf 'host': '127.0.0.1', 21000af1935SKevin Wolf 'port': '22' 21100af1935SKevin Wolf }, 21200af1935SKevin Wolf 'host-key-check': { 21300af1935SKevin Wolf 'mode': 'none' 21456ea7450SKevin Wolf } 21556ea7450SKevin Wolf }, 21600af1935SKevin Wolf 'size': 4194304 }) 21700af1935SKevin Wolf vm.shutdown() 218