xref: /openbmc/qemu/tests/qemu-iotests/132 (revision e9e51b71)
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        result = self.vm.qmp('drive-mirror', device='drive0', sync='full',
51                             target=target_img)
52        self.assert_qmp(result, 'return', {})
53        self.vm.hmp_qemu_io('drive0', 'discard 0 64k')
54        self.complete_and_wait('drive0')
55        self.vm.shutdown()
56        self.assertTrue(iotests.compare_images(test_img, target_img),
57                        'target image does not match source after mirroring')
58
59if __name__ == '__main__':
60    iotests.main(supported_fmts=['raw', 'qcow2'],
61                 supported_protocols=['file'])
62