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