2020-01-30 19:32:30 +03:00
|
|
|
#!/usr/bin/env python3
|
2021-01-16 16:44:19 +03:00
|
|
|
# group: rw
|
2019-10-09 11:41:58 +03:00
|
|
|
#
|
|
|
|
# Test nbd reconnect
|
|
|
|
#
|
|
|
|
# Copyright (c) 2019 Virtuozzo International GmbH.
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License
|
|
|
|
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
#
|
|
|
|
|
|
|
|
import time
|
2021-02-05 19:37:16 +03:00
|
|
|
import os
|
2019-10-09 11:41:58 +03:00
|
|
|
|
|
|
|
import iotests
|
2021-02-05 19:37:16 +03:00
|
|
|
from iotests import qemu_img_create, file_path, qemu_nbd_popen
|
2019-10-25 17:50:22 +03:00
|
|
|
|
2024-01-25 16:52:37 +03:00
|
|
|
disk_a, disk_b = file_path('disk_a', 'disk_b')
|
|
|
|
nbd_sock = file_path('nbd-sock', base_dir=iotests.sock_dir)
|
2019-10-09 11:41:58 +03:00
|
|
|
nbd_uri = 'nbd+unix:///?socket=' + nbd_sock
|
2020-11-18 21:04:28 +03:00
|
|
|
wait_limit = 3.0
|
2019-10-09 11:41:58 +03:00
|
|
|
wait_step = 0.2
|
|
|
|
|
|
|
|
|
2021-02-05 19:37:16 +03:00
|
|
|
class TestNbdReconnect(iotests.QMPTestCase):
|
2021-02-05 19:37:18 +03:00
|
|
|
def init_vm(self, disk_size):
|
|
|
|
qemu_img_create('-f', iotests.imgfmt, disk_a, str(disk_size))
|
|
|
|
qemu_img_create('-f', iotests.imgfmt, disk_b, str(disk_size))
|
2021-02-05 19:37:16 +03:00
|
|
|
self.vm = iotests.VM().add_drive(disk_a)
|
|
|
|
self.vm.launch()
|
2021-02-05 19:37:18 +03:00
|
|
|
self.vm.hmp_qemu_io('drive0', 'write 0 {}'.format(disk_size))
|
2021-02-05 19:37:16 +03:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.vm.shutdown()
|
|
|
|
os.remove(disk_a)
|
|
|
|
os.remove(disk_b)
|
|
|
|
|
2021-02-05 19:37:18 +03:00
|
|
|
def start_job(self, job):
|
|
|
|
"""Stat job with nbd target and kill the server"""
|
|
|
|
assert job in ('blockdev-backup', 'blockdev-mirror')
|
2021-02-05 19:37:16 +03:00
|
|
|
with qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b):
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd('blockdev-add',
|
|
|
|
{'node-name': 'backup0',
|
|
|
|
'driver': 'raw',
|
|
|
|
'file': {'driver': 'nbd',
|
|
|
|
'server': {'type': 'unix',
|
|
|
|
'path': nbd_sock},
|
|
|
|
'reconnect-delay': 10}})
|
|
|
|
self.vm.cmd(job, device='drive0',
|
|
|
|
sync='full', target='backup0',
|
|
|
|
speed=(1 * 1024 * 1024))
|
2021-02-05 19:37:16 +03:00
|
|
|
|
|
|
|
# Wait for some progress
|
|
|
|
t = 0.0
|
|
|
|
while t < wait_limit:
|
|
|
|
jobs = self.vm.qmp('query-block-jobs')['return']
|
|
|
|
if jobs and jobs[0]['offset'] > 0:
|
|
|
|
break
|
|
|
|
time.sleep(wait_step)
|
|
|
|
t += wait_step
|
2020-07-28 00:58:44 +03:00
|
|
|
|
2021-02-05 19:37:16 +03:00
|
|
|
self.assertTrue(jobs and jobs[0]['offset'] > 0) # job started
|
2020-07-28 00:58:44 +03:00
|
|
|
|
2021-02-05 19:37:16 +03:00
|
|
|
jobs = self.vm.qmp('query-block-jobs')['return']
|
|
|
|
# Check that job is still in progress
|
2021-02-05 19:37:18 +03:00
|
|
|
self.assertTrue(jobs)
|
|
|
|
self.assertTrue(jobs[0]['offset'] < jobs[0]['len'])
|
2019-10-09 11:41:58 +03:00
|
|
|
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd('block-job-set-speed', device='drive0', speed=0)
|
2019-10-09 11:41:58 +03:00
|
|
|
|
2021-02-05 19:37:16 +03:00
|
|
|
# Emulate server down time for 1 second
|
|
|
|
time.sleep(1)
|
2019-10-09 11:41:58 +03:00
|
|
|
|
2021-02-05 19:37:18 +03:00
|
|
|
def test_backup(self):
|
|
|
|
size = 5 * 1024 * 1024
|
|
|
|
self.init_vm(size)
|
|
|
|
self.start_job('blockdev-backup')
|
|
|
|
|
2021-02-05 19:37:16 +03:00
|
|
|
with qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b):
|
|
|
|
e = self.vm.event_wait('BLOCK_JOB_COMPLETED')
|
|
|
|
self.assertEqual(e['data']['offset'], size)
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd('blockdev-del', node_name='backup0')
|
2019-10-09 11:41:58 +03:00
|
|
|
|
2021-02-05 19:37:20 +03:00
|
|
|
def cancel_job(self):
|
2023-10-06 18:41:25 +03:00
|
|
|
self.vm.cmd('block-job-cancel', device='drive0', force=True)
|
2021-02-05 19:37:18 +03:00
|
|
|
|
|
|
|
start_t = time.time()
|
|
|
|
self.vm.event_wait('BLOCK_JOB_CANCELLED')
|
|
|
|
delta_t = time.time() - start_t
|
2022-08-02 15:31:01 +03:00
|
|
|
self.assertTrue(delta_t < 5.0)
|
2021-02-05 19:37:18 +03:00
|
|
|
|
2021-02-05 19:37:20 +03:00
|
|
|
def test_mirror_cancel(self):
|
|
|
|
# Mirror speed limit doesn't work well enough, it seems that mirror
|
|
|
|
# will run many parallel requests anyway. MAX_IN_FLIGHT is 16 and
|
|
|
|
# MAX_IO_BYTES is 1M in mirror.c, so let's use 20M disk.
|
|
|
|
self.init_vm(20 * 1024 * 1024)
|
|
|
|
self.start_job('blockdev-mirror')
|
|
|
|
self.cancel_job()
|
|
|
|
|
|
|
|
def test_backup_cancel(self):
|
|
|
|
self.init_vm(5 * 1024 * 1024)
|
|
|
|
self.start_job('blockdev-backup')
|
|
|
|
self.cancel_job()
|
|
|
|
|
2019-10-09 11:41:58 +03:00
|
|
|
|
2021-02-05 19:37:16 +03:00
|
|
|
if __name__ == '__main__':
|
|
|
|
iotests.main(supported_fmts=['qcow2'])
|