2020-01-30 17:32:30 +01:00
|
|
|
#!/usr/bin/env python3
|
2021-01-16 14:44:19 +01:00
|
|
|
# group: rw
|
2019-10-09 10:41:58 +02: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 17:37:16 +01:00
|
|
|
import os
|
2019-10-09 10:41:58 +02:00
|
|
|
|
|
|
|
import iotests
|
2021-02-05 17:37:16 +01:00
|
|
|
from iotests import qemu_img_create, file_path, qemu_nbd_popen
|
2019-10-25 16:50:22 +02:00
|
|
|
|
2019-10-09 10:41:58 +02:00
|
|
|
disk_a, disk_b, nbd_sock = file_path('disk_a', 'disk_b', 'nbd-sock')
|
|
|
|
nbd_uri = 'nbd+unix:///?socket=' + nbd_sock
|
2020-11-18 19:04:28 +01:00
|
|
|
wait_limit = 3.0
|
2019-10-09 10:41:58 +02:00
|
|
|
wait_step = 0.2
|
|
|
|
|
|
|
|
|
2021-02-05 17:37:16 +01:00
|
|
|
class TestNbdReconnect(iotests.QMPTestCase):
|
2021-02-05 17:37:18 +01: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 17:37:16 +01:00
|
|
|
self.vm = iotests.VM().add_drive(disk_a)
|
|
|
|
self.vm.launch()
|
2021-02-05 17:37:18 +01:00
|
|
|
self.vm.hmp_qemu_io('drive0', 'write 0 {}'.format(disk_size))
|
2021-02-05 17:37:16 +01:00
|
|
|
|
|
|
|
def tearDown(self):
|
|
|
|
self.vm.shutdown()
|
|
|
|
os.remove(disk_a)
|
|
|
|
os.remove(disk_b)
|
|
|
|
|
2021-02-05 17:37:18 +01: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 17:37:16 +01:00
|
|
|
with qemu_nbd_popen('-k', nbd_sock, '-f', iotests.imgfmt, disk_b):
|
|
|
|
result = self.vm.qmp('blockdev-add',
|
|
|
|
**{'node_name': 'backup0',
|
|
|
|
'driver': 'raw',
|
|
|
|
'file': {'driver': 'nbd',
|
|
|
|
'server': {'type': 'unix',
|
|
|
|
'path': nbd_sock},
|
|
|
|
'reconnect-delay': 10}})
|
|
|
|
self.assert_qmp(result, 'return', {})
|
2021-02-05 17:37:18 +01:00
|
|
|
result = self.vm.qmp(job, device='drive0',
|
2021-02-05 17:37:16 +01:00
|
|
|
sync='full', target='backup0',
|
|
|
|
speed=(1 * 1024 * 1024))
|
|
|
|
self.assert_qmp(result, 'return', {})
|
|
|
|
|
|
|
|
# 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-27 23:58:44 +02:00
|
|
|
|
2021-02-05 17:37:16 +01:00
|
|
|
self.assertTrue(jobs and jobs[0]['offset'] > 0) # job started
|
2020-07-27 23:58:44 +02:00
|
|
|
|
2021-02-05 17:37:16 +01:00
|
|
|
jobs = self.vm.qmp('query-block-jobs')['return']
|
|
|
|
# Check that job is still in progress
|
2021-02-05 17:37:18 +01:00
|
|
|
self.assertTrue(jobs)
|
|
|
|
self.assertTrue(jobs[0]['offset'] < jobs[0]['len'])
|
2019-10-09 10:41:58 +02:00
|
|
|
|
2021-02-05 17:37:16 +01:00
|
|
|
result = self.vm.qmp('block-job-set-speed', device='drive0', speed=0)
|
|
|
|
self.assert_qmp(result, 'return', {})
|
2019-10-09 10:41:58 +02:00
|
|
|
|
2021-02-05 17:37:16 +01:00
|
|
|
# Emulate server down time for 1 second
|
|
|
|
time.sleep(1)
|
2019-10-09 10:41:58 +02:00
|
|
|
|
2021-02-05 17:37:18 +01:00
|
|
|
def test_backup(self):
|
|
|
|
size = 5 * 1024 * 1024
|
|
|
|
self.init_vm(size)
|
|
|
|
self.start_job('blockdev-backup')
|
|
|
|
|
2021-02-05 17:37:16 +01: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)
|
|
|
|
result = self.vm.qmp('blockdev-del', node_name='backup0')
|
|
|
|
self.assert_qmp(result, 'return', {})
|
2019-10-09 10:41:58 +02:00
|
|
|
|
2021-02-05 17:37:20 +01:00
|
|
|
def cancel_job(self):
|
2021-04-21 09:58:58 +02:00
|
|
|
result = self.vm.qmp('block-job-cancel', device='drive0', force=True)
|
2021-02-05 17:37:18 +01:00
|
|
|
self.assert_qmp(result, 'return', {})
|
|
|
|
|
|
|
|
start_t = time.time()
|
|
|
|
self.vm.event_wait('BLOCK_JOB_CANCELLED')
|
|
|
|
delta_t = time.time() - start_t
|
2022-08-02 14:31:01 +02:00
|
|
|
self.assertTrue(delta_t < 5.0)
|
2021-02-05 17:37:18 +01:00
|
|
|
|
2021-02-05 17:37:20 +01: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 10:41:58 +02:00
|
|
|
|
2021-02-05 17:37:16 +01:00
|
|
|
if __name__ == '__main__':
|
|
|
|
iotests.main(supported_fmts=['qcow2'])
|