4 # Tests for drive-backup and blockdev-backup
6 # Copyright (C) 2013, 2014 Red Hat, Inc.
10 # This program is free software; you can redistribute it and/or modify
11 # it under the terms of the GNU General Public License as published by
12 # the Free Software Foundation; either version 2 of the License, or
13 # (at your option) any later version.
15 # This program is distributed in the hope that it will be useful,
16 # but WITHOUT ANY WARRANTY; without even the implied warranty of
17 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 # GNU General Public License for more details.
20 # You should have received a copy of the GNU General Public License
21 # along with this program. If not, see <http://www.gnu.org/licenses/>.
27 from iotests import qemu_img, qemu_io
29 test_img = os.path.join(iotests.test_dir, 'test.img')
30 target_img = os.path.join(iotests.test_dir, 'target.img')
31 blockdev_target_img = os.path.join(iotests.test_dir, 'blockdev-target.img')
33 image_len = 64 * 1024 * 1024 # MB
36 qemu_img('create', '-f', iotests.imgfmt, test_img, str(image_len))
37 qemu_io('-f', iotests.imgfmt, '-c', 'write -P0x11 0 64k', test_img)
38 qemu_io('-f', iotests.imgfmt, '-c', 'write -P0x00 64k 128k', test_img)
39 qemu_io('-f', iotests.imgfmt, '-c', 'write -P0x22 162k 32k', test_img)
40 qemu_io('-f', iotests.imgfmt, '-c', 'write -P0xd5 1M 32k', test_img)
41 qemu_io('-f', iotests.imgfmt, '-c', 'write -P0xdc 32M 124k', test_img)
42 qemu_io('-f', iotests.imgfmt, '-c', 'write -P0x33 67043328 64k', test_img)
48 class TestSingleDrive(iotests.QMPTestCase):
50 qemu_img('create', '-f', iotests.imgfmt, blockdev_target_img, str(image_len))
52 self.vm = iotests.VM()
53 self.vm.add_drive('blkdebug::' + test_img, 'node-name=source')
54 self.vm.add_drive(blockdev_target_img, 'node-name=target',
56 if iotests.qemu_default_machine == 'pc':
57 self.vm.add_drive(None, 'media=cdrom', 'ide')
62 os.remove(blockdev_target_img)
68 def do_test_cancel(self, cmd, target):
69 self.assert_no_active_block_jobs()
71 self.vm.pause_drive('drive0')
72 result = self.vm.qmp(cmd, device='drive0', target=target, sync='full')
73 self.assert_qmp(result, 'return', {})
75 event = self.cancel_and_wait(resume=True)
76 self.assert_qmp(event, 'data/type', 'backup')
78 def test_cancel_drive_backup(self):
79 self.do_test_cancel('drive-backup', target_img)
81 def test_cancel_blockdev_backup(self):
82 self.do_test_cancel('blockdev-backup', 'drive1')
84 def do_test_pause(self, cmd, target, image):
85 self.assert_no_active_block_jobs()
87 self.vm.pause_drive('drive0')
88 result = self.vm.qmp(cmd, device='drive0',
89 target=target, sync='full')
90 self.assert_qmp(result, 'return', {})
92 self.pause_job('drive0', wait=False)
93 self.vm.resume_drive('drive0')
94 self.pause_wait('drive0')
96 result = self.vm.qmp('query-block-jobs')
97 offset = self.dictpath(result, 'return[0]/offset')
100 result = self.vm.qmp('query-block-jobs')
101 self.assert_qmp(result, 'return[0]/offset', offset)
103 result = self.vm.qmp('block-job-resume', device='drive0')
104 self.assert_qmp(result, 'return', {})
106 self.wait_until_completed()
109 self.assertTrue(iotests.compare_images(test_img, image),
110 'target image does not match source after backup')
112 def test_pause_drive_backup(self):
113 self.do_test_pause('drive-backup', target_img, target_img)
115 def test_pause_blockdev_backup(self):
116 self.do_test_pause('blockdev-backup', 'drive1', blockdev_target_img)
118 def do_test_resize_blockdev_backup(self, device, node):
120 result = self.vm.qmp('block_resize', device=device, size=65536)
121 self.assert_qmp(result, 'error/class', 'GenericError')
123 result = self.vm.qmp('block_resize', node_name=node, size=65536)
124 self.assert_qmp(result, 'error/class', 'GenericError')
126 result = self.vm.qmp('blockdev-backup', job_id='job0', device='drive0',
127 target='drive1', sync='full', auto_finalize=False,
129 self.assert_qmp(result, 'return', {})
131 self.vm.run_job('job0', auto_finalize=False, pre_finalize=pre_finalize)
133 def test_source_resize_blockdev_backup(self):
134 self.do_test_resize_blockdev_backup('drive0', 'source')
136 def test_target_resize_blockdev_backup(self):
137 self.do_test_resize_blockdev_backup('drive1', 'target')
139 def do_test_target_size(self, size):
140 result = self.vm.qmp('block_resize', device='drive1', size=size)
141 self.assert_qmp(result, 'return', {})
143 result = self.vm.qmp('blockdev-backup', job_id='job0', device='drive0',
144 target='drive1', sync='full')
145 self.assert_qmp(result, 'error/class', 'GenericError')
147 def test_small_target(self):
148 self.do_test_target_size(image_len // 2)
150 def test_large_target(self):
151 self.do_test_target_size(image_len * 2)
153 def test_medium_not_found(self):
154 if iotests.qemu_default_machine != 'pc':
157 result = self.vm.qmp('drive-backup', device='drive2', # CD-ROM
158 target=target_img, sync='full')
159 self.assert_qmp(result, 'error/class', 'GenericError')
161 def test_medium_not_found_blockdev_backup(self):
162 if iotests.qemu_default_machine != 'pc':
165 result = self.vm.qmp('blockdev-backup', device='drive2', # CD-ROM
166 target='drive1', sync='full')
167 self.assert_qmp(result, 'error/class', 'GenericError')
169 def test_image_not_found(self):
170 result = self.vm.qmp('drive-backup', device='drive0',
171 target=target_img, sync='full', mode='existing')
172 self.assert_qmp(result, 'error/class', 'GenericError')
174 def test_invalid_format(self):
175 result = self.vm.qmp('drive-backup', device='drive0',
176 target=target_img, sync='full',
177 format='spaghetti-noodles')
178 self.assert_qmp(result, 'error/class', 'GenericError')
180 def do_test_device_not_found(self, cmd, **args):
181 result = self.vm.qmp(cmd, **args)
182 self.assert_qmp(result, 'error/class', 'GenericError')
184 def test_device_not_found(self):
185 self.do_test_device_not_found('drive-backup', device='nonexistent',
186 target=target_img, sync='full')
188 self.do_test_device_not_found('blockdev-backup', device='nonexistent',
189 target='drive0', sync='full')
191 self.do_test_device_not_found('blockdev-backup', device='drive0',
192 target='nonexistent', sync='full')
194 self.do_test_device_not_found('blockdev-backup', device='nonexistent',
195 target='nonexistent', sync='full')
197 def test_target_is_source(self):
198 result = self.vm.qmp('blockdev-backup', device='drive0',
199 target='drive0', sync='full')
200 self.assert_qmp(result, 'error/class', 'GenericError')
202 class TestSetSpeed(iotests.QMPTestCase):
204 qemu_img('create', '-f', iotests.imgfmt, blockdev_target_img, str(image_len))
206 self.vm = iotests.VM().add_drive('blkdebug::' + test_img)
207 self.vm.add_drive(blockdev_target_img, interface="none")
212 os.remove(blockdev_target_img)
214 os.remove(target_img)
218 def do_test_set_speed(self, cmd, target):
219 self.assert_no_active_block_jobs()
221 self.vm.pause_drive('drive0')
222 result = self.vm.qmp(cmd, device='drive0', target=target, sync='full')
223 self.assert_qmp(result, 'return', {})
226 result = self.vm.qmp('query-block-jobs')
227 self.assert_qmp(result, 'return[0]/device', 'drive0')
228 self.assert_qmp(result, 'return[0]/speed', 0)
230 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=8 * 1024 * 1024)
231 self.assert_qmp(result, 'return', {})
233 # Ensure the speed we set was accepted
234 result = self.vm.qmp('query-block-jobs')
235 self.assert_qmp(result, 'return[0]/device', 'drive0')
236 self.assert_qmp(result, 'return[0]/speed', 8 * 1024 * 1024)
238 event = self.cancel_and_wait(resume=True)
239 self.assert_qmp(event, 'data/type', 'backup')
241 # Check setting speed option works
242 self.vm.pause_drive('drive0')
243 result = self.vm.qmp(cmd, device='drive0',
244 target=target, sync='full', speed=4*1024*1024)
245 self.assert_qmp(result, 'return', {})
247 result = self.vm.qmp('query-block-jobs')
248 self.assert_qmp(result, 'return[0]/device', 'drive0')
249 self.assert_qmp(result, 'return[0]/speed', 4 * 1024 * 1024)
251 event = self.cancel_and_wait(resume=True)
252 self.assert_qmp(event, 'data/type', 'backup')
254 def test_set_speed_drive_backup(self):
255 self.do_test_set_speed('drive-backup', target_img)
257 def test_set_speed_blockdev_backup(self):
258 self.do_test_set_speed('blockdev-backup', 'drive1')
260 def do_test_set_speed_invalid(self, cmd, target):
261 self.assert_no_active_block_jobs()
263 result = self.vm.qmp(cmd, device='drive0',
264 target=target, sync='full', speed=-1)
265 self.assert_qmp(result, 'error/class', 'GenericError')
267 self.assert_no_active_block_jobs()
269 self.vm.pause_drive('drive0')
270 result = self.vm.qmp(cmd, device='drive0',
271 target=target, sync='full')
272 self.assert_qmp(result, 'return', {})
274 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=-1)
275 self.assert_qmp(result, 'error/class', 'GenericError')
277 event = self.cancel_and_wait(resume=True)
278 self.assert_qmp(event, 'data/type', 'backup')
280 def test_set_speed_invalid_drive_backup(self):
281 self.do_test_set_speed_invalid('drive-backup', target_img)
283 def test_set_speed_invalid_blockdev_backup(self):
284 self.do_test_set_speed_invalid('blockdev-backup', 'drive1')
286 # Note: We cannot use pause_drive() here, or the transaction command
287 # would stall. Instead, we limit the block job speed here.
288 class TestSingleTransaction(iotests.QMPTestCase):
290 qemu_img('create', '-f', iotests.imgfmt, blockdev_target_img, str(image_len))
292 self.vm = iotests.VM().add_drive(test_img)
293 self.vm.add_drive(blockdev_target_img, interface="none")
294 if iotests.qemu_default_machine == 'pc':
295 self.vm.add_drive(None, 'media=cdrom', 'ide')
300 os.remove(blockdev_target_img)
302 os.remove(target_img)
306 def do_test_cancel(self, cmd, target):
307 self.assert_no_active_block_jobs()
309 result = self.vm.qmp('transaction', actions=[{
311 'data': { 'device': 'drive0',
314 'speed': 64 * 1024 },
318 self.assert_qmp(result, 'return', {})
320 event = self.cancel_and_wait()
321 self.assert_qmp(event, 'data/type', 'backup')
323 def test_cancel_drive_backup(self):
324 self.do_test_cancel('drive-backup', target_img)
326 def test_cancel_blockdev_backup(self):
327 self.do_test_cancel('blockdev-backup', 'drive1')
329 def do_test_pause(self, cmd, target, image):
330 self.assert_no_active_block_jobs()
332 result = self.vm.qmp('transaction', actions=[{
334 'data': { 'device': 'drive0',
337 'speed': 64 * 1024 },
340 self.assert_qmp(result, 'return', {})
342 self.pause_job('drive0', wait=False)
344 result = self.vm.qmp('block-job-set-speed', device='drive0', speed=0)
345 self.assert_qmp(result, 'return', {})
347 self.pause_wait('drive0')
349 result = self.vm.qmp('query-block-jobs')
350 offset = self.dictpath(result, 'return[0]/offset')
353 result = self.vm.qmp('query-block-jobs')
354 self.assert_qmp(result, 'return[0]/offset', offset)
356 result = self.vm.qmp('block-job-resume', device='drive0')
357 self.assert_qmp(result, 'return', {})
359 self.wait_until_completed()
362 self.assertTrue(iotests.compare_images(test_img, image),
363 'target image does not match source after backup')
365 def test_pause_drive_backup(self):
366 self.do_test_pause('drive-backup', target_img, target_img)
368 def test_pause_blockdev_backup(self):
369 self.do_test_pause('blockdev-backup', 'drive1', blockdev_target_img)
371 def do_test_medium_not_found(self, cmd, target):
372 if iotests.qemu_default_machine != 'pc':
375 result = self.vm.qmp('transaction', actions=[{
377 'data': { 'device': 'drive2', # CD-ROM
382 self.assert_qmp(result, 'error/class', 'GenericError')
384 def test_medium_not_found_drive_backup(self):
385 self.do_test_medium_not_found('drive-backup', target_img)
387 def test_medium_not_found_blockdev_backup(self):
388 self.do_test_medium_not_found('blockdev-backup', 'drive1')
390 def test_image_not_found(self):
391 result = self.vm.qmp('transaction', actions=[{
392 'type': 'drive-backup',
393 'data': { 'device': 'drive0',
395 'target': target_img,
399 self.assert_qmp(result, 'error/class', 'GenericError')
401 def test_device_not_found(self):
402 result = self.vm.qmp('transaction', actions=[{
403 'type': 'drive-backup',
404 'data': { 'device': 'nonexistent',
406 'target': target_img,
410 self.assert_qmp(result, 'error/class', 'GenericError')
412 result = self.vm.qmp('transaction', actions=[{
413 'type': 'blockdev-backup',
414 'data': { 'device': 'nonexistent',
419 self.assert_qmp(result, 'error/class', 'GenericError')
421 result = self.vm.qmp('transaction', actions=[{
422 'type': 'blockdev-backup',
423 'data': { 'device': 'drive0',
424 'target': 'nonexistent',
428 self.assert_qmp(result, 'error/class', 'GenericError')
430 result = self.vm.qmp('transaction', actions=[{
431 'type': 'blockdev-backup',
432 'data': { 'device': 'nonexistent',
433 'target': 'nonexistent',
437 self.assert_qmp(result, 'error/class', 'GenericError')
439 def test_target_is_source(self):
440 result = self.vm.qmp('transaction', actions=[{
441 'type': 'blockdev-backup',
442 'data': { 'device': 'drive0',
447 self.assert_qmp(result, 'error/class', 'GenericError')
449 def test_abort(self):
450 result = self.vm.qmp('transaction', actions=[{
451 'type': 'drive-backup',
452 'data': { 'device': 'nonexistent',
454 'target': target_img,
461 self.assert_qmp(result, 'error/class', 'GenericError')
462 self.assert_no_active_block_jobs()
464 result = self.vm.qmp('transaction', actions=[{
465 'type': 'blockdev-backup',
466 'data': { 'device': 'nonexistent',
474 self.assert_qmp(result, 'error/class', 'GenericError')
475 self.assert_no_active_block_jobs()
477 result = self.vm.qmp('transaction', actions=[{
478 'type': 'blockdev-backup',
479 'data': { 'device': 'drive0',
480 'target': 'nonexistent',
487 self.assert_qmp(result, 'error/class', 'GenericError')
488 self.assert_no_active_block_jobs()
491 class TestCompressedToQcow2(iotests.QMPTestCase):
492 image_len = 64 * 1024 * 1024 # MB
493 target_fmt = {'type': 'qcow2', 'args': (), 'drive-opts': ''}
497 os.remove(blockdev_target_img)
499 os.remove(target_img)
503 def do_prepare_drives(self, attach_target):
504 self.vm = iotests.VM().add_drive('blkdebug::' + test_img,
505 opts=self.target_fmt['drive-opts'])
507 qemu_img('create', '-f', self.target_fmt['type'], blockdev_target_img,
508 str(self.image_len), *self.target_fmt['args'])
510 self.vm.add_drive(blockdev_target_img,
511 img_format=self.target_fmt['type'],
513 opts=self.target_fmt['drive-opts'])
517 def do_test_compress_complete(self, cmd, attach_target, **args):
518 self.do_prepare_drives(attach_target)
520 self.assert_no_active_block_jobs()
522 result = self.vm.qmp(cmd, device='drive0', sync='full', compress=True, **args)
523 self.assert_qmp(result, 'return', {})
525 self.wait_until_completed()
528 self.assertTrue(iotests.compare_images(test_img, blockdev_target_img,
530 self.target_fmt['type']),
531 'target image does not match source after backup')
533 def test_complete_compress_drive_backup(self):
534 self.do_test_compress_complete('drive-backup', False,
535 target=blockdev_target_img,
538 def test_complete_compress_blockdev_backup(self):
539 self.do_test_compress_complete('blockdev-backup',
540 True, target='drive1')
542 def do_test_compress_cancel(self, cmd, attach_target, **args):
543 self.do_prepare_drives(attach_target)
545 self.assert_no_active_block_jobs()
547 self.vm.pause_drive('drive0')
548 result = self.vm.qmp(cmd, device='drive0', sync='full', compress=True, **args)
549 self.assert_qmp(result, 'return', {})
551 event = self.cancel_and_wait(resume=True)
552 self.assert_qmp(event, 'data/type', 'backup')
556 def test_compress_cancel_drive_backup(self):
557 self.do_test_compress_cancel('drive-backup', False,
558 target=blockdev_target_img,
561 def test_compress_cancel_blockdev_backup(self):
562 self.do_test_compress_cancel('blockdev-backup', True,
565 def do_test_compress_pause(self, cmd, attach_target, **args):
566 self.do_prepare_drives(attach_target)
568 self.assert_no_active_block_jobs()
570 self.vm.pause_drive('drive0')
571 result = self.vm.qmp(cmd, device='drive0', sync='full', compress=True, **args)
572 self.assert_qmp(result, 'return', {})
574 self.pause_job('drive0', wait=False)
575 self.vm.resume_drive('drive0')
576 self.pause_wait('drive0')
578 result = self.vm.qmp('query-block-jobs')
579 offset = self.dictpath(result, 'return[0]/offset')
582 result = self.vm.qmp('query-block-jobs')
583 self.assert_qmp(result, 'return[0]/offset', offset)
585 result = self.vm.qmp('block-job-resume', device='drive0')
586 self.assert_qmp(result, 'return', {})
588 self.wait_until_completed()
591 self.assertTrue(iotests.compare_images(test_img, blockdev_target_img,
593 self.target_fmt['type']),
594 'target image does not match source after backup')
596 def test_compress_pause_drive_backup(self):
597 self.do_test_compress_pause('drive-backup', False,
598 target=blockdev_target_img,
601 def test_compress_pause_blockdev_backup(self):
602 self.do_test_compress_pause('blockdev-backup', True,
606 class TestCompressedToVmdk(TestCompressedToQcow2):
607 target_fmt = {'type': 'vmdk', 'args': ('-o', 'subformat=streamOptimized'),
608 'drive-opts': 'cache.no-flush=on'}
610 @iotests.skip_if_unsupported(['vmdk'])
615 if __name__ == '__main__':
616 iotests.main(supported_fmts=['raw', 'qcow2'],
617 supported_protocols=['file'])