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 243*16cea4eeSKevin Wolf def do_test_resize(self, device, node): 244*16cea4eeSKevin Wolf def pre_finalize(): 245*16cea4eeSKevin Wolf if device: 246*16cea4eeSKevin Wolf result = self.vm.qmp('block_resize', device=device, size=65536) 247*16cea4eeSKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 248*16cea4eeSKevin Wolf 249*16cea4eeSKevin Wolf result = self.vm.qmp('block_resize', node_name=node, size=65536) 250*16cea4eeSKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 251*16cea4eeSKevin Wolf 252*16cea4eeSKevin Wolf result = self.vm.qmp(self.qmp_cmd, job_id='job0', device='drive0', 253*16cea4eeSKevin Wolf sync='full', target=self.qmp_target, 254*16cea4eeSKevin Wolf auto_finalize=False, auto_dismiss=False) 255*16cea4eeSKevin Wolf self.assert_qmp(result, 'return', {}) 256*16cea4eeSKevin Wolf 257*16cea4eeSKevin Wolf result = self.vm.run_job('job0', auto_finalize=False, 258*16cea4eeSKevin Wolf pre_finalize=pre_finalize) 259*16cea4eeSKevin Wolf self.assertEqual(result, None) 260*16cea4eeSKevin Wolf 261*16cea4eeSKevin Wolf def test_source_resize(self): 262*16cea4eeSKevin Wolf self.do_test_resize('drive0', 'top') 263*16cea4eeSKevin Wolf 264*16cea4eeSKevin Wolf def test_target_resize(self): 265*16cea4eeSKevin Wolf self.do_test_resize(None, self.qmp_target) 266*16cea4eeSKevin Wolf 267*16cea4eeSKevin Wolf def do_test_target_size(self, size): 268*16cea4eeSKevin Wolf result = self.vm.qmp('block_resize', node_name=self.qmp_target, 269*16cea4eeSKevin Wolf size=size) 270*16cea4eeSKevin Wolf self.assert_qmp(result, 'return', {}) 271*16cea4eeSKevin Wolf 272*16cea4eeSKevin Wolf result = self.vm.qmp(self.qmp_cmd, job_id='job0', 273*16cea4eeSKevin Wolf device='drive0', sync='full', auto_dismiss=False, 274*16cea4eeSKevin Wolf target=self.qmp_target) 275*16cea4eeSKevin Wolf self.assert_qmp(result, 'return', {}) 276*16cea4eeSKevin Wolf 277*16cea4eeSKevin Wolf result = self.vm.run_job('job0') 278*16cea4eeSKevin Wolf self.assertEqual(result, 'Source and target image have different sizes') 279*16cea4eeSKevin Wolf 280*16cea4eeSKevin Wolf def test_small_target(self): 281*16cea4eeSKevin Wolf self.do_test_target_size(self.image_len // 2) 282*16cea4eeSKevin Wolf 283*16cea4eeSKevin Wolf def test_large_target(self): 284*16cea4eeSKevin Wolf self.do_test_target_size(self.image_len * 2) 285*16cea4eeSKevin Wolf 28694ca2c73SFam Zheng test_large_cluster = None 28794ca2c73SFam Zheng test_image_not_found = None 28894ca2c73SFam Zheng test_small_buffer2 = None 28994ca2c73SFam Zheng 2903b9f27d2SFam Zhengclass TestSingleDriveZeroLength(TestSingleDrive): 2913b9f27d2SFam Zheng image_len = 0 2923b9f27d2SFam Zheng test_small_buffer2 = None 2933b9f27d2SFam Zheng test_large_cluster = None 2943b9f27d2SFam Zheng 29594ca2c73SFam Zhengclass TestSingleBlockdevZeroLength(TestSingleBlockdev): 29694ca2c73SFam Zheng image_len = 0 297*16cea4eeSKevin Wolf test_small_target = None 298*16cea4eeSKevin Wolf test_large_target = None 29994ca2c73SFam Zheng 3005a0f6fd5SKevin Wolfclass TestSingleDriveUnalignedLength(TestSingleDrive): 3015a0f6fd5SKevin Wolf image_len = 1025 * 1024 3025a0f6fd5SKevin Wolf test_small_buffer2 = None 3035a0f6fd5SKevin Wolf test_large_cluster = None 3045a0f6fd5SKevin Wolf 30594ca2c73SFam Zhengclass TestSingleBlockdevUnalignedLength(TestSingleBlockdev): 30694ca2c73SFam Zheng image_len = 1025 * 1024 30794ca2c73SFam Zheng 308866323f3SFam Zhengclass TestMirrorNoBacking(iotests.QMPTestCase): 30944c7ca5eSPaolo Bonzini image_len = 2 * 1024 * 1024 # MB 31044c7ca5eSPaolo Bonzini 31144c7ca5eSPaolo Bonzini def setUp(self): 3122499a096SStefan Hajnoczi iotests.create_image(backing_img, TestMirrorNoBacking.image_len) 31344c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 31444c7ca5eSPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 31544c7ca5eSPaolo Bonzini self.vm.launch() 31644c7ca5eSPaolo Bonzini 31744c7ca5eSPaolo Bonzini def tearDown(self): 31844c7ca5eSPaolo Bonzini self.vm.shutdown() 31944c7ca5eSPaolo Bonzini os.remove(test_img) 32044c7ca5eSPaolo Bonzini os.remove(backing_img) 321866323f3SFam Zheng try: 32244c7ca5eSPaolo Bonzini os.remove(target_backing_img) 323866323f3SFam Zheng except: 324866323f3SFam Zheng pass 32544c7ca5eSPaolo Bonzini os.remove(target_img) 32644c7ca5eSPaolo Bonzini 32744c7ca5eSPaolo Bonzini def test_complete(self): 328ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 32944c7ca5eSPaolo Bonzini 33044c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img) 33144c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 33244c7ca5eSPaolo Bonzini mode='existing', target=target_img) 33344c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 33444c7ca5eSPaolo Bonzini 33544c7ca5eSPaolo Bonzini self.complete_and_wait() 33644c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block') 33744c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', target_img) 33844c7ca5eSPaolo Bonzini self.vm.shutdown() 339866323f3SFam Zheng self.assertTrue(iotests.compare_images(test_img, target_img), 34044c7ca5eSPaolo Bonzini 'target image does not match source after mirroring') 34144c7ca5eSPaolo Bonzini 34244c7ca5eSPaolo Bonzini def test_cancel(self): 343ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 34444c7ca5eSPaolo Bonzini 34544c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img) 34644c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 34744c7ca5eSPaolo Bonzini mode='existing', target=target_img) 34844c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 34944c7ca5eSPaolo Bonzini 3502575fe16SStefan Hajnoczi self.wait_ready_and_cancel() 35144c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block') 35244c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', test_img) 35344c7ca5eSPaolo Bonzini self.vm.shutdown() 354866323f3SFam Zheng self.assertTrue(iotests.compare_images(test_img, target_img), 35544c7ca5eSPaolo Bonzini 'target image does not match source after mirroring') 35644c7ca5eSPaolo Bonzini 357b812f671SPaolo Bonzini def test_large_cluster(self): 358ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 359b812f671SPaolo Bonzini 360b812f671SPaolo Bonzini # qemu-img create fails if the image is not there 361b812f671SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'size=%d' 362b812f671SPaolo Bonzini %(TestMirrorNoBacking.image_len), target_backing_img) 363b812f671SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s' 364b812f671SPaolo Bonzini % (TestMirrorNoBacking.image_len, target_backing_img), target_img) 365b812f671SPaolo Bonzini 366b812f671SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 367b812f671SPaolo Bonzini mode='existing', target=target_img) 368b812f671SPaolo Bonzini self.assert_qmp(result, 'return', {}) 369b812f671SPaolo Bonzini 370b812f671SPaolo Bonzini self.complete_and_wait() 371b812f671SPaolo Bonzini result = self.vm.qmp('query-block') 372b812f671SPaolo Bonzini self.assert_qmp(result, 'return[0]/inserted/file', target_img) 373b812f671SPaolo Bonzini self.vm.shutdown() 374866323f3SFam Zheng self.assertTrue(iotests.compare_images(test_img, target_img), 375b812f671SPaolo Bonzini 'target image does not match source after mirroring') 376b812f671SPaolo Bonzini 377866323f3SFam Zhengclass TestMirrorResized(iotests.QMPTestCase): 378a04eca10SVishvananda Ishaya backing_len = 1 * 1024 * 1024 # MB 379a04eca10SVishvananda Ishaya image_len = 2 * 1024 * 1024 # MB 380a04eca10SVishvananda Ishaya 381a04eca10SVishvananda Ishaya def setUp(self): 3822499a096SStefan Hajnoczi iotests.create_image(backing_img, TestMirrorResized.backing_len) 383a04eca10SVishvananda Ishaya qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 384a04eca10SVishvananda Ishaya qemu_img('resize', test_img, '2M') 385a04eca10SVishvananda Ishaya self.vm = iotests.VM().add_drive(test_img) 386a04eca10SVishvananda Ishaya self.vm.launch() 387a04eca10SVishvananda Ishaya 388a04eca10SVishvananda Ishaya def tearDown(self): 389a04eca10SVishvananda Ishaya self.vm.shutdown() 390a04eca10SVishvananda Ishaya os.remove(test_img) 391a04eca10SVishvananda Ishaya os.remove(backing_img) 392a04eca10SVishvananda Ishaya try: 393a04eca10SVishvananda Ishaya os.remove(target_img) 394a04eca10SVishvananda Ishaya except OSError: 395a04eca10SVishvananda Ishaya pass 396a04eca10SVishvananda Ishaya 397a04eca10SVishvananda Ishaya def test_complete_top(self): 398ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 399a04eca10SVishvananda Ishaya 400a04eca10SVishvananda Ishaya result = self.vm.qmp('drive-mirror', device='drive0', sync='top', 401a04eca10SVishvananda Ishaya target=target_img) 402a04eca10SVishvananda Ishaya self.assert_qmp(result, 'return', {}) 403a04eca10SVishvananda Ishaya 404a04eca10SVishvananda Ishaya self.complete_and_wait() 405a04eca10SVishvananda Ishaya result = self.vm.qmp('query-block') 406a04eca10SVishvananda Ishaya self.assert_qmp(result, 'return[0]/inserted/file', target_img) 407a04eca10SVishvananda Ishaya self.vm.shutdown() 4083a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 409a04eca10SVishvananda Ishaya 'target image does not match source after mirroring') 410a04eca10SVishvananda Ishaya 411a04eca10SVishvananda Ishaya def test_complete_full(self): 412ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 413a04eca10SVishvananda Ishaya 414a04eca10SVishvananda Ishaya result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 415a04eca10SVishvananda Ishaya target=target_img) 416a04eca10SVishvananda Ishaya self.assert_qmp(result, 'return', {}) 417a04eca10SVishvananda Ishaya 418a04eca10SVishvananda Ishaya self.complete_and_wait() 419a04eca10SVishvananda Ishaya result = self.vm.qmp('query-block') 420a04eca10SVishvananda Ishaya self.assert_qmp(result, 'return[0]/inserted/file', target_img) 421a04eca10SVishvananda Ishaya self.vm.shutdown() 4223a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 423a04eca10SVishvananda Ishaya 'target image does not match source after mirroring') 424a04eca10SVishvananda Ishaya 425866323f3SFam Zhengclass TestReadErrors(iotests.QMPTestCase): 4269dfa9f59SPaolo Bonzini image_len = 2 * 1024 * 1024 # MB 4279dfa9f59SPaolo Bonzini 4289dfa9f59SPaolo Bonzini # this should be a multiple of twice the default granularity 4299dfa9f59SPaolo Bonzini # so that we hit this offset first in state 1 4309dfa9f59SPaolo Bonzini MIRROR_GRANULARITY = 1024 * 1024 4319dfa9f59SPaolo Bonzini 4329dfa9f59SPaolo Bonzini def create_blkdebug_file(self, name, event, errno): 4339dfa9f59SPaolo Bonzini file = open(name, 'w') 4349dfa9f59SPaolo Bonzini file.write(''' 4359dfa9f59SPaolo Bonzini[inject-error] 4369dfa9f59SPaolo Bonzinistate = "1" 4379dfa9f59SPaolo Bonzinievent = "%s" 4389dfa9f59SPaolo Bonzinierrno = "%d" 4399dfa9f59SPaolo Bonziniimmediately = "off" 4409dfa9f59SPaolo Bonzinionce = "on" 4419dfa9f59SPaolo Bonzinisector = "%d" 4429dfa9f59SPaolo Bonzini 4439dfa9f59SPaolo Bonzini[set-state] 4449dfa9f59SPaolo Bonzinistate = "1" 4459dfa9f59SPaolo Bonzinievent = "%s" 4469dfa9f59SPaolo Bonzininew_state = "2" 4479dfa9f59SPaolo Bonzini 4489dfa9f59SPaolo Bonzini[set-state] 4499dfa9f59SPaolo Bonzinistate = "2" 4509dfa9f59SPaolo Bonzinievent = "%s" 4519dfa9f59SPaolo Bonzininew_state = "1" 4529a3a9a63SMax Reitz''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event)) 4539dfa9f59SPaolo Bonzini file.close() 4549dfa9f59SPaolo Bonzini 4559dfa9f59SPaolo Bonzini def setUp(self): 4569dfa9f59SPaolo Bonzini self.blkdebug_file = backing_img + ".blkdebug" 4572499a096SStefan Hajnoczi iotests.create_image(backing_img, TestReadErrors.image_len) 4589dfa9f59SPaolo Bonzini self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5) 4599dfa9f59SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, 4609dfa9f59SPaolo Bonzini '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw' 4619dfa9f59SPaolo Bonzini % (self.blkdebug_file, backing_img), 4629dfa9f59SPaolo Bonzini test_img) 463b812f671SPaolo Bonzini # Write something for tests that use sync='top' 464b812f671SPaolo Bonzini qemu_io('-c', 'write %d 512' % (self.MIRROR_GRANULARITY + 65536), 465b812f671SPaolo Bonzini test_img) 4669dfa9f59SPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 4679dfa9f59SPaolo Bonzini self.vm.launch() 4689dfa9f59SPaolo Bonzini 4699dfa9f59SPaolo Bonzini def tearDown(self): 4709dfa9f59SPaolo Bonzini self.vm.shutdown() 4719dfa9f59SPaolo Bonzini os.remove(test_img) 472db11d1eeSKevin Wolf os.remove(target_img) 4739dfa9f59SPaolo Bonzini os.remove(backing_img) 4749dfa9f59SPaolo Bonzini os.remove(self.blkdebug_file) 4759dfa9f59SPaolo Bonzini 4769dfa9f59SPaolo Bonzini def test_report_read(self): 477ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 4789dfa9f59SPaolo Bonzini 4799dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 4809dfa9f59SPaolo Bonzini target=target_img) 4819dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 4829dfa9f59SPaolo Bonzini 4839dfa9f59SPaolo Bonzini completed = False 4849dfa9f59SPaolo Bonzini error = False 4859dfa9f59SPaolo Bonzini while not completed: 4869dfa9f59SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 4879dfa9f59SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 4889dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 4899dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 4909dfa9f59SPaolo Bonzini error = True 4919dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_READY': 4929dfa9f59SPaolo Bonzini self.assertTrue(False, 'job completed unexpectedly') 4939dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_COMPLETED': 4949dfa9f59SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 4959dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/type', 'mirror') 4969dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 4979dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/error', 'Input/output error') 4989dfa9f59SPaolo Bonzini completed = True 4991dac83f1SKevin Wolf elif event['event'] == 'JOB_STATUS_CHANGE': 5001dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 5019dfa9f59SPaolo Bonzini 502ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 5039dfa9f59SPaolo Bonzini 5049dfa9f59SPaolo Bonzini def test_ignore_read(self): 505ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 5069dfa9f59SPaolo Bonzini 5079dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 5089dfa9f59SPaolo Bonzini target=target_img, on_source_error='ignore') 5099dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 5109dfa9f59SPaolo Bonzini 5119dfa9f59SPaolo Bonzini event = self.vm.get_qmp_event(wait=True) 5121dac83f1SKevin Wolf while event['event'] == 'JOB_STATUS_CHANGE': 5131dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 5141dac83f1SKevin Wolf event = self.vm.get_qmp_event(wait=True) 5151dac83f1SKevin Wolf 516fa1cfb40SKevin Wolf self.assertEqual(event['event'], 'BLOCK_JOB_ERROR') 5179dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 5189dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 5199dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 5209dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 5219dfa9f59SPaolo Bonzini self.complete_and_wait() 5229dfa9f59SPaolo Bonzini 523b812f671SPaolo Bonzini def test_large_cluster(self): 524ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 525b812f671SPaolo Bonzini 526b812f671SPaolo Bonzini # Test COW into the target image. The first half of the 527b812f671SPaolo Bonzini # cluster at MIRROR_GRANULARITY has to be copied from 528b812f671SPaolo Bonzini # backing_img, even though sync='top'. 529b812f671SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-ocluster_size=131072,backing_file=%s' %(backing_img), target_img) 530b812f671SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='top', 531b812f671SPaolo Bonzini on_source_error='ignore', 532b812f671SPaolo Bonzini mode='existing', target=target_img) 533b812f671SPaolo Bonzini self.assert_qmp(result, 'return', {}) 534b812f671SPaolo Bonzini 535b812f671SPaolo Bonzini event = self.vm.get_qmp_event(wait=True) 5361dac83f1SKevin Wolf while event['event'] == 'JOB_STATUS_CHANGE': 5371dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 5381dac83f1SKevin Wolf event = self.vm.get_qmp_event(wait=True) 5391dac83f1SKevin Wolf 540fa1cfb40SKevin Wolf self.assertEqual(event['event'], 'BLOCK_JOB_ERROR') 541b812f671SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 542b812f671SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 543b812f671SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 544b812f671SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 545b812f671SPaolo Bonzini self.complete_and_wait() 546b812f671SPaolo Bonzini self.vm.shutdown() 547b812f671SPaolo Bonzini 548b812f671SPaolo Bonzini # Detach blkdebug to compare images successfully 549b812f671SPaolo Bonzini qemu_img('rebase', '-f', iotests.imgfmt, '-u', '-b', backing_img, test_img) 5503a3918c3SStefan Hajnoczi self.assertTrue(iotests.compare_images(test_img, target_img), 551b812f671SPaolo Bonzini 'target image does not match source after mirroring') 552b812f671SPaolo Bonzini 5539dfa9f59SPaolo Bonzini def test_stop_read(self): 554ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 5559dfa9f59SPaolo Bonzini 5569dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 5579dfa9f59SPaolo Bonzini target=target_img, on_source_error='stop') 5589dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 5599dfa9f59SPaolo Bonzini 5609dfa9f59SPaolo Bonzini error = False 5619dfa9f59SPaolo Bonzini ready = False 5629dfa9f59SPaolo Bonzini while not ready: 5639dfa9f59SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 5649dfa9f59SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 5659dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 5669dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'read') 5679dfa9f59SPaolo Bonzini 5689dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 5699dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', True) 5709dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'failed') 5719dfa9f59SPaolo Bonzini 5729dfa9f59SPaolo Bonzini result = self.vm.qmp('block-job-resume', device='drive0') 5739dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 5749dfa9f59SPaolo Bonzini error = True 5759dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_READY': 5769dfa9f59SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 5779dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 5789dfa9f59SPaolo Bonzini ready = True 5799dfa9f59SPaolo Bonzini 5809dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 5819dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 5829dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'ok') 5839dfa9f59SPaolo Bonzini 5849dfa9f59SPaolo Bonzini self.complete_and_wait(wait_ready=False) 585ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 5869dfa9f59SPaolo Bonzini 587866323f3SFam Zhengclass TestWriteErrors(iotests.QMPTestCase): 5889dfa9f59SPaolo Bonzini image_len = 2 * 1024 * 1024 # MB 5899dfa9f59SPaolo Bonzini 5909dfa9f59SPaolo Bonzini # this should be a multiple of twice the default granularity 5919dfa9f59SPaolo Bonzini # so that we hit this offset first in state 1 5929dfa9f59SPaolo Bonzini MIRROR_GRANULARITY = 1024 * 1024 5939dfa9f59SPaolo Bonzini 5949dfa9f59SPaolo Bonzini def create_blkdebug_file(self, name, event, errno): 5959dfa9f59SPaolo Bonzini file = open(name, 'w') 5969dfa9f59SPaolo Bonzini file.write(''' 5979dfa9f59SPaolo Bonzini[inject-error] 5989dfa9f59SPaolo Bonzinistate = "1" 5999dfa9f59SPaolo Bonzinievent = "%s" 6009dfa9f59SPaolo Bonzinierrno = "%d" 6019dfa9f59SPaolo Bonziniimmediately = "off" 6029dfa9f59SPaolo Bonzinionce = "on" 6039dfa9f59SPaolo Bonzinisector = "%d" 6049dfa9f59SPaolo Bonzini 6059dfa9f59SPaolo Bonzini[set-state] 6069dfa9f59SPaolo Bonzinistate = "1" 6079dfa9f59SPaolo Bonzinievent = "%s" 6089dfa9f59SPaolo Bonzininew_state = "2" 6099dfa9f59SPaolo Bonzini 6109dfa9f59SPaolo Bonzini[set-state] 6119dfa9f59SPaolo Bonzinistate = "2" 6129dfa9f59SPaolo Bonzinievent = "%s" 6139dfa9f59SPaolo Bonzininew_state = "1" 6149a3a9a63SMax Reitz''' % (event, errno, self.MIRROR_GRANULARITY // 512, event, event)) 6159dfa9f59SPaolo Bonzini file.close() 6169dfa9f59SPaolo Bonzini 6179dfa9f59SPaolo Bonzini def setUp(self): 6189dfa9f59SPaolo Bonzini self.blkdebug_file = target_img + ".blkdebug" 6192499a096SStefan Hajnoczi iotests.create_image(backing_img, TestWriteErrors.image_len) 6209dfa9f59SPaolo Bonzini self.create_blkdebug_file(self.blkdebug_file, "write_aio", 5) 6219dfa9f59SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-obacking_file=%s' %(backing_img), test_img) 6229dfa9f59SPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 6239dfa9f59SPaolo Bonzini self.target_img = 'blkdebug:%s:%s' % (self.blkdebug_file, target_img) 6249dfa9f59SPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-osize=%d' %(TestWriteErrors.image_len), target_img) 6259dfa9f59SPaolo Bonzini self.vm.launch() 6269dfa9f59SPaolo Bonzini 6279dfa9f59SPaolo Bonzini def tearDown(self): 6289dfa9f59SPaolo Bonzini self.vm.shutdown() 6299dfa9f59SPaolo Bonzini os.remove(test_img) 630db11d1eeSKevin Wolf os.remove(target_img) 6319dfa9f59SPaolo Bonzini os.remove(backing_img) 6329dfa9f59SPaolo Bonzini os.remove(self.blkdebug_file) 6339dfa9f59SPaolo Bonzini 6349dfa9f59SPaolo Bonzini def test_report_write(self): 635ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 6369dfa9f59SPaolo Bonzini 6379dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 6389dfa9f59SPaolo Bonzini mode='existing', target=self.target_img) 6399dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 6409dfa9f59SPaolo Bonzini 6419dfa9f59SPaolo Bonzini completed = False 6429dfa9f59SPaolo Bonzini error = False 6439dfa9f59SPaolo Bonzini while not completed: 6449dfa9f59SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 6459dfa9f59SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 6469dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 6479dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'write') 6489dfa9f59SPaolo Bonzini error = True 6499dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_READY': 6509dfa9f59SPaolo Bonzini self.assertTrue(False, 'job completed unexpectedly') 6519dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_COMPLETED': 6529dfa9f59SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 6539dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/type', 'mirror') 6549dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 6559dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/error', 'Input/output error') 6569dfa9f59SPaolo Bonzini completed = True 6579dfa9f59SPaolo Bonzini 658ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 6599dfa9f59SPaolo Bonzini 6609dfa9f59SPaolo Bonzini def test_ignore_write(self): 661ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 6629dfa9f59SPaolo Bonzini 6639dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 6649dfa9f59SPaolo Bonzini mode='existing', target=self.target_img, 6659dfa9f59SPaolo Bonzini on_target_error='ignore') 6669dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 6679dfa9f59SPaolo Bonzini 6681dac83f1SKevin Wolf event = self.vm.event_wait(name='BLOCK_JOB_ERROR') 669fa1cfb40SKevin Wolf self.assertEqual(event['event'], 'BLOCK_JOB_ERROR') 6709dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 6719dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'write') 6729dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 6739dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 6749dfa9f59SPaolo Bonzini self.complete_and_wait() 6759dfa9f59SPaolo Bonzini 6769dfa9f59SPaolo Bonzini def test_stop_write(self): 677ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 6789dfa9f59SPaolo Bonzini 6799dfa9f59SPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 6809dfa9f59SPaolo Bonzini mode='existing', target=self.target_img, 6819dfa9f59SPaolo Bonzini on_target_error='stop') 6829dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 6839dfa9f59SPaolo Bonzini 6849dfa9f59SPaolo Bonzini error = False 6859dfa9f59SPaolo Bonzini ready = False 6869dfa9f59SPaolo Bonzini while not ready: 6879dfa9f59SPaolo Bonzini for event in self.vm.get_qmp_events(wait=True): 6889dfa9f59SPaolo Bonzini if event['event'] == 'BLOCK_JOB_ERROR': 6899dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 6909dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/operation', 'write') 6919dfa9f59SPaolo Bonzini 6929dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 6939dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', True) 6949dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'failed') 6959dfa9f59SPaolo Bonzini 6969dfa9f59SPaolo Bonzini result = self.vm.qmp('block-job-resume', device='drive0') 6979dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return', {}) 6989dfa9f59SPaolo Bonzini 6999dfa9f59SPaolo Bonzini result = self.vm.qmp('query-block-jobs') 7009dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/paused', False) 7019dfa9f59SPaolo Bonzini self.assert_qmp(result, 'return[0]/io-status', 'ok') 7029dfa9f59SPaolo Bonzini error = True 7039dfa9f59SPaolo Bonzini elif event['event'] == 'BLOCK_JOB_READY': 7049dfa9f59SPaolo Bonzini self.assertTrue(error, 'job completed unexpectedly') 7059dfa9f59SPaolo Bonzini self.assert_qmp(event, 'data/device', 'drive0') 7069dfa9f59SPaolo Bonzini ready = True 7079dfa9f59SPaolo Bonzini 7089dfa9f59SPaolo Bonzini self.complete_and_wait(wait_ready=False) 709ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 7109dfa9f59SPaolo Bonzini 711866323f3SFam Zhengclass TestSetSpeed(iotests.QMPTestCase): 71244c7ca5eSPaolo Bonzini image_len = 80 * 1024 * 1024 # MB 71344c7ca5eSPaolo Bonzini 71444c7ca5eSPaolo Bonzini def setUp(self): 71544c7ca5eSPaolo Bonzini qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 71644c7ca5eSPaolo Bonzini qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 71744c7ca5eSPaolo Bonzini self.vm = iotests.VM().add_drive(test_img) 71844c7ca5eSPaolo Bonzini self.vm.launch() 71944c7ca5eSPaolo Bonzini 72044c7ca5eSPaolo Bonzini def tearDown(self): 72144c7ca5eSPaolo Bonzini self.vm.shutdown() 72244c7ca5eSPaolo Bonzini os.remove(test_img) 72344c7ca5eSPaolo Bonzini os.remove(backing_img) 72444c7ca5eSPaolo Bonzini os.remove(target_img) 72544c7ca5eSPaolo Bonzini 72644c7ca5eSPaolo Bonzini def test_set_speed(self): 727ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 72844c7ca5eSPaolo Bonzini 72944c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 73044c7ca5eSPaolo Bonzini target=target_img) 73144c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 73244c7ca5eSPaolo Bonzini 73344c7ca5eSPaolo Bonzini # Default speed is 0 73444c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block-jobs') 73544c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/device', 'drive0') 73644c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/speed', 0) 73744c7ca5eSPaolo Bonzini 73844c7ca5eSPaolo Bonzini result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024) 73944c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 74044c7ca5eSPaolo Bonzini 74144c7ca5eSPaolo Bonzini # Ensure the speed we set was accepted 74244c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block-jobs') 74344c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/device', 'drive0') 74444c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024) 74544c7ca5eSPaolo Bonzini 7462575fe16SStefan Hajnoczi self.wait_ready_and_cancel() 74744c7ca5eSPaolo Bonzini 74844c7ca5eSPaolo Bonzini # Check setting speed in drive-mirror works 74944c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 75044c7ca5eSPaolo Bonzini target=target_img, speed=4*1024*1024) 75144c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 75244c7ca5eSPaolo Bonzini 75344c7ca5eSPaolo Bonzini result = self.vm.qmp('query-block-jobs') 75444c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/device', 'drive0') 75544c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024) 75644c7ca5eSPaolo Bonzini 7572575fe16SStefan Hajnoczi self.wait_ready_and_cancel() 75844c7ca5eSPaolo Bonzini 75944c7ca5eSPaolo Bonzini def test_set_speed_invalid(self): 760ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 76144c7ca5eSPaolo Bonzini 76244c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 76344c7ca5eSPaolo Bonzini target=target_img, speed=-1) 76444c7ca5eSPaolo Bonzini self.assert_qmp(result, 'error/class', 'GenericError') 76544c7ca5eSPaolo Bonzini 766ecc1c88eSStefan Hajnoczi self.assert_no_active_block_jobs() 76744c7ca5eSPaolo Bonzini 76844c7ca5eSPaolo Bonzini result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 76944c7ca5eSPaolo Bonzini target=target_img) 77044c7ca5eSPaolo Bonzini self.assert_qmp(result, 'return', {}) 77144c7ca5eSPaolo Bonzini 77244c7ca5eSPaolo Bonzini result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1) 77344c7ca5eSPaolo Bonzini self.assert_qmp(result, 'error/class', 'GenericError') 77444c7ca5eSPaolo Bonzini 7752575fe16SStefan Hajnoczi self.wait_ready_and_cancel() 77644c7ca5eSPaolo Bonzini 777866323f3SFam Zhengclass TestUnbackedSource(iotests.QMPTestCase): 778c15badeeSMax Reitz image_len = 2 * 1024 * 1024 # MB 779c15badeeSMax Reitz 780c15badeeSMax Reitz def setUp(self): 781c15badeeSMax Reitz qemu_img('create', '-f', iotests.imgfmt, test_img, 782c15badeeSMax Reitz str(TestUnbackedSource.image_len)) 7839463ee1fSMax Reitz self.vm = iotests.VM() 784c15badeeSMax Reitz self.vm.launch() 7859463ee1fSMax Reitz result = self.vm.qmp('blockdev-add', node_name='drive0', 7869463ee1fSMax Reitz driver=iotests.imgfmt, 7879463ee1fSMax Reitz file={ 7889463ee1fSMax Reitz 'driver': 'file', 7899463ee1fSMax Reitz 'filename': test_img, 7909463ee1fSMax Reitz }) 7919463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 792c15badeeSMax Reitz 793c15badeeSMax Reitz def tearDown(self): 794c15badeeSMax Reitz self.vm.shutdown() 795c15badeeSMax Reitz os.remove(test_img) 796c15badeeSMax Reitz os.remove(target_img) 797c15badeeSMax Reitz 798171d6431SMax Reitz def test_absolute_paths_full(self): 799171d6431SMax Reitz self.assert_no_active_block_jobs() 8009463ee1fSMax Reitz result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0', 801171d6431SMax Reitz sync='full', target=target_img, 802171d6431SMax Reitz mode='absolute-paths') 803171d6431SMax Reitz self.assert_qmp(result, 'return', {}) 804171d6431SMax Reitz self.complete_and_wait() 805c15badeeSMax Reitz self.assert_no_active_block_jobs() 806c15badeeSMax Reitz 807171d6431SMax Reitz def test_absolute_paths_top(self): 808171d6431SMax Reitz self.assert_no_active_block_jobs() 8099463ee1fSMax Reitz result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0', 810171d6431SMax Reitz sync='top', target=target_img, 811171d6431SMax Reitz mode='absolute-paths') 812171d6431SMax Reitz self.assert_qmp(result, 'return', {}) 813171d6431SMax Reitz self.complete_and_wait() 814171d6431SMax Reitz self.assert_no_active_block_jobs() 815171d6431SMax Reitz 816171d6431SMax Reitz def test_absolute_paths_none(self): 817171d6431SMax Reitz self.assert_no_active_block_jobs() 8189463ee1fSMax Reitz result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0', 819171d6431SMax Reitz sync='none', target=target_img, 820c15badeeSMax Reitz mode='absolute-paths') 821c15badeeSMax Reitz self.assert_qmp(result, 'return', {}) 822c15badeeSMax Reitz self.complete_and_wait() 823c15badeeSMax Reitz self.assert_no_active_block_jobs() 824c15badeeSMax Reitz 8259463ee1fSMax Reitz def test_existing_full(self): 8269463ee1fSMax Reitz qemu_img('create', '-f', iotests.imgfmt, target_img, 8279463ee1fSMax Reitz str(self.image_len)) 8289463ee1fSMax Reitz qemu_io('-c', 'write -P 42 0 64k', target_img) 8299463ee1fSMax Reitz 8309463ee1fSMax Reitz self.assert_no_active_block_jobs() 8319463ee1fSMax Reitz result = self.vm.qmp('drive-mirror', job_id='drive0', device='drive0', 8329463ee1fSMax Reitz sync='full', target=target_img, mode='existing') 8339463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8349463ee1fSMax Reitz self.complete_and_wait() 8359463ee1fSMax Reitz self.assert_no_active_block_jobs() 8369463ee1fSMax Reitz 8379463ee1fSMax Reitz result = self.vm.qmp('blockdev-del', node_name='drive0') 8389463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8399463ee1fSMax Reitz 8409463ee1fSMax Reitz self.assertTrue(iotests.compare_images(test_img, target_img), 8419463ee1fSMax Reitz 'target image does not match source after mirroring') 8429463ee1fSMax Reitz 8439463ee1fSMax Reitz def test_blockdev_full(self): 8449463ee1fSMax Reitz qemu_img('create', '-f', iotests.imgfmt, target_img, 8459463ee1fSMax Reitz str(self.image_len)) 8469463ee1fSMax Reitz qemu_io('-c', 'write -P 42 0 64k', target_img) 8479463ee1fSMax Reitz 8489463ee1fSMax Reitz result = self.vm.qmp('blockdev-add', node_name='target', 8499463ee1fSMax Reitz driver=iotests.imgfmt, 8509463ee1fSMax Reitz file={ 8519463ee1fSMax Reitz 'driver': 'file', 8529463ee1fSMax Reitz 'filename': target_img, 8539463ee1fSMax Reitz }) 8549463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8559463ee1fSMax Reitz 8569463ee1fSMax Reitz self.assert_no_active_block_jobs() 8579463ee1fSMax Reitz result = self.vm.qmp('blockdev-mirror', job_id='drive0', device='drive0', 8589463ee1fSMax Reitz sync='full', target='target') 8599463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8609463ee1fSMax Reitz self.complete_and_wait() 8619463ee1fSMax Reitz self.assert_no_active_block_jobs() 8629463ee1fSMax Reitz 8639463ee1fSMax Reitz result = self.vm.qmp('blockdev-del', node_name='drive0') 8649463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8659463ee1fSMax Reitz 8669463ee1fSMax Reitz result = self.vm.qmp('blockdev-del', node_name='target') 8679463ee1fSMax Reitz self.assert_qmp(result, 'return', {}) 8689463ee1fSMax Reitz 8699463ee1fSMax Reitz self.assertTrue(iotests.compare_images(test_img, target_img), 8709463ee1fSMax Reitz 'target image does not match source after mirroring') 8719463ee1fSMax Reitz 872ccee3d8fSJohn Snowclass TestGranularity(iotests.QMPTestCase): 873ccee3d8fSJohn Snow image_len = 10 * 1024 * 1024 # MB 874ccee3d8fSJohn Snow 875ccee3d8fSJohn Snow def setUp(self): 876ccee3d8fSJohn Snow qemu_img('create', '-f', iotests.imgfmt, test_img, 877ccee3d8fSJohn Snow str(TestGranularity.image_len)) 878ccee3d8fSJohn Snow qemu_io('-c', 'write 0 %d' % (self.image_len), 879ccee3d8fSJohn Snow test_img) 880ccee3d8fSJohn Snow self.vm = iotests.VM().add_drive(test_img) 881ccee3d8fSJohn Snow self.vm.launch() 882ccee3d8fSJohn Snow 883ccee3d8fSJohn Snow def tearDown(self): 884ccee3d8fSJohn Snow self.vm.shutdown() 885ccee3d8fSJohn Snow self.assertTrue(iotests.compare_images(test_img, target_img), 886ccee3d8fSJohn Snow 'target image does not match source after mirroring') 887ccee3d8fSJohn Snow os.remove(test_img) 888ccee3d8fSJohn Snow os.remove(target_img) 889ccee3d8fSJohn Snow 890ccee3d8fSJohn Snow def test_granularity(self): 891ccee3d8fSJohn Snow self.assert_no_active_block_jobs() 892ccee3d8fSJohn Snow result = self.vm.qmp('drive-mirror', device='drive0', 893ccee3d8fSJohn Snow sync='full', target=target_img, 894ccee3d8fSJohn Snow mode='absolute-paths', granularity=8192) 895ccee3d8fSJohn Snow self.assert_qmp(result, 'return', {}) 8961dac83f1SKevin Wolf 897ccee3d8fSJohn Snow event = self.vm.get_qmp_event(wait=60.0) 8981dac83f1SKevin Wolf while event['event'] == 'JOB_STATUS_CHANGE': 8991dac83f1SKevin Wolf self.assert_qmp(event, 'data/id', 'drive0') 9001dac83f1SKevin Wolf event = self.vm.get_qmp_event(wait=60.0) 9011dac83f1SKevin Wolf 902ccee3d8fSJohn Snow # Failures will manifest as COMPLETED/ERROR. 903ccee3d8fSJohn Snow self.assert_qmp(event, 'event', 'BLOCK_JOB_READY') 904ccee3d8fSJohn Snow self.complete_and_wait(drive='drive0', wait_ready=False) 905ccee3d8fSJohn Snow self.assert_no_active_block_jobs() 906ccee3d8fSJohn Snow 907866323f3SFam Zhengclass TestRepairQuorum(iotests.QMPTestCase): 908d88964aeSBenoît Canet """ This class test quorum file repair using drive-mirror. 909d88964aeSBenoît Canet It's mostly a fork of TestSingleDrive """ 910d88964aeSBenoît Canet image_len = 1 * 1024 * 1024 # MB 911d88964aeSBenoît Canet IMAGES = [ quorum_img1, quorum_img2, quorum_img3 ] 912d88964aeSBenoît Canet 9139442bebeSThomas Huth @iotests.skip_if_unsupported(['quorum']) 914d88964aeSBenoît Canet def setUp(self): 915d88964aeSBenoît Canet self.vm = iotests.VM() 916d88964aeSBenoît Canet 9170ed82f7aSMax Reitz if iotests.qemu_default_machine == 'pc': 9180ed82f7aSMax Reitz self.vm.add_drive(None, 'media=cdrom', 'ide') 9190ed82f7aSMax Reitz 920d88964aeSBenoît Canet # Add each individual quorum images 921d88964aeSBenoît Canet for i in self.IMAGES: 922d88964aeSBenoît Canet qemu_img('create', '-f', iotests.imgfmt, i, 92389e21945SMax Reitz str(self.image_len)) 924d88964aeSBenoît Canet # Assign a node name to each quorum image in order to manipulate 925d88964aeSBenoît Canet # them 926d88964aeSBenoît Canet opts = "node-name=img%i" % self.IMAGES.index(i) 927f718ca14SMax Reitz opts += ',driver=%s' % iotests.imgfmt 928f718ca14SMax Reitz opts += ',file.driver=file' 929f718ca14SMax Reitz opts += ',file.filename=%s' % i 930f718ca14SMax Reitz self.vm = self.vm.add_blockdev(opts) 931d88964aeSBenoît Canet 932d88964aeSBenoît Canet self.vm.launch() 933d88964aeSBenoît Canet 934d88964aeSBenoît Canet #assemble the quorum block device from the individual files 9350153d2f5SKevin Wolf args = { "driver": "quorum", "node-name": "quorum0", 9360153d2f5SKevin Wolf "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] } 937d88964aeSBenoît Canet result = self.vm.qmp("blockdev-add", **args) 938d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 939d88964aeSBenoît Canet 940d88964aeSBenoît Canet 941d88964aeSBenoît Canet def tearDown(self): 942d88964aeSBenoît Canet self.vm.shutdown() 943a1da1878SMax Reitz for i in self.IMAGES + [ quorum_repair_img, quorum_snapshot_file, 944a1da1878SMax Reitz nbd_sock_path ]: 945d88964aeSBenoît Canet # Do a try/except because the test may have deleted some images 946d88964aeSBenoît Canet try: 947d88964aeSBenoît Canet os.remove(i) 948d88964aeSBenoît Canet except OSError: 949d88964aeSBenoît Canet pass 950d88964aeSBenoît Canet 951d88964aeSBenoît Canet def test_complete(self): 952476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 953476fb028SKevin Wolf sync='full', node_name="repair0", replaces="img1", 954d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 955d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 956d88964aeSBenoît Canet 957476fb028SKevin Wolf self.complete_and_wait(drive="job0") 958e71fc0baSFam Zheng self.assert_has_block_node("repair0", quorum_repair_img) 959c351afd6SMax Reitz self.vm.assert_block_path('quorum0', '/children.1', 'repair0') 960d88964aeSBenoît Canet self.vm.shutdown() 961d88964aeSBenoît Canet self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img), 962d88964aeSBenoît Canet 'target image does not match source after mirroring') 963d88964aeSBenoît Canet 964d88964aeSBenoît Canet def test_cancel(self): 965476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 966476fb028SKevin Wolf sync='full', node_name="repair0", replaces="img1", 967d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 968d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 969d88964aeSBenoît Canet 970476fb028SKevin Wolf self.cancel_and_wait(drive="job0", force=True) 971d88964aeSBenoît Canet # here we check that the last registered quorum file has not been 972d88964aeSBenoît Canet # swapped out and unref 973e71fc0baSFam Zheng self.assert_has_block_node(None, quorum_img3) 974d88964aeSBenoît Canet 975d88964aeSBenoît Canet def test_cancel_after_ready(self): 976476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 977476fb028SKevin Wolf sync='full', node_name="repair0", replaces="img1", 978d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 979d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 980d88964aeSBenoît Canet 981476fb028SKevin Wolf self.wait_ready_and_cancel(drive="job0") 982d88964aeSBenoît Canet # here we check that the last registered quorum file has not been 983d88964aeSBenoît Canet # swapped out and unref 984e71fc0baSFam Zheng self.assert_has_block_node(None, quorum_img3) 985d88964aeSBenoît Canet self.vm.shutdown() 986d88964aeSBenoît Canet self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img), 987d88964aeSBenoît Canet 'target image does not match source after mirroring') 988d88964aeSBenoît Canet 989d88964aeSBenoît Canet def test_pause(self): 990476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 991476fb028SKevin Wolf sync='full', node_name="repair0", replaces="img1", 992d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 993d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 994d88964aeSBenoît Canet 9952c93c5cbSKevin Wolf self.pause_job('job0') 996d88964aeSBenoît Canet 997d88964aeSBenoît Canet result = self.vm.qmp('query-block-jobs') 998d88964aeSBenoît Canet offset = self.dictpath(result, 'return[0]/offset') 999d88964aeSBenoît Canet 10002c93c5cbSKevin Wolf time.sleep(0.5) 1001d88964aeSBenoît Canet result = self.vm.qmp('query-block-jobs') 1002d88964aeSBenoît Canet self.assert_qmp(result, 'return[0]/offset', offset) 1003d88964aeSBenoît Canet 1004476fb028SKevin Wolf result = self.vm.qmp('block-job-resume', device='job0') 1005d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 1006d88964aeSBenoît Canet 1007476fb028SKevin Wolf self.complete_and_wait(drive="job0") 1008d88964aeSBenoît Canet self.vm.shutdown() 1009d88964aeSBenoît Canet self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img), 1010d88964aeSBenoît Canet 'target image does not match source after mirroring') 1011d88964aeSBenoît Canet 1012d88964aeSBenoît Canet def test_medium_not_found(self): 1013d8683155SBo Tu if iotests.qemu_default_machine != 'pc': 1014d8683155SBo Tu return 1015d8683155SBo Tu 1016476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='drive0', # CD-ROM 10170ed82f7aSMax Reitz sync='full', 1018d88964aeSBenoît Canet node_name='repair0', 1019d88964aeSBenoît Canet replaces='img1', 1020d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1021d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1022d88964aeSBenoît Canet 1023d88964aeSBenoît Canet def test_image_not_found(self): 1024476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1025476fb028SKevin Wolf sync='full', node_name='repair0', replaces='img1', 1026476fb028SKevin Wolf mode='existing', target=quorum_repair_img, 1027476fb028SKevin Wolf format=iotests.imgfmt) 1028d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1029d88964aeSBenoît Canet 1030d88964aeSBenoît Canet def test_device_not_found(self): 1031476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', 1032476fb028SKevin Wolf device='nonexistent', sync='full', 1033d88964aeSBenoît Canet node_name='repair0', 1034d88964aeSBenoît Canet replaces='img1', 1035d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 10360524e93aSKevin Wolf self.assert_qmp(result, 'error/class', 'GenericError') 1037d88964aeSBenoît Canet 1038d88964aeSBenoît Canet def test_wrong_sync_mode(self): 1039476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', device='quorum0', job_id='job0', 1040d88964aeSBenoît Canet node_name='repair0', 1041d88964aeSBenoît Canet replaces='img1', 1042d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1043d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1044d88964aeSBenoît Canet 1045d88964aeSBenoît Canet def test_no_node_name(self): 1046476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1047476fb028SKevin Wolf sync='full', replaces='img1', 1048d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1049d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1050d88964aeSBenoît Canet 105167cc32ebSVeres Lajos def test_nonexistent_replaces(self): 1052476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1053476fb028SKevin Wolf sync='full', node_name='repair0', replaces='img77', 1054d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1055d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1056d88964aeSBenoît Canet 1057d88964aeSBenoît Canet def test_after_a_quorum_snapshot(self): 1058d88964aeSBenoît Canet result = self.vm.qmp('blockdev-snapshot-sync', node_name='img1', 1059d88964aeSBenoît Canet snapshot_file=quorum_snapshot_file, 1060d88964aeSBenoît Canet snapshot_node_name="snap1"); 1061d88964aeSBenoît Canet 1062476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1063476fb028SKevin Wolf sync='full', node_name='repair0', replaces="img1", 1064d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1065d88964aeSBenoît Canet self.assert_qmp(result, 'error/class', 'GenericError') 1066d88964aeSBenoît Canet 1067476fb028SKevin Wolf result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 1068476fb028SKevin Wolf sync='full', node_name='repair0', replaces="snap1", 1069d88964aeSBenoît Canet target=quorum_repair_img, format=iotests.imgfmt) 1070d88964aeSBenoît Canet self.assert_qmp(result, 'return', {}) 1071d88964aeSBenoît Canet 1072476fb028SKevin Wolf self.complete_and_wait('job0') 1073e71fc0baSFam Zheng self.assert_has_block_node("repair0", quorum_repair_img) 1074c351afd6SMax Reitz self.vm.assert_block_path('quorum0', '/children.1', 'repair0') 1075d88964aeSBenoît Canet 1076a1da1878SMax Reitz def test_with_other_parent(self): 1077a1da1878SMax Reitz """ 1078a1da1878SMax Reitz Check that we cannot replace a Quorum child when it has other 1079a1da1878SMax Reitz parents. 1080a1da1878SMax Reitz """ 1081a1da1878SMax Reitz result = self.vm.qmp('nbd-server-start', 1082a1da1878SMax Reitz addr={ 1083a1da1878SMax Reitz 'type': 'unix', 1084a1da1878SMax Reitz 'data': {'path': nbd_sock_path} 1085a1da1878SMax Reitz }) 1086a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1087a1da1878SMax Reitz 1088a1da1878SMax Reitz result = self.vm.qmp('nbd-server-add', device='img1') 1089a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1090a1da1878SMax Reitz 1091a1da1878SMax Reitz result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0', 1092a1da1878SMax Reitz sync='full', node_name='repair0', replaces='img1', 1093a1da1878SMax Reitz target=quorum_repair_img, format=iotests.imgfmt) 1094a1da1878SMax Reitz self.assert_qmp(result, 'error/desc', 1095a1da1878SMax Reitz "Cannot replace 'img1' by a node mirrored from " 1096a1da1878SMax Reitz "'quorum0', because it cannot be guaranteed that doing " 1097a1da1878SMax Reitz "so would not lead to an abrupt change of visible data") 1098a1da1878SMax Reitz 1099a1da1878SMax Reitz def test_with_other_parents_after_mirror_start(self): 1100a1da1878SMax Reitz """ 1101a1da1878SMax Reitz The same as test_with_other_parent(), but add the NBD server 1102a1da1878SMax Reitz only when the mirror job is already running. 1103a1da1878SMax Reitz """ 1104a1da1878SMax Reitz result = self.vm.qmp('nbd-server-start', 1105a1da1878SMax Reitz addr={ 1106a1da1878SMax Reitz 'type': 'unix', 1107a1da1878SMax Reitz 'data': {'path': nbd_sock_path} 1108a1da1878SMax Reitz }) 1109a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1110a1da1878SMax Reitz 1111a1da1878SMax Reitz result = self.vm.qmp('drive-mirror', job_id='mirror', device='quorum0', 1112a1da1878SMax Reitz sync='full', node_name='repair0', replaces='img1', 1113a1da1878SMax Reitz target=quorum_repair_img, format=iotests.imgfmt) 1114a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1115a1da1878SMax Reitz 1116a1da1878SMax Reitz result = self.vm.qmp('nbd-server-add', device='img1') 1117a1da1878SMax Reitz self.assert_qmp(result, 'return', {}) 1118a1da1878SMax Reitz 1119a1da1878SMax Reitz # The full error message goes to stderr, we will check it later 1120a1da1878SMax Reitz self.complete_and_wait('mirror', 1121a1da1878SMax Reitz completion_error='Operation not permitted') 1122a1da1878SMax Reitz 1123a1da1878SMax Reitz # Should not have been replaced 1124a1da1878SMax Reitz self.vm.assert_block_path('quorum0', '/children.1', 'img1') 1125a1da1878SMax Reitz 1126a1da1878SMax Reitz # Check the full error message now 1127a1da1878SMax Reitz self.vm.shutdown() 1128a1da1878SMax Reitz log = self.vm.get_log() 1129a1da1878SMax Reitz log = re.sub(r'^\[I \d+\.\d+\] OPENED\n', '', log) 1130a1da1878SMax Reitz log = re.sub(r'^Formatting.*\n', '', log) 1131a1da1878SMax Reitz log = re.sub(r'\n\[I \+\d+\.\d+\] CLOSED\n?$', '', log) 1132a1da1878SMax Reitz log = re.sub(r'^%s: ' % os.path.basename(iotests.qemu_prog), '', log) 1133a1da1878SMax Reitz 1134a1da1878SMax Reitz self.assertEqual(log, 1135a1da1878SMax Reitz "Can no longer replace 'img1' by 'repair0', because " + 1136a1da1878SMax Reitz "it can no longer be guaranteed that doing so would " + 1137a1da1878SMax Reitz "not lead to an abrupt change of visible data") 1138a1da1878SMax Reitz 1139a1da1878SMax Reitz 11405694923aSMax Reitz# Test mirroring with a source that does not have any parents (not even a 11415694923aSMax Reitz# BlockBackend) 11425694923aSMax Reitzclass TestOrphanedSource(iotests.QMPTestCase): 11435694923aSMax Reitz def setUp(self): 11445694923aSMax Reitz blk0 = { 'node-name': 'src', 11455694923aSMax Reitz 'driver': 'null-co' } 11465694923aSMax Reitz 11475694923aSMax Reitz blk1 = { 'node-name': 'dest', 11485694923aSMax Reitz 'driver': 'null-co' } 11495694923aSMax Reitz 11505694923aSMax Reitz blk2 = { 'node-name': 'dest-ro', 11515694923aSMax Reitz 'driver': 'null-co', 11525694923aSMax Reitz 'read-only': 'on' } 11535694923aSMax Reitz 11545694923aSMax Reitz self.vm = iotests.VM() 115562a94288SKevin Wolf self.vm.add_blockdev(self.vm.qmp_to_opts(blk0)) 115662a94288SKevin Wolf self.vm.add_blockdev(self.vm.qmp_to_opts(blk1)) 115762a94288SKevin Wolf self.vm.add_blockdev(self.vm.qmp_to_opts(blk2)) 11585694923aSMax Reitz self.vm.launch() 11595694923aSMax Reitz 11605694923aSMax Reitz def tearDown(self): 11615694923aSMax Reitz self.vm.shutdown() 11625694923aSMax Reitz 11635694923aSMax Reitz def test_no_job_id(self): 11645694923aSMax Reitz self.assert_no_active_block_jobs() 11655694923aSMax Reitz 11665694923aSMax Reitz result = self.vm.qmp('blockdev-mirror', device='src', sync='full', 11675694923aSMax Reitz target='dest') 11685694923aSMax Reitz self.assert_qmp(result, 'error/class', 'GenericError') 11695694923aSMax Reitz 11705694923aSMax Reitz def test_success(self): 11715694923aSMax Reitz self.assert_no_active_block_jobs() 11725694923aSMax Reitz 11735694923aSMax Reitz result = self.vm.qmp('blockdev-mirror', job_id='job', device='src', 11745694923aSMax Reitz sync='full', target='dest') 11755694923aSMax Reitz self.assert_qmp(result, 'return', {}) 11765694923aSMax Reitz 11775694923aSMax Reitz self.complete_and_wait('job') 11785694923aSMax Reitz 11795694923aSMax Reitz def test_failing_permissions(self): 11805694923aSMax Reitz self.assert_no_active_block_jobs() 11815694923aSMax Reitz 11825694923aSMax Reitz result = self.vm.qmp('blockdev-mirror', device='src', sync='full', 11835694923aSMax Reitz target='dest-ro') 11845694923aSMax Reitz self.assert_qmp(result, 'error/class', 'GenericError') 11855694923aSMax Reitz 11869592fe45SMax Reitz def test_failing_permission_in_complete(self): 11879592fe45SMax Reitz self.assert_no_active_block_jobs() 11889592fe45SMax Reitz 11899592fe45SMax Reitz # Unshare consistent-read on the target 11909592fe45SMax Reitz # (The mirror job does not care) 11919592fe45SMax Reitz result = self.vm.qmp('blockdev-add', 11929592fe45SMax Reitz driver='blkdebug', 11939592fe45SMax Reitz node_name='dest-perm', 11949592fe45SMax Reitz image='dest', 11959592fe45SMax Reitz unshare_child_perms=['consistent-read']) 11969592fe45SMax Reitz self.assert_qmp(result, 'return', {}) 11979592fe45SMax Reitz 11989592fe45SMax Reitz result = self.vm.qmp('blockdev-mirror', job_id='job', device='src', 11999592fe45SMax Reitz sync='full', target='dest', 12009592fe45SMax Reitz filter_node_name='mirror-filter') 12019592fe45SMax Reitz self.assert_qmp(result, 'return', {}) 12029592fe45SMax Reitz 12039592fe45SMax Reitz # Require consistent-read on the source 12049592fe45SMax Reitz # (We can only add this node once the job has started, or it 12059592fe45SMax Reitz # will complain that it does not want to run on non-root nodes) 12069592fe45SMax Reitz result = self.vm.qmp('blockdev-add', 12079592fe45SMax Reitz driver='blkdebug', 12089592fe45SMax Reitz node_name='src-perm', 12099592fe45SMax Reitz image='src', 12109592fe45SMax Reitz take_child_perms=['consistent-read']) 12119592fe45SMax Reitz self.assert_qmp(result, 'return', {}) 12129592fe45SMax Reitz 12139592fe45SMax Reitz # While completing, mirror will attempt to replace src by 12149592fe45SMax Reitz # dest, which must fail because src-perm requires 12159592fe45SMax Reitz # consistent-read but dest-perm does not share it; thus 12169592fe45SMax Reitz # aborting the job when it is supposed to complete 12179592fe45SMax Reitz self.complete_and_wait('job', 12189592fe45SMax Reitz completion_error='Operation not permitted') 12199592fe45SMax Reitz 12209592fe45SMax Reitz # Assert that all of our nodes are still there (except for the 12219592fe45SMax Reitz # mirror filter, which should be gone despite the failure) 12229592fe45SMax Reitz nodes = self.vm.qmp('query-named-block-nodes')['return'] 12239592fe45SMax Reitz nodes = [node['node-name'] for node in nodes] 12249592fe45SMax Reitz 12259592fe45SMax Reitz for expect in ('src', 'src-perm', 'dest', 'dest-perm'): 12269592fe45SMax Reitz self.assertTrue(expect in nodes, '%s disappeared' % expect) 12279592fe45SMax Reitz self.assertFalse('mirror-filter' in nodes, 12289592fe45SMax Reitz 'Mirror filter node did not disappear') 12299592fe45SMax Reitz 1230c45a88f4SMax Reitz# Test cases for @replaces that do not necessarily involve Quorum 1231c45a88f4SMax Reitzclass TestReplaces(iotests.QMPTestCase): 1232c45a88f4SMax Reitz # Each of these test cases needs their own block graph, so do not 1233c45a88f4SMax Reitz # create any nodes here 1234c45a88f4SMax Reitz def setUp(self): 1235c45a88f4SMax Reitz self.vm = iotests.VM() 1236c45a88f4SMax Reitz self.vm.launch() 1237c45a88f4SMax Reitz 1238c45a88f4SMax Reitz def tearDown(self): 1239c45a88f4SMax Reitz self.vm.shutdown() 1240c45a88f4SMax Reitz for img in (test_img, target_img): 1241c45a88f4SMax Reitz try: 1242c45a88f4SMax Reitz os.remove(img) 1243c45a88f4SMax Reitz except OSError: 1244c45a88f4SMax Reitz pass 1245c45a88f4SMax Reitz 1246c45a88f4SMax Reitz @iotests.skip_if_unsupported(['copy-on-read']) 1247c45a88f4SMax Reitz def test_replace_filter(self): 1248c45a88f4SMax Reitz """ 1249c45a88f4SMax Reitz Check that we can replace filter nodes. 1250c45a88f4SMax Reitz """ 1251c45a88f4SMax Reitz result = self.vm.qmp('blockdev-add', **{ 1252c45a88f4SMax Reitz 'driver': 'copy-on-read', 1253c45a88f4SMax Reitz 'node-name': 'filter0', 1254c45a88f4SMax Reitz 'file': { 1255c45a88f4SMax Reitz 'driver': 'copy-on-read', 1256c45a88f4SMax Reitz 'node-name': 'filter1', 1257c45a88f4SMax Reitz 'file': { 1258c45a88f4SMax Reitz 'driver': 'null-co' 1259c45a88f4SMax Reitz } 1260c45a88f4SMax Reitz } 1261c45a88f4SMax Reitz }) 1262c45a88f4SMax Reitz self.assert_qmp(result, 'return', {}) 1263c45a88f4SMax Reitz 1264c45a88f4SMax Reitz result = self.vm.qmp('blockdev-add', 1265c45a88f4SMax Reitz node_name='target', driver='null-co') 1266c45a88f4SMax Reitz self.assert_qmp(result, 'return', {}) 1267c45a88f4SMax Reitz 1268c45a88f4SMax Reitz result = self.vm.qmp('blockdev-mirror', job_id='mirror', device='filter0', 1269c45a88f4SMax Reitz target='target', sync='full', replaces='filter1') 1270c45a88f4SMax Reitz self.assert_qmp(result, 'return', {}) 1271c45a88f4SMax Reitz 1272c45a88f4SMax Reitz self.complete_and_wait('mirror') 1273c45a88f4SMax Reitz 1274c45a88f4SMax Reitz self.vm.assert_block_path('filter0', '/file', 'target') 1275c45a88f4SMax Reitz 127644c7ca5eSPaolo Bonziniif __name__ == '__main__': 1277103cbc77SMax Reitz iotests.main(supported_fmts=['qcow2', 'qed'], 1278877d18f2SThomas Huth supported_protocols=['file'], 1279877d18f2SThomas Huth supported_platforms=['linux', 'freebsd', 'netbsd', 'openbsd']) 1280