1*00af1935SKevin 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# 7*00af1935SKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 8*00af1935SKevin 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 23*00af1935SKevin Wolfimport iotests 24*00af1935SKevin Wolfimport subprocess 25*00af1935SKevin Wolfimport re 2656ea7450SKevin Wolf 27*00af1935SKevin Wolfiotests.verify_image_format(supported_fmts=['raw']) 28*00af1935SKevin Wolfiotests.verify_protocol(supported=['ssh']) 2956ea7450SKevin Wolf 30*00af1935SKevin Wolfdef filter_hash(msg): 31*00af1935SKevin Wolf return re.sub("'hash': '[0-9a-f]+'", "'hash': HASH", msg) 3256ea7450SKevin Wolf 33*00af1935SKevin Wolfdef blockdev_create(vm, options): 34*00af1935SKevin Wolf result = vm.qmp_log('x-blockdev-create', job_id='job0', options=options, 35*00af1935SKevin Wolf filters=[iotests.filter_testfiles, filter_hash]) 3656ea7450SKevin Wolf 37*00af1935SKevin Wolf if 'return' in result: 38*00af1935SKevin Wolf assert result['return'] == {} 39*00af1935SKevin Wolf vm.run_job('job0') 40*00af1935SKevin Wolf iotests.log("") 4156ea7450SKevin Wolf 42*00af1935SKevin Wolfwith iotests.FilePath('t.img') as disk_path, \ 43*00af1935SKevin Wolf iotests.VM() as vm: 4456ea7450SKevin Wolf 45*00af1935SKevin Wolf remote_path = iotests.remote_filename(disk_path) 4656ea7450SKevin Wolf 47*00af1935SKevin Wolf # 48*00af1935SKevin Wolf # Successful image creation (defaults) 49*00af1935SKevin Wolf # 50*00af1935SKevin Wolf iotests.log("=== Successful image creation (defaults) ===") 51*00af1935SKevin Wolf iotests.log("") 5256ea7450SKevin Wolf 53*00af1935SKevin Wolf vm.launch() 54*00af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 55*00af1935SKevin Wolf 'location': { 56*00af1935SKevin Wolf 'path': disk_path, 57*00af1935SKevin Wolf 'server': { 58*00af1935SKevin Wolf 'host': '127.0.0.1', 59*00af1935SKevin Wolf 'port': '22' 6056ea7450SKevin Wolf } 6156ea7450SKevin Wolf }, 62*00af1935SKevin Wolf 'size': 4194304 }) 63*00af1935SKevin Wolf vm.shutdown() 6456ea7450SKevin Wolf 65*00af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 66*00af1935SKevin Wolf iotests.log("") 67*00af1935SKevin Wolf iotests.img_info_log(disk_path) 6856ea7450SKevin Wolf 69*00af1935SKevin Wolf # 70*00af1935SKevin Wolf # Test host-key-check options 71*00af1935SKevin Wolf # 72*00af1935SKevin Wolf iotests.log("=== Test host-key-check options ===") 73*00af1935SKevin Wolf iotests.log("") 7456ea7450SKevin Wolf 75*00af1935SKevin Wolf vm.launch() 76*00af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 77*00af1935SKevin Wolf 'location': { 78*00af1935SKevin Wolf 'path': disk_path, 79*00af1935SKevin Wolf 'server': { 80*00af1935SKevin Wolf 'host': '127.0.0.1', 81*00af1935SKevin Wolf 'port': '22' 8256ea7450SKevin Wolf }, 83*00af1935SKevin Wolf 'host-key-check': { 84*00af1935SKevin Wolf 'mode': 'none' 8556ea7450SKevin Wolf } 8656ea7450SKevin Wolf }, 87*00af1935SKevin Wolf 'size': 8388608 }) 88*00af1935SKevin Wolf vm.shutdown() 8956ea7450SKevin Wolf 90*00af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 9156ea7450SKevin Wolf 92*00af1935SKevin Wolf vm.launch() 93*00af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 94*00af1935SKevin Wolf 'location': { 95*00af1935SKevin Wolf 'path': disk_path, 96*00af1935SKevin Wolf 'server': { 97*00af1935SKevin Wolf 'host': '127.0.0.1', 98*00af1935SKevin Wolf 'port': '22' 9956ea7450SKevin Wolf }, 100*00af1935SKevin Wolf 'host-key-check': { 101*00af1935SKevin Wolf 'mode': 'known_hosts' 10256ea7450SKevin Wolf } 10356ea7450SKevin Wolf }, 104*00af1935SKevin Wolf 'size': 4194304 }) 105*00af1935SKevin Wolf vm.shutdown() 10656ea7450SKevin Wolf 107*00af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 10856ea7450SKevin Wolf 109*00af1935SKevin Wolf md5_key = subprocess.check_output( 110*00af1935SKevin Wolf 'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + 111*00af1935SKevin Wolf 'cut -d" " -f3 | base64 -d | md5sum -b | cut -d" " -f1', 112*00af1935SKevin Wolf shell=True).rstrip() 11356ea7450SKevin Wolf 114*00af1935SKevin Wolf vm.launch() 115*00af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 116*00af1935SKevin Wolf 'location': { 117*00af1935SKevin Wolf 'path': disk_path, 118*00af1935SKevin Wolf 'server': { 119*00af1935SKevin Wolf 'host': '127.0.0.1', 120*00af1935SKevin Wolf 'port': '22' 12156ea7450SKevin Wolf }, 122*00af1935SKevin Wolf 'host-key-check': { 123*00af1935SKevin Wolf 'mode': 'hash', 124*00af1935SKevin Wolf 'type': 'md5', 125*00af1935SKevin Wolf 'hash': 'wrong', 12656ea7450SKevin Wolf } 12756ea7450SKevin Wolf }, 128*00af1935SKevin Wolf 'size': 2097152 }) 129*00af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 130*00af1935SKevin Wolf 'location': { 131*00af1935SKevin Wolf 'path': disk_path, 132*00af1935SKevin Wolf 'server': { 133*00af1935SKevin Wolf 'host': '127.0.0.1', 134*00af1935SKevin Wolf 'port': '22' 13556ea7450SKevin Wolf }, 136*00af1935SKevin Wolf 'host-key-check': { 137*00af1935SKevin Wolf 'mode': 'hash', 138*00af1935SKevin Wolf 'type': 'md5', 139*00af1935SKevin Wolf 'hash': md5_key, 14056ea7450SKevin Wolf } 14156ea7450SKevin Wolf }, 142*00af1935SKevin Wolf 'size': 8388608 }) 143*00af1935SKevin Wolf vm.shutdown() 14456ea7450SKevin Wolf 145*00af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 14656ea7450SKevin Wolf 147*00af1935SKevin Wolf sha1_key = subprocess.check_output( 148*00af1935SKevin Wolf 'ssh-keyscan -t rsa 127.0.0.1 2>/dev/null | grep -v "\\^#" | ' + 149*00af1935SKevin Wolf 'cut -d" " -f3 | base64 -d | sha1sum -b | cut -d" " -f1', 150*00af1935SKevin Wolf shell=True).rstrip() 15156ea7450SKevin Wolf 152*00af1935SKevin Wolf vm.launch() 153*00af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 154*00af1935SKevin Wolf 'location': { 155*00af1935SKevin Wolf 'path': disk_path, 156*00af1935SKevin Wolf 'server': { 157*00af1935SKevin Wolf 'host': '127.0.0.1', 158*00af1935SKevin Wolf 'port': '22' 15956ea7450SKevin Wolf }, 160*00af1935SKevin Wolf 'host-key-check': { 161*00af1935SKevin Wolf 'mode': 'hash', 162*00af1935SKevin Wolf 'type': 'sha1', 163*00af1935SKevin Wolf 'hash': 'wrong', 16456ea7450SKevin Wolf } 16556ea7450SKevin Wolf }, 166*00af1935SKevin Wolf 'size': 2097152 }) 167*00af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 168*00af1935SKevin Wolf 'location': { 169*00af1935SKevin Wolf 'path': disk_path, 170*00af1935SKevin Wolf 'server': { 171*00af1935SKevin Wolf 'host': '127.0.0.1', 172*00af1935SKevin Wolf 'port': '22' 17356ea7450SKevin Wolf }, 174*00af1935SKevin Wolf 'host-key-check': { 175*00af1935SKevin Wolf 'mode': 'hash', 176*00af1935SKevin Wolf 'type': 'sha1', 177*00af1935SKevin Wolf 'hash': sha1_key, 17856ea7450SKevin Wolf } 17956ea7450SKevin Wolf }, 180*00af1935SKevin Wolf 'size': 4194304 }) 181*00af1935SKevin Wolf vm.shutdown() 18256ea7450SKevin Wolf 183*00af1935SKevin Wolf iotests.img_info_log(remote_path, filter_path=disk_path) 18456ea7450SKevin Wolf 185*00af1935SKevin Wolf # 186*00af1935SKevin Wolf # Invalid path and user 187*00af1935SKevin Wolf # 188*00af1935SKevin Wolf iotests.log("=== Invalid path and user ===") 189*00af1935SKevin Wolf iotests.log("") 19056ea7450SKevin Wolf 191*00af1935SKevin Wolf vm.launch() 192*00af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 193*00af1935SKevin Wolf 'location': { 194*00af1935SKevin Wolf 'path': '/this/is/not/an/existing/path', 195*00af1935SKevin Wolf 'server': { 196*00af1935SKevin Wolf 'host': '127.0.0.1', 197*00af1935SKevin Wolf 'port': '22' 198*00af1935SKevin Wolf }, 199*00af1935SKevin Wolf 'host-key-check': { 200*00af1935SKevin Wolf 'mode': 'none' 20156ea7450SKevin Wolf } 20256ea7450SKevin Wolf }, 203*00af1935SKevin Wolf 'size': 4194304 }) 204*00af1935SKevin Wolf blockdev_create(vm, { 'driver': 'ssh', 205*00af1935SKevin Wolf 'location': { 206*00af1935SKevin Wolf 'path': disk_path, 207*00af1935SKevin Wolf 'user': 'invalid user', 208*00af1935SKevin Wolf 'server': { 209*00af1935SKevin Wolf 'host': '127.0.0.1', 210*00af1935SKevin Wolf 'port': '22' 211*00af1935SKevin Wolf }, 212*00af1935SKevin Wolf 'host-key-check': { 213*00af1935SKevin Wolf 'mode': 'none' 21456ea7450SKevin Wolf } 21556ea7450SKevin Wolf }, 216*00af1935SKevin Wolf 'size': 4194304 }) 217*00af1935SKevin Wolf vm.shutdown() 218