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 conf_file = file_path('nbd-fault-injector.conf')
31 nbd_sock = file_path('nbd-sock', base_dir=iotests.sock_dir)
34 def make_conf_file(event):
36 Create configuration file for the nbd-fault-injector.py
38 :param event: which event the server should close a connection on
40 with open(conf_file, 'w') as conff:
41 conff.write('[inject-error]\nevent={}\nwhen=after'.format(event))
44 def start_server_NBD(event):
47 srv = subprocess.Popen(['./nbd-fault-injector.py', '--classic-negotiation',
48 nbd_sock, conf_file], stdout=subprocess.PIPE,
49 stderr=subprocess.STDOUT, universal_newlines=True)
50 line = srv.stdout.readline()
51 if 'Listening on ' in line:
52 log('NBD server: started')
54 log('NBD server: ' + line.rstrip())
59 def start_client_NBD():
60 log('NBD client: QEMU-IO write')
61 args = iotests.qemu_io_args_no_fmt + \
62 ['-c', 'write -P 0x7 0 3M', '--image-opts',
63 'driver=nbd,server.type=unix,server.path={},'
64 'reconnect-delay=7'.format(nbd_sock)]
65 clt = subprocess.Popen(args, stdout=subprocess.PIPE,
66 stderr=subprocess.STDOUT,
67 universal_newlines=True)
71 def check_proc_NBD(proc, connector):
73 outs, errs = proc.communicate(timeout=10)
75 if proc.returncode < 0:
76 log('NBD {}: EXIT SIGNAL {}\n'.format(connector, proc.returncode))
79 msg = outs.split('\n', 1)
80 log('NBD {}: {}'.format(connector, msg[0]))
82 except subprocess.TimeoutExpired:
84 log('NBD {}: ERROR timeout expired'.format(connector))
86 if connector == 'server':
91 srv = start_server_NBD('data')
92 clt = start_client_NBD()
93 # The server should close the connection after a client write request
94 check_proc_NBD(srv, 'server')
95 # Start the NBD server again
96 srv = start_server_NBD('reply')
97 # The client should reconnect and complete the write operation
98 check_proc_NBD(clt, 'client')
99 # Make it sure that server terminated
100 check_proc_NBD(srv, 'server')