1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3 2*9dd003a9SVladimir Sementsov-Ogievskiy# group: rw 3e38da020SMax Reitz# 4e38da020SMax Reitz# Tests for active mirroring 5e38da020SMax Reitz# 6e38da020SMax Reitz# Copyright (C) 2018 Red Hat, Inc. 7e38da020SMax Reitz# 8e38da020SMax Reitz# This program is free software; you can redistribute it and/or modify 9e38da020SMax Reitz# it under the terms of the GNU General Public License as published by 10e38da020SMax Reitz# the Free Software Foundation; either version 2 of the License, or 11e38da020SMax Reitz# (at your option) any later version. 12e38da020SMax Reitz# 13e38da020SMax Reitz# This program is distributed in the hope that it will be useful, 14e38da020SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 15e38da020SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16e38da020SMax Reitz# GNU General Public License for more details. 17e38da020SMax Reitz# 18e38da020SMax Reitz# You should have received a copy of the GNU General Public License 19e38da020SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 20e38da020SMax Reitz# 21e38da020SMax Reitz 22e38da020SMax Reitzimport os 23e38da020SMax Reitzimport iotests 24e38da020SMax Reitzfrom iotests import qemu_img 25e38da020SMax Reitz 26e38da020SMax Reitzsource_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt) 27e38da020SMax Reitztarget_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt) 28e38da020SMax Reitz 29e38da020SMax Reitzclass TestActiveMirror(iotests.QMPTestCase): 30e38da020SMax Reitz image_len = 128 * 1024 * 1024 # MB 31e38da020SMax Reitz potential_writes_in_flight = True 32e38da020SMax Reitz 33e38da020SMax Reitz def setUp(self): 34e38da020SMax Reitz qemu_img('create', '-f', iotests.imgfmt, source_img, '128M') 35e38da020SMax Reitz qemu_img('create', '-f', iotests.imgfmt, target_img, '128M') 36e38da020SMax Reitz 37e38da020SMax Reitz blk_source = {'id': 'source', 38e38da020SMax Reitz 'if': 'none', 39e38da020SMax Reitz 'node-name': 'source-node', 40e38da020SMax Reitz 'driver': iotests.imgfmt, 41e38da020SMax Reitz 'file': {'driver': 'file', 42e38da020SMax Reitz 'filename': source_img}} 43e38da020SMax Reitz 44e38da020SMax Reitz blk_target = {'node-name': 'target-node', 45e38da020SMax Reitz 'driver': iotests.imgfmt, 46e38da020SMax Reitz 'file': {'driver': 'file', 47e38da020SMax Reitz 'filename': target_img}} 48e38da020SMax Reitz 49e38da020SMax Reitz self.vm = iotests.VM() 50e38da020SMax Reitz self.vm.add_drive_raw(self.vm.qmp_to_opts(blk_source)) 51e38da020SMax Reitz self.vm.add_blockdev(self.vm.qmp_to_opts(blk_target)) 52e38da020SMax Reitz self.vm.add_device('virtio-blk,drive=source') 53e38da020SMax Reitz self.vm.launch() 54e38da020SMax Reitz 55e38da020SMax Reitz def tearDown(self): 56e38da020SMax Reitz self.vm.shutdown() 57e38da020SMax Reitz 58e38da020SMax Reitz if not self.potential_writes_in_flight: 59e38da020SMax Reitz self.assertTrue(iotests.compare_images(source_img, target_img), 60e38da020SMax Reitz 'mirror target does not match source') 61e38da020SMax Reitz 62e38da020SMax Reitz os.remove(source_img) 63e38da020SMax Reitz os.remove(target_img) 64e38da020SMax Reitz 65e38da020SMax Reitz def doActiveIO(self, sync_source_and_target): 66e38da020SMax Reitz # Fill the source image 67e38da020SMax Reitz self.vm.hmp_qemu_io('source', 68e38da020SMax Reitz 'write -P 1 0 %i' % self.image_len); 69e38da020SMax Reitz 70e38da020SMax Reitz # Start some background requests 719a3a9a63SMax Reitz for offset in range(1 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024): 72e38da020SMax Reitz self.vm.hmp_qemu_io('source', 'aio_write -P 2 %i 1M' % offset) 739a3a9a63SMax Reitz for offset in range(2 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024): 74e38da020SMax Reitz self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset) 75e38da020SMax Reitz 76e38da020SMax Reitz # Start the block job 77e38da020SMax Reitz result = self.vm.qmp('blockdev-mirror', 78e38da020SMax Reitz job_id='mirror', 79e38da020SMax Reitz filter_node_name='mirror-node', 80e38da020SMax Reitz device='source-node', 81e38da020SMax Reitz target='target-node', 82e38da020SMax Reitz sync='full', 83e38da020SMax Reitz copy_mode='write-blocking') 84e38da020SMax Reitz self.assert_qmp(result, 'return', {}) 85e38da020SMax Reitz 86e38da020SMax Reitz # Start some more requests 879a3a9a63SMax Reitz for offset in range(3 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024): 88e38da020SMax Reitz self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset) 899a3a9a63SMax Reitz for offset in range(4 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024): 90e38da020SMax Reitz self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset) 91e38da020SMax Reitz 92e38da020SMax Reitz # Wait for the READY event 93e38da020SMax Reitz self.wait_ready(drive='mirror') 94e38da020SMax Reitz 95e38da020SMax Reitz # Now start some final requests; all of these (which land on 96e38da020SMax Reitz # the source) should be settled using the active mechanism. 97e38da020SMax Reitz # The mirror code itself asserts that the source BDS's dirty 98e38da020SMax Reitz # bitmap will stay clean between READY and COMPLETED. 999a3a9a63SMax Reitz for offset in range(5 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024): 100e38da020SMax Reitz self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset) 1019a3a9a63SMax Reitz for offset in range(6 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024): 102e38da020SMax Reitz self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset) 103e38da020SMax Reitz 104e38da020SMax Reitz if sync_source_and_target: 105e38da020SMax Reitz # If source and target should be in sync after the mirror, 106e38da020SMax Reitz # we have to flush before completion 107e38da020SMax Reitz self.vm.hmp_qemu_io('source', 'aio_flush') 108e38da020SMax Reitz self.potential_writes_in_flight = False 109e38da020SMax Reitz 110e38da020SMax Reitz self.complete_and_wait(drive='mirror', wait_ready=False) 111e38da020SMax Reitz 112e38da020SMax Reitz def testActiveIO(self): 113e38da020SMax Reitz self.doActiveIO(False) 114e38da020SMax Reitz 115e38da020SMax Reitz def testActiveIOFlushed(self): 116e38da020SMax Reitz self.doActiveIO(True) 117e38da020SMax Reitz 11819ba4651SMax Reitz def testUnalignedActiveIO(self): 11919ba4651SMax Reitz # Fill the source image 12019ba4651SMax Reitz result = self.vm.hmp_qemu_io('source', 'write -P 1 0 2M') 12119ba4651SMax Reitz 12219ba4651SMax Reitz # Start the block job (very slowly) 12319ba4651SMax Reitz result = self.vm.qmp('blockdev-mirror', 12419ba4651SMax Reitz job_id='mirror', 12519ba4651SMax Reitz filter_node_name='mirror-node', 12619ba4651SMax Reitz device='source-node', 12719ba4651SMax Reitz target='target-node', 12819ba4651SMax Reitz sync='full', 12919ba4651SMax Reitz copy_mode='write-blocking', 13019ba4651SMax Reitz buf_size=(1048576 // 4), 13119ba4651SMax Reitz speed=1) 13219ba4651SMax Reitz self.assert_qmp(result, 'return', {}) 13319ba4651SMax Reitz 13419ba4651SMax Reitz # Start an unaligned request to a dirty area 13519ba4651SMax Reitz result = self.vm.hmp_qemu_io('source', 'write -P 2 %i 1' % (1048576 + 42)) 13619ba4651SMax Reitz 13719ba4651SMax Reitz # Let the job finish 13819ba4651SMax Reitz result = self.vm.qmp('block-job-set-speed', device='mirror', speed=0) 13919ba4651SMax Reitz self.assert_qmp(result, 'return', {}) 14019ba4651SMax Reitz self.complete_and_wait(drive='mirror') 14119ba4651SMax Reitz 14219ba4651SMax Reitz self.potential_writes_in_flight = False 143e38da020SMax Reitz 144e38da020SMax Reitz 145e38da020SMax Reitzif __name__ == '__main__': 146103cbc77SMax Reitz iotests.main(supported_fmts=['qcow2', 'raw'], 147103cbc77SMax Reitz supported_protocols=['file']) 148