xref: /openbmc/qemu/tests/qemu-iotests/151 (revision e38da02091eeed56bb370ec9d72c4367d4e9ada3)
1*e38da020SMax Reitz#!/usr/bin/env python
2*e38da020SMax Reitz#
3*e38da020SMax Reitz# Tests for active mirroring
4*e38da020SMax Reitz#
5*e38da020SMax Reitz# Copyright (C) 2018 Red Hat, Inc.
6*e38da020SMax Reitz#
7*e38da020SMax Reitz# This program is free software; you can redistribute it and/or modify
8*e38da020SMax Reitz# it under the terms of the GNU General Public License as published by
9*e38da020SMax Reitz# the Free Software Foundation; either version 2 of the License, or
10*e38da020SMax Reitz# (at your option) any later version.
11*e38da020SMax Reitz#
12*e38da020SMax Reitz# This program is distributed in the hope that it will be useful,
13*e38da020SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
14*e38da020SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15*e38da020SMax Reitz# GNU General Public License for more details.
16*e38da020SMax Reitz#
17*e38da020SMax Reitz# You should have received a copy of the GNU General Public License
18*e38da020SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19*e38da020SMax Reitz#
20*e38da020SMax Reitz
21*e38da020SMax Reitzimport os
22*e38da020SMax Reitzimport iotests
23*e38da020SMax Reitzfrom iotests import qemu_img
24*e38da020SMax Reitz
25*e38da020SMax Reitzsource_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
26*e38da020SMax Reitztarget_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
27*e38da020SMax Reitz
28*e38da020SMax Reitzclass TestActiveMirror(iotests.QMPTestCase):
29*e38da020SMax Reitz    image_len = 128 * 1024 * 1024 # MB
30*e38da020SMax Reitz    potential_writes_in_flight = True
31*e38da020SMax Reitz
32*e38da020SMax Reitz    def setUp(self):
33*e38da020SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, source_img, '128M')
34*e38da020SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, target_img, '128M')
35*e38da020SMax Reitz
36*e38da020SMax Reitz        blk_source = {'id': 'source',
37*e38da020SMax Reitz                      'if': 'none',
38*e38da020SMax Reitz                      'node-name': 'source-node',
39*e38da020SMax Reitz                      'driver': iotests.imgfmt,
40*e38da020SMax Reitz                      'file': {'driver': 'file',
41*e38da020SMax Reitz                               'filename': source_img}}
42*e38da020SMax Reitz
43*e38da020SMax Reitz        blk_target = {'node-name': 'target-node',
44*e38da020SMax Reitz                      'driver': iotests.imgfmt,
45*e38da020SMax Reitz                      'file': {'driver': 'file',
46*e38da020SMax Reitz                               'filename': target_img}}
47*e38da020SMax Reitz
48*e38da020SMax Reitz        self.vm = iotests.VM()
49*e38da020SMax Reitz        self.vm.add_drive_raw(self.vm.qmp_to_opts(blk_source))
50*e38da020SMax Reitz        self.vm.add_blockdev(self.vm.qmp_to_opts(blk_target))
51*e38da020SMax Reitz        self.vm.add_device('virtio-blk,drive=source')
52*e38da020SMax Reitz        self.vm.launch()
53*e38da020SMax Reitz
54*e38da020SMax Reitz    def tearDown(self):
55*e38da020SMax Reitz        self.vm.shutdown()
56*e38da020SMax Reitz
57*e38da020SMax Reitz        if not self.potential_writes_in_flight:
58*e38da020SMax Reitz            self.assertTrue(iotests.compare_images(source_img, target_img),
59*e38da020SMax Reitz                            'mirror target does not match source')
60*e38da020SMax Reitz
61*e38da020SMax Reitz        os.remove(source_img)
62*e38da020SMax Reitz        os.remove(target_img)
63*e38da020SMax Reitz
64*e38da020SMax Reitz    def doActiveIO(self, sync_source_and_target):
65*e38da020SMax Reitz        # Fill the source image
66*e38da020SMax Reitz        self.vm.hmp_qemu_io('source',
67*e38da020SMax Reitz                            'write -P 1 0 %i' % self.image_len);
68*e38da020SMax Reitz
69*e38da020SMax Reitz        # Start some background requests
70*e38da020SMax Reitz        for offset in range(1 * self.image_len / 8, 3 * self.image_len / 8, 1024 * 1024):
71*e38da020SMax Reitz            self.vm.hmp_qemu_io('source', 'aio_write -P 2 %i 1M' % offset)
72*e38da020SMax Reitz        for offset in range(2 * self.image_len / 8, 3 * self.image_len / 8, 1024 * 1024):
73*e38da020SMax Reitz            self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
74*e38da020SMax Reitz
75*e38da020SMax Reitz        # Start the block job
76*e38da020SMax Reitz        result = self.vm.qmp('blockdev-mirror',
77*e38da020SMax Reitz                             job_id='mirror',
78*e38da020SMax Reitz                             filter_node_name='mirror-node',
79*e38da020SMax Reitz                             device='source-node',
80*e38da020SMax Reitz                             target='target-node',
81*e38da020SMax Reitz                             sync='full',
82*e38da020SMax Reitz                             copy_mode='write-blocking')
83*e38da020SMax Reitz        self.assert_qmp(result, 'return', {})
84*e38da020SMax Reitz
85*e38da020SMax Reitz        # Start some more requests
86*e38da020SMax Reitz        for offset in range(3 * self.image_len / 8, 5 * self.image_len / 8, 1024 * 1024):
87*e38da020SMax Reitz            self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
88*e38da020SMax Reitz        for offset in range(4 * self.image_len / 8, 5 * self.image_len / 8, 1024 * 1024):
89*e38da020SMax Reitz            self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
90*e38da020SMax Reitz
91*e38da020SMax Reitz        # Wait for the READY event
92*e38da020SMax Reitz        self.wait_ready(drive='mirror')
93*e38da020SMax Reitz
94*e38da020SMax Reitz        # Now start some final requests; all of these (which land on
95*e38da020SMax Reitz        # the source) should be settled using the active mechanism.
96*e38da020SMax Reitz        # The mirror code itself asserts that the source BDS's dirty
97*e38da020SMax Reitz        # bitmap will stay clean between READY and COMPLETED.
98*e38da020SMax Reitz        for offset in range(5 * self.image_len / 8, 7 * self.image_len / 8, 1024 * 1024):
99*e38da020SMax Reitz            self.vm.hmp_qemu_io('source', 'aio_write -P 3 %i 1M' % offset)
100*e38da020SMax Reitz        for offset in range(6 * self.image_len / 8, 7 * self.image_len / 8, 1024 * 1024):
101*e38da020SMax Reitz            self.vm.hmp_qemu_io('source', 'aio_write -z %i 1M' % offset)
102*e38da020SMax Reitz
103*e38da020SMax Reitz        if sync_source_and_target:
104*e38da020SMax Reitz            # If source and target should be in sync after the mirror,
105*e38da020SMax Reitz            # we have to flush before completion
106*e38da020SMax Reitz            self.vm.hmp_qemu_io('source', 'aio_flush')
107*e38da020SMax Reitz            self.potential_writes_in_flight = False
108*e38da020SMax Reitz
109*e38da020SMax Reitz        self.complete_and_wait(drive='mirror', wait_ready=False)
110*e38da020SMax Reitz
111*e38da020SMax Reitz    def testActiveIO(self):
112*e38da020SMax Reitz        self.doActiveIO(False)
113*e38da020SMax Reitz
114*e38da020SMax Reitz    def testActiveIOFlushed(self):
115*e38da020SMax Reitz        self.doActiveIO(True)
116*e38da020SMax Reitz
117*e38da020SMax Reitz
118*e38da020SMax Reitz
119*e38da020SMax Reitzif __name__ == '__main__':
120*e38da020SMax Reitz    iotests.main(supported_fmts=['qcow2', 'raw'])
121