iotests: Make 136 less flaky
[qemu/ar7.git] / tests / qemu-iotests / 041
bloba860a31e9a666556fcf71ed5fcb81c8506d6a1c3
1 #!/usr/bin/env python
3 # Tests for image mirroring.
5 # Copyright (C) 2012 Red Hat, Inc.
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.
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.
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/>.
21 import time
22 import os
23 import iotests
24 from iotests import qemu_img, qemu_io
26 backing_img = os.path.join(iotests.test_dir, 'backing.img')
27 target_backing_img = os.path.join(iotests.test_dir, 'target-backing.img')
28 test_img = os.path.join(iotests.test_dir, 'test.img')
29 target_img = os.path.join(iotests.test_dir, 'target.img')
31 quorum_img1 = os.path.join(iotests.test_dir, 'quorum1.img')
32 quorum_img2 = os.path.join(iotests.test_dir, 'quorum2.img')
33 quorum_img3 = os.path.join(iotests.test_dir, 'quorum3.img')
34 quorum_repair_img = os.path.join(iotests.test_dir, 'quorum_repair.img')
35 quorum_snapshot_file = os.path.join(iotests.test_dir, 'quorum_snapshot.img')
37 class TestSingleDrive(iotests.QMPTestCase):
38 image_len = 1 * 1024 * 1024 # MB
39 qmp_cmd = 'drive-mirror'
40 qmp_target = target_img
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()
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
59 def test_complete(self):
60 self.assert_no_active_block_jobs()
62 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
63 target=self.qmp_target)
64 self.assert_qmp(result, 'return', {})
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')
73 def test_cancel(self):
74 self.assert_no_active_block_jobs()
76 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
77 target=self.qmp_target)
78 self.assert_qmp(result, 'return', {})
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()
85 def test_cancel_after_ready(self):
86 self.assert_no_active_block_jobs()
88 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
89 target=self.qmp_target)
90 self.assert_qmp(result, 'return', {})
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')
99 def test_pause(self):
100 self.assert_no_active_block_jobs()
102 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
103 target=self.qmp_target)
104 self.assert_qmp(result, 'return', {})
106 self.pause_job('drive0')
108 result = self.vm.qmp('query-block-jobs')
109 offset = self.dictpath(result, 'return[0]/offset')
111 time.sleep(0.5)
112 result = self.vm.qmp('query-block-jobs')
113 self.assert_qmp(result, 'return[0]/offset', offset)
115 result = self.vm.qmp('block-job-resume', device='drive0')
116 self.assert_qmp(result, 'return', {})
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')
123 def test_small_buffer(self):
124 self.assert_no_active_block_jobs()
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', {})
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')
138 def test_small_buffer2(self):
139 self.assert_no_active_block_jobs()
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', {})
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')
154 def test_large_cluster(self):
155 self.assert_no_active_block_jobs()
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', {})
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')
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()
175 result = self.vm.qmp(self.qmp_cmd, device='drive0', sync='full',
176 target=self.qmp_target)
177 self.assert_qmp(result, 'return', {})
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)
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')
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)
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')
204 self.vm.shutdown()
206 def test_medium_not_found(self):
207 if iotests.qemu_default_machine != 'pc':
208 return
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')
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')
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')
224 class TestSingleBlockdev(TestSingleDrive):
225 qmp_cmd = 'blockdev-mirror'
226 qmp_target = 'node1'
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', {})
237 test_large_cluster = None
238 test_image_not_found = None
239 test_small_buffer2 = None
241 class TestSingleDriveZeroLength(TestSingleDrive):
242 image_len = 0
243 test_small_buffer2 = None
244 test_large_cluster = None
246 class TestSingleBlockdevZeroLength(TestSingleBlockdev):
247 image_len = 0
249 class TestSingleDriveUnalignedLength(TestSingleDrive):
250 image_len = 1025 * 1024
251 test_small_buffer2 = None
252 test_large_cluster = None
254 class TestSingleBlockdevUnalignedLength(TestSingleBlockdev):
255 image_len = 1025 * 1024
257 class TestMirrorNoBacking(iotests.QMPTestCase):
258 image_len = 2 * 1024 * 1024 # MB
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()
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)
276 def test_complete(self):
277 self.assert_no_active_block_jobs()
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', {})
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')
291 def test_cancel(self):
292 self.assert_no_active_block_jobs()
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', {})
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')
306 def test_large_cluster(self):
307 self.assert_no_active_block_jobs()
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)
315 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
316 mode='existing', target=target_img)
317 self.assert_qmp(result, 'return', {})
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')
326 class TestMirrorResized(iotests.QMPTestCase):
327 backing_len = 1 * 1024 * 1024 # MB
328 image_len = 2 * 1024 * 1024 # MB
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()
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
346 def test_complete_top(self):
347 self.assert_no_active_block_jobs()
349 result = self.vm.qmp('drive-mirror', device='drive0', sync='top',
350 target=target_img)
351 self.assert_qmp(result, 'return', {})
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')
360 def test_complete_full(self):
361 self.assert_no_active_block_jobs()
363 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
364 target=target_img)
365 self.assert_qmp(result, 'return', {})
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')
374 class TestReadErrors(iotests.QMPTestCase):
375 image_len = 2 * 1024 * 1024 # MB
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
381 def create_blkdebug_file(self, name, event, errno):
382 file = open(name, 'w')
383 file.write('''
384 [inject-error]
385 state = "1"
386 event = "%s"
387 errno = "%d"
388 immediately = "off"
389 once = "on"
390 sector = "%d"
392 [set-state]
393 state = "1"
394 event = "%s"
395 new_state = "2"
397 [set-state]
398 state = "2"
399 event = "%s"
400 new_state = "1"
401 ''' % (event, errno, self.MIRROR_GRANULARITY / 512, event, event))
402 file.close()
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()
418 def tearDown(self):
419 self.vm.shutdown()
420 os.remove(test_img)
421 os.remove(target_img)
422 os.remove(backing_img)
423 os.remove(self.blkdebug_file)
425 def test_report_read(self):
426 self.assert_no_active_block_jobs()
428 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
429 target=target_img)
430 self.assert_qmp(result, 'return', {})
432 completed = False
433 error = False
434 while not completed:
435 for event in self.vm.get_qmp_events(wait=True):
436 if event['event'] == 'BLOCK_JOB_ERROR':
437 self.assert_qmp(event, 'data/device', 'drive0')
438 self.assert_qmp(event, 'data/operation', 'read')
439 error = True
440 elif event['event'] == 'BLOCK_JOB_READY':
441 self.assertTrue(False, 'job completed unexpectedly')
442 elif event['event'] == 'BLOCK_JOB_COMPLETED':
443 self.assertTrue(error, 'job completed unexpectedly')
444 self.assert_qmp(event, 'data/type', 'mirror')
445 self.assert_qmp(event, 'data/device', 'drive0')
446 self.assert_qmp(event, 'data/error', 'Input/output error')
447 completed = True
449 self.assert_no_active_block_jobs()
450 self.vm.shutdown()
452 def test_ignore_read(self):
453 self.assert_no_active_block_jobs()
455 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
456 target=target_img, on_source_error='ignore')
457 self.assert_qmp(result, 'return', {})
459 event = self.vm.get_qmp_event(wait=True)
460 self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
461 self.assert_qmp(event, 'data/device', 'drive0')
462 self.assert_qmp(event, 'data/operation', 'read')
463 result = self.vm.qmp('query-block-jobs')
464 self.assert_qmp(result, 'return[0]/paused', False)
465 self.complete_and_wait()
466 self.vm.shutdown()
468 def test_large_cluster(self):
469 self.assert_no_active_block_jobs()
471 # Test COW into the target image. The first half of the
472 # cluster at MIRROR_GRANULARITY has to be copied from
473 # backing_img, even though sync='top'.
474 qemu_img('create', '-f', iotests.imgfmt, '-ocluster_size=131072,backing_file=%s' %(backing_img), target_img)
475 result = self.vm.qmp('drive-mirror', device='drive0', sync='top',
476 on_source_error='ignore',
477 mode='existing', target=target_img)
478 self.assert_qmp(result, 'return', {})
480 event = self.vm.get_qmp_event(wait=True)
481 self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
482 self.assert_qmp(event, 'data/device', 'drive0')
483 self.assert_qmp(event, 'data/operation', 'read')
484 result = self.vm.qmp('query-block-jobs')
485 self.assert_qmp(result, 'return[0]/paused', False)
486 self.complete_and_wait()
487 self.vm.shutdown()
489 # Detach blkdebug to compare images successfully
490 qemu_img('rebase', '-f', iotests.imgfmt, '-u', '-b', backing_img, test_img)
491 self.assertTrue(iotests.compare_images(test_img, target_img),
492 'target image does not match source after mirroring')
494 def test_stop_read(self):
495 self.assert_no_active_block_jobs()
497 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
498 target=target_img, on_source_error='stop')
499 self.assert_qmp(result, 'return', {})
501 error = False
502 ready = False
503 while not ready:
504 for event in self.vm.get_qmp_events(wait=True):
505 if event['event'] == 'BLOCK_JOB_ERROR':
506 self.assert_qmp(event, 'data/device', 'drive0')
507 self.assert_qmp(event, 'data/operation', 'read')
509 result = self.vm.qmp('query-block-jobs')
510 self.assert_qmp(result, 'return[0]/paused', True)
511 self.assert_qmp(result, 'return[0]/io-status', 'failed')
513 result = self.vm.qmp('block-job-resume', device='drive0')
514 self.assert_qmp(result, 'return', {})
515 error = True
516 elif event['event'] == 'BLOCK_JOB_READY':
517 self.assertTrue(error, 'job completed unexpectedly')
518 self.assert_qmp(event, 'data/device', 'drive0')
519 ready = True
521 result = self.vm.qmp('query-block-jobs')
522 self.assert_qmp(result, 'return[0]/paused', False)
523 self.assert_qmp(result, 'return[0]/io-status', 'ok')
525 self.complete_and_wait(wait_ready=False)
526 self.assert_no_active_block_jobs()
527 self.vm.shutdown()
529 class TestWriteErrors(iotests.QMPTestCase):
530 image_len = 2 * 1024 * 1024 # MB
532 # this should be a multiple of twice the default granularity
533 # so that we hit this offset first in state 1
534 MIRROR_GRANULARITY = 1024 * 1024
536 def create_blkdebug_file(self, name, event, errno):
537 file = open(name, 'w')
538 file.write('''
539 [inject-error]
540 state = "1"
541 event = "%s"
542 errno = "%d"
543 immediately = "off"
544 once = "on"
545 sector = "%d"
547 [set-state]
548 state = "1"
549 event = "%s"
550 new_state = "2"
552 [set-state]
553 state = "2"
554 event = "%s"
555 new_state = "1"
556 ''' % (event, errno, self.MIRROR_GRANULARITY / 512, event, event))
557 file.close()
559 def setUp(self):
560 self.blkdebug_file = target_img + ".blkdebug"
561 iotests.create_image(backing_img, TestWriteErrors.image_len)
562 self.create_blkdebug_file(self.blkdebug_file, "write_aio", 5)
563 qemu_img('create', '-f', iotests.imgfmt, '-obacking_file=%s' %(backing_img), test_img)
564 self.vm = iotests.VM().add_drive(test_img)
565 self.target_img = 'blkdebug:%s:%s' % (self.blkdebug_file, target_img)
566 qemu_img('create', '-f', iotests.imgfmt, '-osize=%d' %(TestWriteErrors.image_len), target_img)
567 self.vm.launch()
569 def tearDown(self):
570 self.vm.shutdown()
571 os.remove(test_img)
572 os.remove(target_img)
573 os.remove(backing_img)
574 os.remove(self.blkdebug_file)
576 def test_report_write(self):
577 self.assert_no_active_block_jobs()
579 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
580 mode='existing', target=self.target_img)
581 self.assert_qmp(result, 'return', {})
583 completed = False
584 error = False
585 while not completed:
586 for event in self.vm.get_qmp_events(wait=True):
587 if event['event'] == 'BLOCK_JOB_ERROR':
588 self.assert_qmp(event, 'data/device', 'drive0')
589 self.assert_qmp(event, 'data/operation', 'write')
590 error = True
591 elif event['event'] == 'BLOCK_JOB_READY':
592 self.assertTrue(False, 'job completed unexpectedly')
593 elif event['event'] == 'BLOCK_JOB_COMPLETED':
594 self.assertTrue(error, 'job completed unexpectedly')
595 self.assert_qmp(event, 'data/type', 'mirror')
596 self.assert_qmp(event, 'data/device', 'drive0')
597 self.assert_qmp(event, 'data/error', 'Input/output error')
598 completed = True
600 self.assert_no_active_block_jobs()
601 self.vm.shutdown()
603 def test_ignore_write(self):
604 self.assert_no_active_block_jobs()
606 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
607 mode='existing', target=self.target_img,
608 on_target_error='ignore')
609 self.assert_qmp(result, 'return', {})
611 event = self.vm.get_qmp_event(wait=True)
612 self.assertEquals(event['event'], 'BLOCK_JOB_ERROR')
613 self.assert_qmp(event, 'data/device', 'drive0')
614 self.assert_qmp(event, 'data/operation', 'write')
615 result = self.vm.qmp('query-block-jobs')
616 self.assert_qmp(result, 'return[0]/paused', False)
617 self.complete_and_wait()
618 self.vm.shutdown()
620 def test_stop_write(self):
621 self.assert_no_active_block_jobs()
623 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
624 mode='existing', target=self.target_img,
625 on_target_error='stop')
626 self.assert_qmp(result, 'return', {})
628 error = False
629 ready = False
630 while not ready:
631 for event in self.vm.get_qmp_events(wait=True):
632 if event['event'] == 'BLOCK_JOB_ERROR':
633 self.assert_qmp(event, 'data/device', 'drive0')
634 self.assert_qmp(event, 'data/operation', 'write')
636 result = self.vm.qmp('query-block-jobs')
637 self.assert_qmp(result, 'return[0]/paused', True)
638 self.assert_qmp(result, 'return[0]/io-status', 'failed')
640 result = self.vm.qmp('block-job-resume', device='drive0')
641 self.assert_qmp(result, 'return', {})
643 result = self.vm.qmp('query-block-jobs')
644 self.assert_qmp(result, 'return[0]/paused', False)
645 self.assert_qmp(result, 'return[0]/io-status', 'ok')
646 error = True
647 elif event['event'] == 'BLOCK_JOB_READY':
648 self.assertTrue(error, 'job completed unexpectedly')
649 self.assert_qmp(event, 'data/device', 'drive0')
650 ready = True
652 self.complete_and_wait(wait_ready=False)
653 self.assert_no_active_block_jobs()
654 self.vm.shutdown()
656 class TestSetSpeed(iotests.QMPTestCase):
657 image_len = 80 * 1024 * 1024 # MB
659 def setUp(self):
660 qemu_img('create', backing_img, str(TestSetSpeed.image_len))
661 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
662 self.vm = iotests.VM().add_drive(test_img)
663 self.vm.launch()
665 def tearDown(self):
666 self.vm.shutdown()
667 os.remove(test_img)
668 os.remove(backing_img)
669 os.remove(target_img)
671 def test_set_speed(self):
672 self.assert_no_active_block_jobs()
674 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
675 target=target_img)
676 self.assert_qmp(result, 'return', {})
678 # Default speed is 0
679 result = self.vm.qmp('query-block-jobs')
680 self.assert_qmp(result, 'return[0]/device', 'drive0')
681 self.assert_qmp(result, 'return[0]/speed', 0)
683 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
684 self.assert_qmp(result, 'return', {})
686 # Ensure the speed we set was accepted
687 result = self.vm.qmp('query-block-jobs')
688 self.assert_qmp(result, 'return[0]/device', 'drive0')
689 self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
691 self.wait_ready_and_cancel()
693 # Check setting speed in drive-mirror works
694 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
695 target=target_img, speed=4*1024*1024)
696 self.assert_qmp(result, 'return', {})
698 result = self.vm.qmp('query-block-jobs')
699 self.assert_qmp(result, 'return[0]/device', 'drive0')
700 self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
702 self.wait_ready_and_cancel()
704 def test_set_speed_invalid(self):
705 self.assert_no_active_block_jobs()
707 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
708 target=target_img, speed=-1)
709 self.assert_qmp(result, 'error/class', 'GenericError')
711 self.assert_no_active_block_jobs()
713 result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
714 target=target_img)
715 self.assert_qmp(result, 'return', {})
717 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
718 self.assert_qmp(result, 'error/class', 'GenericError')
720 self.wait_ready_and_cancel()
722 class TestUnbackedSource(iotests.QMPTestCase):
723 image_len = 2 * 1024 * 1024 # MB
725 def setUp(self):
726 qemu_img('create', '-f', iotests.imgfmt, test_img,
727 str(TestUnbackedSource.image_len))
728 self.vm = iotests.VM().add_drive(test_img)
729 self.vm.launch()
731 def tearDown(self):
732 self.vm.shutdown()
733 os.remove(test_img)
734 os.remove(target_img)
736 def test_absolute_paths_full(self):
737 self.assert_no_active_block_jobs()
738 result = self.vm.qmp('drive-mirror', device='drive0',
739 sync='full', target=target_img,
740 mode='absolute-paths')
741 self.assert_qmp(result, 'return', {})
742 self.complete_and_wait()
743 self.assert_no_active_block_jobs()
745 def test_absolute_paths_top(self):
746 self.assert_no_active_block_jobs()
747 result = self.vm.qmp('drive-mirror', device='drive0',
748 sync='top', target=target_img,
749 mode='absolute-paths')
750 self.assert_qmp(result, 'return', {})
751 self.complete_and_wait()
752 self.assert_no_active_block_jobs()
754 def test_absolute_paths_none(self):
755 self.assert_no_active_block_jobs()
756 result = self.vm.qmp('drive-mirror', device='drive0',
757 sync='none', target=target_img,
758 mode='absolute-paths')
759 self.assert_qmp(result, 'return', {})
760 self.complete_and_wait()
761 self.assert_no_active_block_jobs()
763 class TestGranularity(iotests.QMPTestCase):
764 image_len = 10 * 1024 * 1024 # MB
766 def setUp(self):
767 qemu_img('create', '-f', iotests.imgfmt, test_img,
768 str(TestGranularity.image_len))
769 qemu_io('-c', 'write 0 %d' % (self.image_len),
770 test_img)
771 self.vm = iotests.VM().add_drive(test_img)
772 self.vm.launch()
774 def tearDown(self):
775 self.vm.shutdown()
776 self.assertTrue(iotests.compare_images(test_img, target_img),
777 'target image does not match source after mirroring')
778 os.remove(test_img)
779 os.remove(target_img)
781 def test_granularity(self):
782 self.assert_no_active_block_jobs()
783 result = self.vm.qmp('drive-mirror', device='drive0',
784 sync='full', target=target_img,
785 mode='absolute-paths', granularity=8192)
786 self.assert_qmp(result, 'return', {})
787 event = self.vm.get_qmp_event(wait=60.0)
788 # Failures will manifest as COMPLETED/ERROR.
789 self.assert_qmp(event, 'event', 'BLOCK_JOB_READY')
790 self.complete_and_wait(drive='drive0', wait_ready=False)
791 self.assert_no_active_block_jobs()
793 class TestRepairQuorum(iotests.QMPTestCase):
794 """ This class test quorum file repair using drive-mirror.
795 It's mostly a fork of TestSingleDrive """
796 image_len = 1 * 1024 * 1024 # MB
797 IMAGES = [ quorum_img1, quorum_img2, quorum_img3 ]
799 def setUp(self):
800 self.vm = iotests.VM()
802 if iotests.qemu_default_machine == 'pc':
803 self.vm.add_drive(None, 'media=cdrom', 'ide')
805 # Add each individual quorum images
806 for i in self.IMAGES:
807 qemu_img('create', '-f', iotests.imgfmt, i,
808 str(TestSingleDrive.image_len))
809 # Assign a node name to each quorum image in order to manipulate
810 # them
811 opts = "node-name=img%i" % self.IMAGES.index(i)
812 self.vm = self.vm.add_drive(i, opts)
814 self.vm.launch()
816 #assemble the quorum block device from the individual files
817 args = { "driver": "quorum", "node-name": "quorum0",
818 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] }
819 if iotests.supports_quorum():
820 result = self.vm.qmp("blockdev-add", **args)
821 self.assert_qmp(result, 'return', {})
824 def tearDown(self):
825 self.vm.shutdown()
826 for i in self.IMAGES + [ quorum_repair_img, quorum_snapshot_file ]:
827 # Do a try/except because the test may have deleted some images
828 try:
829 os.remove(i)
830 except OSError:
831 pass
833 def test_complete(self):
834 if not iotests.supports_quorum():
835 return
837 self.assert_no_active_block_jobs()
839 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
840 sync='full', node_name="repair0", replaces="img1",
841 target=quorum_repair_img, format=iotests.imgfmt)
842 self.assert_qmp(result, 'return', {})
844 self.complete_and_wait(drive="job0")
845 self.assert_has_block_node("repair0", quorum_repair_img)
846 # TODO: a better test requiring some QEMU infrastructure will be added
847 # to check that this file is really driven by quorum
848 self.vm.shutdown()
849 self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
850 'target image does not match source after mirroring')
852 def test_cancel(self):
853 if not iotests.supports_quorum():
854 return
856 self.assert_no_active_block_jobs()
858 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
859 sync='full', node_name="repair0", replaces="img1",
860 target=quorum_repair_img, format=iotests.imgfmt)
861 self.assert_qmp(result, 'return', {})
863 self.cancel_and_wait(drive="job0", force=True)
864 # here we check that the last registered quorum file has not been
865 # swapped out and unref
866 self.assert_has_block_node(None, quorum_img3)
867 self.vm.shutdown()
869 def test_cancel_after_ready(self):
870 if not iotests.supports_quorum():
871 return
873 self.assert_no_active_block_jobs()
875 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
876 sync='full', node_name="repair0", replaces="img1",
877 target=quorum_repair_img, format=iotests.imgfmt)
878 self.assert_qmp(result, 'return', {})
880 self.wait_ready_and_cancel(drive="job0")
881 # here we check that the last registered quorum file has not been
882 # swapped out and unref
883 self.assert_has_block_node(None, quorum_img3)
884 self.vm.shutdown()
885 self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
886 'target image does not match source after mirroring')
888 def test_pause(self):
889 if not iotests.supports_quorum():
890 return
892 self.assert_no_active_block_jobs()
894 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
895 sync='full', node_name="repair0", replaces="img1",
896 target=quorum_repair_img, format=iotests.imgfmt)
897 self.assert_qmp(result, 'return', {})
899 self.pause_job('job0')
901 result = self.vm.qmp('query-block-jobs')
902 offset = self.dictpath(result, 'return[0]/offset')
904 time.sleep(0.5)
905 result = self.vm.qmp('query-block-jobs')
906 self.assert_qmp(result, 'return[0]/offset', offset)
908 result = self.vm.qmp('block-job-resume', device='job0')
909 self.assert_qmp(result, 'return', {})
911 self.complete_and_wait(drive="job0")
912 self.vm.shutdown()
913 self.assertTrue(iotests.compare_images(quorum_img2, quorum_repair_img),
914 'target image does not match source after mirroring')
916 def test_medium_not_found(self):
917 if not iotests.supports_quorum():
918 return
920 if iotests.qemu_default_machine != 'pc':
921 return
923 result = self.vm.qmp('drive-mirror', job_id='job0', device='drive0', # CD-ROM
924 sync='full',
925 node_name='repair0',
926 replaces='img1',
927 target=quorum_repair_img, format=iotests.imgfmt)
928 self.assert_qmp(result, 'error/class', 'GenericError')
930 def test_image_not_found(self):
931 if not iotests.supports_quorum():
932 return
934 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
935 sync='full', node_name='repair0', replaces='img1',
936 mode='existing', target=quorum_repair_img,
937 format=iotests.imgfmt)
938 self.assert_qmp(result, 'error/class', 'GenericError')
940 def test_device_not_found(self):
941 if not iotests.supports_quorum():
942 return
944 result = self.vm.qmp('drive-mirror', job_id='job0',
945 device='nonexistent', sync='full',
946 node_name='repair0',
947 replaces='img1',
948 target=quorum_repair_img, format=iotests.imgfmt)
949 self.assert_qmp(result, 'error/class', 'GenericError')
951 def test_wrong_sync_mode(self):
952 if not iotests.supports_quorum():
953 return
955 result = self.vm.qmp('drive-mirror', device='quorum0', job_id='job0',
956 node_name='repair0',
957 replaces='img1',
958 target=quorum_repair_img, format=iotests.imgfmt)
959 self.assert_qmp(result, 'error/class', 'GenericError')
961 def test_no_node_name(self):
962 if not iotests.supports_quorum():
963 return
965 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
966 sync='full', replaces='img1',
967 target=quorum_repair_img, format=iotests.imgfmt)
968 self.assert_qmp(result, 'error/class', 'GenericError')
970 def test_nonexistent_replaces(self):
971 if not iotests.supports_quorum():
972 return
974 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
975 sync='full', node_name='repair0', replaces='img77',
976 target=quorum_repair_img, format=iotests.imgfmt)
977 self.assert_qmp(result, 'error/class', 'GenericError')
979 def test_after_a_quorum_snapshot(self):
980 if not iotests.supports_quorum():
981 return
983 result = self.vm.qmp('blockdev-snapshot-sync', node_name='img1',
984 snapshot_file=quorum_snapshot_file,
985 snapshot_node_name="snap1");
987 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
988 sync='full', node_name='repair0', replaces="img1",
989 target=quorum_repair_img, format=iotests.imgfmt)
990 self.assert_qmp(result, 'error/class', 'GenericError')
992 result = self.vm.qmp('drive-mirror', job_id='job0', device='quorum0',
993 sync='full', node_name='repair0', replaces="snap1",
994 target=quorum_repair_img, format=iotests.imgfmt)
995 self.assert_qmp(result, 'return', {})
997 self.complete_and_wait('job0')
998 self.assert_has_block_node("repair0", quorum_repair_img)
999 # TODO: a better test requiring some QEMU infrastructure will be added
1000 # to check that this file is really driven by quorum
1001 self.vm.shutdown()
1003 # Test mirroring with a source that does not have any parents (not even a
1004 # BlockBackend)
1005 class TestOrphanedSource(iotests.QMPTestCase):
1006 def setUp(self):
1007 blk0 = { 'node-name': 'src',
1008 'driver': 'null-co' }
1010 blk1 = { 'node-name': 'dest',
1011 'driver': 'null-co' }
1013 blk2 = { 'node-name': 'dest-ro',
1014 'driver': 'null-co',
1015 'read-only': 'on' }
1017 self.vm = iotests.VM()
1018 self.vm.add_blockdev(self.qmp_to_opts(blk0))
1019 self.vm.add_blockdev(self.qmp_to_opts(blk1))
1020 self.vm.add_blockdev(self.qmp_to_opts(blk2))
1021 self.vm.launch()
1023 def tearDown(self):
1024 self.vm.shutdown()
1026 def test_no_job_id(self):
1027 self.assert_no_active_block_jobs()
1029 result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
1030 target='dest')
1031 self.assert_qmp(result, 'error/class', 'GenericError')
1033 def test_success(self):
1034 self.assert_no_active_block_jobs()
1036 result = self.vm.qmp('blockdev-mirror', job_id='job', device='src',
1037 sync='full', target='dest')
1038 self.assert_qmp(result, 'return', {})
1040 self.complete_and_wait('job')
1042 def test_failing_permissions(self):
1043 self.assert_no_active_block_jobs()
1045 result = self.vm.qmp('blockdev-mirror', device='src', sync='full',
1046 target='dest-ro')
1047 self.assert_qmp(result, 'error/class', 'GenericError')
1049 if __name__ == '__main__':
1050 iotests.main(supported_fmts=['qcow2', 'qed'])