4 # Test NBD client reconnection
6 # Copyright (c) 2019 Virtuozzo International GmbH
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from iotests import file_path, log
27 iotests.script_initialize()
30 nbd_sock, conf_file = file_path('nbd-sock', 'nbd-fault-injector.conf')
33 def make_conf_file(event):
35 Create configuration file for the nbd-fault-injector.py
37 :param event: which event the server should close a connection on
39 with open(conf_file, 'w') as conff:
40 conff.write('[inject-error]\nevent={}\nwhen=after'.format(event))
43 def start_server_NBD(event):
46 srv = subprocess.Popen(['./nbd-fault-injector.py', '--classic-negotiation',
47 nbd_sock, conf_file], stdout=subprocess.PIPE,
48 stderr=subprocess.STDOUT, universal_newlines=True)
49 line = srv.stdout.readline()
50 if 'Listening on ' in line:
51 log('NBD server: started')
53 log('NBD server: ' + line.rstrip())
58 def start_client_NBD():
59 log('NBD client: QEMU-IO write')
60 args = iotests.qemu_io_args_no_fmt + \
61 ['-c', 'write -P 0x7 0 3M', '--image-opts',
62 'driver=nbd,server.type=unix,server.path={},'
63 'reconnect-delay=7'.format(nbd_sock)]
64 clt = subprocess.Popen(args, stdout=subprocess.PIPE,
65 stderr=subprocess.STDOUT,
66 universal_newlines=True)
70 def check_proc_NBD(proc, connector):
72 outs, errs = proc.communicate(timeout=10)
74 if proc.returncode < 0:
75 log('NBD {}: EXIT SIGNAL {}\n'.format(connector, proc.returncode))
78 msg = outs.split('\n', 1)
79 log('NBD {}: {}'.format(connector, msg[0]))
81 except subprocess.TimeoutExpired:
83 log('NBD {}: ERROR timeout expired'.format(connector))
85 if connector == 'server':
90 srv = start_server_NBD('data')
91 clt = start_client_NBD()
92 # The server should close the connection after a client write request
93 check_proc_NBD(srv, 'server')
94 # Start the NBD server again
95 srv = start_server_NBD('reply')
96 # The client should reconnect and complete the write operation
97 check_proc_NBD(clt, 'client')
98 # Make it sure that server terminated
99 check_proc_NBD(srv, 'server')