xref: /openbmc/qemu/tests/qemu-iotests/264 (revision e9e51b71)
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
23
24import iotests
25from iotests import qemu_img_create, file_path, qemu_nbd_popen, log
26
27iotests.script_initialize(
28    supported_fmts=['qcow2'],
29)
30
31disk_a, disk_b, nbd_sock = file_path('disk_a', 'disk_b', 'nbd-sock')
32nbd_uri = 'nbd+unix:///?socket=' + nbd_sock
33size = 5 * 1024 * 1024
34wait_limit = 3.0
35wait_step = 0.2
36
37qemu_img_create('-f', iotests.imgfmt, disk_a, str(size))
38qemu_img_create('-f', iotests.imgfmt, disk_b, str(size))
39
40with qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b):
41    vm = iotests.VM().add_drive(disk_a)
42    vm.launch()
43    vm.hmp_qemu_io('drive0', 'write 0 {}'.format(size))
44
45    vm.qmp_log('blockdev-add', filters=[iotests.filter_qmp_testfiles],
46               **{'node_name': 'backup0',
47                  'driver': 'raw',
48                  'file': {'driver': 'nbd',
49                           'server': {'type': 'unix', 'path': nbd_sock},
50                           'reconnect-delay': 10}})
51    vm.qmp_log('blockdev-backup', device='drive0', sync='full',
52               target='backup0', speed=(1 * 1024 * 1024))
53
54    # Wait for some progress
55    t = 0.0
56    while t < wait_limit:
57        jobs = vm.qmp('query-block-jobs')['return']
58        if jobs and jobs[0]['offset'] > 0:
59            break
60        time.sleep(wait_step)
61        t += wait_step
62
63    if jobs and jobs[0]['offset'] > 0:
64        log('Backup job is started')
65
66jobs = vm.qmp('query-block-jobs')['return']
67if jobs and jobs[0]['offset'] < jobs[0]['len']:
68    log('Backup job is still in progress')
69
70vm.qmp_log('block-job-set-speed', device='drive0', speed=0)
71
72# Emulate server down time for 1 second
73time.sleep(1)
74
75with qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b):
76    e = vm.event_wait('BLOCK_JOB_COMPLETED')
77    log('Backup completed: {}'.format(e['data']['offset']))
78    vm.qmp_log('blockdev-del', node_name='backup0')
79    vm.shutdown()
80