Docs/RCU: Correct sample code of qatomic_rcu_set
[qemu/ar7.git] / tests / qemu-iotests / 277
blobd34f87021f8e3a45f2361d083a042670555aa6a1
1 #!/usr/bin/env python3
3 # Test NBD client reconnection
5 # Copyright (c) 2019 Virtuozzo International GmbH
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 import os
22 import subprocess
23 import iotests
24 from iotests import file_path, log
26 iotests.script_initialize()
29 nbd_sock, conf_file = file_path('nbd-sock', 'nbd-fault-injector.conf')
32 def make_conf_file(event):
33     """
34     Create configuration file for the nbd-fault-injector.py
36     :param event: which event the server should close a connection on
37     """
38     with open(conf_file, 'w') as conff:
39         conff.write('[inject-error]\nevent={}\nwhen=after'.format(event))
42 def start_server_NBD(event):
43     make_conf_file(event)
45     srv = subprocess.Popen(['nbd-fault-injector.py', '--classic-negotiation',
46                            nbd_sock, conf_file], stdout=subprocess.PIPE,
47                            stderr=subprocess.STDOUT, universal_newlines=True)
48     line = srv.stdout.readline()
49     if 'Listening on ' in line:
50         log('NBD server: started')
51     else:
52         log('NBD server: ' + line.rstrip())
54     return srv
57 def start_client_NBD():
58     log('NBD client: QEMU-IO write')
59     args = iotests.qemu_io_args_no_fmt + \
60         ['-c', 'write -P 0x7 0 3M', '--image-opts',
61          'driver=nbd,server.type=unix,server.path={},'
62          'reconnect-delay=7'.format(nbd_sock)]
63     clt = subprocess.Popen(args, stdout=subprocess.PIPE,
64                            stderr=subprocess.STDOUT,
65                            universal_newlines=True)
66     return clt
69 def check_proc_NBD(proc, connector):
70     try:
71         outs, errs = proc.communicate(timeout=10)
73         if proc.returncode < 0:
74             log('NBD {}: EXIT SIGNAL {}\n'.format(connector, proc.returncode))
75             log(outs)
76         else:
77             msg = outs.split('\n', 1)
78             log('NBD {}: {}'.format(connector, msg[0]))
80     except subprocess.TimeoutExpired:
81         proc.kill()
82         log('NBD {}: ERROR timeout expired'.format(connector))
83     finally:
84         if connector == 'server':
85             os.remove(nbd_sock)
86             os.remove(conf_file)
89 srv = start_server_NBD('data')
90 clt = start_client_NBD()
91 # The server should close the connection after a client write request
92 check_proc_NBD(srv, 'server')
93 # Start the NBD server again
94 srv = start_server_NBD('reply')
95 # The client should reconnect and complete the write operation
96 check_proc_NBD(clt, 'client')
97 # Make it sure that server terminated
98 check_proc_NBD(srv, 'server')