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 TestSingleDriveZeroLength(TestSingleDrive
):
212 test_small_buffer2
= None
213 test_large_cluster
= None
215 class TestSingleBlockdevZeroLength(TestSingleBlockdev
):
218 class TestSingleDriveUnalignedLength(TestSingleDrive
):
219 image_len
= 1025 * 1024
220 test_small_buffer2
= None
221 test_large_cluster
= None
223 class TestSingleBlockdevUnalignedLength(TestSingleBlockdev
):
224 image_len
= 1025 * 1024
226 class TestMirrorNoBacking(iotests
.QMPTestCase
):
227 image_len
= 2 * 1024 * 1024 # MB
230 iotests
.create_image(backing_img
, TestMirrorNoBacking
.image_len
)
231 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
232 self
.vm
= iotests
.VM().add_drive(test_img
)
238 os
.remove(backing_img
)
240 os
.remove(target_backing_img
)
243 os
.remove(target_img
)
245 def test_complete(self
):
246 self
.assert_no_active_block_jobs()
248 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, target_img
)
249 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
250 mode
='existing', target
=target_img
)
251 self
.assert_qmp(result
, 'return', {})
253 self
.complete_and_wait()
254 result
= self
.vm
.qmp('query-block')
255 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
257 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
258 'target image does not match source after mirroring')
260 def test_cancel(self
):
261 self
.assert_no_active_block_jobs()
263 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, target_img
)
264 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
265 mode
='existing', target
=target_img
)
266 self
.assert_qmp(result
, 'return', {})
268 self
.wait_ready_and_cancel()
269 result
= self
.vm
.qmp('query-block')
270 self
.assert_qmp(result
, 'return[0]/inserted/file', test_img
)
272 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
273 'target image does not match source after mirroring')
275 def test_large_cluster(self
):
276 self
.assert_no_active_block_jobs()
278 # qemu-img create fails if the image is not there
279 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'size=%d'
280 %(TestMirrorNoBacking
.image_len
), target_backing_img
)
281 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'cluster_size=%d,backing_file=%s'
282 % (TestMirrorNoBacking
.image_len
, target_backing_img
), target_img
)
284 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
285 mode
='existing', target
=target_img
)
286 self
.assert_qmp(result
, 'return', {})
288 self
.complete_and_wait()
289 result
= self
.vm
.qmp('query-block')
290 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
292 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
293 'target image does not match source after mirroring')
295 class TestMirrorResized(iotests
.QMPTestCase
):
296 backing_len
= 1 * 1024 * 1024 # MB
297 image_len
= 2 * 1024 * 1024 # MB
300 iotests
.create_image(backing_img
, TestMirrorResized
.backing_len
)
301 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
302 qemu_img('resize', test_img
, '2M')
303 self
.vm
= iotests
.VM().add_drive(test_img
)
309 os
.remove(backing_img
)
311 os
.remove(target_img
)
315 def test_complete_top(self
):
316 self
.assert_no_active_block_jobs()
318 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='top',
320 self
.assert_qmp(result
, 'return', {})
322 self
.complete_and_wait()
323 result
= self
.vm
.qmp('query-block')
324 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
326 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
327 'target image does not match source after mirroring')
329 def test_complete_full(self
):
330 self
.assert_no_active_block_jobs()
332 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
334 self
.assert_qmp(result
, 'return', {})
336 self
.complete_and_wait()
337 result
= self
.vm
.qmp('query-block')
338 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
340 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
341 'target image does not match source after mirroring')
343 class TestReadErrors(iotests
.QMPTestCase
):
344 image_len
= 2 * 1024 * 1024 # MB
346 # this should be a multiple of twice the default granularity
347 # so that we hit this offset first in state 1
348 MIRROR_GRANULARITY
= 1024 * 1024
350 def create_blkdebug_file(self
, name
, event
, errno
):
351 file = open(name
, 'w')
370 ''' % (event
, errno
, self
.MIRROR_GRANULARITY
/ 512, event
, event
))
374 self
.blkdebug_file
= backing_img
+ ".blkdebug"
375 iotests
.create_image(backing_img
, TestReadErrors
.image_len
)
376 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 5)
377 qemu_img('create', '-f', iotests
.imgfmt
,
378 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
379 % (self
.blkdebug_file
, backing_img
),
381 # Write something for tests that use sync='top'
382 qemu_io('-c', 'write %d 512' % (self
.MIRROR_GRANULARITY
+ 65536),
384 self
.vm
= iotests
.VM().add_drive(test_img
)
390 os
.remove(backing_img
)
391 os
.remove(self
.blkdebug_file
)
393 def test_report_read(self
):
394 self
.assert_no_active_block_jobs()
396 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
398 self
.assert_qmp(result
, 'return', {})
403 for event
in self
.vm
.get_qmp_events(wait
=True):
404 if event
['event'] == 'BLOCK_JOB_ERROR':
405 self
.assert_qmp(event
, 'data/device', 'drive0')
406 self
.assert_qmp(event
, 'data/operation', 'read')
408 elif event
['event'] == 'BLOCK_JOB_READY':
409 self
.assertTrue(False, 'job completed unexpectedly')
410 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
411 self
.assertTrue(error
, 'job completed unexpectedly')
412 self
.assert_qmp(event
, 'data/type', 'mirror')
413 self
.assert_qmp(event
, 'data/device', 'drive0')
414 self
.assert_qmp(event
, 'data/error', 'Input/output error')
417 self
.assert_no_active_block_jobs()
420 def test_ignore_read(self
):
421 self
.assert_no_active_block_jobs()
423 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
424 target
=target_img
, on_source_error
='ignore')
425 self
.assert_qmp(result
, 'return', {})
427 event
= self
.vm
.get_qmp_event(wait
=True)
428 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
429 self
.assert_qmp(event
, 'data/device', 'drive0')
430 self
.assert_qmp(event
, 'data/operation', 'read')
431 result
= self
.vm
.qmp('query-block-jobs')
432 self
.assert_qmp(result
, 'return[0]/paused', False)
433 self
.complete_and_wait()
436 def test_large_cluster(self
):
437 self
.assert_no_active_block_jobs()
439 # Test COW into the target image. The first half of the
440 # cluster at MIRROR_GRANULARITY has to be copied from
441 # backing_img, even though sync='top'.
442 qemu_img('create', '-f', iotests
.imgfmt
, '-ocluster_size=131072,backing_file=%s' %(backing_img), target_img
)
443 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='top',
444 on_source_error
='ignore',
445 mode
='existing', target
=target_img
)
446 self
.assert_qmp(result
, 'return', {})
448 event
= self
.vm
.get_qmp_event(wait
=True)
449 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
450 self
.assert_qmp(event
, 'data/device', 'drive0')
451 self
.assert_qmp(event
, 'data/operation', 'read')
452 result
= self
.vm
.qmp('query-block-jobs')
453 self
.assert_qmp(result
, 'return[0]/paused', False)
454 self
.complete_and_wait()
457 # Detach blkdebug to compare images successfully
458 qemu_img('rebase', '-f', iotests
.imgfmt
, '-u', '-b', backing_img
, test_img
)
459 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
460 'target image does not match source after mirroring')
462 def test_stop_read(self
):
463 self
.assert_no_active_block_jobs()
465 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
466 target
=target_img
, on_source_error
='stop')
467 self
.assert_qmp(result
, 'return', {})
472 for event
in self
.vm
.get_qmp_events(wait
=True):
473 if event
['event'] == 'BLOCK_JOB_ERROR':
474 self
.assert_qmp(event
, 'data/device', 'drive0')
475 self
.assert_qmp(event
, 'data/operation', 'read')
477 result
= self
.vm
.qmp('query-block-jobs')
478 self
.assert_qmp(result
, 'return[0]/paused', True)
479 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
481 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
482 self
.assert_qmp(result
, 'return', {})
484 elif event
['event'] == 'BLOCK_JOB_READY':
485 self
.assertTrue(error
, 'job completed unexpectedly')
486 self
.assert_qmp(event
, 'data/device', 'drive0')
489 result
= self
.vm
.qmp('query-block-jobs')
490 self
.assert_qmp(result
, 'return[0]/paused', False)
491 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
493 self
.complete_and_wait(wait_ready
=False)
494 self
.assert_no_active_block_jobs()
497 class TestWriteErrors(iotests
.QMPTestCase
):
498 image_len
= 2 * 1024 * 1024 # MB
500 # this should be a multiple of twice the default granularity
501 # so that we hit this offset first in state 1
502 MIRROR_GRANULARITY
= 1024 * 1024
504 def create_blkdebug_file(self
, name
, event
, errno
):
505 file = open(name
, 'w')
524 ''' % (event
, errno
, self
.MIRROR_GRANULARITY
/ 512, event
, event
))
528 self
.blkdebug_file
= target_img
+ ".blkdebug"
529 iotests
.create_image(backing_img
, TestWriteErrors
.image_len
)
530 self
.create_blkdebug_file(self
.blkdebug_file
, "write_aio", 5)
531 qemu_img('create', '-f', iotests
.imgfmt
, '-obacking_file=%s' %(backing_img), test_img
)
532 self
.vm
= iotests
.VM().add_drive(test_img
)
533 self
.target_img
= 'blkdebug:%s:%s' % (self
.blkdebug_file
, target_img
)
534 qemu_img('create', '-f', iotests
.imgfmt
, '-osize=%d' %(TestWriteErrors
.image_len
), target_img
)
540 os
.remove(backing_img
)
541 os
.remove(self
.blkdebug_file
)
543 def test_report_write(self
):
544 self
.assert_no_active_block_jobs()
546 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
547 mode
='existing', target
=self
.target_img
)
548 self
.assert_qmp(result
, 'return', {})
553 for event
in self
.vm
.get_qmp_events(wait
=True):
554 if event
['event'] == 'BLOCK_JOB_ERROR':
555 self
.assert_qmp(event
, 'data/device', 'drive0')
556 self
.assert_qmp(event
, 'data/operation', 'write')
558 elif event
['event'] == 'BLOCK_JOB_READY':
559 self
.assertTrue(False, 'job completed unexpectedly')
560 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
561 self
.assertTrue(error
, 'job completed unexpectedly')
562 self
.assert_qmp(event
, 'data/type', 'mirror')
563 self
.assert_qmp(event
, 'data/device', 'drive0')
564 self
.assert_qmp(event
, 'data/error', 'Input/output error')
567 self
.assert_no_active_block_jobs()
570 def test_ignore_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 on_target_error
='ignore')
576 self
.assert_qmp(result
, 'return', {})
578 event
= self
.vm
.get_qmp_event(wait
=True)
579 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
580 self
.assert_qmp(event
, 'data/device', 'drive0')
581 self
.assert_qmp(event
, 'data/operation', 'write')
582 result
= self
.vm
.qmp('query-block-jobs')
583 self
.assert_qmp(result
, 'return[0]/paused', False)
584 self
.complete_and_wait()
587 def test_stop_write(self
):
588 self
.assert_no_active_block_jobs()
590 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
591 mode
='existing', target
=self
.target_img
,
592 on_target_error
='stop')
593 self
.assert_qmp(result
, 'return', {})
598 for event
in self
.vm
.get_qmp_events(wait
=True):
599 if event
['event'] == 'BLOCK_JOB_ERROR':
600 self
.assert_qmp(event
, 'data/device', 'drive0')
601 self
.assert_qmp(event
, 'data/operation', 'write')
603 result
= self
.vm
.qmp('query-block-jobs')
604 self
.assert_qmp(result
, 'return[0]/paused', True)
605 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
607 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
608 self
.assert_qmp(result
, 'return', {})
610 result
= self
.vm
.qmp('query-block-jobs')
611 self
.assert_qmp(result
, 'return[0]/paused', False)
612 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
614 elif event
['event'] == 'BLOCK_JOB_READY':
615 self
.assertTrue(error
, 'job completed unexpectedly')
616 self
.assert_qmp(event
, 'data/device', 'drive0')
619 self
.complete_and_wait(wait_ready
=False)
620 self
.assert_no_active_block_jobs()
623 class TestSetSpeed(iotests
.QMPTestCase
):
624 image_len
= 80 * 1024 * 1024 # MB
627 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
628 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
629 self
.vm
= iotests
.VM().add_drive(test_img
)
635 os
.remove(backing_img
)
636 os
.remove(target_img
)
638 def test_set_speed(self
):
639 self
.assert_no_active_block_jobs()
641 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
643 self
.assert_qmp(result
, 'return', {})
646 result
= self
.vm
.qmp('query-block-jobs')
647 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
648 self
.assert_qmp(result
, 'return[0]/speed', 0)
650 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
651 self
.assert_qmp(result
, 'return', {})
653 # Ensure the speed we set was accepted
654 result
= self
.vm
.qmp('query-block-jobs')
655 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
656 self
.assert_qmp(result
, 'return[0]/speed', 8 * 1024 * 1024)
658 self
.wait_ready_and_cancel()
660 # Check setting speed in drive-mirror works
661 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
662 target
=target_img
, speed
=4*1024*1024)
663 self
.assert_qmp(result
, 'return', {})
665 result
= self
.vm
.qmp('query-block-jobs')
666 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
667 self
.assert_qmp(result
, 'return[0]/speed', 4 * 1024 * 1024)
669 self
.wait_ready_and_cancel()
671 def test_set_speed_invalid(self
):
672 self
.assert_no_active_block_jobs()
674 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
675 target
=target_img
, speed
=-1)
676 self
.assert_qmp(result
, 'error/class', 'GenericError')
678 self
.assert_no_active_block_jobs()
680 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
682 self
.assert_qmp(result
, 'return', {})
684 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=-1)
685 self
.assert_qmp(result
, 'error/class', 'GenericError')
687 self
.wait_ready_and_cancel()
689 class TestUnbackedSource(iotests
.QMPTestCase
):
690 image_len
= 2 * 1024 * 1024 # MB
693 qemu_img('create', '-f', iotests
.imgfmt
, test_img
,
694 str(TestUnbackedSource
.image_len
))
695 self
.vm
= iotests
.VM().add_drive(test_img
)
701 os
.remove(target_img
)
703 def test_absolute_paths_full(self
):
704 self
.assert_no_active_block_jobs()
705 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
706 sync
='full', target
=target_img
,
707 mode
='absolute-paths')
708 self
.assert_qmp(result
, 'return', {})
709 self
.complete_and_wait()
710 self
.assert_no_active_block_jobs()
712 def test_absolute_paths_top(self
):
713 self
.assert_no_active_block_jobs()
714 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
715 sync
='top', target
=target_img
,
716 mode
='absolute-paths')
717 self
.assert_qmp(result
, 'return', {})
718 self
.complete_and_wait()
719 self
.assert_no_active_block_jobs()
721 def test_absolute_paths_none(self
):
722 self
.assert_no_active_block_jobs()
723 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
724 sync
='none', target
=target_img
,
725 mode
='absolute-paths')
726 self
.assert_qmp(result
, 'return', {})
727 self
.complete_and_wait()
728 self
.assert_no_active_block_jobs()
730 class TestGranularity(iotests
.QMPTestCase
):
731 image_len
= 10 * 1024 * 1024 # MB
734 qemu_img('create', '-f', iotests
.imgfmt
, test_img
,
735 str(TestGranularity
.image_len
))
736 qemu_io('-c', 'write 0 %d' % (self
.image_len
),
738 self
.vm
= iotests
.VM().add_drive(test_img
)
743 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
744 'target image does not match source after mirroring')
746 os
.remove(target_img
)
748 def test_granularity(self
):
749 self
.assert_no_active_block_jobs()
750 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
751 sync
='full', target
=target_img
,
752 mode
='absolute-paths', granularity
=8192)
753 self
.assert_qmp(result
, 'return', {})
754 event
= self
.vm
.get_qmp_event(wait
=60.0)
755 # Failures will manifest as COMPLETED/ERROR.
756 self
.assert_qmp(event
, 'event', 'BLOCK_JOB_READY')
757 self
.complete_and_wait(drive
='drive0', wait_ready
=False)
758 self
.assert_no_active_block_jobs()
760 class TestRepairQuorum(iotests
.QMPTestCase
):
761 """ This class test quorum file repair using drive-mirror.
762 It's mostly a fork of TestSingleDrive """
763 image_len
= 1 * 1024 * 1024 # MB
764 IMAGES
= [ quorum_img1
, quorum_img2
, quorum_img3
]
766 def has_quorum(self
):
767 return 'quorum' in iotests
.qemu_img_pipe('--help')
770 self
.vm
= iotests
.VM()
772 if iotests
.qemu_default_machine
== 'pc':
773 self
.vm
.add_drive(None, 'media=cdrom', 'ide')
775 # Add each individual quorum images
776 for i
in self
.IMAGES
:
777 qemu_img('create', '-f', iotests
.imgfmt
, i
,
778 str(TestSingleDrive
.image_len
))
779 # Assign a node name to each quorum image in order to manipulate
781 opts
= "node-name=img%i" % self
.IMAGES
.index(i
)
782 self
.vm
= self
.vm
.add_drive(i
, opts
)
786 #assemble the quorum block device from the individual files
787 args
= { "options" : { "driver": "quorum", "id": "quorum0",
788 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] } }
789 if self
.has_quorum():
790 result
= self
.vm
.qmp("blockdev-add", **args
)
791 self
.assert_qmp(result
, 'return', {})
796 for i
in self
.IMAGES
+ [ quorum_repair_img
]:
797 # Do a try/except because the test may have deleted some images
803 def test_complete(self
):
804 if not self
.has_quorum():
807 self
.assert_no_active_block_jobs()
809 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
812 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
813 self
.assert_qmp(result
, 'return', {})
815 self
.complete_and_wait(drive
="quorum0")
816 self
.assert_has_block_node("repair0", quorum_repair_img
)
817 # TODO: a better test requiring some QEMU infrastructure will be added
818 # to check that this file is really driven by quorum
820 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
821 'target image does not match source after mirroring')
823 def test_cancel(self
):
824 if not self
.has_quorum():
827 self
.assert_no_active_block_jobs()
829 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
832 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
833 self
.assert_qmp(result
, 'return', {})
835 self
.cancel_and_wait(drive
="quorum0", force
=True)
836 # here we check that the last registered quorum file has not been
837 # swapped out and unref
838 self
.assert_has_block_node(None, quorum_img3
)
841 def test_cancel_after_ready(self
):
842 if not self
.has_quorum():
845 self
.assert_no_active_block_jobs()
847 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', sync
='full',
850 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
851 self
.assert_qmp(result
, 'return', {})
853 self
.wait_ready_and_cancel(drive
="quorum0")
854 # here we check that the last registered quorum file has not been
855 # swapped out and unref
856 self
.assert_has_block_node(None, 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 self
.assert_has_block_node("repair0", quorum_repair_img
)
978 # TODO: a better test requiring some QEMU infrastructure will be added
979 # to check that this file is really driven by quorum
982 if __name__
== '__main__':
983 iotests
.main(supported_fmts
=['qcow2', 'qed'])