xref: /openbmc/qemu/tests/qemu-iotests/132 (revision 3e964275)
1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test mirror with unmap
5#
6# Copyright (C) 2015 Red Hat, Inc.
7#
8# This program is free software; you can redistribute it and/or modify
9# it under the terms of the GNU General Public License as published by
10# the Free Software Foundation; either version 2 of the License, or
11# (at your option) any later version.
12#
13# This program is distributed in the hope that it will be useful,
14# but WITHOUT ANY WARRANTY; without even the implied warranty of
15# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16# GNU General Public License for more details.
17#
18# You should have received a copy of the GNU General Public License
19# along with this program.  If not, see <http://www.gnu.org/licenses/>.
20#
21
22import time
23import os
24import iotests
25from iotests import qemu_img, qemu_io
26
27test_img = os.path.join(iotests.test_dir, 'test.img')
28target_img = os.path.join(iotests.test_dir, 'target.img')
29
30class TestSingleDrive(iotests.QMPTestCase):
31    image_len = 2 * 1024 * 1024 # MB
32
33    def setUp(self):
34        # Write data to the image so we can compare later
35        qemu_img('create', '-f', iotests.imgfmt, test_img, str(TestSingleDrive.image_len))
36        qemu_io('-f', iotests.imgfmt, '-c', 'write -P0x5d 0 2M', test_img)
37
38        self.vm = iotests.VM().add_drive(test_img, 'discard=unmap')
39        self.vm.launch()
40
41    def tearDown(self):
42        self.vm.shutdown()
43        os.remove(test_img)
44        try:
45            os.remove(target_img)
46        except OSError:
47            pass
48
49    def test_mirror_discard(self):
50        self.vm.cmd('drive-mirror', device='drive0', sync='full',
51                    target=target_img)
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