]>
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 result
= self
.vm
.qmp('block-job-pause', device
='drive0')
107 self
.assert_qmp(result
, 'return', {})
110 result
= self
.vm
.qmp('query-block-jobs')
111 offset
= self
.dictpath(result
, 'return[0]/offset')
114 result
= self
.vm
.qmp('query-block-jobs')
115 self
.assert_qmp(result
, 'return[0]/offset', offset
)
117 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
118 self
.assert_qmp(result
, 'return', {})
120 self
.complete_and_wait()
122 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
123 'target image does not match source after mirroring')
125 def test_small_buffer(self
):
126 self
.assert_no_active_block_jobs()
128 # A small buffer is rounded up automatically
129 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
130 buf_size
=4096, target
=self
.qmp_target
)
131 self
.assert_qmp(result
, 'return', {})
133 self
.complete_and_wait()
134 result
= self
.vm
.qmp('query-block')
135 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
137 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
138 'target image does not match source after mirroring')
140 def test_small_buffer2(self
):
141 self
.assert_no_active_block_jobs()
143 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'cluster_size=%d,size=%d'
144 % (self
.image_len
, self
.image_len
), target_img
)
145 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
146 buf_size
=65536, mode
='existing', target
=self
.qmp_target
)
147 self
.assert_qmp(result
, 'return', {})
149 self
.complete_and_wait()
150 result
= self
.vm
.qmp('query-block')
151 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
153 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
154 'target image does not match source after mirroring')
156 def test_large_cluster(self
):
157 self
.assert_no_active_block_jobs()
159 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'cluster_size=%d,backing_file=%s'
160 % (self
.image_len
, backing_img
), target_img
)
161 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
162 mode
='existing', target
=self
.qmp_target
)
163 self
.assert_qmp(result
, 'return', {})
165 self
.complete_and_wait()
166 result
= self
.vm
.qmp('query-block')
167 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
169 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
170 'target image does not match source after mirroring')
172 # Tests that the insertion of the mirror_top filter node doesn't make a
173 # difference to query-block
174 def test_implicit_node(self
):
175 self
.assert_no_active_block_jobs()
177 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
178 target
=self
.qmp_target
)
179 self
.assert_qmp(result
, 'return', {})
181 result
= self
.vm
.qmp('query-block')
182 self
.assert_qmp(result
, 'return[0]/inserted/file', test_img
)
183 self
.assert_qmp(result
, 'return[0]/inserted/drv', iotests
.imgfmt
)
184 self
.assert_qmp(result
, 'return[0]/inserted/backing_file', backing_img
)
185 self
.assert_qmp(result
, 'return[0]/inserted/backing_file_depth', 1)
186 self
.assert_qmp(result
, 'return[0]/inserted/image/filename', test_img
)
187 self
.assert_qmp(result
, 'return[0]/inserted/image/backing-image/filename', backing_img
)
189 result
= self
.vm
.qmp('query-blockstats')
190 self
.assert_qmp(result
, 'return[0]/node-name', 'top')
191 self
.assert_qmp(result
, 'return[0]/backing/node-name', 'base')
193 self
.cancel_and_wait(force
=True)
194 result
= self
.vm
.qmp('query-block')
195 self
.assert_qmp(result
, 'return[0]/inserted/file', test_img
)
196 self
.assert_qmp(result
, 'return[0]/inserted/drv', iotests
.imgfmt
)
197 self
.assert_qmp(result
, 'return[0]/inserted/backing_file', backing_img
)
198 self
.assert_qmp(result
, 'return[0]/inserted/backing_file_depth', 1)
199 self
.assert_qmp(result
, 'return[0]/inserted/image/filename', test_img
)
200 self
.assert_qmp(result
, 'return[0]/inserted/image/backing-image/filename', backing_img
)
202 result
= self
.vm
.qmp('query-blockstats')
203 self
.assert_qmp(result
, 'return[0]/node-name', 'top')
204 self
.assert_qmp(result
, 'return[0]/backing/node-name', 'base')
208 def test_medium_not_found(self
):
209 if iotests
.qemu_default_machine
!= 'pc':
212 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='ide1-cd0', sync
='full',
213 target
=self
.qmp_target
)
214 self
.assert_qmp(result
, 'error/class', 'GenericError')
216 def test_image_not_found(self
):
217 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='drive0', sync
='full',
218 mode
='existing', target
=self
.qmp_target
)
219 self
.assert_qmp(result
, 'error/class', 'GenericError')
221 def test_device_not_found(self
):
222 result
= self
.vm
.qmp(self
.qmp_cmd
, device
='nonexistent', sync
='full',
223 target
=self
.qmp_target
)
224 self
.assert_qmp(result
, 'error/class', 'GenericError')
226 class TestSingleBlockdev(TestSingleDrive
):
227 qmp_cmd
= 'blockdev-mirror'
231 TestSingleDrive
.setUp(self
)
232 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, target_img
)
233 args
= {'driver': iotests
.imgfmt
,
234 'node-name': self
.qmp_target
,
235 'file': { 'filename': target_img
, 'driver': 'file' } }
236 result
= self
.vm
.qmp("blockdev-add", **args
)
237 self
.assert_qmp(result
, 'return', {})
239 test_large_cluster
= None
240 test_image_not_found
= None
241 test_small_buffer2
= None
243 class TestSingleDriveZeroLength(TestSingleDrive
):
245 test_small_buffer2
= None
246 test_large_cluster
= None
248 class TestSingleBlockdevZeroLength(TestSingleBlockdev
):
251 class TestSingleDriveUnalignedLength(TestSingleDrive
):
252 image_len
= 1025 * 1024
253 test_small_buffer2
= None
254 test_large_cluster
= None
256 class TestSingleBlockdevUnalignedLength(TestSingleBlockdev
):
257 image_len
= 1025 * 1024
259 class TestMirrorNoBacking(iotests
.QMPTestCase
):
260 image_len
= 2 * 1024 * 1024 # MB
263 iotests
.create_image(backing_img
, TestMirrorNoBacking
.image_len
)
264 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
265 self
.vm
= iotests
.VM().add_drive(test_img
)
271 os
.remove(backing_img
)
273 os
.remove(target_backing_img
)
276 os
.remove(target_img
)
278 def test_complete(self
):
279 self
.assert_no_active_block_jobs()
281 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, target_img
)
282 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
283 mode
='existing', target
=target_img
)
284 self
.assert_qmp(result
, 'return', {})
286 self
.complete_and_wait()
287 result
= self
.vm
.qmp('query-block')
288 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
290 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
291 'target image does not match source after mirroring')
293 def test_cancel(self
):
294 self
.assert_no_active_block_jobs()
296 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, target_img
)
297 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
298 mode
='existing', target
=target_img
)
299 self
.assert_qmp(result
, 'return', {})
301 self
.wait_ready_and_cancel()
302 result
= self
.vm
.qmp('query-block')
303 self
.assert_qmp(result
, 'return[0]/inserted/file', test_img
)
305 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
306 'target image does not match source after mirroring')
308 def test_large_cluster(self
):
309 self
.assert_no_active_block_jobs()
311 # qemu-img create fails if the image is not there
312 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'size=%d'
313 %(TestMirrorNoBacking
.image_len
), target_backing_img
)
314 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'cluster_size=%d,backing_file=%s'
315 % (TestMirrorNoBacking
.image_len
, target_backing_img
), target_img
)
317 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
318 mode
='existing', target
=target_img
)
319 self
.assert_qmp(result
, 'return', {})
321 self
.complete_and_wait()
322 result
= self
.vm
.qmp('query-block')
323 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
325 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
326 'target image does not match source after mirroring')
328 class TestMirrorResized(iotests
.QMPTestCase
):
329 backing_len
= 1 * 1024 * 1024 # MB
330 image_len
= 2 * 1024 * 1024 # MB
333 iotests
.create_image(backing_img
, TestMirrorResized
.backing_len
)
334 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
335 qemu_img('resize', test_img
, '2M')
336 self
.vm
= iotests
.VM().add_drive(test_img
)
342 os
.remove(backing_img
)
344 os
.remove(target_img
)
348 def test_complete_top(self
):
349 self
.assert_no_active_block_jobs()
351 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='top',
353 self
.assert_qmp(result
, 'return', {})
355 self
.complete_and_wait()
356 result
= self
.vm
.qmp('query-block')
357 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
359 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
360 'target image does not match source after mirroring')
362 def test_complete_full(self
):
363 self
.assert_no_active_block_jobs()
365 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
367 self
.assert_qmp(result
, 'return', {})
369 self
.complete_and_wait()
370 result
= self
.vm
.qmp('query-block')
371 self
.assert_qmp(result
, 'return[0]/inserted/file', target_img
)
373 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
374 'target image does not match source after mirroring')
376 class TestReadErrors(iotests
.QMPTestCase
):
377 image_len
= 2 * 1024 * 1024 # MB
379 # this should be a multiple of twice the default granularity
380 # so that we hit this offset first in state 1
381 MIRROR_GRANULARITY
= 1024 * 1024
383 def create_blkdebug_file(self
, name
, event
, errno
):
384 file = open(name
, 'w')
403 ''' % (event
, errno
, self
.MIRROR_GRANULARITY
/ 512, event
, event
))
407 self
.blkdebug_file
= backing_img
+ ".blkdebug"
408 iotests
.create_image(backing_img
, TestReadErrors
.image_len
)
409 self
.create_blkdebug_file(self
.blkdebug_file
, "read_aio", 5)
410 qemu_img('create', '-f', iotests
.imgfmt
,
411 '-o', 'backing_file=blkdebug:%s:%s,backing_fmt=raw'
412 % (self
.blkdebug_file
, backing_img
),
414 # Write something for tests that use sync='top'
415 qemu_io('-c', 'write %d 512' % (self
.MIRROR_GRANULARITY
+ 65536),
417 self
.vm
= iotests
.VM().add_drive(test_img
)
423 os
.remove(backing_img
)
424 os
.remove(self
.blkdebug_file
)
426 def test_report_read(self
):
427 self
.assert_no_active_block_jobs()
429 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
431 self
.assert_qmp(result
, 'return', {})
436 for event
in self
.vm
.get_qmp_events(wait
=True):
437 if event
['event'] == 'BLOCK_JOB_ERROR':
438 self
.assert_qmp(event
, 'data/device', 'drive0')
439 self
.assert_qmp(event
, 'data/operation', 'read')
441 elif event
['event'] == 'BLOCK_JOB_READY':
442 self
.assertTrue(False, 'job completed unexpectedly')
443 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
444 self
.assertTrue(error
, 'job completed unexpectedly')
445 self
.assert_qmp(event
, 'data/type', 'mirror')
446 self
.assert_qmp(event
, 'data/device', 'drive0')
447 self
.assert_qmp(event
, 'data/error', 'Input/output error')
450 self
.assert_no_active_block_jobs()
453 def test_ignore_read(self
):
454 self
.assert_no_active_block_jobs()
456 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
457 target
=target_img
, on_source_error
='ignore')
458 self
.assert_qmp(result
, 'return', {})
460 event
= self
.vm
.get_qmp_event(wait
=True)
461 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
462 self
.assert_qmp(event
, 'data/device', 'drive0')
463 self
.assert_qmp(event
, 'data/operation', 'read')
464 result
= self
.vm
.qmp('query-block-jobs')
465 self
.assert_qmp(result
, 'return[0]/paused', False)
466 self
.complete_and_wait()
469 def test_large_cluster(self
):
470 self
.assert_no_active_block_jobs()
472 # Test COW into the target image. The first half of the
473 # cluster at MIRROR_GRANULARITY has to be copied from
474 # backing_img, even though sync='top'.
475 qemu_img('create', '-f', iotests
.imgfmt
, '-ocluster_size=131072,backing_file=%s' %(backing_img), target_img
)
476 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='top',
477 on_source_error
='ignore',
478 mode
='existing', target
=target_img
)
479 self
.assert_qmp(result
, 'return', {})
481 event
= self
.vm
.get_qmp_event(wait
=True)
482 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
483 self
.assert_qmp(event
, 'data/device', 'drive0')
484 self
.assert_qmp(event
, 'data/operation', 'read')
485 result
= self
.vm
.qmp('query-block-jobs')
486 self
.assert_qmp(result
, 'return[0]/paused', False)
487 self
.complete_and_wait()
490 # Detach blkdebug to compare images successfully
491 qemu_img('rebase', '-f', iotests
.imgfmt
, '-u', '-b', backing_img
, test_img
)
492 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
493 'target image does not match source after mirroring')
495 def test_stop_read(self
):
496 self
.assert_no_active_block_jobs()
498 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
499 target
=target_img
, on_source_error
='stop')
500 self
.assert_qmp(result
, 'return', {})
505 for event
in self
.vm
.get_qmp_events(wait
=True):
506 if event
['event'] == 'BLOCK_JOB_ERROR':
507 self
.assert_qmp(event
, 'data/device', 'drive0')
508 self
.assert_qmp(event
, 'data/operation', 'read')
510 result
= self
.vm
.qmp('query-block-jobs')
511 self
.assert_qmp(result
, 'return[0]/paused', True)
512 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
514 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
515 self
.assert_qmp(result
, 'return', {})
517 elif event
['event'] == 'BLOCK_JOB_READY':
518 self
.assertTrue(error
, 'job completed unexpectedly')
519 self
.assert_qmp(event
, 'data/device', 'drive0')
522 result
= self
.vm
.qmp('query-block-jobs')
523 self
.assert_qmp(result
, 'return[0]/paused', False)
524 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
526 self
.complete_and_wait(wait_ready
=False)
527 self
.assert_no_active_block_jobs()
530 class TestWriteErrors(iotests
.QMPTestCase
):
531 image_len
= 2 * 1024 * 1024 # MB
533 # this should be a multiple of twice the default granularity
534 # so that we hit this offset first in state 1
535 MIRROR_GRANULARITY
= 1024 * 1024
537 def create_blkdebug_file(self
, name
, event
, errno
):
538 file = open(name
, 'w')
557 ''' % (event
, errno
, self
.MIRROR_GRANULARITY
/ 512, event
, event
))
561 self
.blkdebug_file
= target_img
+ ".blkdebug"
562 iotests
.create_image(backing_img
, TestWriteErrors
.image_len
)
563 self
.create_blkdebug_file(self
.blkdebug_file
, "write_aio", 5)
564 qemu_img('create', '-f', iotests
.imgfmt
, '-obacking_file=%s' %(backing_img), test_img
)
565 self
.vm
= iotests
.VM().add_drive(test_img
)
566 self
.target_img
= 'blkdebug:%s:%s' % (self
.blkdebug_file
, target_img
)
567 qemu_img('create', '-f', iotests
.imgfmt
, '-osize=%d' %(TestWriteErrors
.image_len
), target_img
)
573 os
.remove(backing_img
)
574 os
.remove(self
.blkdebug_file
)
576 def test_report_write(self
):
577 self
.assert_no_active_block_jobs()
579 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
580 mode
='existing', target
=self
.target_img
)
581 self
.assert_qmp(result
, 'return', {})
586 for event
in self
.vm
.get_qmp_events(wait
=True):
587 if event
['event'] == 'BLOCK_JOB_ERROR':
588 self
.assert_qmp(event
, 'data/device', 'drive0')
589 self
.assert_qmp(event
, 'data/operation', 'write')
591 elif event
['event'] == 'BLOCK_JOB_READY':
592 self
.assertTrue(False, 'job completed unexpectedly')
593 elif event
['event'] == 'BLOCK_JOB_COMPLETED':
594 self
.assertTrue(error
, 'job completed unexpectedly')
595 self
.assert_qmp(event
, 'data/type', 'mirror')
596 self
.assert_qmp(event
, 'data/device', 'drive0')
597 self
.assert_qmp(event
, 'data/error', 'Input/output error')
600 self
.assert_no_active_block_jobs()
603 def test_ignore_write(self
):
604 self
.assert_no_active_block_jobs()
606 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
607 mode
='existing', target
=self
.target_img
,
608 on_target_error
='ignore')
609 self
.assert_qmp(result
, 'return', {})
611 event
= self
.vm
.get_qmp_event(wait
=True)
612 self
.assertEquals(event
['event'], 'BLOCK_JOB_ERROR')
613 self
.assert_qmp(event
, 'data/device', 'drive0')
614 self
.assert_qmp(event
, 'data/operation', 'write')
615 result
= self
.vm
.qmp('query-block-jobs')
616 self
.assert_qmp(result
, 'return[0]/paused', False)
617 self
.complete_and_wait()
620 def test_stop_write(self
):
621 self
.assert_no_active_block_jobs()
623 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
624 mode
='existing', target
=self
.target_img
,
625 on_target_error
='stop')
626 self
.assert_qmp(result
, 'return', {})
631 for event
in self
.vm
.get_qmp_events(wait
=True):
632 if event
['event'] == 'BLOCK_JOB_ERROR':
633 self
.assert_qmp(event
, 'data/device', 'drive0')
634 self
.assert_qmp(event
, 'data/operation', 'write')
636 result
= self
.vm
.qmp('query-block-jobs')
637 self
.assert_qmp(result
, 'return[0]/paused', True)
638 self
.assert_qmp(result
, 'return[0]/io-status', 'failed')
640 result
= self
.vm
.qmp('block-job-resume', device
='drive0')
641 self
.assert_qmp(result
, 'return', {})
643 result
= self
.vm
.qmp('query-block-jobs')
644 self
.assert_qmp(result
, 'return[0]/paused', False)
645 self
.assert_qmp(result
, 'return[0]/io-status', 'ok')
647 elif event
['event'] == 'BLOCK_JOB_READY':
648 self
.assertTrue(error
, 'job completed unexpectedly')
649 self
.assert_qmp(event
, 'data/device', 'drive0')
652 self
.complete_and_wait(wait_ready
=False)
653 self
.assert_no_active_block_jobs()
656 class TestSetSpeed(iotests
.QMPTestCase
):
657 image_len
= 80 * 1024 * 1024 # MB
660 qemu_img('create', backing_img
, str(TestSetSpeed
.image_len
))
661 qemu_img('create', '-f', iotests
.imgfmt
, '-o', 'backing_file=%s' % backing_img
, test_img
)
662 self
.vm
= iotests
.VM().add_drive(test_img
)
668 os
.remove(backing_img
)
669 os
.remove(target_img
)
671 def test_set_speed(self
):
672 self
.assert_no_active_block_jobs()
674 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
676 self
.assert_qmp(result
, 'return', {})
679 result
= self
.vm
.qmp('query-block-jobs')
680 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
681 self
.assert_qmp(result
, 'return[0]/speed', 0)
683 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=8 * 1024 * 1024)
684 self
.assert_qmp(result
, 'return', {})
686 # Ensure the speed we set was accepted
687 result
= self
.vm
.qmp('query-block-jobs')
688 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
689 self
.assert_qmp(result
, 'return[0]/speed', 8 * 1024 * 1024)
691 self
.wait_ready_and_cancel()
693 # Check setting speed in drive-mirror works
694 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
695 target
=target_img
, speed
=4*1024*1024)
696 self
.assert_qmp(result
, 'return', {})
698 result
= self
.vm
.qmp('query-block-jobs')
699 self
.assert_qmp(result
, 'return[0]/device', 'drive0')
700 self
.assert_qmp(result
, 'return[0]/speed', 4 * 1024 * 1024)
702 self
.wait_ready_and_cancel()
704 def test_set_speed_invalid(self
):
705 self
.assert_no_active_block_jobs()
707 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
708 target
=target_img
, speed
=-1)
709 self
.assert_qmp(result
, 'error/class', 'GenericError')
711 self
.assert_no_active_block_jobs()
713 result
= self
.vm
.qmp('drive-mirror', device
='drive0', sync
='full',
715 self
.assert_qmp(result
, 'return', {})
717 result
= self
.vm
.qmp('block-job-set-speed', device
='drive0', speed
=-1)
718 self
.assert_qmp(result
, 'error/class', 'GenericError')
720 self
.wait_ready_and_cancel()
722 class TestUnbackedSource(iotests
.QMPTestCase
):
723 image_len
= 2 * 1024 * 1024 # MB
726 qemu_img('create', '-f', iotests
.imgfmt
, test_img
,
727 str(TestUnbackedSource
.image_len
))
728 self
.vm
= iotests
.VM().add_drive(test_img
)
734 os
.remove(target_img
)
736 def test_absolute_paths_full(self
):
737 self
.assert_no_active_block_jobs()
738 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
739 sync
='full', target
=target_img
,
740 mode
='absolute-paths')
741 self
.assert_qmp(result
, 'return', {})
742 self
.complete_and_wait()
743 self
.assert_no_active_block_jobs()
745 def test_absolute_paths_top(self
):
746 self
.assert_no_active_block_jobs()
747 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
748 sync
='top', target
=target_img
,
749 mode
='absolute-paths')
750 self
.assert_qmp(result
, 'return', {})
751 self
.complete_and_wait()
752 self
.assert_no_active_block_jobs()
754 def test_absolute_paths_none(self
):
755 self
.assert_no_active_block_jobs()
756 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
757 sync
='none', target
=target_img
,
758 mode
='absolute-paths')
759 self
.assert_qmp(result
, 'return', {})
760 self
.complete_and_wait()
761 self
.assert_no_active_block_jobs()
763 class TestGranularity(iotests
.QMPTestCase
):
764 image_len
= 10 * 1024 * 1024 # MB
767 qemu_img('create', '-f', iotests
.imgfmt
, test_img
,
768 str(TestGranularity
.image_len
))
769 qemu_io('-c', 'write 0 %d' % (self
.image_len
),
771 self
.vm
= iotests
.VM().add_drive(test_img
)
776 self
.assertTrue(iotests
.compare_images(test_img
, target_img
),
777 'target image does not match source after mirroring')
779 os
.remove(target_img
)
781 def test_granularity(self
):
782 self
.assert_no_active_block_jobs()
783 result
= self
.vm
.qmp('drive-mirror', device
='drive0',
784 sync
='full', target
=target_img
,
785 mode
='absolute-paths', granularity
=8192)
786 self
.assert_qmp(result
, 'return', {})
787 event
= self
.vm
.get_qmp_event(wait
=60.0)
788 # Failures will manifest as COMPLETED/ERROR.
789 self
.assert_qmp(event
, 'event', 'BLOCK_JOB_READY')
790 self
.complete_and_wait(drive
='drive0', wait_ready
=False)
791 self
.assert_no_active_block_jobs()
793 class TestRepairQuorum(iotests
.QMPTestCase
):
794 """ This class test quorum file repair using drive-mirror.
795 It's mostly a fork of TestSingleDrive """
796 image_len
= 1 * 1024 * 1024 # MB
797 IMAGES
= [ quorum_img1
, quorum_img2
, quorum_img3
]
800 self
.vm
= iotests
.VM()
802 if iotests
.qemu_default_machine
== 'pc':
803 self
.vm
.add_drive(None, 'media=cdrom', 'ide')
805 # Add each individual quorum images
806 for i
in self
.IMAGES
:
807 qemu_img('create', '-f', iotests
.imgfmt
, i
,
808 str(TestSingleDrive
.image_len
))
809 # Assign a node name to each quorum image in order to manipulate
811 opts
= "node-name=img%i" % self
.IMAGES
.index(i
)
812 self
.vm
= self
.vm
.add_drive(i
, opts
)
816 #assemble the quorum block device from the individual files
817 args
= { "driver": "quorum", "node-name": "quorum0",
818 "vote-threshold": 2, "children": [ "img0", "img1", "img2" ] }
819 if iotests
.supports_quorum():
820 result
= self
.vm
.qmp("blockdev-add", **args
)
821 self
.assert_qmp(result
, 'return', {})
826 for i
in self
.IMAGES
+ [ quorum_repair_img
]:
827 # Do a try/except because the test may have deleted some images
833 def test_complete(self
):
834 if not iotests
.supports_quorum():
837 self
.assert_no_active_block_jobs()
839 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
840 sync
='full', node_name
="repair0", replaces
="img1",
841 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
842 self
.assert_qmp(result
, 'return', {})
844 self
.complete_and_wait(drive
="job0")
845 self
.assert_has_block_node("repair0", quorum_repair_img
)
846 # TODO: a better test requiring some QEMU infrastructure will be added
847 # to check that this file is really driven by quorum
849 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
850 'target image does not match source after mirroring')
852 def test_cancel(self
):
853 if not iotests
.supports_quorum():
856 self
.assert_no_active_block_jobs()
858 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
859 sync
='full', node_name
="repair0", replaces
="img1",
860 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
861 self
.assert_qmp(result
, 'return', {})
863 self
.cancel_and_wait(drive
="job0", force
=True)
864 # here we check that the last registered quorum file has not been
865 # swapped out and unref
866 self
.assert_has_block_node(None, quorum_img3
)
869 def test_cancel_after_ready(self
):
870 if not iotests
.supports_quorum():
873 self
.assert_no_active_block_jobs()
875 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
876 sync
='full', node_name
="repair0", replaces
="img1",
877 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
878 self
.assert_qmp(result
, 'return', {})
880 self
.wait_ready_and_cancel(drive
="job0")
881 # here we check that the last registered quorum file has not been
882 # swapped out and unref
883 self
.assert_has_block_node(None, quorum_img3
)
885 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
886 'target image does not match source after mirroring')
888 def test_pause(self
):
889 if not iotests
.supports_quorum():
892 self
.assert_no_active_block_jobs()
894 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
895 sync
='full', node_name
="repair0", replaces
="img1",
896 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
897 self
.assert_qmp(result
, 'return', {})
899 result
= self
.vm
.qmp('block-job-pause', device
='job0')
900 self
.assert_qmp(result
, 'return', {})
903 result
= self
.vm
.qmp('query-block-jobs')
904 offset
= self
.dictpath(result
, 'return[0]/offset')
907 result
= self
.vm
.qmp('query-block-jobs')
908 self
.assert_qmp(result
, 'return[0]/offset', offset
)
910 result
= self
.vm
.qmp('block-job-resume', device
='job0')
911 self
.assert_qmp(result
, 'return', {})
913 self
.complete_and_wait(drive
="job0")
915 self
.assertTrue(iotests
.compare_images(quorum_img2
, quorum_repair_img
),
916 'target image does not match source after mirroring')
918 def test_medium_not_found(self
):
919 if not iotests
.supports_quorum():
922 if iotests
.qemu_default_machine
!= 'pc':
925 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='drive0', # CD-ROM
929 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
930 self
.assert_qmp(result
, 'error/class', 'GenericError')
932 def test_image_not_found(self
):
933 if not iotests
.supports_quorum():
936 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
937 sync
='full', node_name
='repair0', replaces
='img1',
938 mode
='existing', target
=quorum_repair_img
,
939 format
=iotests
.imgfmt
)
940 self
.assert_qmp(result
, 'error/class', 'GenericError')
942 def test_device_not_found(self
):
943 if not iotests
.supports_quorum():
946 result
= self
.vm
.qmp('drive-mirror', job_id
='job0',
947 device
='nonexistent', sync
='full',
950 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
951 self
.assert_qmp(result
, 'error/class', 'GenericError')
953 def test_wrong_sync_mode(self
):
954 if not iotests
.supports_quorum():
957 result
= self
.vm
.qmp('drive-mirror', device
='quorum0', job_id
='job0',
960 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
961 self
.assert_qmp(result
, 'error/class', 'GenericError')
963 def test_no_node_name(self
):
964 if not iotests
.supports_quorum():
967 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
968 sync
='full', replaces
='img1',
969 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
970 self
.assert_qmp(result
, 'error/class', 'GenericError')
972 def test_nonexistent_replaces(self
):
973 if not iotests
.supports_quorum():
976 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
977 sync
='full', node_name
='repair0', replaces
='img77',
978 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
979 self
.assert_qmp(result
, 'error/class', 'GenericError')
981 def test_after_a_quorum_snapshot(self
):
982 if not iotests
.supports_quorum():
985 result
= self
.vm
.qmp('blockdev-snapshot-sync', node_name
='img1',
986 snapshot_file
=quorum_snapshot_file
,
987 snapshot_node_name
="snap1");
989 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
990 sync
='full', node_name
='repair0', replaces
="img1",
991 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
992 self
.assert_qmp(result
, 'error/class', 'GenericError')
994 result
= self
.vm
.qmp('drive-mirror', job_id
='job0', device
='quorum0',
995 sync
='full', node_name
='repair0', replaces
="snap1",
996 target
=quorum_repair_img
, format
=iotests
.imgfmt
)
997 self
.assert_qmp(result
, 'return', {})
999 self
.complete_and_wait('job0')
1000 self
.assert_has_block_node("repair0", quorum_repair_img
)
1001 # TODO: a better test requiring some QEMU infrastructure will be added
1002 # to check that this file is really driven by quorum
1005 # Test mirroring with a source that does not have any parents (not even a
1007 class TestOrphanedSource(iotests
.QMPTestCase
):
1009 blk0
= { 'node-name': 'src',
1010 'driver': 'null-co' }
1012 blk1
= { 'node-name': 'dest',
1013 'driver': 'null-co' }
1015 blk2
= { 'node-name': 'dest-ro',
1016 'driver': 'null-co',
1019 self
.vm
= iotests
.VM()
1020 self
.vm
.add_blockdev(self
.qmp_to_opts(blk0
))
1021 self
.vm
.add_blockdev(self
.qmp_to_opts(blk1
))
1022 self
.vm
.add_blockdev(self
.qmp_to_opts(blk2
))
1028 def test_no_job_id(self
):
1029 self
.assert_no_active_block_jobs()
1031 result
= self
.vm
.qmp('blockdev-mirror', device
='src', sync
='full',
1033 self
.assert_qmp(result
, 'error/class', 'GenericError')
1035 def test_success(self
):
1036 self
.assert_no_active_block_jobs()
1038 result
= self
.vm
.qmp('blockdev-mirror', job_id
='job', device
='src',
1039 sync
='full', target
='dest')
1040 self
.assert_qmp(result
, 'return', {})
1042 self
.complete_and_wait('job')
1044 def test_failing_permissions(self
):
1045 self
.assert_no_active_block_jobs()
1047 result
= self
.vm
.qmp('blockdev-mirror', device
='src', sync
='full',
1049 self
.assert_qmp(result
, 'error/class', 'GenericError')
1051 if __name__
== '__main__':
1052 iotests
.main(supported_fmts
=['qcow2', 'qed'])