]>
git.proxmox.com Git - mirror_qemu.git/blob - tests/qemu-iotests/041
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
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')
53 os
.remove(backing_img
)
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
)
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
)
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
)
96 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
97 'target image does not match source after mirroring')
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')
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()
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
)
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
)
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
)
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')
206 def test_medium_not_found(self
):
207 if iotests
.qemu_default_machine
!= 'pc':
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'
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
):
243 test_small_buffer2
= None
244 test_large_cluster
= None
246 class TestSingleBlockdevZeroLength(TestSingleBlockdev
):
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
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
)
269 os
.remove(backing_img
)
271 os
.remove(target_backing_img
)
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
)
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
)
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
)
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
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
)
340 os
.remove(backing_img
)
342 os
.remove(target_img
)
346 def test_complete_top(self
):
347 self
.assert_no_active_block_jobs()
349 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='top',
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
)
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',
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
)
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')
401 ''' % (event
, errno
, self
.MIRROR_GRANULARITY
/ 512, event
, event
))
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
),
412 # Write something for tests that use sync='top'
413 qemu_io('-c', 'write %d 512' % (self
.MIRROR_GRANULARITY
+ 65536),
415 self
.vm
= iotests
.VM().add_drive(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',
430 self
.assert_qmp(result
, 'return', {})
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')
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')
448 elif event
['event'] == 'JOB_STATUS_CHANGE':
449 self
.assert_qmp(event
, 'data/id', 'drive0')
451 self
.assert_no_active_block_jobs()
454 def test_ignore_read(self
):
455 self
.assert_no_active_block_jobs()
457 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
458 target
=target_img
, on_source_error
='ignore')
459 self
.assert_qmp(result
, 'return', {})
461 event
= self
.vm
.get_qmp_event(wait
=True)
462 while event
['event'] == 'JOB_STATUS_CHANGE':
463 self
.assert_qmp(event
, 'data/id', 'drive0')
464 event
= self
.vm
.get_qmp_event(wait
=True)
466 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
467 self
.assert_qmp(event
, 'data/device', 'drive0')
468 self
.assert_qmp(event
, 'data/operation', 'read')
469 result
= self
.vm
.qmp('query-block-jobs')
470 self
.assert_qmp(result
, 'return[0]/paused', False)
471 self
.complete_and_wait()
474 def test_large_cluster(self
):
475 self
.assert_no_active_block_jobs()
477 # Test COW into the target image. The first half of the
478 # cluster at MIRROR_GRANULARITY has to be copied from
479 # backing_img, even though sync='top'.
480 qemu_img('create', '-f', iotests
.imgfmt
, '-ocluster_size=131072,backing_file=%s' %(backing_img), target_img
)
481 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='top',
482 on_source_error
='ignore',
483 mode
='existing', target
=target_img
)
484 self
.assert_qmp(result
, 'return', {})
486 event
= self
.vm
.get_qmp_event(wait
=True)
487 while event
['event'] == 'JOB_STATUS_CHANGE':
488 self
.assert_qmp(event
, 'data/id', 'drive0')
489 event
= self
.vm
.get_qmp_event(wait
=True)
491 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
492 self
.assert_qmp(event
, 'data/device', 'drive0')
493 self
.assert_qmp(event
, 'data/operation', 'read')
494 result
= self
.vm
.qmp('query-block-jobs')
495 self
.assert_qmp(result
, 'return[0]/paused', False)
496 self
.complete_and_wait()
499 # Detach blkdebug to compare images successfully
500 qemu_img('rebase', '-f', iotests
.imgfmt
, '-u', '-b', backing_img
, test_img
)
501 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
502 'target image does not match source after mirroring')
504 def test_stop_read(self
):
505 self
.assert_no_active_block_jobs()
507 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
508 target
=target_img
, on_source_error
='stop')
509 self
.assert_qmp(result
, 'return', {})
514 for event
in self
.vm
.get_qmp_events(wait
=True):
515 if event
['event'] == 'BLOCK_JOB_ERROR':
516 self
.assert_qmp(event
, 'data/device', 'drive0')
517 self
.assert_qmp(event
, 'data/operation', 'read')
519 result
= self
.vm
.qmp('query-block-jobs')
520 self
.assert_qmp(result
, 'return[0]/paused', True)
521 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
523 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
524 self
.assert_qmp(result
, 'return', {})
526 elif event
['event'] == 'BLOCK_JOB_READY':
527 self
.assertTrue(error
, 'job completed unexpectedly')
528 self
.assert_qmp(event
, 'data/device', 'drive0')
531 result
= self
.vm
.qmp('query-block-jobs')
532 self
.assert_qmp(result
, 'return[0]/paused', False)
533 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
535 self
.complete_and_wait(wait_ready
=False)
536 self
.assert_no_active_block_jobs()
539 class TestWriteErrors(iotests
.QMPTestCase
):
540 image_len
= 2 * 1024 * 1024 # MB
542 # this should be a multiple of twice the default granularity
543 # so that we hit this offset first in state 1
544 MIRROR_GRANULARITY
= 1024 * 1024
546 def create_blkdebug_file(self
, name
, event
, errno
):
547 file = open(name
, 'w')
566 ''' % (event
, errno
, self
.MIRROR_GRANULARITY
/ 512, event
, event
))
570 self
.blkdebug_file
= target_img
+ ".blkdebug"
571 iotests
.create_image(backing_img
, TestWriteErrors
.image_len
)
572 self
.create_blkdebug_file(self
.blkdebug_file
, "write_aio", 5)
573 qemu_img('create', '-f', iotests
.imgfmt
, '-obacking_file=%s' %(backing_img), test_img
)
574 self
.vm
= iotests
.VM().add_drive(test_img
)
575 self
.target_img
= 'blkdebug:%s:%s' % (self
.blkdebug_file
, target_img
)
576 qemu_img('create', '-f', iotests
.imgfmt
, '-osize=%d' %(TestWriteErrors
.image_len
), target_img
)
582 os
.remove(target_img
)
583 os
.remove(backing_img
)
584 os
.remove(self
.blkdebug_file
)
586 def test_report_write(self
):
587 self
.assert_no_active_block_jobs()
589 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
590 mode
='existing', target
=self
.target_img
)
591 self
.assert_qmp(result
, 'return', {})
596 for event
in self
.vm
.get_qmp_events(wait
=True):
597 if event
['event'] == 'BLOCK_JOB_ERROR':
598 self
.assert_qmp(event
, 'data/device', 'drive0')
599 self
.assert_qmp(event
, 'data/operation', 'write')
601 elif event
['event'] == 'BLOCK_JOB_READY':
602 self
.assertTrue(False, 'job completed unexpectedly')
603 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
604 self
.assertTrue(error
, 'job completed unexpectedly')
605 self
.assert_qmp(event
, 'data/type', 'mirror')
606 self
.assert_qmp(event
, 'data/device', 'drive0')
607 self
.assert_qmp(event
, 'data/error', 'Input/output error')
610 self
.assert_no_active_block_jobs()
613 def test_ignore_write(self
):
614 self
.assert_no_active_block_jobs()
616 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
617 mode
='existing', target
=self
.target_img
,
618 on_target_error
='ignore')
619 self
.assert_qmp(result
, 'return', {})
621 event
= self
.vm
.event_wait(name
='BLOCK_JOB_ERROR')
622 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
623 self
.assert_qmp(event
, 'data/device', 'drive0')
624 self
.assert_qmp(event
, 'data/operation', 'write')
625 result
= self
.vm
.qmp('query-block-jobs')
626 self
.assert_qmp(result
, 'return[0]/paused', False)
627 self
.complete_and_wait()
630 def test_stop_write(self
):
631 self
.assert_no_active_block_jobs()
633 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
634 mode
='existing', target
=self
.target_img
,
635 on_target_error
='stop')
636 self
.assert_qmp(result
, 'return', {})
641 for event
in self
.vm
.get_qmp_events(wait
=True):
642 if event
['event'] == 'BLOCK_JOB_ERROR':
643 self
.assert_qmp(event
, 'data/device', 'drive0')
644 self
.assert_qmp(event
, 'data/operation', 'write')
646 result
= self
.vm
.qmp('query-block-jobs')
647 self
.assert_qmp(result
, 'return[0]/paused', True)
648 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
650 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
651 self
.assert_qmp(result
, 'return', {})
653 result
= self
.vm
.qmp('query-block-jobs')
654 self
.assert_qmp(result
, 'return[0]/paused', False)
655 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
657 elif event
['event'] == 'BLOCK_JOB_READY':
658 self
.assertTrue(error
, 'job completed unexpectedly')
659 self
.assert_qmp(event
, 'data/device', 'drive0')
662 self
.complete_and_wait(wait_ready
=False)
663 self
.assert_no_active_block_jobs()
666 class TestSetSpeed(iotests
.QMPTestCase
):
667 image_len
= 80 * 1024 * 1024 # MB
670 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
671 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
672 self
.vm
= iotests
.VM().add_drive(test_img
)
678 os
.remove(backing_img
)
679 os
.remove(target_img
)
681 def test_set_speed(self
):
682 self
.assert_no_active_block_jobs()
684 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
686 self
.assert_qmp(result
, 'return', {})
689 result
= self
.vm
.qmp('query-block-jobs')
690 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
691 self
.assert_qmp(result
, 'return[0]/speed', 0)
693 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
694 self
.assert_qmp(result
, 'return', {})
696 # Ensure the speed we set was accepted
697 result
= self
.vm
.qmp('query-block-jobs')
698 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
699 self
.assert_qmp(result
, 'return[0]/speed', 8 * 1024 * 1024)
701 self
.wait_ready_and_cancel()
703 # Check setting speed in drive-mirror works
704 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
705 target
=target_img
, speed
=4*1024*1024)
706 self
.assert_qmp(result
, 'return', {})
708 result
= self
.vm
.qmp('query-block-jobs')
709 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
710 self
.assert_qmp(result
, 'return[0]/speed', 4 * 1024 * 1024)
712 self
.wait_ready_and_cancel()
714 def test_set_speed_invalid(self
):
715 self
.assert_no_active_block_jobs()
717 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
718 target
=target_img
, speed
=-1)
719 self
.assert_qmp(result
, 'error/class', 'GenericError')
721 self
.assert_no_active_block_jobs()
723 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
725 self
.assert_qmp(result
, 'return', {})
727 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=-1)
728 self
.assert_qmp(result
, 'error/class', 'GenericError')
730 self
.wait_ready_and_cancel()
732 class TestUnbackedSource(iotests
.QMPTestCase
):
733 image_len
= 2 * 1024 * 1024 # MB
736 qemu_img('create', '-f', iotests
.imgfmt
, test_img
,
737 str(TestUnbackedSource
.image_len
))
738 self
.vm
= iotests
.VM().add_drive(test_img
)
744 os
.remove(target_img
)
746 def test_absolute_paths_full(self
):
747 self
.assert_no_active_block_jobs()
748 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
749 sync
='full', target
=target_img
,
750 mode
='absolute-paths')
751 self
.assert_qmp(result
, 'return', {})
752 self
.complete_and_wait()
753 self
.assert_no_active_block_jobs()
755 def test_absolute_paths_top(self
):
756 self
.assert_no_active_block_jobs()
757 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
758 sync
='top', target
=target_img
,
759 mode
='absolute-paths')
760 self
.assert_qmp(result
, 'return', {})
761 self
.complete_and_wait()
762 self
.assert_no_active_block_jobs()
764 def test_absolute_paths_none(self
):
765 self
.assert_no_active_block_jobs()
766 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
767 sync
='none', target
=target_img
,
768 mode
='absolute-paths')
769 self
.assert_qmp(result
, 'return', {})
770 self
.complete_and_wait()
771 self
.assert_no_active_block_jobs()
773 class TestGranularity(iotests
.QMPTestCase
):
774 image_len
= 10 * 1024 * 1024 # MB
777 qemu_img('create', '-f', iotests
.imgfmt
, test_img
,
778 str(TestGranularity
.image_len
))
779 qemu_io('-c', 'write 0 %d' % (self
.image_len
),
781 self
.vm
= iotests
.VM().add_drive(test_img
)
786 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
787 'target image does not match source after mirroring')
789 os
.remove(target_img
)
791 def test_granularity(self
):
792 self
.assert_no_active_block_jobs()
793 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
794 sync
='full', target
=target_img
,
795 mode
='absolute-paths', granularity
=8192)
796 self
.assert_qmp(result
, 'return', {})
798 event
= self
.vm
.get_qmp_event(wait
=60.0)
799 while event
['event'] == 'JOB_STATUS_CHANGE':
800 self
.assert_qmp(event
, 'data/id', 'drive0')
801 event
= self
.vm
.get_qmp_event(wait
=60.0)
803 # Failures will manifest as COMPLETED/ERROR.
804 self
.assert_qmp(event
, 'event', 'BLOCK_JOB_READY')
805 self
.complete_and_wait(drive
='drive0', wait_ready
=False)
806 self
.assert_no_active_block_jobs()
808 class TestRepairQuorum(iotests
.QMPTestCase
):
809 """ This class test quorum file repair using drive-mirror.
810 It's mostly a fork of TestSingleDrive """
811 image_len
= 1 * 1024 * 1024 # MB
812 IMAGES
= [ quorum_img1
, quorum_img2
, quorum_img3
]
815 self
.vm
= iotests
.VM()
817 if iotests
.qemu_default_machine
== 'pc':
818 self
.vm
.add_drive(None, 'media=cdrom', 'ide')
820 # Add each individual quorum images
821 for i
in self
.IMAGES
:
822 qemu_img('create', '-f', iotests
.imgfmt
, i
,
823 str(TestSingleDrive
.image_len
))
824 # Assign a node name to each quorum image in order to manipulate
826 opts
= "node-name=img%i" % self
.IMAGES
.index(i
)
827 self
.vm
= self
.vm
.add_drive(i
, opts
)
831 #assemble the quorum block device from the individual files
832 args
= { "driver": "quorum", "node-name": "quorum0",
833 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] }
834 if iotests
.supports_quorum():
835 result
= self
.vm
.qmp("blockdev-add", **args
)
836 self
.assert_qmp(result
, 'return', {})
841 for i
in self
.IMAGES
+ [ quorum_repair_img
, quorum_snapshot_file
]:
842 # Do a try/except because the test may have deleted some images
848 def test_complete(self
):
849 if not iotests
.supports_quorum():
852 self
.assert_no_active_block_jobs()
854 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
855 sync
='full', node_name
="repair0", replaces
="img1",
856 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
857 self
.assert_qmp(result
, 'return', {})
859 self
.complete_and_wait(drive
="job0")
860 self
.assert_has_block_node("repair0", quorum_repair_img
)
861 # TODO: a better test requiring some QEMU infrastructure will be added
862 # to check that this file is really driven by quorum
864 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
865 'target image does not match source after mirroring')
867 def test_cancel(self
):
868 if not iotests
.supports_quorum():
871 self
.assert_no_active_block_jobs()
873 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
874 sync
='full', node_name
="repair0", replaces
="img1",
875 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
876 self
.assert_qmp(result
, 'return', {})
878 self
.cancel_and_wait(drive
="job0", force
=True)
879 # here we check that the last registered quorum file has not been
880 # swapped out and unref
881 self
.assert_has_block_node(None, quorum_img3
)
884 def test_cancel_after_ready(self
):
885 if not iotests
.supports_quorum():
888 self
.assert_no_active_block_jobs()
890 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
891 sync
='full', node_name
="repair0", replaces
="img1",
892 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
893 self
.assert_qmp(result
, 'return', {})
895 self
.wait_ready_and_cancel(drive
="job0")
896 # here we check that the last registered quorum file has not been
897 # swapped out and unref
898 self
.assert_has_block_node(None, quorum_img3
)
900 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
901 'target image does not match source after mirroring')
903 def test_pause(self
):
904 if not iotests
.supports_quorum():
907 self
.assert_no_active_block_jobs()
909 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
910 sync
='full', node_name
="repair0", replaces
="img1",
911 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
912 self
.assert_qmp(result
, 'return', {})
914 self
.pause_job('job0')
916 result
= self
.vm
.qmp('query-block-jobs')
917 offset
= self
.dictpath(result
, 'return[0]/offset')
920 result
= self
.vm
.qmp('query-block-jobs')
921 self
.assert_qmp(result
, 'return[0]/offset', offset
)
923 result
= self
.vm
.qmp('block-job-resume', device
='job0')
924 self
.assert_qmp(result
, 'return', {})
926 self
.complete_and_wait(drive
="job0")
928 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
929 'target image does not match source after mirroring')
931 def test_medium_not_found(self
):
932 if not iotests
.supports_quorum():
935 if iotests
.qemu_default_machine
!= 'pc':
938 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='drive0', # CD-ROM
942 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
943 self
.assert_qmp(result
, 'error/class', 'GenericError')
945 def test_image_not_found(self
):
946 if not iotests
.supports_quorum():
949 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
950 sync
='full', node_name
='repair0', replaces
='img1',
951 mode
='existing', target
=quorum_repair_img
,
952 format
=iotests
.imgfmt
)
953 self
.assert_qmp(result
, 'error/class', 'GenericError')
955 def test_device_not_found(self
):
956 if not iotests
.supports_quorum():
959 result
= self
.vm
.qmp('drive-mirror', job_id
='job0',
960 device
='nonexistent', sync
='full',
963 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
964 self
.assert_qmp(result
, 'error/class', 'GenericError')
966 def test_wrong_sync_mode(self
):
967 if not iotests
.supports_quorum():
970 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', job_id
='job0',
973 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
974 self
.assert_qmp(result
, 'error/class', 'GenericError')
976 def test_no_node_name(self
):
977 if not iotests
.supports_quorum():
980 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
981 sync
='full', replaces
='img1',
982 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
983 self
.assert_qmp(result
, 'error/class', 'GenericError')
985 def test_nonexistent_replaces(self
):
986 if not iotests
.supports_quorum():
989 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
990 sync
='full', node_name
='repair0', replaces
='img77',
991 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
992 self
.assert_qmp(result
, 'error/class', 'GenericError')
994 def test_after_a_quorum_snapshot(self
):
995 if not iotests
.supports_quorum():
998 result
= self
.vm
.qmp('blockdev-snapshot-sync', node_name
='img1',
999 snapshot_file
=quorum_snapshot_file
,
1000 snapshot_node_name
="snap1");
1002 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
1003 sync
='full', node_name
='repair0', replaces
="img1",
1004 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
1005 self
.assert_qmp(result
, 'error/class', 'GenericError')
1007 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
1008 sync
='full', node_name
='repair0', replaces
="snap1",
1009 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
1010 self
.assert_qmp(result
, 'return', {})
1012 self
.complete_and_wait('job0')
1013 self
.assert_has_block_node("repair0", quorum_repair_img
)
1014 # TODO: a better test requiring some QEMU infrastructure will be added
1015 # to check that this file is really driven by quorum
1018 # Test mirroring with a source that does not have any parents (not even a
1020 class TestOrphanedSource(iotests
.QMPTestCase
):
1022 blk0
= { 'node-name': 'src',
1023 'driver': 'null-co' }
1025 blk1
= { 'node-name': 'dest',
1026 'driver': 'null-co' }
1028 blk2
= { 'node-name': 'dest-ro',
1029 'driver': 'null-co',
1032 self
.vm
= iotests
.VM()
1033 self
.vm
.add_blockdev(self
.vm
.qmp_to_opts(blk0
))
1034 self
.vm
.add_blockdev(self
.vm
.qmp_to_opts(blk1
))
1035 self
.vm
.add_blockdev(self
.vm
.qmp_to_opts(blk2
))
1041 def test_no_job_id(self
):
1042 self
.assert_no_active_block_jobs()
1044 result
= self
.vm
.qmp('blockdev-mirror', device
='src', sync
='full',
1046 self
.assert_qmp(result
, 'error/class', 'GenericError')
1048 def test_success(self
):
1049 self
.assert_no_active_block_jobs()
1051 result
= self
.vm
.qmp('blockdev-mirror', job_id
='job', device
='src',
1052 sync
='full', target
='dest')
1053 self
.assert_qmp(result
, 'return', {})
1055 self
.complete_and_wait('job')
1057 def test_failing_permissions(self
):
1058 self
.assert_no_active_block_jobs()
1060 result
= self
.vm
.qmp('blockdev-mirror', device
='src', sync
='full',
1062 self
.assert_qmp(result
, 'error/class', 'GenericError')
1064 if __name__
== '__main__':
1065 iotests
.main(supported_fmts
=['qcow2', 'qed'])