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
|
|
|
|
2019-10-09 11:41:58 +03: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 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):
|
|
|
|
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 19:37:18 +03:00
|
|
|
result = self.vm.qmp(job, device='drive0',
|
2021-02-05 19:37:16 +03: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-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
|
|
|
|
2021-02-05 19:37:16 +03:00
|
|
|
result = self.vm.qmp('block-job-set-speed', device='drive0', speed=0)
|
|
|
|
self.assert_qmp(result, 'return', {})
|
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)
|
|
|
|
result = self.vm.qmp('blockdev-del', node_name='backup0')
|
|
|
|
self.assert_qmp(result, 'return', {})
|
2019-10-09 11:41:58 +03:00
|
|
|
|
2021-02-05 19:37:20 +03:00
|
|
|
def cancel_job(self):
|
2021-02-05 19:37:18 +03:00
|
|
|
result = self.vm.qmp('block-job-cancel', device='drive0')
|
|
|
|
self.assert_qmp(result, 'return', {})
|
|
|
|
|
|
|
|
start_t = time.time()
|
|
|
|
self.vm.event_wait('BLOCK_JOB_CANCELLED')
|
|
|
|
delta_t = time.time() - start_t
|
|
|
|
self.assertTrue(delta_t < 2.0)
|
|
|
|
|
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'])
|