xref: /openbmc/qemu/tests/qemu-iotests/151 (revision 3d9569b8)
1#!/usr/bin/env python
2#
3# Tests for active mirroring
4#
5# Copyright (C) 2018 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 os
22import iotests
23from iotests import qemu_img
24
25source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
26target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
27
28class TestActiveMirror(iotests.QMPTestCase):
29    image_len = 128 * 1024 * 1024 # MB
30    potential_writes_in_flight = True
31
32    def setUp(self):
33        qemu_img('create', '-f', iotests.imgfmt, source_img, '128M')
34        qemu_img('create', '-f', iotests.imgfmt, target_img, '128M')
35
36        blk_source = {'id': 'source',
37                      'if': 'none',
38                      'node-name': 'source-node',
39                      'driver': iotests.imgfmt,
40                      'file': {'driver': 'file',
41                               'filename': source_img}}
42
43        blk_target = {'node-name': 'target-node',
44                      'driver': iotests.imgfmt,
45                      'file': {'driver': 'file',
46                               'filename': target_img}}
47
48        self.vm = iotests.VM()
49        self.vm.add_drive_raw(self.vm.qmp_to_opts(blk_source))
50        self.vm.add_blockdev(self.vm.qmp_to_opts(blk_target))
51        self.vm.add_device('virtio-blk,drive=source')
52        self.vm.launch()
53
54    def tearDown(self):
55        self.vm.shutdown()
56
57        if not self.potential_writes_in_flight:
58            self.assertTrue(iotests.compare_images(source_img, target_img),
59                            'mirror target does not match source')
60
61        os.remove(source_img)
62        os.remove(target_img)
63
64    def doActiveIO(self, sync_source_and_target):
65        # Fill the source image
66        self.vm.hmp_qemu_io('source',
67                            'write -P 1 0 %i' % self.image_len);
68
69        # Start some background requests
70        for offset in range(1 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
71            self.vm.hmp_qemu_io('source', 'aio_write -P 2 %i 1M' % offset)
72        for offset in range(2 * self.image_len // 8, 3 * self.image_len // 8, 1024 * 1024):
73            self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
74
75        # Start the block job
76        result = self.vm.qmp('blockdev-mirror',
77                             job_id='mirror',
78                             filter_node_name='mirror-node',
79                             device='source-node',
80                             target='target-node',
81                             sync='full',
82                             copy_mode='write-blocking')
83        self.assert_qmp(result, 'return', {})
84
85        # Start some more requests
86        for offset in range(3 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
87            self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
88        for offset in range(4 * self.image_len // 8, 5 * self.image_len // 8, 1024 * 1024):
89            self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
90
91        # Wait for the READY event
92        self.wait_ready(drive='mirror')
93
94        # Now start some final requests; all of these (which land on
95        # the source) should be settled using the active mechanism.
96        # The mirror code itself asserts that the source BDS's dirty
97        # bitmap will stay clean between READY and COMPLETED.
98        for offset in range(5 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
99            self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
100        for offset in range(6 * self.image_len // 8, 7 * self.image_len // 8, 1024 * 1024):
101            self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
102
103        if sync_source_and_target:
104            # If source and target should be in sync after the mirror,
105            # we have to flush before completion
106            self.vm.hmp_qemu_io('source', 'aio_flush')
107            self.potential_writes_in_flight = False
108
109        self.complete_and_wait(drive='mirror', wait_ready=False)
110
111    def testActiveIO(self):
112        self.doActiveIO(False)
113
114    def testActiveIOFlushed(self):
115        self.doActiveIO(True)
116
117
118
119if __name__ == '__main__':
120    iotests.main(supported_fmts=['qcow2', 'raw'])
121