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