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/>.
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
41 not_found_error
= 'DeviceNotFound'
44 iotests
.create_image(backing_img
, self
.image_len
)
45 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
46 self
.vm
= iotests
.VM().add_drive(test_img
)
47 if iotests
.qemu_default_machine
== 'pc':
48 self
.vm
.add_drive(None, 'media=cdrom', 'ide')
54 os
.remove(backing_img
)
60 def test_complete(self
):
61 self
.assert_no_active_block_jobs()
63 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
64 target
=self
.qmp_target
)
65 self
.assert_qmp(result
, 'return', {})
67 self
.complete_and_wait()
68 result
= self
.vm
.qmp('query-block')
69 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
71 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
72 'target image does not match source after mirroring')
74 def test_cancel(self
):
75 self
.assert_no_active_block_jobs()
77 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
78 target
=self
.qmp_target
)
79 self
.assert_qmp(result
, 'return', {})
81 self
.cancel_and_wait(force
=True)
82 result
= self
.vm
.qmp('query-block')
83 self
.assert_qmp(result
, 'return[0]/inserted/file', test_img
)
86 def test_cancel_after_ready(self
):
87 self
.assert_no_active_block_jobs()
89 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
90 target
=self
.qmp_target
)
91 self
.assert_qmp(result
, 'return', {})
93 self
.wait_ready_and_cancel()
94 result
= self
.vm
.qmp('query-block')
95 self
.assert_qmp(result
, 'return[0]/inserted/file', test_img
)
97 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
98 'target image does not match source after mirroring')
100 def test_pause(self
):
101 self
.assert_no_active_block_jobs()
103 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
104 target
=self
.qmp_target
)
105 self
.assert_qmp(result
, 'return', {})
107 result
= self
.vm
.qmp('block-job-pause', device
='drive0')
108 self
.assert_qmp(result
, 'return', {})
111 result
= self
.vm
.qmp('query-block-jobs')
112 offset
= self
.dictpath(result
, 'return[0]/offset')
115 result
= self
.vm
.qmp('query-block-jobs')
116 self
.assert_qmp(result
, 'return[0]/offset', offset
)
118 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
119 self
.assert_qmp(result
, 'return', {})
121 self
.complete_and_wait()
123 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
124 'target image does not match source after mirroring')
126 def test_small_buffer(self
):
127 self
.assert_no_active_block_jobs()
129 # A small buffer is rounded up automatically
130 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
131 buf_size
=4096, target
=self
.qmp_target
)
132 self
.assert_qmp(result
, 'return', {})
134 self
.complete_and_wait()
135 result
= self
.vm
.qmp('query-block')
136 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
138 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
139 'target image does not match source after mirroring')
141 def test_small_buffer2(self
):
142 self
.assert_no_active_block_jobs()
144 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'cluster_size=%d,size=%d'
145 % (self
.image_len
, self
.image_len
), target_img
)
146 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
147 buf_size
=65536, mode
='existing', target
=self
.qmp_target
)
148 self
.assert_qmp(result
, 'return', {})
150 self
.complete_and_wait()
151 result
= self
.vm
.qmp('query-block')
152 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
154 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
155 'target image does not match source after mirroring')
157 def test_large_cluster(self
):
158 self
.assert_no_active_block_jobs()
160 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'cluster_size=%d,backing_file=%s'
161 % (self
.image_len
, backing_img
), target_img
)
162 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
163 mode
='existing', target
=self
.qmp_target
)
164 self
.assert_qmp(result
, 'return', {})
166 self
.complete_and_wait()
167 result
= self
.vm
.qmp('query-block')
168 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
170 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
171 'target image does not match source after mirroring')
173 def test_medium_not_found(self
):
174 if iotests
.qemu_default_machine
!= 'pc':
177 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='ide1-cd0', sync
='full',
178 target
=self
.qmp_target
)
179 self
.assert_qmp(result
, 'error/class', self
.not_found_error
)
181 def test_image_not_found(self
):
182 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
183 mode
='existing', target
=self
.qmp_target
)
184 self
.assert_qmp(result
, 'error/class', 'GenericError')
186 def test_device_not_found(self
):
187 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='nonexistent', sync
='full',
188 target
=self
.qmp_target
)
189 self
.assert_qmp(result
, 'error/class', self
.not_found_error
)
191 class TestSingleBlockdev(TestSingleDrive
):
192 qmp_cmd
= 'blockdev-mirror'
194 not_found_error
= 'GenericError'
197 TestSingleDrive
.setUp(self
)
198 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, target_img
)
200 {'driver': iotests
.imgfmt
,
201 'node-name': self
.qmp_target
,
202 'file': { 'filename': target_img
, 'driver': 'file' } } }
203 result
= self
.vm
.qmp("blockdev-add", **args
)
204 self
.assert_qmp(result
, 'return', {})
206 test_large_cluster
= None
207 test_image_not_found
= None
208 test_small_buffer2
= None
210 class TestBlockdevAttached(iotests
.QMPTestCase
):
211 image_len
= 1 * 1024 * 1024 # MB
214 iotests
.create_image(backing_img
, self
.image_len
)
215 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
216 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, target_img
)
217 self
.vm
= iotests
.VM().add_drive(test_img
)
223 os
.remove(target_img
)
225 def test_blockdev_attached(self
):
226 self
.assert_no_active_block_jobs()
228 {'driver': iotests
.imgfmt
,
230 'file': { 'filename': target_img
, 'driver': 'file' } } }
231 result
= self
.vm
.qmp("blockdev-add", **args
)
232 self
.assert_qmp(result
, 'return', {})
233 result
= self
.vm
.qmp('blockdev-mirror', device
='drive0', sync
='full',
235 self
.assert_qmp(result
, 'error/class', 'GenericError')
237 class TestSingleDriveZeroLength(TestSingleDrive
):
239 test_small_buffer2
= None
240 test_large_cluster
= None
242 class TestSingleBlockdevZeroLength(TestSingleBlockdev
):
245 class TestSingleDriveUnalignedLength(TestSingleDrive
):
246 image_len
= 1025 * 1024
247 test_small_buffer2
= None
248 test_large_cluster
= None
250 class TestSingleBlockdevUnalignedLength(TestSingleBlockdev
):
251 image_len
= 1025 * 1024
253 class TestMirrorNoBacking(iotests
.QMPTestCase
):
254 image_len
= 2 * 1024 * 1024 # MB
257 iotests
.create_image(backing_img
, TestMirrorNoBacking
.image_len
)
258 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
259 self
.vm
= iotests
.VM().add_drive(test_img
)
265 os
.remove(backing_img
)
267 os
.remove(target_backing_img
)
270 os
.remove(target_img
)
272 def test_complete(self
):
273 self
.assert_no_active_block_jobs()
275 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, target_img
)
276 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
277 mode
='existing', target
=target_img
)
278 self
.assert_qmp(result
, 'return', {})
280 self
.complete_and_wait()
281 result
= self
.vm
.qmp('query-block')
282 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
284 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
285 'target image does not match source after mirroring')
287 def test_cancel(self
):
288 self
.assert_no_active_block_jobs()
290 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, target_img
)
291 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
292 mode
='existing', target
=target_img
)
293 self
.assert_qmp(result
, 'return', {})
295 self
.wait_ready_and_cancel()
296 result
= self
.vm
.qmp('query-block')
297 self
.assert_qmp(result
, 'return[0]/inserted/file', test_img
)
299 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
300 'target image does not match source after mirroring')
302 def test_large_cluster(self
):
303 self
.assert_no_active_block_jobs()
305 # qemu-img create fails if the image is not there
306 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'size=%d'
307 %(TestMirrorNoBacking
.image_len
), target_backing_img
)
308 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'cluster_size=%d,backing_file=%s'
309 % (TestMirrorNoBacking
.image_len
, target_backing_img
), target_img
)
311 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
312 mode
='existing', target
=target_img
)
313 self
.assert_qmp(result
, 'return', {})
315 self
.complete_and_wait()
316 result
= self
.vm
.qmp('query-block')
317 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
319 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
320 'target image does not match source after mirroring')
322 class TestMirrorResized(iotests
.QMPTestCase
):
323 backing_len
= 1 * 1024 * 1024 # MB
324 image_len
= 2 * 1024 * 1024 # MB
327 iotests
.create_image(backing_img
, TestMirrorResized
.backing_len
)
328 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
329 qemu_img('resize', test_img
, '2M')
330 self
.vm
= iotests
.VM().add_drive(test_img
)
336 os
.remove(backing_img
)
338 os
.remove(target_img
)
342 def test_complete_top(self
):
343 self
.assert_no_active_block_jobs()
345 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='top',
347 self
.assert_qmp(result
, 'return', {})
349 self
.complete_and_wait()
350 result
= self
.vm
.qmp('query-block')
351 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
353 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
354 'target image does not match source after mirroring')
356 def test_complete_full(self
):
357 self
.assert_no_active_block_jobs()
359 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
361 self
.assert_qmp(result
, 'return', {})
363 self
.complete_and_wait()
364 result
= self
.vm
.qmp('query-block')
365 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
367 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
368 'target image does not match source after mirroring')
370 class TestReadErrors(iotests
.QMPTestCase
):
371 image_len
= 2 * 1024 * 1024 # MB
373 # this should be a multiple of twice the default granularity
374 # so that we hit this offset first in state 1
375 MIRROR_GRANULARITY
= 1024 * 1024
377 def create_blkdebug_file(self
, name
, event
, errno
):
378 file = open(name
, 'w')
397 ''' % (event
, errno
, self
.MIRROR_GRANULARITY
/ 512, event
, event
))
401 self
.blkdebug_file
= backing_img
+ ".blkdebug"
402 iotests
.create_image(backing_img
, TestReadErrors
.image_len
)
403 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 5)
404 qemu_img('create', '-f', iotests
.imgfmt
,
405 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
406 % (self
.blkdebug_file
, backing_img
),
408 # Write something for tests that use sync='top'
409 qemu_io('-c', 'write %d 512' % (self
.MIRROR_GRANULARITY
+ 65536),
411 self
.vm
= iotests
.VM().add_drive(test_img
)
417 os
.remove(backing_img
)
418 os
.remove(self
.blkdebug_file
)
420 def test_report_read(self
):
421 self
.assert_no_active_block_jobs()
423 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
425 self
.assert_qmp(result
, 'return', {})
430 for event
in self
.vm
.get_qmp_events(wait
=True):
431 if event
['event'] == 'BLOCK_JOB_ERROR':
432 self
.assert_qmp(event
, 'data/device', 'drive0')
433 self
.assert_qmp(event
, 'data/operation', 'read')
435 elif event
['event'] == 'BLOCK_JOB_READY':
436 self
.assertTrue(False, 'job completed unexpectedly')
437 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
438 self
.assertTrue(error
, 'job completed unexpectedly')
439 self
.assert_qmp(event
, 'data/type', 'mirror')
440 self
.assert_qmp(event
, 'data/device', 'drive0')
441 self
.assert_qmp(event
, 'data/error', 'Input/output error')
444 self
.assert_no_active_block_jobs()
447 def test_ignore_read(self
):
448 self
.assert_no_active_block_jobs()
450 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
451 target
=target_img
, on_source_error
='ignore')
452 self
.assert_qmp(result
, 'return', {})
454 event
= self
.vm
.get_qmp_event(wait
=True)
455 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
456 self
.assert_qmp(event
, 'data/device', 'drive0')
457 self
.assert_qmp(event
, 'data/operation', 'read')
458 result
= self
.vm
.qmp('query-block-jobs')
459 self
.assert_qmp(result
, 'return[0]/paused', False)
460 self
.complete_and_wait()
463 def test_large_cluster(self
):
464 self
.assert_no_active_block_jobs()
466 # Test COW into the target image. The first half of the
467 # cluster at MIRROR_GRANULARITY has to be copied from
468 # backing_img, even though sync='top'.
469 qemu_img('create', '-f', iotests
.imgfmt
, '-ocluster_size=131072,backing_file=%s' %(backing_img), target_img
)
470 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='top',
471 on_source_error
='ignore',
472 mode
='existing', target
=target_img
)
473 self
.assert_qmp(result
, 'return', {})
475 event
= self
.vm
.get_qmp_event(wait
=True)
476 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
477 self
.assert_qmp(event
, 'data/device', 'drive0')
478 self
.assert_qmp(event
, 'data/operation', 'read')
479 result
= self
.vm
.qmp('query-block-jobs')
480 self
.assert_qmp(result
, 'return[0]/paused', False)
481 self
.complete_and_wait()
484 # Detach blkdebug to compare images successfully
485 qemu_img('rebase', '-f', iotests
.imgfmt
, '-u', '-b', backing_img
, test_img
)
486 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
487 'target image does not match source after mirroring')
489 def test_stop_read(self
):
490 self
.assert_no_active_block_jobs()
492 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
493 target
=target_img
, on_source_error
='stop')
494 self
.assert_qmp(result
, 'return', {})
499 for event
in self
.vm
.get_qmp_events(wait
=True):
500 if event
['event'] == 'BLOCK_JOB_ERROR':
501 self
.assert_qmp(event
, 'data/device', 'drive0')
502 self
.assert_qmp(event
, 'data/operation', 'read')
504 result
= self
.vm
.qmp('query-block-jobs')
505 self
.assert_qmp(result
, 'return[0]/paused', True)
506 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
508 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
509 self
.assert_qmp(result
, 'return', {})
511 elif event
['event'] == 'BLOCK_JOB_READY':
512 self
.assertTrue(error
, 'job completed unexpectedly')
513 self
.assert_qmp(event
, 'data/device', 'drive0')
516 result
= self
.vm
.qmp('query-block-jobs')
517 self
.assert_qmp(result
, 'return[0]/paused', False)
518 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
520 self
.complete_and_wait(wait_ready
=False)
521 self
.assert_no_active_block_jobs()
524 class TestWriteErrors(iotests
.QMPTestCase
):
525 image_len
= 2 * 1024 * 1024 # MB
527 # this should be a multiple of twice the default granularity
528 # so that we hit this offset first in state 1
529 MIRROR_GRANULARITY
= 1024 * 1024
531 def create_blkdebug_file(self
, name
, event
, errno
):
532 file = open(name
, 'w')
551 ''' % (event
, errno
, self
.MIRROR_GRANULARITY
/ 512, event
, event
))
555 self
.blkdebug_file
= target_img
+ ".blkdebug"
556 iotests
.create_image(backing_img
, TestWriteErrors
.image_len
)
557 self
.create_blkdebug_file(self
.blkdebug_file
, "write_aio", 5)
558 qemu_img('create', '-f', iotests
.imgfmt
, '-obacking_file=%s' %(backing_img), test_img
)
559 self
.vm
= iotests
.VM().add_drive(test_img
)
560 self
.target_img
= 'blkdebug:%s:%s' % (self
.blkdebug_file
, target_img
)
561 qemu_img('create', '-f', iotests
.imgfmt
, '-osize=%d' %(TestWriteErrors
.image_len
), target_img
)
567 os
.remove(backing_img
)
568 os
.remove(self
.blkdebug_file
)
570 def test_report_write(self
):
571 self
.assert_no_active_block_jobs()
573 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
574 mode
='existing', target
=self
.target_img
)
575 self
.assert_qmp(result
, 'return', {})
580 for event
in self
.vm
.get_qmp_events(wait
=True):
581 if event
['event'] == 'BLOCK_JOB_ERROR':
582 self
.assert_qmp(event
, 'data/device', 'drive0')
583 self
.assert_qmp(event
, 'data/operation', 'write')
585 elif event
['event'] == 'BLOCK_JOB_READY':
586 self
.assertTrue(False, 'job completed unexpectedly')
587 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
588 self
.assertTrue(error
, 'job completed unexpectedly')
589 self
.assert_qmp(event
, 'data/type', 'mirror')
590 self
.assert_qmp(event
, 'data/device', 'drive0')
591 self
.assert_qmp(event
, 'data/error', 'Input/output error')
594 self
.assert_no_active_block_jobs()
597 def test_ignore_write(self
):
598 self
.assert_no_active_block_jobs()
600 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
601 mode
='existing', target
=self
.target_img
,
602 on_target_error
='ignore')
603 self
.assert_qmp(result
, 'return', {})
605 event
= self
.vm
.get_qmp_event(wait
=True)
606 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
607 self
.assert_qmp(event
, 'data/device', 'drive0')
608 self
.assert_qmp(event
, 'data/operation', 'write')
609 result
= self
.vm
.qmp('query-block-jobs')
610 self
.assert_qmp(result
, 'return[0]/paused', False)
611 self
.complete_and_wait()
614 def test_stop_write(self
):
615 self
.assert_no_active_block_jobs()
617 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
618 mode
='existing', target
=self
.target_img
,
619 on_target_error
='stop')
620 self
.assert_qmp(result
, 'return', {})
625 for event
in self
.vm
.get_qmp_events(wait
=True):
626 if event
['event'] == 'BLOCK_JOB_ERROR':
627 self
.assert_qmp(event
, 'data/device', 'drive0')
628 self
.assert_qmp(event
, 'data/operation', 'write')
630 result
= self
.vm
.qmp('query-block-jobs')
631 self
.assert_qmp(result
, 'return[0]/paused', True)
632 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
634 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
635 self
.assert_qmp(result
, 'return', {})
637 result
= self
.vm
.qmp('query-block-jobs')
638 self
.assert_qmp(result
, 'return[0]/paused', False)
639 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
641 elif event
['event'] == 'BLOCK_JOB_READY':
642 self
.assertTrue(error
, 'job completed unexpectedly')
643 self
.assert_qmp(event
, 'data/device', 'drive0')
646 self
.complete_and_wait(wait_ready
=False)
647 self
.assert_no_active_block_jobs()
650 class TestSetSpeed(iotests
.QMPTestCase
):
651 image_len
= 80 * 1024 * 1024 # MB
654 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
655 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
656 self
.vm
= iotests
.VM().add_drive(test_img
)
662 os
.remove(backing_img
)
663 os
.remove(target_img
)
665 def test_set_speed(self
):
666 self
.assert_no_active_block_jobs()
668 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
670 self
.assert_qmp(result
, 'return', {})
673 result
= self
.vm
.qmp('query-block-jobs')
674 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
675 self
.assert_qmp(result
, 'return[0]/speed', 0)
677 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
678 self
.assert_qmp(result
, 'return', {})
680 # Ensure the speed we set was accepted
681 result
= self
.vm
.qmp('query-block-jobs')
682 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
683 self
.assert_qmp(result
, 'return[0]/speed', 8 * 1024 * 1024)
685 self
.wait_ready_and_cancel()
687 # Check setting speed in drive-mirror works
688 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
689 target
=target_img
, speed
=4*1024*1024)
690 self
.assert_qmp(result
, 'return', {})
692 result
= self
.vm
.qmp('query-block-jobs')
693 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
694 self
.assert_qmp(result
, 'return[0]/speed', 4 * 1024 * 1024)
696 self
.wait_ready_and_cancel()
698 def test_set_speed_invalid(self
):
699 self
.assert_no_active_block_jobs()
701 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
702 target
=target_img
, speed
=-1)
703 self
.assert_qmp(result
, 'error/class', 'GenericError')
705 self
.assert_no_active_block_jobs()
707 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
709 self
.assert_qmp(result
, 'return', {})
711 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=-1)
712 self
.assert_qmp(result
, 'error/class', 'GenericError')
714 self
.wait_ready_and_cancel()
716 class TestUnbackedSource(iotests
.QMPTestCase
):
717 image_len
= 2 * 1024 * 1024 # MB
720 qemu_img('create', '-f', iotests
.imgfmt
, test_img
,
721 str(TestUnbackedSource
.image_len
))
722 self
.vm
= iotests
.VM().add_drive(test_img
)
728 os
.remove(target_img
)
730 def test_absolute_paths_full(self
):
731 self
.assert_no_active_block_jobs()
732 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
733 sync
='full', target
=target_img
,
734 mode
='absolute-paths')
735 self
.assert_qmp(result
, 'return', {})
736 self
.complete_and_wait()
737 self
.assert_no_active_block_jobs()
739 def test_absolute_paths_top(self
):
740 self
.assert_no_active_block_jobs()
741 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
742 sync
='top', target
=target_img
,
743 mode
='absolute-paths')
744 self
.assert_qmp(result
, 'return', {})
745 self
.complete_and_wait()
746 self
.assert_no_active_block_jobs()
748 def test_absolute_paths_none(self
):
749 self
.assert_no_active_block_jobs()
750 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
751 sync
='none', target
=target_img
,
752 mode
='absolute-paths')
753 self
.assert_qmp(result
, 'return', {})
754 self
.complete_and_wait()
755 self
.assert_no_active_block_jobs()
757 class TestRepairQuorum(iotests
.QMPTestCase
):
758 """ This class test quorum file repair using drive-mirror.
759 It's mostly a fork of TestSingleDrive """
760 image_len
= 1 * 1024 * 1024 # MB
761 IMAGES
= [ quorum_img1
, quorum_img2
, quorum_img3
]
763 def has_quorum(self
):
764 return 'quorum' in iotests
.qemu_img_pipe('--help')
767 self
.vm
= iotests
.VM()
769 if iotests
.qemu_default_machine
== 'pc':
770 self
.vm
.add_drive(None, 'media=cdrom', 'ide')
772 # Add each individual quorum images
773 for i
in self
.IMAGES
:
774 qemu_img('create', '-f', iotests
.imgfmt
, i
,
775 str(TestSingleDrive
.image_len
))
776 # Assign a node name to each quorum image in order to manipulate
778 opts
= "node-name=img%i" % self
.IMAGES
.index(i
)
779 self
.vm
= self
.vm
.add_drive(i
, opts
)
783 #assemble the quorum block device from the individual files
784 args
= { "options" : { "driver": "quorum", "id": "quorum0",
785 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] } }
786 if self
.has_quorum():
787 result
= self
.vm
.qmp("blockdev-add", **args
)
788 self
.assert_qmp(result
, 'return', {})
793 for i
in self
.IMAGES
+ [ quorum_repair_img
]:
794 # Do a try/except because the test may have deleted some images
800 def test_complete(self
):
801 if not self
.has_quorum():
804 self
.assert_no_active_block_jobs()
806 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
809 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
810 self
.assert_qmp(result
, 'return', {})
812 self
.complete_and_wait(drive
="quorum0")
813 result
= self
.vm
.qmp('query-named-block-nodes')
814 self
.assert_qmp(result
, 'return[0]/file', quorum_repair_img
)
815 # TODO: a better test requiring some QEMU infrastructure will be added
816 # to check that this file is really driven by quorum
818 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
819 'target image does not match source after mirroring')
821 def test_cancel(self
):
822 if not self
.has_quorum():
825 self
.assert_no_active_block_jobs()
827 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
830 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
831 self
.assert_qmp(result
, 'return', {})
833 self
.cancel_and_wait(drive
="quorum0", force
=True)
834 # here we check that the last registered quorum file has not been
835 # swapped out and unref
836 result
= self
.vm
.qmp('query-named-block-nodes')
837 self
.assert_qmp(result
, 'return[1]/file', quorum_img3
)
840 def test_cancel_after_ready(self
):
841 if not self
.has_quorum():
844 self
.assert_no_active_block_jobs()
846 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
849 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
850 self
.assert_qmp(result
, 'return', {})
852 self
.wait_ready_and_cancel(drive
="quorum0")
853 result
= self
.vm
.qmp('query-named-block-nodes')
854 # here we check that the last registered quorum file has not been
855 # swapped out and unref
856 self
.assert_qmp(result
, 'return[1]/file', quorum_img3
)
858 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
859 'target image does not match source after mirroring')
861 def test_pause(self
):
862 if not self
.has_quorum():
865 self
.assert_no_active_block_jobs()
867 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
870 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
871 self
.assert_qmp(result
, 'return', {})
873 result
= self
.vm
.qmp('block-job-pause', device
='quorum0')
874 self
.assert_qmp(result
, 'return', {})
877 result
= self
.vm
.qmp('query-block-jobs')
878 offset
= self
.dictpath(result
, 'return[0]/offset')
881 result
= self
.vm
.qmp('query-block-jobs')
882 self
.assert_qmp(result
, 'return[0]/offset', offset
)
884 result
= self
.vm
.qmp('block-job-resume', device
='quorum0')
885 self
.assert_qmp(result
, 'return', {})
887 self
.complete_and_wait(drive
="quorum0")
889 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
890 'target image does not match source after mirroring')
892 def test_medium_not_found(self
):
893 if not self
.has_quorum():
896 if iotests
.qemu_default_machine
!= 'pc':
899 result
= self
.vm
.qmp('drive-mirror', device
='drive0', # CD-ROM
903 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
904 self
.assert_qmp(result
, 'error/class', 'GenericError')
906 def test_image_not_found(self
):
907 if not self
.has_quorum():
910 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
914 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
915 self
.assert_qmp(result
, 'error/class', 'GenericError')
917 def test_device_not_found(self
):
918 if not self
.has_quorum():
921 result
= self
.vm
.qmp('drive-mirror', device
='nonexistent', sync
='full',
924 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
925 self
.assert_qmp(result
, 'error/class', 'DeviceNotFound')
927 def test_wrong_sync_mode(self
):
928 if not self
.has_quorum():
931 result
= self
.vm
.qmp('drive-mirror', device
='quorum0',
934 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
935 self
.assert_qmp(result
, 'error/class', 'GenericError')
937 def test_no_node_name(self
):
938 if not self
.has_quorum():
941 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
943 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
944 self
.assert_qmp(result
, 'error/class', 'GenericError')
946 def test_nonexistent_replaces(self
):
947 if not self
.has_quorum():
950 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
953 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
954 self
.assert_qmp(result
, 'error/class', 'GenericError')
956 def test_after_a_quorum_snapshot(self
):
957 if not self
.has_quorum():
960 result
= self
.vm
.qmp('blockdev-snapshot-sync', node_name
='img1',
961 snapshot_file
=quorum_snapshot_file
,
962 snapshot_node_name
="snap1");
964 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
967 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
968 self
.assert_qmp(result
, 'error/class', 'GenericError')
970 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
973 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
974 self
.assert_qmp(result
, 'return', {})
976 self
.complete_and_wait(drive
="quorum0")
977 result
= self
.vm
.qmp('query-named-block-nodes')
978 self
.assert_qmp(result
, 'return[0]/file', quorum_repair_img
)
979 # TODO: a better test requiring some QEMU infrastructure will be added
980 # to check that this file is really driven by quorum
983 if __name__
== '__main__':
984 iotests
.main(supported_fmts
=['qcow2', 'qed'])