1903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3 244c7ca5eSPaolo Bonzini# 344c7ca5eSPaolo Bonzini# Tests for image mirroring. 444c7ca5eSPaolo Bonzini# 544c7ca5eSPaolo Bonzini# Copyright (C) 2012 Red Hat, Inc. 644c7ca5eSPaolo Bonzini# 744c7ca5eSPaolo Bonzini# This program is free software; you can redistribute it and/or modify 844c7ca5eSPaolo Bonzini# it under the terms of the GNU General Public License as published by 944c7ca5eSPaolo Bonzini# the Free Software Foundation; either version 2 of the License, or 1044c7ca5eSPaolo Bonzini# (at your option) any later version. 1144c7ca5eSPaolo Bonzini# 1244c7ca5eSPaolo Bonzini# This program is distributed in the hope that it will be useful, 1344c7ca5eSPaolo Bonzini# but WITHOUT ANY WARRANTY; without even the implied warranty of 1444c7ca5eSPaolo Bonzini# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 1544c7ca5eSPaolo Bonzini# GNU General Public License for more details. 1644c7ca5eSPaolo Bonzini# 1744c7ca5eSPaolo Bonzini# You should have received a copy of the GNU General Public License 1844c7ca5eSPaolo Bonzini# along with this program. If not, see <http://www.gnu.org/licenses/>. 1944c7ca5eSPaolo Bonzini# 2044c7ca5eSPaolo Bonzini 2144c7ca5eSPaolo Bonziniimport time 2244c7ca5eSPaolo Bonziniimport os 23a1da1878SMax Reitzimport re 2444c7ca5eSPaolo Bonziniimport iotests 2544c7ca5eSPaolo Bonzinifrom iotests import qemu_img, qemu_io 2644c7ca5eSPaolo Bonzini 2744c7ca5eSPaolo Bonzinibacking_img = os.path.join(iotests.test_dir, 'backing.img') 2844c7ca5eSPaolo Bonzinitarget_backing_img = os.path.join(iotests.test_dir, 'target-backing.img') 2944c7ca5eSPaolo Bonzinitest_img = os.path.join(iotests.test_dir, 'test.img') 3044c7ca5eSPaolo Bonzinitarget_img = os.path.join(iotests.test_dir, 'target.img') 3144c7ca5eSPaolo Bonzini 32d88964aeSBenoît Canetquorum_img1 = os.path.join(iotests.test_dir, 'quorum1.img') 33d88964aeSBenoît Canetquorum_img2 = os.path.join(iotests.test_dir, 'quorum2.img') 34d88964aeSBenoît Canetquorum_img3 = os.path.join(iotests.test_dir, 'quorum3.img') 35d88964aeSBenoît Canetquorum_repair_img = os.path.join(iotests.test_dir, 'quorum_repair.img') 36d88964aeSBenoît Canetquorum_snapshot_file = os.path.join(iotests.test_dir, 'quorum_snapshot.img') 37d88964aeSBenoît Canet 38e5ac52d8SMax Reitznbd_sock_path = os.path.join(iotests.sock_dir, 'nbd.sock') 39a1da1878SMax Reitz 40866323f3SFam Zhengclass TestSingleDrive(iotests.QMPTestCase): 4144c7ca5eSPaolo Bonzini image_len = 1 * 1024 * 1024 # MB 4294ca2c73SFam Zheng qmp_cmd = 'drive-mirror' 4394ca2c73SFam Zheng qmp_target = target_img 4444c7ca5eSPaolo Bonzini 4544c7ca5eSPaolo Bonzini def setUp(self): 463b9f27d2SFam Zheng iotests.create_image(backing_img, self.image_len) 4744c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 48d3c8c674SKevin Wolf self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=base") 490ed82f7aSMax Reitz if iotests.qemu_default_machine == 'pc': 500ed82f7aSMax Reitz self.vm.add_drive(None, 'media=cdrom', 'ide') 5144c7ca5eSPaolo Bonzini self.vm.launch() 5244c7ca5eSPaolo Bonzini 5344c7ca5eSPaolo Bonzini def tearDown(self): 5444c7ca5eSPaolo Bonzini self.vm.shutdown() 5544c7ca5eSPaolo Bonzini os.remove(test_img) 5644c7ca5eSPaolo Bonzini os.remove(backing_img) 5744c7ca5eSPaolo Bonzini try: 5844c7ca5eSPaolo Bonzini os.remove(target_img) 5944c7ca5eSPaolo Bonzini except OSError: 6044c7ca5eSPaolo Bonzini pass 6144c7ca5eSPaolo Bonzini 6244c7ca5eSPaolo Bonzini def test_complete(self): 63ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 6444c7ca5eSPaolo Bonzini 6594ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 6694ca2c73SFam Zheng target=self.qmp_target) 6744c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 6844c7ca5eSPaolo Bonzini 6944c7ca5eSPaolo Bonzini self.complete_and_wait() 7044c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block') 7144c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', target_img) 7244c7ca5eSPaolo Bonzini self.vm.shutdown() 733a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 7444c7ca5eSPaolo Bonzini 'target image does not match source after mirroring') 7544c7ca5eSPaolo Bonzini 7644c7ca5eSPaolo Bonzini def test_cancel(self): 77ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 7844c7ca5eSPaolo Bonzini 7994ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 8094ca2c73SFam Zheng target=self.qmp_target) 8144c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 8244c7ca5eSPaolo Bonzini 832575fe16SStefan Hajnoczi self.cancel_and_wait(force=True) 8444c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block') 8544c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', test_img) 8644c7ca5eSPaolo Bonzini 8744c7ca5eSPaolo Bonzini def test_cancel_after_ready(self): 88ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 8944c7ca5eSPaolo Bonzini 9094ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 9194ca2c73SFam Zheng target=self.qmp_target) 9244c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 9344c7ca5eSPaolo Bonzini 942575fe16SStefan Hajnoczi self.wait_ready_and_cancel() 9544c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block') 9644c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', test_img) 9744c7ca5eSPaolo Bonzini self.vm.shutdown() 983a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 9944c7ca5eSPaolo Bonzini 'target image does not match source after mirroring') 10044c7ca5eSPaolo Bonzini 10144c7ca5eSPaolo Bonzini def test_pause(self): 102ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 10344c7ca5eSPaolo Bonzini 10494ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 10594ca2c73SFam Zheng target=self.qmp_target) 10644c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 10744c7ca5eSPaolo Bonzini 1082c93c5cbSKevin Wolf self.pause_job('drive0') 10944c7ca5eSPaolo Bonzini 11044c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block-jobs') 11144c7ca5eSPaolo Bonzini offset = self.dictpath(result, 'return[0]/offset') 11244c7ca5eSPaolo Bonzini 1132c93c5cbSKevin Wolf time.sleep(0.5) 11444c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block-jobs') 11544c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/offset', offset) 11644c7ca5eSPaolo Bonzini 11744c7ca5eSPaolo Bonzini result = self.vm.qmp('block-job-resume', device='drive0') 11844c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 11944c7ca5eSPaolo Bonzini 12044c7ca5eSPaolo Bonzini self.complete_and_wait() 12144c7ca5eSPaolo Bonzini self.vm.shutdown() 1223a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 12344c7ca5eSPaolo Bonzini 'target image does not match source after mirroring') 12444c7ca5eSPaolo Bonzini 12508e4ed6cSPaolo Bonzini def test_small_buffer(self): 126ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 12708e4ed6cSPaolo Bonzini 12808e4ed6cSPaolo Bonzini # A small buffer is rounded up automatically 12994ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 13094ca2c73SFam Zheng buf_size=4096, target=self.qmp_target) 13108e4ed6cSPaolo Bonzini self.assert_qmp(result, 'return', {}) 13208e4ed6cSPaolo Bonzini 13308e4ed6cSPaolo Bonzini self.complete_and_wait() 13408e4ed6cSPaolo Bonzini result = self.vm.qmp('query-block') 13508e4ed6cSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', target_img) 13608e4ed6cSPaolo Bonzini self.vm.shutdown() 1373a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 13808e4ed6cSPaolo Bonzini 'target image does not match source after mirroring') 13908e4ed6cSPaolo Bonzini 14008e4ed6cSPaolo Bonzini def test_small_buffer2(self): 141ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 14208e4ed6cSPaolo Bonzini 14308e4ed6cSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,size=%d' 1443b9f27d2SFam Zheng % (self.image_len, self.image_len), target_img) 14594ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 14694ca2c73SFam Zheng buf_size=65536, mode='existing', target=self.qmp_target) 14708e4ed6cSPaolo Bonzini self.assert_qmp(result, 'return', {}) 14808e4ed6cSPaolo Bonzini 14908e4ed6cSPaolo Bonzini self.complete_and_wait() 15008e4ed6cSPaolo Bonzini result = self.vm.qmp('query-block') 15108e4ed6cSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', target_img) 15208e4ed6cSPaolo Bonzini self.vm.shutdown() 1533a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 15408e4ed6cSPaolo Bonzini 'target image does not match source after mirroring') 15508e4ed6cSPaolo Bonzini 15644c7ca5eSPaolo Bonzini def test_large_cluster(self): 157ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 15844c7ca5eSPaolo Bonzini 15944c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s' 1603b9f27d2SFam Zheng % (self.image_len, backing_img), target_img) 16194ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 16294ca2c73SFam Zheng mode='existing', target=self.qmp_target) 16344c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 16444c7ca5eSPaolo Bonzini 16544c7ca5eSPaolo Bonzini self.complete_and_wait() 16644c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block') 16744c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', target_img) 16844c7ca5eSPaolo Bonzini self.vm.shutdown() 1693a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 17044c7ca5eSPaolo Bonzini 'target image does not match source after mirroring') 17144c7ca5eSPaolo Bonzini 172d3c8c674SKevin Wolf # Tests that the insertion of the mirror_top filter node doesn't make a 173d3c8c674SKevin Wolf # difference to query-block 174d3c8c674SKevin Wolf def test_implicit_node(self): 175d3c8c674SKevin Wolf self.assert_no_active_block_jobs() 176d3c8c674SKevin Wolf 177d3c8c674SKevin Wolf result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 178d3c8c674SKevin Wolf target=self.qmp_target) 179d3c8c674SKevin Wolf self.assert_qmp(result, 'return', {}) 180d3c8c674SKevin Wolf 181d3c8c674SKevin Wolf result = self.vm.qmp('query-block') 182d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/file', test_img) 183d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt) 184d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img) 185d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1) 186d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img) 187d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img) 188d3c8c674SKevin Wolf 189d3c8c674SKevin Wolf result = self.vm.qmp('query-blockstats') 190d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/node-name', 'top') 191d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/backing/node-name', 'base') 192d3c8c674SKevin Wolf 193d3c8c674SKevin Wolf self.cancel_and_wait(force=True) 194d3c8c674SKevin Wolf result = self.vm.qmp('query-block') 195d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/file', test_img) 196d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt) 197d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img) 198d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1) 199d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img) 200d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img) 201d3c8c674SKevin Wolf 202d3c8c674SKevin Wolf result = self.vm.qmp('query-blockstats') 203d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/node-name', 'top') 204d3c8c674SKevin Wolf self.assert_qmp(result, 'return[0]/backing/node-name', 'base') 205d3c8c674SKevin Wolf 20644c7ca5eSPaolo Bonzini def test_medium_not_found(self): 207d8683155SBo Tu if iotests.qemu_default_machine != 'pc': 208d8683155SBo Tu return 209d8683155SBo Tu 21094ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='ide1-cd0', sync='full', 21194ca2c73SFam Zheng target=self.qmp_target) 2120524e93aSKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 21344c7ca5eSPaolo Bonzini 21444c7ca5eSPaolo Bonzini def test_image_not_found(self): 21594ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 21694ca2c73SFam Zheng mode='existing', target=self.qmp_target) 21744c7ca5eSPaolo Bonzini self.assert_qmp(result, 'error/class', 'GenericError') 21844c7ca5eSPaolo Bonzini 21944c7ca5eSPaolo Bonzini def test_device_not_found(self): 22094ca2c73SFam Zheng result = self.vm.qmp(self.qmp_cmd, device='nonexistent', sync='full', 22194ca2c73SFam Zheng target=self.qmp_target) 2220524e93aSKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 22394ca2c73SFam Zheng 22494ca2c73SFam Zhengclass TestSingleBlockdev(TestSingleDrive): 22594ca2c73SFam Zheng qmp_cmd = 'blockdev-mirror' 22694ca2c73SFam Zheng qmp_target = 'node1' 22794ca2c73SFam Zheng 22894ca2c73SFam Zheng def setUp(self): 22994ca2c73SFam Zheng TestSingleDrive.setUp(self) 23094ca2c73SFam Zheng qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img) 2310153d2f5SKevin Wolf args = {'driver': iotests.imgfmt, 23294ca2c73SFam Zheng 'node-name': self.qmp_target, 2330153d2f5SKevin Wolf 'file': { 'filename': target_img, 'driver': 'file' } } 23494ca2c73SFam Zheng result = self.vm.qmp("blockdev-add", **args) 23594ca2c73SFam Zheng self.assert_qmp(result, 'return', {}) 23694ca2c73SFam Zheng 23786fae10cSKevin Wolf def test_mirror_to_self(self): 23886fae10cSKevin Wolf result = self.vm.qmp(self.qmp_cmd, job_id='job0', 23986fae10cSKevin Wolf device=self.qmp_target, sync='full', 24086fae10cSKevin Wolf target=self.qmp_target) 24186fae10cSKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 24286fae10cSKevin Wolf 24316cea4eeSKevin Wolf def do_test_resize(self, device, node): 24416cea4eeSKevin Wolf def pre_finalize(): 24516cea4eeSKevin Wolf if device: 24616cea4eeSKevin Wolf result = self.vm.qmp('block_resize', device=device, size=65536) 24716cea4eeSKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 24816cea4eeSKevin Wolf 24916cea4eeSKevin Wolf result = self.vm.qmp('block_resize', node_name=node, size=65536) 25016cea4eeSKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 25116cea4eeSKevin Wolf 25216cea4eeSKevin Wolf result = self.vm.qmp(self.qmp_cmd, job_id='job0', device='drive0', 25316cea4eeSKevin Wolf sync='full', target=self.qmp_target, 25416cea4eeSKevin Wolf auto_finalize=False, auto_dismiss=False) 25516cea4eeSKevin Wolf self.assert_qmp(result, 'return', {}) 25616cea4eeSKevin Wolf 25716cea4eeSKevin Wolf result = self.vm.run_job('job0', auto_finalize=False, 25816cea4eeSKevin Wolf pre_finalize=pre_finalize) 25916cea4eeSKevin Wolf self.assertEqual(result, None) 26016cea4eeSKevin Wolf 26116cea4eeSKevin Wolf def test_source_resize(self): 26216cea4eeSKevin Wolf self.do_test_resize('drive0', 'top') 26316cea4eeSKevin Wolf 26416cea4eeSKevin Wolf def test_target_resize(self): 26516cea4eeSKevin Wolf self.do_test_resize(None, self.qmp_target) 26616cea4eeSKevin Wolf 26716cea4eeSKevin Wolf def do_test_target_size(self, size): 26816cea4eeSKevin Wolf result = self.vm.qmp('block_resize', node_name=self.qmp_target, 26916cea4eeSKevin Wolf size=size) 27016cea4eeSKevin Wolf self.assert_qmp(result, 'return', {}) 27116cea4eeSKevin Wolf 27216cea4eeSKevin Wolf result = self.vm.qmp(self.qmp_cmd, job_id='job0', 27316cea4eeSKevin Wolf device='drive0', sync='full', auto_dismiss=False, 27416cea4eeSKevin Wolf target=self.qmp_target) 27516cea4eeSKevin Wolf self.assert_qmp(result, 'return', {}) 27616cea4eeSKevin Wolf 27716cea4eeSKevin Wolf result = self.vm.run_job('job0') 27816cea4eeSKevin Wolf self.assertEqual(result, 'Source and target image have different sizes') 27916cea4eeSKevin Wolf 280*c7070942SMax Reitz # qed does not support shrinking 281*c7070942SMax Reitz @iotests.skip_for_formats(('qed')) 28216cea4eeSKevin Wolf def test_small_target(self): 28316cea4eeSKevin Wolf self.do_test_target_size(self.image_len // 2) 28416cea4eeSKevin Wolf 28516cea4eeSKevin Wolf def test_large_target(self): 28616cea4eeSKevin Wolf self.do_test_target_size(self.image_len * 2) 28716cea4eeSKevin Wolf 28894ca2c73SFam Zheng test_large_cluster = None 28994ca2c73SFam Zheng test_image_not_found = None 29094ca2c73SFam Zheng test_small_buffer2 = None 29194ca2c73SFam Zheng 2923b9f27d2SFam Zhengclass TestSingleDriveZeroLength(TestSingleDrive): 2933b9f27d2SFam Zheng image_len = 0 2943b9f27d2SFam Zheng test_small_buffer2 = None 2953b9f27d2SFam Zheng test_large_cluster = None 2963b9f27d2SFam Zheng 29794ca2c73SFam Zhengclass TestSingleBlockdevZeroLength(TestSingleBlockdev): 29894ca2c73SFam Zheng image_len = 0 29916cea4eeSKevin Wolf test_small_target = None 30016cea4eeSKevin Wolf test_large_target = None 30194ca2c73SFam Zheng 3025a0f6fd5SKevin Wolfclass TestSingleDriveUnalignedLength(TestSingleDrive): 3035a0f6fd5SKevin Wolf image_len = 1025 * 1024 3045a0f6fd5SKevin Wolf test_small_buffer2 = None 3055a0f6fd5SKevin Wolf test_large_cluster = None 3065a0f6fd5SKevin Wolf 30794ca2c73SFam Zhengclass TestSingleBlockdevUnalignedLength(TestSingleBlockdev): 30894ca2c73SFam Zheng image_len = 1025 * 1024 30994ca2c73SFam Zheng 310866323f3SFam Zhengclass TestMirrorNoBacking(iotests.QMPTestCase): 31144c7ca5eSPaolo Bonzini image_len = 2 * 1024 * 1024 # MB 31244c7ca5eSPaolo Bonzini 31344c7ca5eSPaolo Bonzini def setUp(self): 3142499a096SStefan Hajnoczi iotests.create_image(backing_img, TestMirrorNoBacking.image_len) 31544c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 31644c7ca5eSPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 31744c7ca5eSPaolo Bonzini self.vm.launch() 31844c7ca5eSPaolo Bonzini 31944c7ca5eSPaolo Bonzini def tearDown(self): 32044c7ca5eSPaolo Bonzini self.vm.shutdown() 32144c7ca5eSPaolo Bonzini os.remove(test_img) 32244c7ca5eSPaolo Bonzini os.remove(backing_img) 323866323f3SFam Zheng try: 32444c7ca5eSPaolo Bonzini os.remove(target_backing_img) 325866323f3SFam Zheng except: 326866323f3SFam Zheng pass 32744c7ca5eSPaolo Bonzini os.remove(target_img) 32844c7ca5eSPaolo Bonzini 32944c7ca5eSPaolo Bonzini def test_complete(self): 330ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 33144c7ca5eSPaolo Bonzini 33244c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img) 33344c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 33444c7ca5eSPaolo Bonzini mode='existing', target=target_img) 33544c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 33644c7ca5eSPaolo Bonzini 33744c7ca5eSPaolo Bonzini self.complete_and_wait() 33844c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block') 33944c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', target_img) 34044c7ca5eSPaolo Bonzini self.vm.shutdown() 341866323f3SFam Zheng self.assertTrue(iotests.compare_images(test_img, target_img), 34244c7ca5eSPaolo Bonzini 'target image does not match source after mirroring') 34344c7ca5eSPaolo Bonzini 34444c7ca5eSPaolo Bonzini def test_cancel(self): 345ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 34644c7ca5eSPaolo Bonzini 34744c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img) 34844c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 34944c7ca5eSPaolo Bonzini mode='existing', target=target_img) 35044c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 35144c7ca5eSPaolo Bonzini 3522575fe16SStefan Hajnoczi self.wait_ready_and_cancel() 35344c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block') 35444c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', test_img) 35544c7ca5eSPaolo Bonzini self.vm.shutdown() 356866323f3SFam Zheng self.assertTrue(iotests.compare_images(test_img, target_img), 35744c7ca5eSPaolo Bonzini 'target image does not match source after mirroring') 35844c7ca5eSPaolo Bonzini 359b812f671SPaolo Bonzini def test_large_cluster(self): 360ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 361b812f671SPaolo Bonzini 362b812f671SPaolo Bonzini # qemu-img create fails if the image is not there 363b812f671SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'size=%d' 364b812f671SPaolo Bonzini %(TestMirrorNoBacking.image_len), target_backing_img) 365b812f671SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s' 366b812f671SPaolo Bonzini % (TestMirrorNoBacking.image_len, target_backing_img), target_img) 367b812f671SPaolo Bonzini 368b812f671SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 369b812f671SPaolo Bonzini mode='existing', target=target_img) 370b812f671SPaolo Bonzini self.assert_qmp(result, 'return', {}) 371b812f671SPaolo Bonzini 372b812f671SPaolo Bonzini self.complete_and_wait() 373b812f671SPaolo Bonzini result = self.vm.qmp('query-block') 374b812f671SPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', target_img) 375b812f671SPaolo Bonzini self.vm.shutdown() 376866323f3SFam Zheng self.assertTrue(iotests.compare_images(test_img, target_img), 377b812f671SPaolo Bonzini 'target image does not match source after mirroring') 378b812f671SPaolo Bonzini 379866323f3SFam Zhengclass TestMirrorResized(iotests.QMPTestCase): 380a04eca10SVishvananda Ishaya backing_len = 1 * 1024 * 1024 # MB 381a04eca10SVishvananda Ishaya image_len = 2 * 1024 * 1024 # MB 382a04eca10SVishvananda Ishaya 383a04eca10SVishvananda Ishaya def setUp(self): 3842499a096SStefan Hajnoczi iotests.create_image(backing_img, TestMirrorResized.backing_len) 385a04eca10SVishvananda Ishaya qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 386a04eca10SVishvananda Ishaya qemu_img('resize', test_img, '2M') 387a04eca10SVishvananda Ishaya self.vm = iotests.VM().add_drive(test_img) 388a04eca10SVishvananda Ishaya self.vm.launch() 389a04eca10SVishvananda Ishaya 390a04eca10SVishvananda Ishaya def tearDown(self): 391a04eca10SVishvananda Ishaya self.vm.shutdown() 392a04eca10SVishvananda Ishaya os.remove(test_img) 393a04eca10SVishvananda Ishaya os.remove(backing_img) 394a04eca10SVishvananda Ishaya try: 395a04eca10SVishvananda Ishaya os.remove(target_img) 396a04eca10SVishvananda Ishaya except OSError: 397a04eca10SVishvananda Ishaya pass 398a04eca10SVishvananda Ishaya 399a04eca10SVishvananda Ishaya def test_complete_top(self): 400ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 401a04eca10SVishvananda Ishaya 402a04eca10SVishvananda Ishaya result = self.vm.qmp('drive-mirror', device='drive0', sync='top', 403a04eca10SVishvananda Ishaya target=target_img) 404a04eca10SVishvananda Ishaya self.assert_qmp(result, 'return', {}) 405a04eca10SVishvananda Ishaya 406a04eca10SVishvananda Ishaya self.complete_and_wait() 407a04eca10SVishvananda Ishaya result = self.vm.qmp('query-block') 408a04eca10SVishvananda Ishaya self.assert_qmp(result, 'return[0]/inserted/file', target_img) 409a04eca10SVishvananda Ishaya self.vm.shutdown() 4103a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 411a04eca10SVishvananda Ishaya 'target image does not match source after mirroring') 412a04eca10SVishvananda Ishaya 413a04eca10SVishvananda Ishaya def test_complete_full(self): 414ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 415a04eca10SVishvananda Ishaya 416a04eca10SVishvananda Ishaya result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 417a04eca10SVishvananda Ishaya target=target_img) 418a04eca10SVishvananda Ishaya self.assert_qmp(result, 'return', {}) 419a04eca10SVishvananda Ishaya 420a04eca10SVishvananda Ishaya self.complete_and_wait() 421a04eca10SVishvananda Ishaya result = self.vm.qmp('query-block') 422a04eca10SVishvananda Ishaya self.assert_qmp(result, 'return[0]/inserted/file', target_img) 423a04eca10SVishvananda Ishaya self.vm.shutdown() 4243a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 425a04eca10SVishvananda Ishaya 'target image does not match source after mirroring') 426a04eca10SVishvananda Ishaya 427866323f3SFam Zhengclass TestReadErrors(iotests.QMPTestCase): 4289dfa9f59SPaolo Bonzini image_len = 2 * 1024 * 1024 # MB 4299dfa9f59SPaolo Bonzini 4309dfa9f59SPaolo Bonzini # this should be a multiple of twice the default granularity 4319dfa9f59SPaolo Bonzini # so that we hit this offset first in state 1 4329dfa9f59SPaolo Bonzini MIRROR_GRANULARITY = 1024 * 1024 4339dfa9f59SPaolo Bonzini 4349dfa9f59SPaolo Bonzini def create_blkdebug_file(self, name, event, errno): 4359dfa9f59SPaolo Bonzini file = open(name, 'w') 4369dfa9f59SPaolo Bonzini file.write(''' 4379dfa9f59SPaolo Bonzini[inject-error] 4389dfa9f59SPaolo Bonzinistate = "1" 4399dfa9f59SPaolo Bonzinievent = "%s" 4409dfa9f59SPaolo Bonzinierrno = "%d" 4419dfa9f59SPaolo Bonziniimmediately = "off" 4429dfa9f59SPaolo Bonzinionce = "on" 4439dfa9f59SPaolo Bonzinisector = "%d" 4449dfa9f59SPaolo Bonzini 4459dfa9f59SPaolo Bonzini[set-state] 4469dfa9f59SPaolo Bonzinistate = "1" 4479dfa9f59SPaolo Bonzinievent = "%s" 4489dfa9f59SPaolo Bonzininew_state = "2" 4499dfa9f59SPaolo Bonzini 4509dfa9f59SPaolo Bonzini[set-state] 4519dfa9f59SPaolo Bonzinistate = "2" 4529dfa9f59SPaolo Bonzinievent = "%s" 4539dfa9f59SPaolo Bonzininew_state = "1" 4549a3a9a63SMax Reitz''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event)) 4559dfa9f59SPaolo Bonzini file.close() 4569dfa9f59SPaolo Bonzini 4579dfa9f59SPaolo Bonzini def setUp(self): 4589dfa9f59SPaolo Bonzini self.blkdebug_file = backing_img + ".blkdebug" 4592499a096SStefan Hajnoczi iotests.create_image(backing_img, TestReadErrors.image_len) 4609dfa9f59SPaolo Bonzini self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5) 4619dfa9f59SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, 4629dfa9f59SPaolo Bonzini '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw' 4639dfa9f59SPaolo Bonzini % (self.blkdebug_file, backing_img), 4649dfa9f59SPaolo Bonzini test_img) 465b812f671SPaolo Bonzini # Write something for tests that use sync='top' 466b812f671SPaolo Bonzini qemu_io('-c', 'write %d 512' % (self.MIRROR_GRANULARITY + 65536), 467b812f671SPaolo Bonzini test_img) 4689dfa9f59SPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 4699dfa9f59SPaolo Bonzini self.vm.launch() 4709dfa9f59SPaolo Bonzini 4719dfa9f59SPaolo Bonzini def tearDown(self): 4729dfa9f59SPaolo Bonzini self.vm.shutdown() 4739dfa9f59SPaolo Bonzini os.remove(test_img) 474db11d1eeSKevin Wolf os.remove(target_img) 4759dfa9f59SPaolo Bonzini os.remove(backing_img) 4769dfa9f59SPaolo Bonzini os.remove(self.blkdebug_file) 4779dfa9f59SPaolo Bonzini 4789dfa9f59SPaolo Bonzini def test_report_read(self): 479ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 4809dfa9f59SPaolo Bonzini 4819dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 4829dfa9f59SPaolo Bonzini target=target_img) 4839dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 4849dfa9f59SPaolo Bonzini 4859dfa9f59SPaolo Bonzini completed = False 4869dfa9f59SPaolo Bonzini error = False 4879dfa9f59SPaolo Bonzini while not completed: 4889dfa9f59SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 4899dfa9f59SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 4909dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 4919dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 4929dfa9f59SPaolo Bonzini error = True 4939dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_READY': 4949dfa9f59SPaolo Bonzini self.assertTrue(False, 'job completed unexpectedly') 4959dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_COMPLETED': 4969dfa9f59SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 4979dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/type', 'mirror') 4989dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 4999dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/error', 'Input/output error') 5009dfa9f59SPaolo Bonzini completed = True 5011dac83f1SKevin Wolf elif event['event'] == 'JOB_STATUS_CHANGE': 5021dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 5039dfa9f59SPaolo Bonzini 504ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 5059dfa9f59SPaolo Bonzini 5069dfa9f59SPaolo Bonzini def test_ignore_read(self): 507ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 5089dfa9f59SPaolo Bonzini 5099dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 5109dfa9f59SPaolo Bonzini target=target_img, on_source_error='ignore') 5119dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 5129dfa9f59SPaolo Bonzini 5139dfa9f59SPaolo Bonzini event = self.vm.get_qmp_event(wait=True) 5141dac83f1SKevin Wolf while event['event'] == 'JOB_STATUS_CHANGE': 5151dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 5161dac83f1SKevin Wolf event = self.vm.get_qmp_event(wait=True) 5171dac83f1SKevin Wolf 518fa1cfb40SKevin Wolf self.assertEqual(event['event'], 'BLOCK_JOB_ERROR') 5199dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 5209dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 5219dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 5229dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 5239dfa9f59SPaolo Bonzini self.complete_and_wait() 5249dfa9f59SPaolo Bonzini 525b812f671SPaolo Bonzini def test_large_cluster(self): 526ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 527b812f671SPaolo Bonzini 528b812f671SPaolo Bonzini # Test COW into the target image. The first half of the 529b812f671SPaolo Bonzini # cluster at MIRROR_GRANULARITY has to be copied from 530b812f671SPaolo Bonzini # backing_img, even though sync='top'. 531b812f671SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-ocluster_size=131072,backing_file=%s' %(backing_img), target_img) 532b812f671SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='top', 533b812f671SPaolo Bonzini on_source_error='ignore', 534b812f671SPaolo Bonzini mode='existing', target=target_img) 535b812f671SPaolo Bonzini self.assert_qmp(result, 'return', {}) 536b812f671SPaolo Bonzini 537b812f671SPaolo Bonzini event = self.vm.get_qmp_event(wait=True) 5381dac83f1SKevin Wolf while event['event'] == 'JOB_STATUS_CHANGE': 5391dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 5401dac83f1SKevin Wolf event = self.vm.get_qmp_event(wait=True) 5411dac83f1SKevin Wolf 542fa1cfb40SKevin Wolf self.assertEqual(event['event'], 'BLOCK_JOB_ERROR') 543b812f671SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 544b812f671SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 545b812f671SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 546b812f671SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 547b812f671SPaolo Bonzini self.complete_and_wait() 548b812f671SPaolo Bonzini self.vm.shutdown() 549b812f671SPaolo Bonzini 550b812f671SPaolo Bonzini # Detach blkdebug to compare images successfully 551b812f671SPaolo Bonzini qemu_img('rebase', '-f', iotests.imgfmt, '-u', '-b', backing_img, test_img) 5523a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 553b812f671SPaolo Bonzini 'target image does not match source after mirroring') 554b812f671SPaolo Bonzini 5559dfa9f59SPaolo Bonzini def test_stop_read(self): 556ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 5579dfa9f59SPaolo Bonzini 5589dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 5599dfa9f59SPaolo Bonzini target=target_img, on_source_error='stop') 5609dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 5619dfa9f59SPaolo Bonzini 5629dfa9f59SPaolo Bonzini error = False 5639dfa9f59SPaolo Bonzini ready = False 5649dfa9f59SPaolo Bonzini while not ready: 5659dfa9f59SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 5669dfa9f59SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 5679dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 5689dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 5699dfa9f59SPaolo Bonzini 5709dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 5719dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', True) 5729dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'failed') 5739dfa9f59SPaolo Bonzini 5749dfa9f59SPaolo Bonzini result = self.vm.qmp('block-job-resume', device='drive0') 5759dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 5769dfa9f59SPaolo Bonzini error = True 5779dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_READY': 5789dfa9f59SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 5799dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 5809dfa9f59SPaolo Bonzini ready = True 5819dfa9f59SPaolo Bonzini 5829dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 5839dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 5849dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'ok') 5859dfa9f59SPaolo Bonzini 5869dfa9f59SPaolo Bonzini self.complete_and_wait(wait_ready=False) 587ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 5889dfa9f59SPaolo Bonzini 589866323f3SFam Zhengclass TestWriteErrors(iotests.QMPTestCase): 5909dfa9f59SPaolo Bonzini image_len = 2 * 1024 * 1024 # MB 5919dfa9f59SPaolo Bonzini 5929dfa9f59SPaolo Bonzini # this should be a multiple of twice the default granularity 5939dfa9f59SPaolo Bonzini # so that we hit this offset first in state 1 5949dfa9f59SPaolo Bonzini MIRROR_GRANULARITY = 1024 * 1024 5959dfa9f59SPaolo Bonzini 5969dfa9f59SPaolo Bonzini def create_blkdebug_file(self, name, event, errno): 5979dfa9f59SPaolo Bonzini file = open(name, 'w') 5989dfa9f59SPaolo Bonzini file.write(''' 5999dfa9f59SPaolo Bonzini[inject-error] 6009dfa9f59SPaolo Bonzinistate = "1" 6019dfa9f59SPaolo Bonzinievent = "%s" 6029dfa9f59SPaolo Bonzinierrno = "%d" 6039dfa9f59SPaolo Bonziniimmediately = "off" 6049dfa9f59SPaolo Bonzinionce = "on" 6059dfa9f59SPaolo Bonzinisector = "%d" 6069dfa9f59SPaolo Bonzini 6079dfa9f59SPaolo Bonzini[set-state] 6089dfa9f59SPaolo Bonzinistate = "1" 6099dfa9f59SPaolo Bonzinievent = "%s" 6109dfa9f59SPaolo Bonzininew_state = "2" 6119dfa9f59SPaolo Bonzini 6129dfa9f59SPaolo Bonzini[set-state] 6139dfa9f59SPaolo Bonzinistate = "2" 6149dfa9f59SPaolo Bonzinievent = "%s" 6159dfa9f59SPaolo Bonzininew_state = "1" 6169a3a9a63SMax Reitz''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event)) 6179dfa9f59SPaolo Bonzini file.close() 6189dfa9f59SPaolo Bonzini 6199dfa9f59SPaolo Bonzini def setUp(self): 6209dfa9f59SPaolo Bonzini self.blkdebug_file = target_img + ".blkdebug" 6212499a096SStefan Hajnoczi iotests.create_image(backing_img, TestWriteErrors.image_len) 6229dfa9f59SPaolo Bonzini self.create_blkdebug_file(self.blkdebug_file, "write_aio", 5) 6239dfa9f59SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-obacking_file=%s' %(backing_img), test_img) 6249dfa9f59SPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 6259dfa9f59SPaolo Bonzini self.target_img = 'blkdebug:%s:%s' % (self.blkdebug_file, target_img) 6269dfa9f59SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-osize=%d' %(TestWriteErrors.image_len), target_img) 6279dfa9f59SPaolo Bonzini self.vm.launch() 6289dfa9f59SPaolo Bonzini 6299dfa9f59SPaolo Bonzini def tearDown(self): 6309dfa9f59SPaolo Bonzini self.vm.shutdown() 6319dfa9f59SPaolo Bonzini os.remove(test_img) 632db11d1eeSKevin Wolf os.remove(target_img) 6339dfa9f59SPaolo Bonzini os.remove(backing_img) 6349dfa9f59SPaolo Bonzini os.remove(self.blkdebug_file) 6359dfa9f59SPaolo Bonzini 6369dfa9f59SPaolo Bonzini def test_report_write(self): 637ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 6389dfa9f59SPaolo Bonzini 6399dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 6409dfa9f59SPaolo Bonzini mode='existing', target=self.target_img) 6419dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 6429dfa9f59SPaolo Bonzini 6439dfa9f59SPaolo Bonzini completed = False 6449dfa9f59SPaolo Bonzini error = False 6459dfa9f59SPaolo Bonzini while not completed: 6469dfa9f59SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 6479dfa9f59SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 6489dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 6499dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'write') 6509dfa9f59SPaolo Bonzini error = True 6519dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_READY': 6529dfa9f59SPaolo Bonzini self.assertTrue(False, 'job completed unexpectedly') 6539dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_COMPLETED': 6549dfa9f59SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 6559dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/type', 'mirror') 6569dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 6579dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/error', 'Input/output error') 6589dfa9f59SPaolo Bonzini completed = True 6599dfa9f59SPaolo Bonzini 660ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 6619dfa9f59SPaolo Bonzini 6629dfa9f59SPaolo Bonzini def test_ignore_write(self): 663ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 6649dfa9f59SPaolo Bonzini 6659dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 6669dfa9f59SPaolo Bonzini mode='existing', target=self.target_img, 6679dfa9f59SPaolo Bonzini on_target_error='ignore') 6689dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 6699dfa9f59SPaolo Bonzini 6701dac83f1SKevin Wolf event = self.vm.event_wait(name='BLOCK_JOB_ERROR') 671fa1cfb40SKevin Wolf self.assertEqual(event['event'], 'BLOCK_JOB_ERROR') 6729dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 6739dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'write') 6749dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 6759dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 6769dfa9f59SPaolo Bonzini self.complete_and_wait() 6779dfa9f59SPaolo Bonzini 6789dfa9f59SPaolo Bonzini def test_stop_write(self): 679ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 6809dfa9f59SPaolo Bonzini 6819dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 6829dfa9f59SPaolo Bonzini mode='existing', target=self.target_img, 6839dfa9f59SPaolo Bonzini on_target_error='stop') 6849dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 6859dfa9f59SPaolo Bonzini 6869dfa9f59SPaolo Bonzini error = False 6879dfa9f59SPaolo Bonzini ready = False 6889dfa9f59SPaolo Bonzini while not ready: 6899dfa9f59SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 6909dfa9f59SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 6919dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 6929dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'write') 6939dfa9f59SPaolo Bonzini 6949dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 6959dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', True) 6969dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'failed') 6979dfa9f59SPaolo Bonzini 6989dfa9f59SPaolo Bonzini result = self.vm.qmp('block-job-resume', device='drive0') 6999dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 7009dfa9f59SPaolo Bonzini 7019dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 7029dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 7039dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'ok') 7049dfa9f59SPaolo Bonzini error = True 7059dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_READY': 7069dfa9f59SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 7079dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 7089dfa9f59SPaolo Bonzini ready = True 7099dfa9f59SPaolo Bonzini 7109dfa9f59SPaolo Bonzini self.complete_and_wait(wait_ready=False) 711ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 7129dfa9f59SPaolo Bonzini 713866323f3SFam Zhengclass TestSetSpeed(iotests.QMPTestCase): 71444c7ca5eSPaolo Bonzini image_len = 80 * 1024 * 1024 # MB 71544c7ca5eSPaolo Bonzini 71644c7ca5eSPaolo Bonzini def setUp(self): 71744c7ca5eSPaolo Bonzini qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 71844c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 71944c7ca5eSPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 72044c7ca5eSPaolo Bonzini self.vm.launch() 72144c7ca5eSPaolo Bonzini 72244c7ca5eSPaolo Bonzini def tearDown(self): 72344c7ca5eSPaolo Bonzini self.vm.shutdown() 72444c7ca5eSPaolo Bonzini os.remove(test_img) 72544c7ca5eSPaolo Bonzini os.remove(backing_img) 72644c7ca5eSPaolo Bonzini os.remove(target_img) 72744c7ca5eSPaolo Bonzini 72844c7ca5eSPaolo Bonzini def test_set_speed(self): 729ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 73044c7ca5eSPaolo Bonzini 73144c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 73244c7ca5eSPaolo Bonzini target=target_img) 73344c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 73444c7ca5eSPaolo Bonzini 73544c7ca5eSPaolo Bonzini # Default speed is 0 73644c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block-jobs') 73744c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/device', 'drive0') 73844c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/speed', 0) 73944c7ca5eSPaolo Bonzini 74044c7ca5eSPaolo Bonzini result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024) 74144c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 74244c7ca5eSPaolo Bonzini 74344c7ca5eSPaolo Bonzini # Ensure the speed we set was accepted 74444c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block-jobs') 74544c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/device', 'drive0') 74644c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024) 74744c7ca5eSPaolo Bonzini 7482575fe16SStefan Hajnoczi self.wait_ready_and_cancel() 74944c7ca5eSPaolo Bonzini 75044c7ca5eSPaolo Bonzini # Check setting speed in drive-mirror works 75144c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 75244c7ca5eSPaolo Bonzini target=target_img, speed=4*1024*1024) 75344c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 75444c7ca5eSPaolo Bonzini 75544c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block-jobs') 75644c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/device', 'drive0') 75744c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024) 75844c7ca5eSPaolo Bonzini 7592575fe16SStefan Hajnoczi self.wait_ready_and_cancel() 76044c7ca5eSPaolo Bonzini 76144c7ca5eSPaolo Bonzini def test_set_speed_invalid(self): 762ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 76344c7ca5eSPaolo Bonzini 76444c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 76544c7ca5eSPaolo Bonzini target=target_img, speed=-1) 76644c7ca5eSPaolo Bonzini self.assert_qmp(result, 'error/class', 'GenericError') 76744c7ca5eSPaolo Bonzini 768ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 76944c7ca5eSPaolo Bonzini 77044c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 77144c7ca5eSPaolo Bonzini target=target_img) 77244c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 77344c7ca5eSPaolo Bonzini 77444c7ca5eSPaolo Bonzini result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1) 77544c7ca5eSPaolo Bonzini self.assert_qmp(result, 'error/class', 'GenericError') 77644c7ca5eSPaolo Bonzini 7772575fe16SStefan Hajnoczi self.wait_ready_and_cancel() 77844c7ca5eSPaolo Bonzini 779866323f3SFam Zhengclass TestUnbackedSource(iotests.QMPTestCase): 780c15badeeSMax Reitz image_len = 2 * 1024 * 1024 # MB 781c15badeeSMax Reitz 782c15badeeSMax Reitz def setUp(self): 783c15badeeSMax Reitz qemu_img('create', '-f', iotests.imgfmt, test_img, 784c15badeeSMax Reitz str(TestUnbackedSource.image_len)) 7859463ee1fSMax Reitz self.vm = iotests.VM() 786c15badeeSMax Reitz self.vm.launch() 7879463ee1fSMax Reitz result = self.vm.qmp('blockdev-add', node_name='drive0', 7889463ee1fSMax Reitz driver=iotests.imgfmt, 7899463ee1fSMax Reitz file={ 7909463ee1fSMax Reitz 'driver': 'file', 7919463ee1fSMax Reitz 'filename': test_img, 7929463ee1fSMax Reitz }) 7939463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 794c15badeeSMax Reitz 795c15badeeSMax Reitz def tearDown(self): 796c15badeeSMax Reitz self.vm.shutdown() 797c15badeeSMax Reitz os.remove(test_img) 798c15badeeSMax Reitz os.remove(target_img) 799c15badeeSMax Reitz 800171d6431SMax Reitz def test_absolute_paths_full(self): 801171d6431SMax Reitz self.assert_no_active_block_jobs() 8029463ee1fSMax Reitz result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0', 803171d6431SMax Reitz sync='full', target=target_img, 804171d6431SMax Reitz mode='absolute-paths') 805171d6431SMax Reitz self.assert_qmp(result, 'return', {}) 806171d6431SMax Reitz self.complete_and_wait() 807c15badeeSMax Reitz self.assert_no_active_block_jobs() 808c15badeeSMax Reitz 809171d6431SMax Reitz def test_absolute_paths_top(self): 810171d6431SMax Reitz self.assert_no_active_block_jobs() 8119463ee1fSMax Reitz result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0', 812171d6431SMax Reitz sync='top', target=target_img, 813171d6431SMax Reitz mode='absolute-paths') 814171d6431SMax Reitz self.assert_qmp(result, 'return', {}) 815171d6431SMax Reitz self.complete_and_wait() 816171d6431SMax Reitz self.assert_no_active_block_jobs() 817171d6431SMax Reitz 818171d6431SMax Reitz def test_absolute_paths_none(self): 819171d6431SMax Reitz self.assert_no_active_block_jobs() 8209463ee1fSMax Reitz result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0', 821171d6431SMax Reitz sync='none', target=target_img, 822c15badeeSMax Reitz mode='absolute-paths') 823c15badeeSMax Reitz self.assert_qmp(result, 'return', {}) 824c15badeeSMax Reitz self.complete_and_wait() 825c15badeeSMax Reitz self.assert_no_active_block_jobs() 826c15badeeSMax Reitz 8279463ee1fSMax Reitz def test_existing_full(self): 8289463ee1fSMax Reitz qemu_img('create', '-f', iotests.imgfmt, target_img, 8299463ee1fSMax Reitz str(self.image_len)) 8309463ee1fSMax Reitz qemu_io('-c', 'write -P 42 0 64k', target_img) 8319463ee1fSMax Reitz 8329463ee1fSMax Reitz self.assert_no_active_block_jobs() 8339463ee1fSMax Reitz result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0', 8349463ee1fSMax Reitz sync='full', target=target_img, mode='existing') 8359463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8369463ee1fSMax Reitz self.complete_and_wait() 8379463ee1fSMax Reitz self.assert_no_active_block_jobs() 8389463ee1fSMax Reitz 8399463ee1fSMax Reitz result = self.vm.qmp('blockdev-del', node_name='drive0') 8409463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8419463ee1fSMax Reitz 8429463ee1fSMax Reitz self.assertTrue(iotests.compare_images(test_img, target_img), 8439463ee1fSMax Reitz 'target image does not match source after mirroring') 8449463ee1fSMax Reitz 8459463ee1fSMax Reitz def test_blockdev_full(self): 8469463ee1fSMax Reitz qemu_img('create', '-f', iotests.imgfmt, target_img, 8479463ee1fSMax Reitz str(self.image_len)) 8489463ee1fSMax Reitz qemu_io('-c', 'write -P 42 0 64k', target_img) 8499463ee1fSMax Reitz 8509463ee1fSMax Reitz result = self.vm.qmp('blockdev-add', node_name='target', 8519463ee1fSMax Reitz driver=iotests.imgfmt, 8529463ee1fSMax Reitz file={ 8539463ee1fSMax Reitz 'driver': 'file', 8549463ee1fSMax Reitz 'filename': target_img, 8559463ee1fSMax Reitz }) 8569463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8579463ee1fSMax Reitz 8589463ee1fSMax Reitz self.assert_no_active_block_jobs() 8599463ee1fSMax Reitz result = self.vm.qmp('blockdev-mirror', job_id='drive0', device='drive0', 8609463ee1fSMax Reitz sync='full', target='target') 8619463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8629463ee1fSMax Reitz self.complete_and_wait() 8639463ee1fSMax Reitz self.assert_no_active_block_jobs() 8649463ee1fSMax Reitz 8659463ee1fSMax Reitz result = self.vm.qmp('blockdev-del', node_name='drive0') 8669463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8679463ee1fSMax Reitz 8689463ee1fSMax Reitz result = self.vm.qmp('blockdev-del', node_name='target') 8699463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8709463ee1fSMax Reitz 8719463ee1fSMax Reitz self.assertTrue(iotests.compare_images(test_img, target_img), 8729463ee1fSMax Reitz 'target image does not match source after mirroring') 8739463ee1fSMax Reitz 874ccee3d8fSJohn Snowclass TestGranularity(iotests.QMPTestCase): 875ccee3d8fSJohn Snow image_len = 10 * 1024 * 1024 # MB 876ccee3d8fSJohn Snow 877ccee3d8fSJohn Snow def setUp(self): 878ccee3d8fSJohn Snow qemu_img('create', '-f', iotests.imgfmt, test_img, 879ccee3d8fSJohn Snow str(TestGranularity.image_len)) 880ccee3d8fSJohn Snow qemu_io('-c', 'write 0 %d' % (self.image_len), 881ccee3d8fSJohn Snow test_img) 882ccee3d8fSJohn Snow self.vm = iotests.VM().add_drive(test_img) 883ccee3d8fSJohn Snow self.vm.launch() 884ccee3d8fSJohn Snow 885ccee3d8fSJohn Snow def tearDown(self): 886ccee3d8fSJohn Snow self.vm.shutdown() 887ccee3d8fSJohn Snow self.assertTrue(iotests.compare_images(test_img, target_img), 888ccee3d8fSJohn Snow 'target image does not match source after mirroring') 889ccee3d8fSJohn Snow os.remove(test_img) 890ccee3d8fSJohn Snow os.remove(target_img) 891ccee3d8fSJohn Snow 892ccee3d8fSJohn Snow def test_granularity(self): 893ccee3d8fSJohn Snow self.assert_no_active_block_jobs() 894ccee3d8fSJohn Snow result = self.vm.qmp('drive-mirror', device='drive0', 895ccee3d8fSJohn Snow sync='full', target=target_img, 896ccee3d8fSJohn Snow mode='absolute-paths', granularity=8192) 897ccee3d8fSJohn Snow self.assert_qmp(result, 'return', {}) 8981dac83f1SKevin Wolf 899ccee3d8fSJohn Snow event = self.vm.get_qmp_event(wait=60.0) 9001dac83f1SKevin Wolf while event['event'] == 'JOB_STATUS_CHANGE': 9011dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 9021dac83f1SKevin Wolf event = self.vm.get_qmp_event(wait=60.0) 9031dac83f1SKevin Wolf 904ccee3d8fSJohn Snow # Failures will manifest as COMPLETED/ERROR. 905ccee3d8fSJohn Snow self.assert_qmp(event, 'event', 'BLOCK_JOB_READY') 906ccee3d8fSJohn Snow self.complete_and_wait(drive='drive0', wait_ready=False) 907ccee3d8fSJohn Snow self.assert_no_active_block_jobs() 908ccee3d8fSJohn Snow 909866323f3SFam Zhengclass TestRepairQuorum(iotests.QMPTestCase): 910d88964aeSBenoît Canet """ This class test quorum file repair using drive-mirror. 911d88964aeSBenoît Canet It's mostly a fork of TestSingleDrive """ 912d88964aeSBenoît Canet image_len = 1 * 1024 * 1024 # MB 913d88964aeSBenoît Canet IMAGES = [ quorum_img1, quorum_img2, quorum_img3 ] 914d88964aeSBenoît Canet 9159442bebeSThomas Huth @iotests.skip_if_unsupported(['quorum']) 916d88964aeSBenoît Canet def setUp(self): 917d88964aeSBenoît Canet self.vm = iotests.VM() 918d88964aeSBenoît Canet 9190ed82f7aSMax Reitz if iotests.qemu_default_machine == 'pc': 9200ed82f7aSMax Reitz self.vm.add_drive(None, 'media=cdrom', 'ide') 9210ed82f7aSMax Reitz 922d88964aeSBenoît Canet # Add each individual quorum images 923d88964aeSBenoît Canet for i in self.IMAGES: 924d88964aeSBenoît Canet qemu_img('create', '-f', iotests.imgfmt, i, 92589e21945SMax Reitz str(self.image_len)) 926d88964aeSBenoît Canet # Assign a node name to each quorum image in order to manipulate 927d88964aeSBenoît Canet # them 928d88964aeSBenoît Canet opts = "node-name=img%i" % self.IMAGES.index(i) 929f718ca14SMax Reitz opts += ',driver=%s' % iotests.imgfmt 930f718ca14SMax Reitz opts += ',file.driver=file' 931f718ca14SMax Reitz opts += ',file.filename=%s' % i 932f718ca14SMax Reitz self.vm = self.vm.add_blockdev(opts) 933d88964aeSBenoît Canet 934d88964aeSBenoît Canet self.vm.launch() 935d88964aeSBenoît Canet 936d88964aeSBenoît Canet #assemble the quorum block device from the individual files 9370153d2f5SKevin Wolf args = { "driver": "quorum", "node-name": "quorum0", 9380153d2f5SKevin Wolf "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] } 939d88964aeSBenoît Canet result = self.vm.qmp("blockdev-add", **args) 940d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 941d88964aeSBenoît Canet 942d88964aeSBenoît Canet 943d88964aeSBenoît Canet def tearDown(self): 944d88964aeSBenoît Canet self.vm.shutdown() 945a1da1878SMax Reitz for i in self.IMAGES + [ quorum_repair_img, quorum_snapshot_file, 946a1da1878SMax Reitz nbd_sock_path ]: 947d88964aeSBenoît Canet # Do a try/except because the test may have deleted some images 948d88964aeSBenoît Canet try: 949d88964aeSBenoît Canet os.remove(i) 950d88964aeSBenoît Canet except OSError: 951d88964aeSBenoît Canet pass 952d88964aeSBenoît Canet 953d88964aeSBenoît Canet def test_complete(self): 954476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 955476fb028SKevin Wolf sync='full', node_name="repair0", replaces="img1", 956d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 957d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 958d88964aeSBenoît Canet 959476fb028SKevin Wolf self.complete_and_wait(drive="job0") 960e71fc0baSFam Zheng self.assert_has_block_node("repair0", quorum_repair_img) 961c351afd6SMax Reitz self.vm.assert_block_path('quorum0', '/children.1', 'repair0') 962d88964aeSBenoît Canet self.vm.shutdown() 963d88964aeSBenoît Canet self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img), 964d88964aeSBenoît Canet 'target image does not match source after mirroring') 965d88964aeSBenoît Canet 966d88964aeSBenoît Canet def test_cancel(self): 967476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 968476fb028SKevin Wolf sync='full', node_name="repair0", replaces="img1", 969d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 970d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 971d88964aeSBenoît Canet 972476fb028SKevin Wolf self.cancel_and_wait(drive="job0", force=True) 973d88964aeSBenoît Canet # here we check that the last registered quorum file has not been 974d88964aeSBenoît Canet # swapped out and unref 975e71fc0baSFam Zheng self.assert_has_block_node(None, quorum_img3) 976d88964aeSBenoît Canet 977d88964aeSBenoît Canet def test_cancel_after_ready(self): 978476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 979476fb028SKevin Wolf sync='full', node_name="repair0", replaces="img1", 980d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 981d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 982d88964aeSBenoît Canet 983476fb028SKevin Wolf self.wait_ready_and_cancel(drive="job0") 984d88964aeSBenoît Canet # here we check that the last registered quorum file has not been 985d88964aeSBenoît Canet # swapped out and unref 986e71fc0baSFam Zheng self.assert_has_block_node(None, quorum_img3) 987d88964aeSBenoît Canet self.vm.shutdown() 988d88964aeSBenoît Canet self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img), 989d88964aeSBenoît Canet 'target image does not match source after mirroring') 990d88964aeSBenoît Canet 991d88964aeSBenoît Canet def test_pause(self): 992476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 993476fb028SKevin Wolf sync='full', node_name="repair0", replaces="img1", 994d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 995d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 996d88964aeSBenoît Canet 9972c93c5cbSKevin Wolf self.pause_job('job0') 998d88964aeSBenoît Canet 999d88964aeSBenoît Canet result = self.vm.qmp('query-block-jobs') 1000d88964aeSBenoît Canet offset = self.dictpath(result, 'return[0]/offset') 1001d88964aeSBenoît Canet 10022c93c5cbSKevin Wolf time.sleep(0.5) 1003d88964aeSBenoît Canet result = self.vm.qmp('query-block-jobs') 1004d88964aeSBenoît Canet self.assert_qmp(result, 'return[0]/offset', offset) 1005d88964aeSBenoît Canet 1006476fb028SKevin Wolf result = self.vm.qmp('block-job-resume', device='job0') 1007d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 1008d88964aeSBenoît Canet 1009476fb028SKevin Wolf self.complete_and_wait(drive="job0") 1010d88964aeSBenoît Canet self.vm.shutdown() 1011d88964aeSBenoît Canet self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img), 1012d88964aeSBenoît Canet 'target image does not match source after mirroring') 1013d88964aeSBenoît Canet 1014d88964aeSBenoît Canet def test_medium_not_found(self): 1015d8683155SBo Tu if iotests.qemu_default_machine != 'pc': 1016d8683155SBo Tu return 1017d8683155SBo Tu 1018476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='drive0', # CD-ROM 10190ed82f7aSMax Reitz sync='full', 1020d88964aeSBenoît Canet node_name='repair0', 1021d88964aeSBenoît Canet replaces='img1', 1022d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1023d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1024d88964aeSBenoît Canet 1025d88964aeSBenoît Canet def test_image_not_found(self): 1026476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1027476fb028SKevin Wolf sync='full', node_name='repair0', replaces='img1', 1028476fb028SKevin Wolf mode='existing', target=quorum_repair_img, 1029476fb028SKevin Wolf format=iotests.imgfmt) 1030d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1031d88964aeSBenoît Canet 1032d88964aeSBenoît Canet def test_device_not_found(self): 1033476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', 1034476fb028SKevin Wolf device='nonexistent', sync='full', 1035d88964aeSBenoît Canet node_name='repair0', 1036d88964aeSBenoît Canet replaces='img1', 1037d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 10380524e93aSKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 1039d88964aeSBenoît Canet 1040d88964aeSBenoît Canet def test_wrong_sync_mode(self): 1041476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', device='quorum0', job_id='job0', 1042d88964aeSBenoît Canet node_name='repair0', 1043d88964aeSBenoît Canet replaces='img1', 1044d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1045d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1046d88964aeSBenoît Canet 1047d88964aeSBenoît Canet def test_no_node_name(self): 1048476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1049476fb028SKevin Wolf sync='full', replaces='img1', 1050d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1051d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1052d88964aeSBenoît Canet 105367cc32ebSVeres Lajos def test_nonexistent_replaces(self): 1054476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1055476fb028SKevin Wolf sync='full', node_name='repair0', replaces='img77', 1056d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1057d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1058d88964aeSBenoît Canet 1059d88964aeSBenoît Canet def test_after_a_quorum_snapshot(self): 1060d88964aeSBenoît Canet result = self.vm.qmp('blockdev-snapshot-sync', node_name='img1', 1061d88964aeSBenoît Canet snapshot_file=quorum_snapshot_file, 1062d88964aeSBenoît Canet snapshot_node_name="snap1"); 1063d88964aeSBenoît Canet 1064476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1065476fb028SKevin Wolf sync='full', node_name='repair0', replaces="img1", 1066d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1067d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1068d88964aeSBenoît Canet 1069476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1070476fb028SKevin Wolf sync='full', node_name='repair0', replaces="snap1", 1071d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1072d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 1073d88964aeSBenoît Canet 1074476fb028SKevin Wolf self.complete_and_wait('job0') 1075e71fc0baSFam Zheng self.assert_has_block_node("repair0", quorum_repair_img) 1076c351afd6SMax Reitz self.vm.assert_block_path('quorum0', '/children.1', 'repair0') 1077d88964aeSBenoît Canet 1078a1da1878SMax Reitz def test_with_other_parent(self): 1079a1da1878SMax Reitz """ 1080a1da1878SMax Reitz Check that we cannot replace a Quorum child when it has other 1081a1da1878SMax Reitz parents. 1082a1da1878SMax Reitz """ 1083a1da1878SMax Reitz result = self.vm.qmp('nbd-server-start', 1084a1da1878SMax Reitz addr={ 1085a1da1878SMax Reitz 'type': 'unix', 1086a1da1878SMax Reitz 'data': {'path': nbd_sock_path} 1087a1da1878SMax Reitz }) 1088a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1089a1da1878SMax Reitz 1090a1da1878SMax Reitz result = self.vm.qmp('nbd-server-add', device='img1') 1091a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1092a1da1878SMax Reitz 1093a1da1878SMax Reitz result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0', 1094a1da1878SMax Reitz sync='full', node_name='repair0', replaces='img1', 1095a1da1878SMax Reitz target=quorum_repair_img, format=iotests.imgfmt) 1096a1da1878SMax Reitz self.assert_qmp(result, 'error/desc', 1097a1da1878SMax Reitz "Cannot replace 'img1' by a node mirrored from " 1098a1da1878SMax Reitz "'quorum0', because it cannot be guaranteed that doing " 1099a1da1878SMax Reitz "so would not lead to an abrupt change of visible data") 1100a1da1878SMax Reitz 1101a1da1878SMax Reitz def test_with_other_parents_after_mirror_start(self): 1102a1da1878SMax Reitz """ 1103a1da1878SMax Reitz The same as test_with_other_parent(), but add the NBD server 1104a1da1878SMax Reitz only when the mirror job is already running. 1105a1da1878SMax Reitz """ 1106a1da1878SMax Reitz result = self.vm.qmp('nbd-server-start', 1107a1da1878SMax Reitz addr={ 1108a1da1878SMax Reitz 'type': 'unix', 1109a1da1878SMax Reitz 'data': {'path': nbd_sock_path} 1110a1da1878SMax Reitz }) 1111a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1112a1da1878SMax Reitz 1113a1da1878SMax Reitz result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0', 1114a1da1878SMax Reitz sync='full', node_name='repair0', replaces='img1', 1115a1da1878SMax Reitz target=quorum_repair_img, format=iotests.imgfmt) 1116a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1117a1da1878SMax Reitz 1118a1da1878SMax Reitz result = self.vm.qmp('nbd-server-add', device='img1') 1119a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1120a1da1878SMax Reitz 1121a1da1878SMax Reitz # The full error message goes to stderr, we will check it later 1122a1da1878SMax Reitz self.complete_and_wait('mirror', 1123a1da1878SMax Reitz completion_error='Operation not permitted') 1124a1da1878SMax Reitz 1125a1da1878SMax Reitz # Should not have been replaced 1126a1da1878SMax Reitz self.vm.assert_block_path('quorum0', '/children.1', 'img1') 1127a1da1878SMax Reitz 1128a1da1878SMax Reitz # Check the full error message now 1129a1da1878SMax Reitz self.vm.shutdown() 1130a1da1878SMax Reitz log = self.vm.get_log() 1131a1da1878SMax Reitz log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log) 1132a1da1878SMax Reitz log = re.sub(r'^Formatting.*\n', '', log) 1133a1da1878SMax Reitz log = re.sub(r'\n\[I \+\d+\.\d+\] CLOSED\n?$', '', log) 1134a1da1878SMax Reitz log = re.sub(r'^%s: ' % os.path.basename(iotests.qemu_prog), '', log) 1135a1da1878SMax Reitz 1136a1da1878SMax Reitz self.assertEqual(log, 1137a1da1878SMax Reitz "Can no longer replace 'img1' by 'repair0', because " + 1138a1da1878SMax Reitz "it can no longer be guaranteed that doing so would " + 1139a1da1878SMax Reitz "not lead to an abrupt change of visible data") 1140a1da1878SMax Reitz 1141a1da1878SMax Reitz 11425694923aSMax Reitz# Test mirroring with a source that does not have any parents (not even a 11435694923aSMax Reitz# BlockBackend) 11445694923aSMax Reitzclass TestOrphanedSource(iotests.QMPTestCase): 11455694923aSMax Reitz def setUp(self): 11465694923aSMax Reitz blk0 = { 'node-name': 'src', 11475694923aSMax Reitz 'driver': 'null-co' } 11485694923aSMax Reitz 11495694923aSMax Reitz blk1 = { 'node-name': 'dest', 11505694923aSMax Reitz 'driver': 'null-co' } 11515694923aSMax Reitz 11525694923aSMax Reitz blk2 = { 'node-name': 'dest-ro', 11535694923aSMax Reitz 'driver': 'null-co', 11545694923aSMax Reitz 'read-only': 'on' } 11555694923aSMax Reitz 11565694923aSMax Reitz self.vm = iotests.VM() 115762a94288SKevin Wolf self.vm.add_blockdev(self.vm.qmp_to_opts(blk0)) 115862a94288SKevin Wolf self.vm.add_blockdev(self.vm.qmp_to_opts(blk1)) 115962a94288SKevin Wolf self.vm.add_blockdev(self.vm.qmp_to_opts(blk2)) 11605694923aSMax Reitz self.vm.launch() 11615694923aSMax Reitz 11625694923aSMax Reitz def tearDown(self): 11635694923aSMax Reitz self.vm.shutdown() 11645694923aSMax Reitz 11655694923aSMax Reitz def test_no_job_id(self): 11665694923aSMax Reitz self.assert_no_active_block_jobs() 11675694923aSMax Reitz 11685694923aSMax Reitz result = self.vm.qmp('blockdev-mirror', device='src', sync='full', 11695694923aSMax Reitz target='dest') 11705694923aSMax Reitz self.assert_qmp(result, 'error/class', 'GenericError') 11715694923aSMax Reitz 11725694923aSMax Reitz def test_success(self): 11735694923aSMax Reitz self.assert_no_active_block_jobs() 11745694923aSMax Reitz 11755694923aSMax Reitz result = self.vm.qmp('blockdev-mirror', job_id='job', device='src', 11765694923aSMax Reitz sync='full', target='dest') 11775694923aSMax Reitz self.assert_qmp(result, 'return', {}) 11785694923aSMax Reitz 11795694923aSMax Reitz self.complete_and_wait('job') 11805694923aSMax Reitz 11815694923aSMax Reitz def test_failing_permissions(self): 11825694923aSMax Reitz self.assert_no_active_block_jobs() 11835694923aSMax Reitz 11845694923aSMax Reitz result = self.vm.qmp('blockdev-mirror', device='src', sync='full', 11855694923aSMax Reitz target='dest-ro') 11865694923aSMax Reitz self.assert_qmp(result, 'error/class', 'GenericError') 11875694923aSMax Reitz 11889592fe45SMax Reitz def test_failing_permission_in_complete(self): 11899592fe45SMax Reitz self.assert_no_active_block_jobs() 11909592fe45SMax Reitz 11919592fe45SMax Reitz # Unshare consistent-read on the target 11929592fe45SMax Reitz # (The mirror job does not care) 11939592fe45SMax Reitz result = self.vm.qmp('blockdev-add', 11949592fe45SMax Reitz driver='blkdebug', 11959592fe45SMax Reitz node_name='dest-perm', 11969592fe45SMax Reitz image='dest', 11979592fe45SMax Reitz unshare_child_perms=['consistent-read']) 11989592fe45SMax Reitz self.assert_qmp(result, 'return', {}) 11999592fe45SMax Reitz 12009592fe45SMax Reitz result = self.vm.qmp('blockdev-mirror', job_id='job', device='src', 12019592fe45SMax Reitz sync='full', target='dest', 12029592fe45SMax Reitz filter_node_name='mirror-filter') 12039592fe45SMax Reitz self.assert_qmp(result, 'return', {}) 12049592fe45SMax Reitz 12059592fe45SMax Reitz # Require consistent-read on the source 12069592fe45SMax Reitz # (We can only add this node once the job has started, or it 12079592fe45SMax Reitz # will complain that it does not want to run on non-root nodes) 12089592fe45SMax Reitz result = self.vm.qmp('blockdev-add', 12099592fe45SMax Reitz driver='blkdebug', 12109592fe45SMax Reitz node_name='src-perm', 12119592fe45SMax Reitz image='src', 12129592fe45SMax Reitz take_child_perms=['consistent-read']) 12139592fe45SMax Reitz self.assert_qmp(result, 'return', {}) 12149592fe45SMax Reitz 12159592fe45SMax Reitz # While completing, mirror will attempt to replace src by 12169592fe45SMax Reitz # dest, which must fail because src-perm requires 12179592fe45SMax Reitz # consistent-read but dest-perm does not share it; thus 12189592fe45SMax Reitz # aborting the job when it is supposed to complete 12199592fe45SMax Reitz self.complete_and_wait('job', 12209592fe45SMax Reitz completion_error='Operation not permitted') 12219592fe45SMax Reitz 12229592fe45SMax Reitz # Assert that all of our nodes are still there (except for the 12239592fe45SMax Reitz # mirror filter, which should be gone despite the failure) 12249592fe45SMax Reitz nodes = self.vm.qmp('query-named-block-nodes')['return'] 12259592fe45SMax Reitz nodes = [node['node-name'] for node in nodes] 12269592fe45SMax Reitz 12279592fe45SMax Reitz for expect in ('src', 'src-perm', 'dest', 'dest-perm'): 12289592fe45SMax Reitz self.assertTrue(expect in nodes, '%s disappeared' % expect) 12299592fe45SMax Reitz self.assertFalse('mirror-filter' in nodes, 12309592fe45SMax Reitz 'Mirror filter node did not disappear') 12319592fe45SMax Reitz 1232c45a88f4SMax Reitz# Test cases for @replaces that do not necessarily involve Quorum 1233c45a88f4SMax Reitzclass TestReplaces(iotests.QMPTestCase): 1234c45a88f4SMax Reitz # Each of these test cases needs their own block graph, so do not 1235c45a88f4SMax Reitz # create any nodes here 1236c45a88f4SMax Reitz def setUp(self): 1237c45a88f4SMax Reitz self.vm = iotests.VM() 1238c45a88f4SMax Reitz self.vm.launch() 1239c45a88f4SMax Reitz 1240c45a88f4SMax Reitz def tearDown(self): 1241c45a88f4SMax Reitz self.vm.shutdown() 1242c45a88f4SMax Reitz for img in (test_img, target_img): 1243c45a88f4SMax Reitz try: 1244c45a88f4SMax Reitz os.remove(img) 1245c45a88f4SMax Reitz except OSError: 1246c45a88f4SMax Reitz pass 1247c45a88f4SMax Reitz 1248c45a88f4SMax Reitz @iotests.skip_if_unsupported(['copy-on-read']) 1249c45a88f4SMax Reitz def test_replace_filter(self): 1250c45a88f4SMax Reitz """ 1251c45a88f4SMax Reitz Check that we can replace filter nodes. 1252c45a88f4SMax Reitz """ 1253c45a88f4SMax Reitz result = self.vm.qmp('blockdev-add', **{ 1254c45a88f4SMax Reitz 'driver': 'copy-on-read', 1255c45a88f4SMax Reitz 'node-name': 'filter0', 1256c45a88f4SMax Reitz 'file': { 1257c45a88f4SMax Reitz 'driver': 'copy-on-read', 1258c45a88f4SMax Reitz 'node-name': 'filter1', 1259c45a88f4SMax Reitz 'file': { 1260c45a88f4SMax Reitz 'driver': 'null-co' 1261c45a88f4SMax Reitz } 1262c45a88f4SMax Reitz } 1263c45a88f4SMax Reitz }) 1264c45a88f4SMax Reitz self.assert_qmp(result, 'return', {}) 1265c45a88f4SMax Reitz 1266c45a88f4SMax Reitz result = self.vm.qmp('blockdev-add', 1267c45a88f4SMax Reitz node_name='target', driver='null-co') 1268c45a88f4SMax Reitz self.assert_qmp(result, 'return', {}) 1269c45a88f4SMax Reitz 1270c45a88f4SMax Reitz result = self.vm.qmp('blockdev-mirror', job_id='mirror', device='filter0', 1271c45a88f4SMax Reitz target='target', sync='full', replaces='filter1') 1272c45a88f4SMax Reitz self.assert_qmp(result, 'return', {}) 1273c45a88f4SMax Reitz 1274c45a88f4SMax Reitz self.complete_and_wait('mirror') 1275c45a88f4SMax Reitz 1276c45a88f4SMax Reitz self.vm.assert_block_path('filter0', '/file', 'target') 1277c45a88f4SMax Reitz 127844c7ca5eSPaolo Bonziniif __name__ == '__main__': 1279103cbc77SMax Reitz iotests.main(supported_fmts=['qcow2', 'qed'], 1280877d18f2SThomas Huth supported_protocols=['file'], 1281877d18f2SThomas Huth supported_platforms=['linux', 'freebsd', 'netbsd', 'openbsd']) 1282