1c6150917SFam Zheng#!/usr/bin/env python 2c6150917SFam Zheng# 3c6150917SFam Zheng# Test mirror with unmap 4c6150917SFam Zheng# 5c6150917SFam Zheng# Copyright (C) 2015 Red Hat, Inc. 6c6150917SFam Zheng# 7c6150917SFam Zheng# This program is free software; you can redistribute it and/or modify 8c6150917SFam Zheng# it under the terms of the GNU General Public License as published by 9c6150917SFam Zheng# the Free Software Foundation; either version 2 of the License, or 10c6150917SFam Zheng# (at your option) any later version. 11c6150917SFam Zheng# 12c6150917SFam Zheng# This program is distributed in the hope that it will be useful, 13c6150917SFam Zheng# but WITHOUT ANY WARRANTY; without even the implied warranty of 14c6150917SFam Zheng# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 15c6150917SFam Zheng# GNU General Public License for more details. 16c6150917SFam Zheng# 17c6150917SFam Zheng# You should have received a copy of the GNU General Public License 18c6150917SFam Zheng# along with this program. If not, see <http://www.gnu.org/licenses/>. 19c6150917SFam Zheng# 20c6150917SFam Zheng 21c6150917SFam Zhengimport time 22c6150917SFam Zhengimport os 23c6150917SFam Zhengimport iotests 24c6150917SFam Zhengfrom iotests import qemu_img, qemu_io 25c6150917SFam Zheng 26c6150917SFam Zhengtest_img = os.path.join(iotests.test_dir, 'test.img') 27c6150917SFam Zhengtarget_img = os.path.join(iotests.test_dir, 'target.img') 28c6150917SFam Zheng 29c6150917SFam Zhengclass TestSingleDrive(iotests.QMPTestCase): 30c6150917SFam Zheng image_len = 2 * 1024 * 1024 # MB 31c6150917SFam Zheng 32c6150917SFam Zheng def setUp(self): 33c6150917SFam Zheng # Write data to the image so we can compare later 34c6150917SFam Zheng qemu_img('create', '-f', iotests.imgfmt, test_img, str(TestSingleDrive.image_len)) 35c6150917SFam Zheng qemu_io('-f', iotests.imgfmt, '-c', 'write -P0x5d 0 2M', test_img) 36c6150917SFam Zheng 37c6150917SFam Zheng self.vm = iotests.VM().add_drive(test_img, 'discard=unmap') 38c6150917SFam Zheng self.vm.launch() 39c6150917SFam Zheng 40c6150917SFam Zheng def tearDown(self): 41c6150917SFam Zheng self.vm.shutdown() 42c6150917SFam Zheng os.remove(test_img) 43c6150917SFam Zheng try: 44c6150917SFam Zheng os.remove(target_img) 45c6150917SFam Zheng except OSError: 46c6150917SFam Zheng pass 47c6150917SFam Zheng 48c6150917SFam Zheng def test_mirror_discard(self): 49c6150917SFam Zheng result = self.vm.qmp('drive-mirror', device='drive0', sync='full', 50c6150917SFam Zheng target=target_img) 51c6150917SFam Zheng self.assert_qmp(result, 'return', {}) 52c6150917SFam Zheng self.vm.hmp_qemu_io('drive0', 'discard 0 64k') 53c6150917SFam Zheng self.complete_and_wait('drive0') 54c6150917SFam Zheng self.vm.shutdown() 55c6150917SFam Zheng self.assertTrue(iotests.compare_images(test_img, target_img), 56c6150917SFam Zheng 'target image does not match source after mirroring') 57c6150917SFam Zheng 58c6150917SFam Zhengif __name__ == '__main__': 59*103cbc77SMax Reitz iotests.main(supported_fmts=['raw', 'qcow2'], 60*103cbc77SMax Reitz supported_protocols=['file']) 61