xref: /openbmc/qemu/tests/qemu-iotests/264 (revision 2e1cacfb)
1#!/usr/bin/env python3
2# group: rw
3#
4# Test nbd reconnect
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 time
23import os
24
25import iotests
26from iotests import qemu_img_create, file_path, qemu_nbd_popen
27
28disk_a, disk_b = file_path('disk_a', 'disk_b')
29nbd_sock = file_path('nbd-sock', base_dir=iotests.sock_dir)
30nbd_uri = 'nbd+unix:///?socket=' + nbd_sock
31wait_limit = 3.0
32wait_step = 0.2
33
34
35class TestNbdReconnect(iotests.QMPTestCase):
36    def init_vm(self, disk_size):
37        qemu_img_create('-f', iotests.imgfmt, disk_a, str(disk_size))
38        qemu_img_create('-f', iotests.imgfmt, disk_b, str(disk_size))
39        self.vm = iotests.VM().add_drive(disk_a)
40        self.vm.launch()
41        self.vm.hmp_qemu_io('drive0', 'write 0 {}'.format(disk_size))
42
43    def tearDown(self):
44        self.vm.shutdown()
45        os.remove(disk_a)
46        os.remove(disk_b)
47
48    def start_job(self, job):
49        """Stat job with nbd target and kill the server"""
50        assert job in ('blockdev-backup', 'blockdev-mirror')
51        with qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b):
52            self.vm.cmd('blockdev-add',
53                        {'node-name': 'backup0',
54                         'driver': 'raw',
55                         'file': {'driver': 'nbd',
56                                  'server': {'type': 'unix',
57                                             'path': nbd_sock},
58                                  'reconnect-delay': 10}})
59            self.vm.cmd(job, device='drive0',
60                        sync='full', target='backup0',
61                        speed=(1 * 1024 * 1024))
62
63            # Wait for some progress
64            t = 0.0
65            while t < wait_limit:
66                jobs = self.vm.qmp('query-block-jobs')['return']
67                if jobs and jobs[0]['offset'] > 0:
68                    break
69                time.sleep(wait_step)
70                t += wait_step
71
72            self.assertTrue(jobs and jobs[0]['offset'] > 0)  # job started
73
74        jobs = self.vm.qmp('query-block-jobs')['return']
75        # Check that job is still in progress
76        self.assertTrue(jobs)
77        self.assertTrue(jobs[0]['offset'] < jobs[0]['len'])
78
79        self.vm.cmd('block-job-set-speed', device='drive0', speed=0)
80
81        # Emulate server down time for 1 second
82        time.sleep(1)
83
84    def test_backup(self):
85        size = 5 * 1024 * 1024
86        self.init_vm(size)
87        self.start_job('blockdev-backup')
88
89        with qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b):
90            e = self.vm.event_wait('BLOCK_JOB_COMPLETED')
91            self.assertEqual(e['data']['offset'], size)
92            self.vm.cmd('blockdev-del', node_name='backup0')
93
94    def cancel_job(self):
95        self.vm.cmd('block-job-cancel', device='drive0', force=True)
96
97        start_t = time.time()
98        self.vm.event_wait('BLOCK_JOB_CANCELLED')
99        delta_t = time.time() - start_t
100        self.assertTrue(delta_t < 5.0)
101
102    def test_mirror_cancel(self):
103        # Mirror speed limit doesn't work well enough, it seems that mirror
104        # will run many parallel requests anyway. MAX_IN_FLIGHT is 16 and
105        # MAX_IO_BYTES is 1M in mirror.c, so let's use 20M disk.
106        self.init_vm(20 * 1024 * 1024)
107        self.start_job('blockdev-mirror')
108        self.cancel_job()
109
110    def test_backup_cancel(self):
111        self.init_vm(5 * 1024 * 1024)
112        self.start_job('blockdev-backup')
113        self.cancel_job()
114
115
116if __name__ == '__main__':
117    iotests.main(supported_fmts=['qcow2'])
118