1*ab7f7e67SVladimir Sementsov-Ogievskiy#!/usr/bin/env python3 2*ab7f7e67SVladimir Sementsov-Ogievskiy# 3*ab7f7e67SVladimir Sementsov-Ogievskiy# Test nbd reconnect on open 4*ab7f7e67SVladimir Sementsov-Ogievskiy# 5*ab7f7e67SVladimir Sementsov-Ogievskiy# Copyright (c) 2020 Virtuozzo International GmbH 6*ab7f7e67SVladimir Sementsov-Ogievskiy# 7*ab7f7e67SVladimir Sementsov-Ogievskiy# This program is free software; you can redistribute it and/or modify 8*ab7f7e67SVladimir Sementsov-Ogievskiy# it under the terms of the GNU General Public License as published by 9*ab7f7e67SVladimir Sementsov-Ogievskiy# the Free Software Foundation; either version 2 of the License, or 10*ab7f7e67SVladimir Sementsov-Ogievskiy# (at your option) any later version. 11*ab7f7e67SVladimir Sementsov-Ogievskiy# 12*ab7f7e67SVladimir Sementsov-Ogievskiy# This program is distributed in the hope that it will be useful, 13*ab7f7e67SVladimir Sementsov-Ogievskiy# but WITHOUT ANY WARRANTY; without even the implied warranty of 14*ab7f7e67SVladimir Sementsov-Ogievskiy# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15*ab7f7e67SVladimir Sementsov-Ogievskiy# GNU General Public License for more details. 16*ab7f7e67SVladimir Sementsov-Ogievskiy# 17*ab7f7e67SVladimir Sementsov-Ogievskiy# You should have received a copy of the GNU General Public License 18*ab7f7e67SVladimir Sementsov-Ogievskiy# along with this program. If not, see <http://www.gnu.org/licenses/>. 19*ab7f7e67SVladimir Sementsov-Ogievskiy# 20*ab7f7e67SVladimir Sementsov-Ogievskiy 21*ab7f7e67SVladimir Sementsov-Ogievskiyimport time 22*ab7f7e67SVladimir Sementsov-Ogievskiy 23*ab7f7e67SVladimir Sementsov-Ogievskiyimport iotests 24*ab7f7e67SVladimir Sementsov-Ogievskiyfrom iotests import qemu_img_create, file_path, qemu_io_popen, qemu_nbd, \ 25*ab7f7e67SVladimir Sementsov-Ogievskiy qemu_io_log, log 26*ab7f7e67SVladimir Sementsov-Ogievskiy 27*ab7f7e67SVladimir Sementsov-Ogievskiyiotests.script_initialize(supported_fmts=['qcow2']) 28*ab7f7e67SVladimir Sementsov-Ogievskiy 29*ab7f7e67SVladimir Sementsov-Ogievskiydisk, nbd_sock = file_path('disk', 'nbd-sock') 30*ab7f7e67SVladimir Sementsov-Ogievskiy 31*ab7f7e67SVladimir Sementsov-Ogievskiy 32*ab7f7e67SVladimir Sementsov-Ogievskiydef create_args(open_timeout): 33*ab7f7e67SVladimir Sementsov-Ogievskiy return ['--image-opts', '-c', 'read 0 1M', 34*ab7f7e67SVladimir Sementsov-Ogievskiy f'driver=nbd,open-timeout={open_timeout},' 35*ab7f7e67SVladimir Sementsov-Ogievskiy f'server.type=unix,server.path={nbd_sock}'] 36*ab7f7e67SVladimir Sementsov-Ogievskiy 37*ab7f7e67SVladimir Sementsov-Ogievskiy 38*ab7f7e67SVladimir Sementsov-Ogievskiydef check_fail_to_connect(open_timeout): 39*ab7f7e67SVladimir Sementsov-Ogievskiy log(f'Check fail to connect with {open_timeout} seconds of timeout') 40*ab7f7e67SVladimir Sementsov-Ogievskiy 41*ab7f7e67SVladimir Sementsov-Ogievskiy start_t = time.time() 42*ab7f7e67SVladimir Sementsov-Ogievskiy qemu_io_log(*create_args(open_timeout)) 43*ab7f7e67SVladimir Sementsov-Ogievskiy delta_t = time.time() - start_t 44*ab7f7e67SVladimir Sementsov-Ogievskiy 45*ab7f7e67SVladimir Sementsov-Ogievskiy max_delta = open_timeout + 0.2 46*ab7f7e67SVladimir Sementsov-Ogievskiy if open_timeout <= delta_t <= max_delta: 47*ab7f7e67SVladimir Sementsov-Ogievskiy log(f'qemu_io finished in {open_timeout}..{max_delta} seconds, OK') 48*ab7f7e67SVladimir Sementsov-Ogievskiy else: 49*ab7f7e67SVladimir Sementsov-Ogievskiy note = 'too early' if delta_t < open_timeout else 'too long' 50*ab7f7e67SVladimir Sementsov-Ogievskiy log(f'qemu_io finished in {delta_t:.1f} seconds, {note}') 51*ab7f7e67SVladimir Sementsov-Ogievskiy 52*ab7f7e67SVladimir Sementsov-Ogievskiy 53*ab7f7e67SVladimir Sementsov-Ogievskiyqemu_img_create('-f', iotests.imgfmt, disk, '1M') 54*ab7f7e67SVladimir Sementsov-Ogievskiy 55*ab7f7e67SVladimir Sementsov-Ogievskiy# Start NBD client when NBD server is not yet running. It should not fail, but 56*ab7f7e67SVladimir Sementsov-Ogievskiy# wait for 5 seconds for the server to be available. 57*ab7f7e67SVladimir Sementsov-Ogievskiyclient = qemu_io_popen(*create_args(5)) 58*ab7f7e67SVladimir Sementsov-Ogievskiy 59*ab7f7e67SVladimir Sementsov-Ogievskiytime.sleep(1) 60*ab7f7e67SVladimir Sementsov-Ogievskiyqemu_nbd('-k', nbd_sock, '-f', iotests.imgfmt, disk) 61*ab7f7e67SVladimir Sementsov-Ogievskiy 62*ab7f7e67SVladimir Sementsov-Ogievskiy# client should succeed 63*ab7f7e67SVladimir Sementsov-Ogievskiylog(client.communicate()[0], filters=[iotests.filter_qemu_io]) 64*ab7f7e67SVladimir Sementsov-Ogievskiy 65*ab7f7e67SVladimir Sementsov-Ogievskiy# Server was started without --persistent flag, so it should be off now. Let's 66*ab7f7e67SVladimir Sementsov-Ogievskiy# check it and at the same time check that with open-timeout=0 client fails 67*ab7f7e67SVladimir Sementsov-Ogievskiy# immediately. 68*ab7f7e67SVladimir Sementsov-Ogievskiycheck_fail_to_connect(0) 69*ab7f7e67SVladimir Sementsov-Ogievskiy 70*ab7f7e67SVladimir Sementsov-Ogievskiy# Check that we will fail after non-zero timeout if server is still unavailable 71*ab7f7e67SVladimir Sementsov-Ogievskiycheck_fail_to_connect(1) 72