1#!/usr/bin/env python3
2#
3# Test mirror with unmap
4#
5# Copyright (C) 2015 Red Hat, Inc.
6#
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.
11#
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.
16#
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/>.
19#
20
21import time
22import os
23import iotests
24from iotests import qemu_img, qemu_io
25
26test_img = os.path.join(iotests.test_dir, 'test.img')
27target_img = os.path.join(iotests.test_dir, 'target.img')
28
29class TestSingleDrive(iotests.QMPTestCase):
30    image_len = 2 * 1024 * 1024 # MB
31
32    def setUp(self):
33        # Write data to the image so we can compare later
34        qemu_img('create', '-f', iotests.imgfmt, test_img, str(TestSingleDrive.image_len))
35        qemu_io('-f', iotests.imgfmt, '-c', 'write -P0x5d 0 2M', test_img)
36
37        self.vm = iotests.VM().add_drive(test_img, 'discard=unmap')
38        self.vm.launch()
39
40    def tearDown(self):
41        self.vm.shutdown()
42        os.remove(test_img)
43        try:
44            os.remove(target_img)
45        except OSError:
46            pass
47
48    def test_mirror_discard(self):
49        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
50                             target=target_img)
51        self.assert_qmp(result, 'return', {})
52        self.vm.hmp_qemu_io('drive0', 'discard 0 64k')
53        self.complete_and_wait('drive0')
54        self.vm.shutdown()
55        self.assertTrue(iotests.compare_images(test_img, target_img),
56                        'target image does not match source after mirroring')
57
58if __name__ == '__main__':
59    iotests.main(supported_fmts=['raw', 'qcow2'],
60                 supported_protocols=['file'])
61