1#!/usr/bin/env python 2# 3# Tests for drive-backup 4# 5# Copyright (C) 2013 Red Hat, Inc. 6# 7# Based on 041. 8# 9# This program is free software; you can redistribute it and/or modify 10# it under the terms of the GNU General Public License as published by 11# the Free Software Foundation; either version 2 of the License, or 12# (at your option) any later version. 13# 14# This program is distributed in the hope that it will be useful, 15# but WITHOUT ANY WARRANTY; without even the implied warranty of 16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17# GNU General Public License for more details. 18# 19# You should have received a copy of the GNU General Public License 20# along with this program. If not, see <http://www.gnu.org/licenses/>. 21# 22 23import time 24import os 25import iotests 26from iotests import qemu_img, qemu_io, create_image 27 28backing_img = os.path.join(iotests.test_dir, 'backing.img') 29test_img = os.path.join(iotests.test_dir, 'test.img') 30target_img = os.path.join(iotests.test_dir, 'target.img') 31 32class TestSyncModesNoneAndTop(iotests.QMPTestCase): 33 image_len = 64 * 1024 * 1024 # MB 34 35 def setUp(self): 36 create_image(backing_img, TestSyncModesNoneAndTop.image_len) 37 qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img) 38 qemu_io('-c', 'write -P0x41 0 512', test_img) 39 qemu_io('-c', 'write -P0xd5 1M 32k', test_img) 40 qemu_io('-c', 'write -P0xdc 32M 124k', test_img) 41 qemu_io('-c', 'write -P0xdc 67043328 64k', test_img) 42 self.vm = iotests.VM().add_drive(test_img) 43 self.vm.launch() 44 45 def tearDown(self): 46 self.vm.shutdown() 47 os.remove(test_img) 48 os.remove(backing_img) 49 try: 50 os.remove(target_img) 51 except OSError: 52 pass 53 54 def test_complete_top(self): 55 self.assert_no_active_block_jobs() 56 result = self.vm.qmp('drive-backup', device='drive0', sync='top', 57 format=iotests.imgfmt, target=target_img) 58 self.assert_qmp(result, 'return', {}) 59 60 self.wait_until_completed(check_offset=False) 61 62 self.assert_no_active_block_jobs() 63 self.vm.shutdown() 64 self.assertTrue(iotests.compare_images(test_img, target_img), 65 'target image does not match source after backup') 66 67 def test_cancel_sync_none(self): 68 self.assert_no_active_block_jobs() 69 70 result = self.vm.qmp('drive-backup', device='drive0', 71 sync='none', target=target_img) 72 self.assert_qmp(result, 'return', {}) 73 time.sleep(1) 74 self.vm.hmp_qemu_io('drive0', 'write -P0x5e 0 512') 75 self.vm.hmp_qemu_io('drive0', 'aio_flush') 76 # Verify that the original contents exist in the target image. 77 78 event = self.cancel_and_wait() 79 self.assert_qmp(event, 'data/type', 'backup') 80 81 self.vm.shutdown() 82 time.sleep(1) 83 self.assertEqual(-1, qemu_io('-c', 'read -P0x41 0 512', target_img).find("verification failed")) 84 85class TestBeforeWriteNotifier(iotests.QMPTestCase): 86 def setUp(self): 87 self.vm = iotests.VM().add_drive_raw("file=blkdebug::null-co://,id=drive0,align=65536,driver=blkdebug") 88 self.vm.launch() 89 90 def tearDown(self): 91 self.vm.shutdown() 92 os.remove(target_img) 93 94 def test_before_write_notifier(self): 95 self.vm.pause_drive("drive0") 96 result = self.vm.qmp('drive-backup', device='drive0', 97 sync='full', target=target_img, 98 format="file", speed=1) 99 self.assert_qmp(result, 'return', {}) 100 result = self.vm.qmp('block-job-pause', device="drive0") 101 self.assert_qmp(result, 'return', {}) 102 # Speed is low enough that this must be an uncopied range, which will 103 # trigger the before write notifier 104 self.vm.hmp_qemu_io('drive0', 'aio_write -P 1 512512 512') 105 self.vm.resume_drive("drive0") 106 result = self.vm.qmp('block-job-resume', device="drive0") 107 self.assert_qmp(result, 'return', {}) 108 event = self.cancel_and_wait() 109 self.assert_qmp(event, 'data/type', 'backup') 110 111if __name__ == '__main__': 112 iotests.main(supported_fmts=['qcow2', 'qed']) 113