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