1#!/usr/bin/env python 2# 3# Tests for image mirroring. 4# 5# Copyright (C) 2012 Red Hat, Inc. 6# 7# This program is free software; you can redistribute it and/or modify 8# it under the terms of the GNU General Public License as published by 9# the Free Software Foundation; either version 2 of the License, or 10# (at your option) any later version. 11# 12# This program is distributed in the hope that it will be useful, 13# but WITHOUT ANY WARRANTY; without even the implied warranty of 14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15# GNU General Public License for more details. 16# 17# You should have received a copy of the GNU General Public License 18# along with this program. If not, see <http://www.gnu.org/licenses/>. 19# 20 21import time 22import os 23import iotests 24from iotests import qemu_img, qemu_io 25 26backing_img = os.path.join(iotests.test_dir, 'backing.img') 27target_backing_img = os.path.join(iotests.test_dir, 'target-backing.img') 28test_img = os.path.join(iotests.test_dir, 'test.img') 29target_img = os.path.join(iotests.test_dir, 'target.img') 30 31quorum_img1 = os.path.join(iotests.test_dir, 'quorum1.img') 32quorum_img2 = os.path.join(iotests.test_dir, 'quorum2.img') 33quorum_img3 = os.path.join(iotests.test_dir, 'quorum3.img') 34quorum_repair_img = os.path.join(iotests.test_dir, 'quorum_repair.img') 35quorum_snapshot_file = os.path.join(iotests.test_dir, 'quorum_snapshot.img') 36 37class TestSingleDrive(iotests.QMPTestCase): 38 image_len = 1 * 1024 * 1024 # MB 39 qmp_cmd = 'drive-mirror' 40 qmp_target = target_img 41 42 def setUp(self): 43 iotests.create_image(backing_img, self.image_len) 44 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 45 self.vm = iotests.VM().add_drive(test_img, "node-name=top,backing.node-name=base") 46 if iotests.qemu_default_machine == 'pc': 47 self.vm.add_drive(None, 'media=cdrom', 'ide') 48 self.vm.launch() 49 50 def tearDown(self): 51 self.vm.shutdown() 52 os.remove(test_img) 53 os.remove(backing_img) 54 try: 55 os.remove(target_img) 56 except OSError: 57 pass 58 59 def test_complete(self): 60 self.assert_no_active_block_jobs() 61 62 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 63 target=self.qmp_target) 64 self.assert_qmp(result, 'return', {}) 65 66 self.complete_and_wait() 67 result = self.vm.qmp('query-block') 68 self.assert_qmp(result, 'return[0]/inserted/file', target_img) 69 self.vm.shutdown() 70 self.assertTrue(iotests.compare_images(test_img, target_img), 71 'target image does not match source after mirroring') 72 73 def test_cancel(self): 74 self.assert_no_active_block_jobs() 75 76 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 77 target=self.qmp_target) 78 self.assert_qmp(result, 'return', {}) 79 80 self.cancel_and_wait(force=True) 81 result = self.vm.qmp('query-block') 82 self.assert_qmp(result, 'return[0]/inserted/file', test_img) 83 self.vm.shutdown() 84 85 def test_cancel_after_ready(self): 86 self.assert_no_active_block_jobs() 87 88 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 89 target=self.qmp_target) 90 self.assert_qmp(result, 'return', {}) 91 92 self.wait_ready_and_cancel() 93 result = self.vm.qmp('query-block') 94 self.assert_qmp(result, 'return[0]/inserted/file', test_img) 95 self.vm.shutdown() 96 self.assertTrue(iotests.compare_images(test_img, target_img), 97 'target image does not match source after mirroring') 98 99 def test_pause(self): 100 self.assert_no_active_block_jobs() 101 102 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 103 target=self.qmp_target) 104 self.assert_qmp(result, 'return', {}) 105 106 self.pause_job('drive0') 107 108 result = self.vm.qmp('query-block-jobs') 109 offset = self.dictpath(result, 'return[0]/offset') 110 111 time.sleep(0.5) 112 result = self.vm.qmp('query-block-jobs') 113 self.assert_qmp(result, 'return[0]/offset', offset) 114 115 result = self.vm.qmp('block-job-resume', device='drive0') 116 self.assert_qmp(result, 'return', {}) 117 118 self.complete_and_wait() 119 self.vm.shutdown() 120 self.assertTrue(iotests.compare_images(test_img, target_img), 121 'target image does not match source after mirroring') 122 123 def test_small_buffer(self): 124 self.assert_no_active_block_jobs() 125 126 # A small buffer is rounded up automatically 127 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 128 buf_size=4096, target=self.qmp_target) 129 self.assert_qmp(result, 'return', {}) 130 131 self.complete_and_wait() 132 result = self.vm.qmp('query-block') 133 self.assert_qmp(result, 'return[0]/inserted/file', target_img) 134 self.vm.shutdown() 135 self.assertTrue(iotests.compare_images(test_img, target_img), 136 'target image does not match source after mirroring') 137 138 def test_small_buffer2(self): 139 self.assert_no_active_block_jobs() 140 141 qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,size=%d' 142 % (self.image_len, self.image_len), target_img) 143 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 144 buf_size=65536, mode='existing', target=self.qmp_target) 145 self.assert_qmp(result, 'return', {}) 146 147 self.complete_and_wait() 148 result = self.vm.qmp('query-block') 149 self.assert_qmp(result, 'return[0]/inserted/file', target_img) 150 self.vm.shutdown() 151 self.assertTrue(iotests.compare_images(test_img, target_img), 152 'target image does not match source after mirroring') 153 154 def test_large_cluster(self): 155 self.assert_no_active_block_jobs() 156 157 qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s' 158 % (self.image_len, backing_img), target_img) 159 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 160 mode='existing', target=self.qmp_target) 161 self.assert_qmp(result, 'return', {}) 162 163 self.complete_and_wait() 164 result = self.vm.qmp('query-block') 165 self.assert_qmp(result, 'return[0]/inserted/file', target_img) 166 self.vm.shutdown() 167 self.assertTrue(iotests.compare_images(test_img, target_img), 168 'target image does not match source after mirroring') 169 170 # Tests that the insertion of the mirror_top filter node doesn't make a 171 # difference to query-block 172 def test_implicit_node(self): 173 self.assert_no_active_block_jobs() 174 175 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 176 target=self.qmp_target) 177 self.assert_qmp(result, 'return', {}) 178 179 result = self.vm.qmp('query-block') 180 self.assert_qmp(result, 'return[0]/inserted/file', test_img) 181 self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt) 182 self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img) 183 self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1) 184 self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img) 185 self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img) 186 187 result = self.vm.qmp('query-blockstats') 188 self.assert_qmp(result, 'return[0]/node-name', 'top') 189 self.assert_qmp(result, 'return[0]/backing/node-name', 'base') 190 191 self.cancel_and_wait(force=True) 192 result = self.vm.qmp('query-block') 193 self.assert_qmp(result, 'return[0]/inserted/file', test_img) 194 self.assert_qmp(result, 'return[0]/inserted/drv', iotests.imgfmt) 195 self.assert_qmp(result, 'return[0]/inserted/backing_file', backing_img) 196 self.assert_qmp(result, 'return[0]/inserted/backing_file_depth', 1) 197 self.assert_qmp(result, 'return[0]/inserted/image/filename', test_img) 198 self.assert_qmp(result, 'return[0]/inserted/image/backing-image/filename', backing_img) 199 200 result = self.vm.qmp('query-blockstats') 201 self.assert_qmp(result, 'return[0]/node-name', 'top') 202 self.assert_qmp(result, 'return[0]/backing/node-name', 'base') 203 204 self.vm.shutdown() 205 206 def test_medium_not_found(self): 207 if iotests.qemu_default_machine != 'pc': 208 return 209 210 result = self.vm.qmp(self.qmp_cmd, device='ide1-cd0', sync='full', 211 target=self.qmp_target) 212 self.assert_qmp(result, 'error/class', 'GenericError') 213 214 def test_image_not_found(self): 215 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full', 216 mode='existing', target=self.qmp_target) 217 self.assert_qmp(result, 'error/class', 'GenericError') 218 219 def test_device_not_found(self): 220 result = self.vm.qmp(self.qmp_cmd, device='nonexistent', sync='full', 221 target=self.qmp_target) 222 self.assert_qmp(result, 'error/class', 'GenericError') 223 224class TestSingleBlockdev(TestSingleDrive): 225 qmp_cmd = 'blockdev-mirror' 226 qmp_target = 'node1' 227 228 def setUp(self): 229 TestSingleDrive.setUp(self) 230 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img) 231 args = {'driver': iotests.imgfmt, 232 'node-name': self.qmp_target, 233 'file': { 'filename': target_img, 'driver': 'file' } } 234 result = self.vm.qmp("blockdev-add", **args) 235 self.assert_qmp(result, 'return', {}) 236 237 test_large_cluster = None 238 test_image_not_found = None 239 test_small_buffer2 = None 240 241class TestSingleDriveZeroLength(TestSingleDrive): 242 image_len = 0 243 test_small_buffer2 = None 244 test_large_cluster = None 245 246class TestSingleBlockdevZeroLength(TestSingleBlockdev): 247 image_len = 0 248 249class TestSingleDriveUnalignedLength(TestSingleDrive): 250 image_len = 1025 * 1024 251 test_small_buffer2 = None 252 test_large_cluster = None 253 254class TestSingleBlockdevUnalignedLength(TestSingleBlockdev): 255 image_len = 1025 * 1024 256 257class TestMirrorNoBacking(iotests.QMPTestCase): 258 image_len = 2 * 1024 * 1024 # MB 259 260 def setUp(self): 261 iotests.create_image(backing_img, TestMirrorNoBacking.image_len) 262 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 263 self.vm = iotests.VM().add_drive(test_img) 264 self.vm.launch() 265 266 def tearDown(self): 267 self.vm.shutdown() 268 os.remove(test_img) 269 os.remove(backing_img) 270 try: 271 os.remove(target_backing_img) 272 except: 273 pass 274 os.remove(target_img) 275 276 def test_complete(self): 277 self.assert_no_active_block_jobs() 278 279 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img) 280 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 281 mode='existing', target=target_img) 282 self.assert_qmp(result, 'return', {}) 283 284 self.complete_and_wait() 285 result = self.vm.qmp('query-block') 286 self.assert_qmp(result, 'return[0]/inserted/file', target_img) 287 self.vm.shutdown() 288 self.assertTrue(iotests.compare_images(test_img, target_img), 289 'target image does not match source after mirroring') 290 291 def test_cancel(self): 292 self.assert_no_active_block_jobs() 293 294 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img) 295 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 296 mode='existing', target=target_img) 297 self.assert_qmp(result, 'return', {}) 298 299 self.wait_ready_and_cancel() 300 result = self.vm.qmp('query-block') 301 self.assert_qmp(result, 'return[0]/inserted/file', test_img) 302 self.vm.shutdown() 303 self.assertTrue(iotests.compare_images(test_img, target_img), 304 'target image does not match source after mirroring') 305 306 def test_large_cluster(self): 307 self.assert_no_active_block_jobs() 308 309 # qemu-img create fails if the image is not there 310 qemu_img('create', '-f', iotests.imgfmt, '-o', 'size=%d' 311 %(TestMirrorNoBacking.image_len), target_backing_img) 312 qemu_img('create', '-f', iotests.imgfmt, '-o', 'cluster_size=%d,backing_file=%s' 313 % (TestMirrorNoBacking.image_len, target_backing_img), target_img) 314 315 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 316 mode='existing', target=target_img) 317 self.assert_qmp(result, 'return', {}) 318 319 self.complete_and_wait() 320 result = self.vm.qmp('query-block') 321 self.assert_qmp(result, 'return[0]/inserted/file', target_img) 322 self.vm.shutdown() 323 self.assertTrue(iotests.compare_images(test_img, target_img), 324 'target image does not match source after mirroring') 325 326class TestMirrorResized(iotests.QMPTestCase): 327 backing_len = 1 * 1024 * 1024 # MB 328 image_len = 2 * 1024 * 1024 # MB 329 330 def setUp(self): 331 iotests.create_image(backing_img, TestMirrorResized.backing_len) 332 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 333 qemu_img('resize', test_img, '2M') 334 self.vm = iotests.VM().add_drive(test_img) 335 self.vm.launch() 336 337 def tearDown(self): 338 self.vm.shutdown() 339 os.remove(test_img) 340 os.remove(backing_img) 341 try: 342 os.remove(target_img) 343 except OSError: 344 pass 345 346 def test_complete_top(self): 347 self.assert_no_active_block_jobs() 348 349 result = self.vm.qmp('drive-mirror', device='drive0', sync='top', 350 target=target_img) 351 self.assert_qmp(result, 'return', {}) 352 353 self.complete_and_wait() 354 result = self.vm.qmp('query-block') 355 self.assert_qmp(result, 'return[0]/inserted/file', target_img) 356 self.vm.shutdown() 357 self.assertTrue(iotests.compare_images(test_img, target_img), 358 'target image does not match source after mirroring') 359 360 def test_complete_full(self): 361 self.assert_no_active_block_jobs() 362 363 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 364 target=target_img) 365 self.assert_qmp(result, 'return', {}) 366 367 self.complete_and_wait() 368 result = self.vm.qmp('query-block') 369 self.assert_qmp(result, 'return[0]/inserted/file', target_img) 370 self.vm.shutdown() 371 self.assertTrue(iotests.compare_images(test_img, target_img), 372 'target image does not match source after mirroring') 373 374class TestReadErrors(iotests.QMPTestCase): 375 image_len = 2 * 1024 * 1024 # MB 376 377 # this should be a multiple of twice the default granularity 378 # so that we hit this offset first in state 1 379 MIRROR_GRANULARITY = 1024 * 1024 380 381 def create_blkdebug_file(self, name, event, errno): 382 file = open(name, 'w') 383 file.write(''' 384[inject-error] 385state = "1" 386event = "%s" 387errno = "%d" 388immediately = "off" 389once = "on" 390sector = "%d" 391 392[set-state] 393state = "1" 394event = "%s" 395new_state = "2" 396 397[set-state] 398state = "2" 399event = "%s" 400new_state = "1" 401''' % (event, errno, self.MIRROR_GRANULARITY / 512, event, event)) 402 file.close() 403 404 def setUp(self): 405 self.blkdebug_file = backing_img + ".blkdebug" 406 iotests.create_image(backing_img, TestReadErrors.image_len) 407 self.create_blkdebug_file(self.blkdebug_file, "read_aio", 5) 408 qemu_img('create', '-f', iotests.imgfmt, 409 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw' 410 % (self.blkdebug_file, backing_img), 411 test_img) 412 # Write something for tests that use sync='top' 413 qemu_io('-c', 'write %d 512' % (self.MIRROR_GRANULARITY + 65536), 414 test_img) 415 self.vm = iotests.VM().add_drive(test_img) 416 self.vm.launch() 417 418 def tearDown(self): 419 self.vm.shutdown() 420 os.remove(test_img) 421 os.remove(backing_img) 422 os.remove(self.blkdebug_file) 423 424 def test_report_read(self): 425 self.assert_no_active_block_jobs() 426 427 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 428 target=target_img) 429 self.assert_qmp(result, 'return', {}) 430 431 completed = False 432 error = False 433 while not completed: 434 for event in self.vm.get_qmp_events(wait=True): 435 if event['event'] == 'BLOCK_JOB_ERROR': 436 self.assert_qmp(event, 'data/device', 'drive0') 437 self.assert_qmp(event, 'data/operation', 'read') 438 error = True 439 elif event['event'] == 'BLOCK_JOB_READY': 440 self.assertTrue(False, 'job completed unexpectedly') 441 elif event['event'] == 'BLOCK_JOB_COMPLETED': 442 self.assertTrue(error, 'job completed unexpectedly') 443 self.assert_qmp(event, 'data/type', 'mirror') 444 self.assert_qmp(event, 'data/device', 'drive0') 445 self.assert_qmp(event, 'data/error', 'Input/output error') 446 completed = True 447 448 self.assert_no_active_block_jobs() 449 self.vm.shutdown() 450 451 def test_ignore_read(self): 452 self.assert_no_active_block_jobs() 453 454 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 455 target=target_img, on_source_error='ignore') 456 self.assert_qmp(result, 'return', {}) 457 458 event = self.vm.get_qmp_event(wait=True) 459 self.assertEquals(event['event'], 'BLOCK_JOB_ERROR') 460 self.assert_qmp(event, 'data/device', 'drive0') 461 self.assert_qmp(event, 'data/operation', 'read') 462 result = self.vm.qmp('query-block-jobs') 463 self.assert_qmp(result, 'return[0]/paused', False) 464 self.complete_and_wait() 465 self.vm.shutdown() 466 467 def test_large_cluster(self): 468 self.assert_no_active_block_jobs() 469 470 # Test COW into the target image. The first half of the 471 # cluster at MIRROR_GRANULARITY has to be copied from 472 # backing_img, even though sync='top'. 473 qemu_img('create', '-f', iotests.imgfmt, '-ocluster_size=131072,backing_file=%s' %(backing_img), target_img) 474 result = self.vm.qmp('drive-mirror', device='drive0', sync='top', 475 on_source_error='ignore', 476 mode='existing', target=target_img) 477 self.assert_qmp(result, 'return', {}) 478 479 event = self.vm.get_qmp_event(wait=True) 480 self.assertEquals(event['event'], 'BLOCK_JOB_ERROR') 481 self.assert_qmp(event, 'data/device', 'drive0') 482 self.assert_qmp(event, 'data/operation', 'read') 483 result = self.vm.qmp('query-block-jobs') 484 self.assert_qmp(result, 'return[0]/paused', False) 485 self.complete_and_wait() 486 self.vm.shutdown() 487 488 # Detach blkdebug to compare images successfully 489 qemu_img('rebase', '-f', iotests.imgfmt, '-u', '-b', backing_img, test_img) 490 self.assertTrue(iotests.compare_images(test_img, target_img), 491 'target image does not match source after mirroring') 492 493 def test_stop_read(self): 494 self.assert_no_active_block_jobs() 495 496 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 497 target=target_img, on_source_error='stop') 498 self.assert_qmp(result, 'return', {}) 499 500 error = False 501 ready = False 502 while not ready: 503 for event in self.vm.get_qmp_events(wait=True): 504 if event['event'] == 'BLOCK_JOB_ERROR': 505 self.assert_qmp(event, 'data/device', 'drive0') 506 self.assert_qmp(event, 'data/operation', 'read') 507 508 result = self.vm.qmp('query-block-jobs') 509 self.assert_qmp(result, 'return[0]/paused', True) 510 self.assert_qmp(result, 'return[0]/io-status', 'failed') 511 512 result = self.vm.qmp('block-job-resume', device='drive0') 513 self.assert_qmp(result, 'return', {}) 514 error = True 515 elif event['event'] == 'BLOCK_JOB_READY': 516 self.assertTrue(error, 'job completed unexpectedly') 517 self.assert_qmp(event, 'data/device', 'drive0') 518 ready = True 519 520 result = self.vm.qmp('query-block-jobs') 521 self.assert_qmp(result, 'return[0]/paused', False) 522 self.assert_qmp(result, 'return[0]/io-status', 'ok') 523 524 self.complete_and_wait(wait_ready=False) 525 self.assert_no_active_block_jobs() 526 self.vm.shutdown() 527 528class TestWriteErrors(iotests.QMPTestCase): 529 image_len = 2 * 1024 * 1024 # MB 530 531 # this should be a multiple of twice the default granularity 532 # so that we hit this offset first in state 1 533 MIRROR_GRANULARITY = 1024 * 1024 534 535 def create_blkdebug_file(self, name, event, errno): 536 file = open(name, 'w') 537 file.write(''' 538[inject-error] 539state = "1" 540event = "%s" 541errno = "%d" 542immediately = "off" 543once = "on" 544sector = "%d" 545 546[set-state] 547state = "1" 548event = "%s" 549new_state = "2" 550 551[set-state] 552state = "2" 553event = "%s" 554new_state = "1" 555''' % (event, errno, self.MIRROR_GRANULARITY / 512, event, event)) 556 file.close() 557 558 def setUp(self): 559 self.blkdebug_file = target_img + ".blkdebug" 560 iotests.create_image(backing_img, TestWriteErrors.image_len) 561 self.create_blkdebug_file(self.blkdebug_file, "write_aio", 5) 562 qemu_img('create', '-f', iotests.imgfmt, '-obacking_file=%s' %(backing_img), test_img) 563 self.vm = iotests.VM().add_drive(test_img) 564 self.target_img = 'blkdebug:%s:%s' % (self.blkdebug_file, target_img) 565 qemu_img('create', '-f', iotests.imgfmt, '-osize=%d' %(TestWriteErrors.image_len), target_img) 566 self.vm.launch() 567 568 def tearDown(self): 569 self.vm.shutdown() 570 os.remove(test_img) 571 os.remove(backing_img) 572 os.remove(self.blkdebug_file) 573 574 def test_report_write(self): 575 self.assert_no_active_block_jobs() 576 577 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 578 mode='existing', target=self.target_img) 579 self.assert_qmp(result, 'return', {}) 580 581 completed = False 582 error = False 583 while not completed: 584 for event in self.vm.get_qmp_events(wait=True): 585 if event['event'] == 'BLOCK_JOB_ERROR': 586 self.assert_qmp(event, 'data/device', 'drive0') 587 self.assert_qmp(event, 'data/operation', 'write') 588 error = True 589 elif event['event'] == 'BLOCK_JOB_READY': 590 self.assertTrue(False, 'job completed unexpectedly') 591 elif event['event'] == 'BLOCK_JOB_COMPLETED': 592 self.assertTrue(error, 'job completed unexpectedly') 593 self.assert_qmp(event, 'data/type', 'mirror') 594 self.assert_qmp(event, 'data/device', 'drive0') 595 self.assert_qmp(event, 'data/error', 'Input/output error') 596 completed = True 597 598 self.assert_no_active_block_jobs() 599 self.vm.shutdown() 600 601 def test_ignore_write(self): 602 self.assert_no_active_block_jobs() 603 604 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 605 mode='existing', target=self.target_img, 606 on_target_error='ignore') 607 self.assert_qmp(result, 'return', {}) 608 609 event = self.vm.get_qmp_event(wait=True) 610 self.assertEquals(event['event'], 'BLOCK_JOB_ERROR') 611 self.assert_qmp(event, 'data/device', 'drive0') 612 self.assert_qmp(event, 'data/operation', 'write') 613 result = self.vm.qmp('query-block-jobs') 614 self.assert_qmp(result, 'return[0]/paused', False) 615 self.complete_and_wait() 616 self.vm.shutdown() 617 618 def test_stop_write(self): 619 self.assert_no_active_block_jobs() 620 621 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 622 mode='existing', target=self.target_img, 623 on_target_error='stop') 624 self.assert_qmp(result, 'return', {}) 625 626 error = False 627 ready = False 628 while not ready: 629 for event in self.vm.get_qmp_events(wait=True): 630 if event['event'] == 'BLOCK_JOB_ERROR': 631 self.assert_qmp(event, 'data/device', 'drive0') 632 self.assert_qmp(event, 'data/operation', 'write') 633 634 result = self.vm.qmp('query-block-jobs') 635 self.assert_qmp(result, 'return[0]/paused', True) 636 self.assert_qmp(result, 'return[0]/io-status', 'failed') 637 638 result = self.vm.qmp('block-job-resume', device='drive0') 639 self.assert_qmp(result, 'return', {}) 640 641 result = self.vm.qmp('query-block-jobs') 642 self.assert_qmp(result, 'return[0]/paused', False) 643 self.assert_qmp(result, 'return[0]/io-status', 'ok') 644 error = True 645 elif event['event'] == 'BLOCK_JOB_READY': 646 self.assertTrue(error, 'job completed unexpectedly') 647 self.assert_qmp(event, 'data/device', 'drive0') 648 ready = True 649 650 self.complete_and_wait(wait_ready=False) 651 self.assert_no_active_block_jobs() 652 self.vm.shutdown() 653 654class TestSetSpeed(iotests.QMPTestCase): 655 image_len = 80 * 1024 * 1024 # MB 656 657 def setUp(self): 658 qemu_img('create', backing_img, str(TestSetSpeed.image_len)) 659 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 660 self.vm = iotests.VM().add_drive(test_img) 661 self.vm.launch() 662 663 def tearDown(self): 664 self.vm.shutdown() 665 os.remove(test_img) 666 os.remove(backing_img) 667 os.remove(target_img) 668 669 def test_set_speed(self): 670 self.assert_no_active_block_jobs() 671 672 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 673 target=target_img) 674 self.assert_qmp(result, 'return', {}) 675 676 # Default speed is 0 677 result = self.vm.qmp('query-block-jobs') 678 self.assert_qmp(result, 'return[0]/device', 'drive0') 679 self.assert_qmp(result, 'return[0]/speed', 0) 680 681 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024) 682 self.assert_qmp(result, 'return', {}) 683 684 # Ensure the speed we set was accepted 685 result = self.vm.qmp('query-block-jobs') 686 self.assert_qmp(result, 'return[0]/device', 'drive0') 687 self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024) 688 689 self.wait_ready_and_cancel() 690 691 # Check setting speed in drive-mirror works 692 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 693 target=target_img, speed=4*1024*1024) 694 self.assert_qmp(result, 'return', {}) 695 696 result = self.vm.qmp('query-block-jobs') 697 self.assert_qmp(result, 'return[0]/device', 'drive0') 698 self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024) 699 700 self.wait_ready_and_cancel() 701 702 def test_set_speed_invalid(self): 703 self.assert_no_active_block_jobs() 704 705 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 706 target=target_img, speed=-1) 707 self.assert_qmp(result, 'error/class', 'GenericError') 708 709 self.assert_no_active_block_jobs() 710 711 result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 712 target=target_img) 713 self.assert_qmp(result, 'return', {}) 714 715 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1) 716 self.assert_qmp(result, 'error/class', 'GenericError') 717 718 self.wait_ready_and_cancel() 719 720class TestUnbackedSource(iotests.QMPTestCase): 721 image_len = 2 * 1024 * 1024 # MB 722 723 def setUp(self): 724 qemu_img('create', '-f', iotests.imgfmt, test_img, 725 str(TestUnbackedSource.image_len)) 726 self.vm = iotests.VM().add_drive(test_img) 727 self.vm.launch() 728 729 def tearDown(self): 730 self.vm.shutdown() 731 os.remove(test_img) 732 os.remove(target_img) 733 734 def test_absolute_paths_full(self): 735 self.assert_no_active_block_jobs() 736 result = self.vm.qmp('drive-mirror', device='drive0', 737 sync='full', target=target_img, 738 mode='absolute-paths') 739 self.assert_qmp(result, 'return', {}) 740 self.complete_and_wait() 741 self.assert_no_active_block_jobs() 742 743 def test_absolute_paths_top(self): 744 self.assert_no_active_block_jobs() 745 result = self.vm.qmp('drive-mirror', device='drive0', 746 sync='top', target=target_img, 747 mode='absolute-paths') 748 self.assert_qmp(result, 'return', {}) 749 self.complete_and_wait() 750 self.assert_no_active_block_jobs() 751 752 def test_absolute_paths_none(self): 753 self.assert_no_active_block_jobs() 754 result = self.vm.qmp('drive-mirror', device='drive0', 755 sync='none', target=target_img, 756 mode='absolute-paths') 757 self.assert_qmp(result, 'return', {}) 758 self.complete_and_wait() 759 self.assert_no_active_block_jobs() 760 761class TestGranularity(iotests.QMPTestCase): 762 image_len = 10 * 1024 * 1024 # MB 763 764 def setUp(self): 765 qemu_img('create', '-f', iotests.imgfmt, test_img, 766 str(TestGranularity.image_len)) 767 qemu_io('-c', 'write 0 %d' % (self.image_len), 768 test_img) 769 self.vm = iotests.VM().add_drive(test_img) 770 self.vm.launch() 771 772 def tearDown(self): 773 self.vm.shutdown() 774 self.assertTrue(iotests.compare_images(test_img, target_img), 775 'target image does not match source after mirroring') 776 os.remove(test_img) 777 os.remove(target_img) 778 779 def test_granularity(self): 780 self.assert_no_active_block_jobs() 781 result = self.vm.qmp('drive-mirror', device='drive0', 782 sync='full', target=target_img, 783 mode='absolute-paths', granularity=8192) 784 self.assert_qmp(result, 'return', {}) 785 event = self.vm.get_qmp_event(wait=60.0) 786 # Failures will manifest as COMPLETED/ERROR. 787 self.assert_qmp(event, 'event', 'BLOCK_JOB_READY') 788 self.complete_and_wait(drive='drive0', wait_ready=False) 789 self.assert_no_active_block_jobs() 790 791class TestRepairQuorum(iotests.QMPTestCase): 792 """ This class test quorum file repair using drive-mirror. 793 It's mostly a fork of TestSingleDrive """ 794 image_len = 1 * 1024 * 1024 # MB 795 IMAGES = [ quorum_img1, quorum_img2, quorum_img3 ] 796 797 def setUp(self): 798 self.vm = iotests.VM() 799 800 if iotests.qemu_default_machine == 'pc': 801 self.vm.add_drive(None, 'media=cdrom', 'ide') 802 803 # Add each individual quorum images 804 for i in self.IMAGES: 805 qemu_img('create', '-f', iotests.imgfmt, i, 806 str(TestSingleDrive.image_len)) 807 # Assign a node name to each quorum image in order to manipulate 808 # them 809 opts = "node-name=img%i" % self.IMAGES.index(i) 810 self.vm = self.vm.add_drive(i, opts) 811 812 self.vm.launch() 813 814 #assemble the quorum block device from the individual files 815 args = { "driver": "quorum", "node-name": "quorum0", 816 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] } 817 if iotests.supports_quorum(): 818 result = self.vm.qmp("blockdev-add", **args) 819 self.assert_qmp(result, 'return', {}) 820 821 822 def tearDown(self): 823 self.vm.shutdown() 824 for i in self.IMAGES + [ quorum_repair_img ]: 825 # Do a try/except because the test may have deleted some images 826 try: 827 os.remove(i) 828 except OSError: 829 pass 830 831 def test_complete(self): 832 if not iotests.supports_quorum(): 833 return 834 835 self.assert_no_active_block_jobs() 836 837 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 838 sync='full', node_name="repair0", replaces="img1", 839 target=quorum_repair_img, format=iotests.imgfmt) 840 self.assert_qmp(result, 'return', {}) 841 842 self.complete_and_wait(drive="job0") 843 self.assert_has_block_node("repair0", quorum_repair_img) 844 # TODO: a better test requiring some QEMU infrastructure will be added 845 # to check that this file is really driven by quorum 846 self.vm.shutdown() 847 self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img), 848 'target image does not match source after mirroring') 849 850 def test_cancel(self): 851 if not iotests.supports_quorum(): 852 return 853 854 self.assert_no_active_block_jobs() 855 856 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 857 sync='full', node_name="repair0", replaces="img1", 858 target=quorum_repair_img, format=iotests.imgfmt) 859 self.assert_qmp(result, 'return', {}) 860 861 self.cancel_and_wait(drive="job0", force=True) 862 # here we check that the last registered quorum file has not been 863 # swapped out and unref 864 self.assert_has_block_node(None, quorum_img3) 865 self.vm.shutdown() 866 867 def test_cancel_after_ready(self): 868 if not iotests.supports_quorum(): 869 return 870 871 self.assert_no_active_block_jobs() 872 873 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 874 sync='full', node_name="repair0", replaces="img1", 875 target=quorum_repair_img, format=iotests.imgfmt) 876 self.assert_qmp(result, 'return', {}) 877 878 self.wait_ready_and_cancel(drive="job0") 879 # here we check that the last registered quorum file has not been 880 # swapped out and unref 881 self.assert_has_block_node(None, quorum_img3) 882 self.vm.shutdown() 883 self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img), 884 'target image does not match source after mirroring') 885 886 def test_pause(self): 887 if not iotests.supports_quorum(): 888 return 889 890 self.assert_no_active_block_jobs() 891 892 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 893 sync='full', node_name="repair0", replaces="img1", 894 target=quorum_repair_img, format=iotests.imgfmt) 895 self.assert_qmp(result, 'return', {}) 896 897 self.pause_job('job0') 898 899 result = self.vm.qmp('query-block-jobs') 900 offset = self.dictpath(result, 'return[0]/offset') 901 902 time.sleep(0.5) 903 result = self.vm.qmp('query-block-jobs') 904 self.assert_qmp(result, 'return[0]/offset', offset) 905 906 result = self.vm.qmp('block-job-resume', device='job0') 907 self.assert_qmp(result, 'return', {}) 908 909 self.complete_and_wait(drive="job0") 910 self.vm.shutdown() 911 self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img), 912 'target image does not match source after mirroring') 913 914 def test_medium_not_found(self): 915 if not iotests.supports_quorum(): 916 return 917 918 if iotests.qemu_default_machine != 'pc': 919 return 920 921 result = self.vm.qmp('drive-mirror', job_id='job0', device='drive0', # CD-ROM 922 sync='full', 923 node_name='repair0', 924 replaces='img1', 925 target=quorum_repair_img, format=iotests.imgfmt) 926 self.assert_qmp(result, 'error/class', 'GenericError') 927 928 def test_image_not_found(self): 929 if not iotests.supports_quorum(): 930 return 931 932 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 933 sync='full', node_name='repair0', replaces='img1', 934 mode='existing', target=quorum_repair_img, 935 format=iotests.imgfmt) 936 self.assert_qmp(result, 'error/class', 'GenericError') 937 938 def test_device_not_found(self): 939 if not iotests.supports_quorum(): 940 return 941 942 result = self.vm.qmp('drive-mirror', job_id='job0', 943 device='nonexistent', sync='full', 944 node_name='repair0', 945 replaces='img1', 946 target=quorum_repair_img, format=iotests.imgfmt) 947 self.assert_qmp(result, 'error/class', 'GenericError') 948 949 def test_wrong_sync_mode(self): 950 if not iotests.supports_quorum(): 951 return 952 953 result = self.vm.qmp('drive-mirror', device='quorum0', job_id='job0', 954 node_name='repair0', 955 replaces='img1', 956 target=quorum_repair_img, format=iotests.imgfmt) 957 self.assert_qmp(result, 'error/class', 'GenericError') 958 959 def test_no_node_name(self): 960 if not iotests.supports_quorum(): 961 return 962 963 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 964 sync='full', replaces='img1', 965 target=quorum_repair_img, format=iotests.imgfmt) 966 self.assert_qmp(result, 'error/class', 'GenericError') 967 968 def test_nonexistent_replaces(self): 969 if not iotests.supports_quorum(): 970 return 971 972 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 973 sync='full', node_name='repair0', replaces='img77', 974 target=quorum_repair_img, format=iotests.imgfmt) 975 self.assert_qmp(result, 'error/class', 'GenericError') 976 977 def test_after_a_quorum_snapshot(self): 978 if not iotests.supports_quorum(): 979 return 980 981 result = self.vm.qmp('blockdev-snapshot-sync', node_name='img1', 982 snapshot_file=quorum_snapshot_file, 983 snapshot_node_name="snap1"); 984 985 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 986 sync='full', node_name='repair0', replaces="img1", 987 target=quorum_repair_img, format=iotests.imgfmt) 988 self.assert_qmp(result, 'error/class', 'GenericError') 989 990 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0', 991 sync='full', node_name='repair0', replaces="snap1", 992 target=quorum_repair_img, format=iotests.imgfmt) 993 self.assert_qmp(result, 'return', {}) 994 995 self.complete_and_wait('job0') 996 self.assert_has_block_node("repair0", quorum_repair_img) 997 # TODO: a better test requiring some QEMU infrastructure will be added 998 # to check that this file is really driven by quorum 999 self.vm.shutdown() 1000 1001# Test mirroring with a source that does not have any parents (not even a 1002# BlockBackend) 1003class TestOrphanedSource(iotests.QMPTestCase): 1004 def setUp(self): 1005 blk0 = { 'node-name': 'src', 1006 'driver': 'null-co' } 1007 1008 blk1 = { 'node-name': 'dest', 1009 'driver': 'null-co' } 1010 1011 blk2 = { 'node-name': 'dest-ro', 1012 'driver': 'null-co', 1013 'read-only': 'on' } 1014 1015 self.vm = iotests.VM() 1016 self.vm.add_blockdev(self.qmp_to_opts(blk0)) 1017 self.vm.add_blockdev(self.qmp_to_opts(blk1)) 1018 self.vm.add_blockdev(self.qmp_to_opts(blk2)) 1019 self.vm.launch() 1020 1021 def tearDown(self): 1022 self.vm.shutdown() 1023 1024 def test_no_job_id(self): 1025 self.assert_no_active_block_jobs() 1026 1027 result = self.vm.qmp('blockdev-mirror', device='src', sync='full', 1028 target='dest') 1029 self.assert_qmp(result, 'error/class', 'GenericError') 1030 1031 def test_success(self): 1032 self.assert_no_active_block_jobs() 1033 1034 result = self.vm.qmp('blockdev-mirror', job_id='job', device='src', 1035 sync='full', target='dest') 1036 self.assert_qmp(result, 'return', {}) 1037 1038 self.complete_and_wait('job') 1039 1040 def test_failing_permissions(self): 1041 self.assert_no_active_block_jobs() 1042 1043 result = self.vm.qmp('blockdev-mirror', device='src', sync='full', 1044 target='dest-ro') 1045 self.assert_qmp(result, 'error/class', 'GenericError') 1046 1047if __name__ == '__main__': 1048 iotests.main(supported_fmts=['qcow2', 'qed']) 1049