xref: /openbmc/qemu/tests/qemu-iotests/277 (revision acf2b9fc)
1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test NBD client reconnection
5#
6# Copyright (c) 2019 Virtuozzo International GmbH
7#
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.
12#
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.
17#
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/>.
20#
21
22import os
23import subprocess
24import iotests
25from iotests import file_path, log
26
27iotests.script_initialize()
28
29
30conf_file = file_path('nbd-fault-injector.conf')
31nbd_sock = file_path('nbd-sock', base_dir=iotests.sock_dir)
32
33
34def make_conf_file(event):
35    """
36    Create configuration file for the nbd-fault-injector.py
37
38    :param event: which event the server should close a connection on
39    """
40    with open(conf_file, 'w') as conff:
41        conff.write('[inject-error]\nevent={}\nwhen=after'.format(event))
42
43
44def start_server_NBD(event):
45    make_conf_file(event)
46
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')
53    else:
54        log('NBD server: ' + line.rstrip())
55
56    return srv
57
58
59def 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)
68    return clt
69
70
71def check_proc_NBD(proc, connector):
72    try:
73        outs, errs = proc.communicate(timeout=10)
74
75        if proc.returncode < 0:
76            log('NBD {}: EXIT SIGNAL {}\n'.format(connector, proc.returncode))
77            log(outs)
78        else:
79            msg = outs.split('\n', 1)
80            log('NBD {}: {}'.format(connector, msg[0]))
81
82    except subprocess.TimeoutExpired:
83        proc.kill()
84        log('NBD {}: ERROR timeout expired'.format(connector))
85    finally:
86        if connector == 'server':
87            os.remove(nbd_sock)
88            os.remove(conf_file)
89
90
91srv = start_server_NBD('data')
92clt = start_client_NBD()
93# The server should close the connection after a client write request
94check_proc_NBD(srv, 'server')
95# Start the NBD server again
96srv = start_server_NBD('reply')
97# The client should reconnect and complete the write operation
98check_proc_NBD(clt, 'client')
99# Make it sure that server terminated
100check_proc_NBD(srv, 'server')
101