target-arm: Fix warn about implicit conversion
[qemu/cris-port.git] / tests / qemu-iotests / 041
blobcbf5e0ba5c60f794a8d5b5ac4aca9f9d1e78b90a
1 #!/usr/bin/env python
3 # Tests for image mirroring.
5 # Copyright (C) 2012 Red Hat, Inc.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 import time
22 import os
23 import iotests
24 from iotests import qemu_img, qemu_io
26 backing_img = os.path.join(iotests.test_dir, 'backing.img')
27 target_backing_img = os.path.join(iotests.test_dir, 'target-backing.img')
28 test_img = os.path.join(iotests.test_dir, 'test.img')
29 target_img = os.path.join(iotests.test_dir, 'target.img')
31 quorum_img1 = os.path.join(iotests.test_dir, 'quorum1.img')
32 quorum_img2 = os.path.join(iotests.test_dir, 'quorum2.img')
33 quorum_img3 = os.path.join(iotests.test_dir, 'quorum3.img')
34 quorum_repair_img = os.path.join(iotests.test_dir, 'quorum_repair.img')
35 quorum_snapshot_file = os.path.join(iotests.test_dir, 'quorum_snapshot.img')
37 class TestSingleDrive(iotests.QMPTestCase):
38 image_len = 1 * 1024 * 1024 # MB
39 qmp_cmd = 'drive-mirror'
40 qmp_target = target_img
41 not_found_error = 'DeviceNotFound'
43 def setUp(self):
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')
49 self.vm.launch()
51 def tearDown(self):
52 self.vm.shutdown()
53 os.remove(test_img)
54 os.remove(backing_img)
55 try:
56 os.remove(target_img)
57 except OSError:
58 pass
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)
70 self.vm.shutdown()
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)
84 self.vm.shutdown()
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)
96 self.vm.shutdown()
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', {})
110 time.sleep(1)
111 result = self.vm.qmp('query-block-jobs')
112 offset = self.dictpath(result, 'return[0]/offset')
114 time.sleep(1)
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()
122 self.vm.shutdown()
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)
137 self.vm.shutdown()
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)
153 self.vm.shutdown()
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)
169 self.vm.shutdown()
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':
175 return
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'
193 qmp_target = 'node1'
194 not_found_error = 'GenericError'
196 def setUp(self):
197 TestSingleDrive.setUp(self)
198 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, target_img)
199 args = {'options':
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):
211 image_len = 0
212 test_small_buffer2 = None
213 test_large_cluster = None
215 class TestSingleBlockdevZeroLength(TestSingleBlockdev):
216 image_len = 0
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
229 def setUp(self):
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)
233 self.vm.launch()
235 def tearDown(self):
236 self.vm.shutdown()
237 os.remove(test_img)
238 os.remove(backing_img)
239 try:
240 os.remove(target_backing_img)
241 except:
242 pass
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)
256 self.vm.shutdown()
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)
271 self.vm.shutdown()
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)
291 self.vm.shutdown()
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
299 def setUp(self):
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)
304 self.vm.launch()
306 def tearDown(self):
307 self.vm.shutdown()
308 os.remove(test_img)
309 os.remove(backing_img)
310 try:
311 os.remove(target_img)
312 except OSError:
313 pass
315 def test_complete_top(self):
316 self.assert_no_active_block_jobs()
318 result = self.vm.qmp('drive-mirror', device='drive0', sync='top',
319 target=target_img)
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)
325 self.vm.shutdown()
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',
333 target=target_img)
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)
339 self.vm.shutdown()
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')
352 file.write('''
353 [inject-error]
354 state = "1"
355 event = "%s"
356 errno = "%d"
357 immediately = "off"
358 once = "on"
359 sector = "%d"
361 [set-state]
362 state = "1"
363 event = "%s"
364 new_state = "2"
366 [set-state]
367 state = "2"
368 event = "%s"
369 new_state = "1"
370 ''' % (event, errno, self.MIRROR_GRANULARITY / 512, event, event))
371 file.close()
373 def setUp(self):
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),
380 test_img)
381 # Write something for tests that use sync='top'
382 qemu_io('-c', 'write %d 512' % (self.MIRROR_GRANULARITY + 65536),
383 test_img)
384 self.vm = iotests.VM().add_drive(test_img)
385 self.vm.launch()
387 def tearDown(self):
388 self.vm.shutdown()
389 os.remove(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',
397 target=target_img)
398 self.assert_qmp(result, 'return', {})
400 completed = False
401 error = False
402 while not completed:
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')
407 error = True
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')
415 completed = True
417 self.assert_no_active_block_jobs()
418 self.vm.shutdown()
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()
434 self.vm.shutdown()
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()
455 self.vm.shutdown()
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', {})
469 error = False
470 ready = False
471 while not ready:
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', {})
483 error = True
484 elif event['event'] == 'BLOCK_JOB_READY':
485 self.assertTrue(error, 'job completed unexpectedly')
486 self.assert_qmp(event, 'data/device', 'drive0')
487 ready = True
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()
495 self.vm.shutdown()
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')
506 file.write('''
507 [inject-error]
508 state = "1"
509 event = "%s"
510 errno = "%d"
511 immediately = "off"
512 once = "on"
513 sector = "%d"
515 [set-state]
516 state = "1"
517 event = "%s"
518 new_state = "2"
520 [set-state]
521 state = "2"
522 event = "%s"
523 new_state = "1"
524 ''' % (event, errno, self.MIRROR_GRANULARITY / 512, event, event))
525 file.close()
527 def setUp(self):
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)
535 self.vm.launch()
537 def tearDown(self):
538 self.vm.shutdown()
539 os.remove(test_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', {})
550 completed = False
551 error = False
552 while not completed:
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')
557 error = True
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')
565 completed = True
567 self.assert_no_active_block_jobs()
568 self.vm.shutdown()
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()
585 self.vm.shutdown()
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', {})
595 error = False
596 ready = False
597 while not ready:
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')
613 error = True
614 elif event['event'] == 'BLOCK_JOB_READY':
615 self.assertTrue(error, 'job completed unexpectedly')
616 self.assert_qmp(event, 'data/device', 'drive0')
617 ready = True
619 self.complete_and_wait(wait_ready=False)
620 self.assert_no_active_block_jobs()
621 self.vm.shutdown()
623 class TestSetSpeed(iotests.QMPTestCase):
624 image_len = 80 * 1024 * 1024 # MB
626 def setUp(self):
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)
630 self.vm.launch()
632 def tearDown(self):
633 self.vm.shutdown()
634 os.remove(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',
642 target=target_img)
643 self.assert_qmp(result, 'return', {})
645 # Default speed is 0
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',
681 target=target_img)
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
692 def setUp(self):
693 qemu_img('create', '-f', iotests.imgfmt, test_img,
694 str(TestUnbackedSource.image_len))
695 self.vm = iotests.VM().add_drive(test_img)
696 self.vm.launch()
698 def tearDown(self):
699 self.vm.shutdown()
700 os.remove(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
733 def setUp(self):
734 qemu_img('create', '-f', iotests.imgfmt, test_img,
735 str(TestGranularity.image_len))
736 qemu_io('-c', 'write 0 %d' % (self.image_len),
737 test_img)
738 self.vm = iotests.VM().add_drive(test_img)
739 self.vm.launch()
741 def tearDown(self):
742 self.vm.shutdown()
743 self.assertTrue(iotests.compare_images(test_img, target_img),
744 'target image does not match source after mirroring')
745 os.remove(test_img)
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')
769 def setUp(self):
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
780 # them
781 opts = "node-name=img%i" % self.IMAGES.index(i)
782 self.vm = self.vm.add_drive(i, opts)
784 self.vm.launch()
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', {})
794 def tearDown(self):
795 self.vm.shutdown()
796 for i in self.IMAGES + [ quorum_repair_img ]:
797 # Do a try/except because the test may have deleted some images
798 try:
799 os.remove(i)
800 except OSError:
801 pass
803 def test_complete(self):
804 if not self.has_quorum():
805 return
807 self.assert_no_active_block_jobs()
809 result = self.vm.qmp('drive-mirror', device='quorum0', sync='full',
810 node_name="repair0",
811 replaces="img1",
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
819 self.vm.shutdown()
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():
825 return
827 self.assert_no_active_block_jobs()
829 result = self.vm.qmp('drive-mirror', device='quorum0', sync='full',
830 node_name="repair0",
831 replaces="img1",
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)
839 self.vm.shutdown()
841 def test_cancel_after_ready(self):
842 if not self.has_quorum():
843 return
845 self.assert_no_active_block_jobs()
847 result = self.vm.qmp('drive-mirror', device='quorum0', sync='full',
848 node_name="repair0",
849 replaces="img1",
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)
857 self.vm.shutdown()
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():
863 return
865 self.assert_no_active_block_jobs()
867 result = self.vm.qmp('drive-mirror', device='quorum0', sync='full',
868 node_name="repair0",
869 replaces="img1",
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', {})
876 time.sleep(1)
877 result = self.vm.qmp('query-block-jobs')
878 offset = self.dictpath(result, 'return[0]/offset')
880 time.sleep(1)
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")
888 self.vm.shutdown()
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():
894 return
896 if iotests.qemu_default_machine != 'pc':
897 return
899 result = self.vm.qmp('drive-mirror', device='drive0', # CD-ROM
900 sync='full',
901 node_name='repair0',
902 replaces='img1',
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():
908 return
910 result = self.vm.qmp('drive-mirror', device='quorum0', sync='full',
911 node_name='repair0',
912 replaces='img1',
913 mode='existing',
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():
919 return
921 result = self.vm.qmp('drive-mirror', device='nonexistent', sync='full',
922 node_name='repair0',
923 replaces='img1',
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():
929 return
931 result = self.vm.qmp('drive-mirror', device='quorum0',
932 node_name='repair0',
933 replaces='img1',
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():
939 return
941 result = self.vm.qmp('drive-mirror', device='quorum0', sync='full',
942 replaces='img1',
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():
948 return
950 result = self.vm.qmp('drive-mirror', device='quorum0', sync='full',
951 node_name='repair0',
952 replaces='img77',
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():
958 return
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',
965 node_name='repair0',
966 replaces="img1",
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',
971 node_name='repair0',
972 replaces="snap1",
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
980 self.vm.shutdown()
982 if __name__ == '__main__':
983 iotests.main(supported_fmts=['qcow2', 'qed'])