xref: /qemu/tests/qemu-iotests/155 (revision 903cb1bf)
1*903cb1bfSPhilippe Mathieu-Daudé#!/usr/bin/env python3
2298c6009SMax Reitz#
3298c6009SMax Reitz# Test whether the backing BDSs are correct after completion of a
4298c6009SMax Reitz# mirror block job; in "existing" modes (drive-mirror with
5298c6009SMax Reitz# mode=existing and blockdev-mirror) the backing chain should not be
6298c6009SMax Reitz# overridden.
7298c6009SMax Reitz#
8298c6009SMax Reitz# Copyright (C) 2016 Red Hat, Inc.
9298c6009SMax Reitz#
10298c6009SMax Reitz# This program is free software; you can redistribute it and/or modify
11298c6009SMax Reitz# it under the terms of the GNU General Public License as published by
12298c6009SMax Reitz# the Free Software Foundation; either version 2 of the License, or
13298c6009SMax Reitz# (at your option) any later version.
14298c6009SMax Reitz#
15298c6009SMax Reitz# This program is distributed in the hope that it will be useful,
16298c6009SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
17298c6009SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18298c6009SMax Reitz# GNU General Public License for more details.
19298c6009SMax Reitz#
20298c6009SMax Reitz# You should have received a copy of the GNU General Public License
21298c6009SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22298c6009SMax Reitz#
23298c6009SMax Reitz
24298c6009SMax Reitzimport os
25298c6009SMax Reitzimport iotests
26298c6009SMax Reitzfrom iotests import qemu_img
27298c6009SMax Reitz
28298c6009SMax Reitzback0_img = os.path.join(iotests.test_dir, 'back0.' + iotests.imgfmt)
29298c6009SMax Reitzback1_img = os.path.join(iotests.test_dir, 'back1.' + iotests.imgfmt)
30298c6009SMax Reitzback2_img = os.path.join(iotests.test_dir, 'back2.' + iotests.imgfmt)
31298c6009SMax Reitzsource_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
32298c6009SMax Reitztarget_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
33298c6009SMax Reitz
34298c6009SMax Reitz
35298c6009SMax Reitz# Class variables for controlling its behavior:
36298c6009SMax Reitz#
37298c6009SMax Reitz# existing: If True, explicitly create the target image and blockdev-add it
38298c6009SMax Reitz# target_backing: If existing is True: Use this filename as the backing file
39298c6009SMax Reitz#                 of the target image
40298c6009SMax Reitz#                 (None: no backing file)
41298c6009SMax Reitz# target_blockdev_backing: If existing is True: Pass this dict as "backing"
42298c6009SMax Reitz#                          for the blockdev-add command
43298c6009SMax Reitz#                          (None: do not pass "backing")
44298c6009SMax Reitz# target_real_backing: If existing is True: The real filename of the backing
45298c6009SMax Reitz#                      image during runtime, only makes sense if
46298c6009SMax Reitz#                      target_blockdev_backing is not None
47298c6009SMax Reitz#                      (None: same as target_backing)
48298c6009SMax Reitz
49298c6009SMax Reitzclass BaseClass(iotests.QMPTestCase):
50298c6009SMax Reitz    target_blockdev_backing = None
51298c6009SMax Reitz    target_real_backing = None
52298c6009SMax Reitz
53298c6009SMax Reitz    def setUp(self):
541d701e0eSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K')
55298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img, back1_img)
56298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img, back2_img)
57298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img, source_img)
58298c6009SMax Reitz
59298c6009SMax Reitz        self.vm = iotests.VM()
60298c6009SMax Reitz        # Add the BDS via blockdev-add so it stays around after the mirror block
61298c6009SMax Reitz        # job has been completed
621d701e0eSMax Reitz        blockdev = {'node-name': 'source',
631d701e0eSMax Reitz                    'driver': iotests.imgfmt,
641d701e0eSMax Reitz                    'file': {'driver': 'file',
651d701e0eSMax Reitz                             'filename': source_img}}
6662a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
672782bb75SMax Reitz        self.vm.add_device('virtio-blk,id=qdev0,drive=source')
681d701e0eSMax Reitz        self.vm.launch()
69298c6009SMax Reitz
70298c6009SMax Reitz        self.assertIntactSourceBackingChain()
71298c6009SMax Reitz
72298c6009SMax Reitz        if self.existing:
73298c6009SMax Reitz            if self.target_backing:
74298c6009SMax Reitz                qemu_img('create', '-f', iotests.imgfmt,
751d701e0eSMax Reitz                         '-b', self.target_backing, target_img, '1440K')
76298c6009SMax Reitz            else:
771d701e0eSMax Reitz                qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
78298c6009SMax Reitz
79298c6009SMax Reitz            if self.cmd == 'blockdev-mirror':
80298c6009SMax Reitz                options = { 'node-name': 'target',
81298c6009SMax Reitz                            'driver': iotests.imgfmt,
82298c6009SMax Reitz                            'file': { 'driver': 'file',
83298c6009SMax Reitz                                      'filename': target_img } }
84298c6009SMax Reitz                if self.target_blockdev_backing:
85298c6009SMax Reitz                    options['backing'] = self.target_blockdev_backing
86298c6009SMax Reitz
870153d2f5SKevin Wolf                result = self.vm.qmp('blockdev-add', **options)
88298c6009SMax Reitz                self.assert_qmp(result, 'return', {})
89298c6009SMax Reitz
90298c6009SMax Reitz    def tearDown(self):
91298c6009SMax Reitz        self.vm.shutdown()
92298c6009SMax Reitz        os.remove(source_img)
93298c6009SMax Reitz        os.remove(back2_img)
94298c6009SMax Reitz        os.remove(back1_img)
95298c6009SMax Reitz        os.remove(back0_img)
96298c6009SMax Reitz        try:
97298c6009SMax Reitz            os.remove(target_img)
98298c6009SMax Reitz        except OSError:
99298c6009SMax Reitz            pass
100298c6009SMax Reitz
1011d701e0eSMax Reitz    def findBlockNode(self, node_name, qdev=None):
1021d701e0eSMax Reitz        if qdev:
103298c6009SMax Reitz            result = self.vm.qmp('query-block')
104298c6009SMax Reitz            for device in result['return']:
1051d701e0eSMax Reitz                if device['qdev'] == qdev:
106298c6009SMax Reitz                    if node_name:
107298c6009SMax Reitz                        self.assert_qmp(device, 'inserted/node-name', node_name)
108298c6009SMax Reitz                    return device['inserted']
109298c6009SMax Reitz        else:
110298c6009SMax Reitz            result = self.vm.qmp('query-named-block-nodes')
111298c6009SMax Reitz            for node in result['return']:
112298c6009SMax Reitz                if node['node-name'] == node_name:
113298c6009SMax Reitz                    return node
114298c6009SMax Reitz
1151d701e0eSMax Reitz        self.fail('Cannot find node %s/%s' % (qdev, node_name))
116298c6009SMax Reitz
117298c6009SMax Reitz    def assertIntactSourceBackingChain(self):
118298c6009SMax Reitz        node = self.findBlockNode('source')
119298c6009SMax Reitz
120298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
121298c6009SMax Reitz                        source_img)
122298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
123298c6009SMax Reitz                        back2_img)
124298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
125298c6009SMax Reitz                        back1_img)
126298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
127298c6009SMax Reitz                        back0_img)
128298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
129298c6009SMax Reitz
130298c6009SMax Reitz    def assertCorrectBackingImage(self, node, default_image):
131298c6009SMax Reitz        if self.existing:
132298c6009SMax Reitz            if self.target_real_backing:
133298c6009SMax Reitz                image = self.target_real_backing
134298c6009SMax Reitz            else:
135298c6009SMax Reitz                image = self.target_backing
136298c6009SMax Reitz        else:
137298c6009SMax Reitz            image = default_image
138298c6009SMax Reitz
139298c6009SMax Reitz        if image:
140298c6009SMax Reitz            self.assert_qmp(node, 'image/backing-image/filename', image)
141298c6009SMax Reitz        else:
142298c6009SMax Reitz            self.assert_qmp_absent(node, 'image/backing-image')
143298c6009SMax Reitz
144298c6009SMax Reitz
145298c6009SMax Reitz# Class variables for controlling its behavior:
146298c6009SMax Reitz#
147298c6009SMax Reitz# cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
148298c6009SMax Reitz
149298c6009SMax Reitzclass MirrorBaseClass(BaseClass):
150298c6009SMax Reitz    def runMirror(self, sync):
151298c6009SMax Reitz        if self.cmd == 'blockdev-mirror':
1521d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
1531d701e0eSMax Reitz                                 sync=sync, target='target')
154298c6009SMax Reitz        else:
155298c6009SMax Reitz            if self.existing:
156298c6009SMax Reitz                mode = 'existing'
157298c6009SMax Reitz            else:
158298c6009SMax Reitz                mode = 'absolute-paths'
1591d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
1601d701e0eSMax Reitz                                 sync=sync, target=target_img,
1611d701e0eSMax Reitz                                 format=iotests.imgfmt, mode=mode,
1621d701e0eSMax Reitz                                 node_name='target')
163298c6009SMax Reitz
164298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
165298c6009SMax Reitz
166298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_READY')
167298c6009SMax Reitz
1681d701e0eSMax Reitz        result = self.vm.qmp('block-job-complete', device='mirror-job')
169298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
170298c6009SMax Reitz
171298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_COMPLETED')
172298c6009SMax Reitz
173298c6009SMax Reitz    def testFull(self):
174298c6009SMax Reitz        self.runMirror('full')
175298c6009SMax Reitz
1762782bb75SMax Reitz        node = self.findBlockNode('target',
1772782bb75SMax Reitz                                  '/machine/peripheral/qdev0/virtio-backend')
178298c6009SMax Reitz        self.assertCorrectBackingImage(node, None)
179298c6009SMax Reitz        self.assertIntactSourceBackingChain()
180298c6009SMax Reitz
181298c6009SMax Reitz    def testTop(self):
182298c6009SMax Reitz        self.runMirror('top')
183298c6009SMax Reitz
1842782bb75SMax Reitz        node = self.findBlockNode('target',
1852782bb75SMax Reitz                                  '/machine/peripheral/qdev0/virtio-backend')
186298c6009SMax Reitz        self.assertCorrectBackingImage(node, back2_img)
187298c6009SMax Reitz        self.assertIntactSourceBackingChain()
188298c6009SMax Reitz
189298c6009SMax Reitz    def testNone(self):
190298c6009SMax Reitz        self.runMirror('none')
191298c6009SMax Reitz
1922782bb75SMax Reitz        node = self.findBlockNode('target',
1932782bb75SMax Reitz                                  '/machine/peripheral/qdev0/virtio-backend')
194298c6009SMax Reitz        self.assertCorrectBackingImage(node, source_img)
195298c6009SMax Reitz        self.assertIntactSourceBackingChain()
196298c6009SMax Reitz
197298c6009SMax Reitz
198298c6009SMax Reitzclass TestDriveMirrorAbsolutePaths(MirrorBaseClass):
199298c6009SMax Reitz    cmd = 'drive-mirror'
200298c6009SMax Reitz    existing = False
201298c6009SMax Reitz
202298c6009SMax Reitzclass TestDriveMirrorExistingNoBacking(MirrorBaseClass):
203298c6009SMax Reitz    cmd = 'drive-mirror'
204298c6009SMax Reitz    existing = True
205298c6009SMax Reitz    target_backing = None
206298c6009SMax Reitz
207298c6009SMax Reitzclass TestDriveMirrorExistingBacking(MirrorBaseClass):
208298c6009SMax Reitz    cmd = 'drive-mirror'
209298c6009SMax Reitz    existing = True
210298c6009SMax Reitz    target_backing = 'null-co://'
211298c6009SMax Reitz
212298c6009SMax Reitzclass TestBlockdevMirrorNoBacking(MirrorBaseClass):
213298c6009SMax Reitz    cmd = 'blockdev-mirror'
214298c6009SMax Reitz    existing = True
215298c6009SMax Reitz    target_backing = None
216298c6009SMax Reitz
217298c6009SMax Reitzclass TestBlockdevMirrorBacking(MirrorBaseClass):
218298c6009SMax Reitz    cmd = 'blockdev-mirror'
219298c6009SMax Reitz    existing = True
220298c6009SMax Reitz    target_backing = 'null-co://'
221298c6009SMax Reitz
222298c6009SMax Reitzclass TestBlockdevMirrorForcedBacking(MirrorBaseClass):
223298c6009SMax Reitz    cmd = 'blockdev-mirror'
224298c6009SMax Reitz    existing = True
225298c6009SMax Reitz    target_backing = None
226298c6009SMax Reitz    target_blockdev_backing = { 'driver': 'null-co' }
227298c6009SMax Reitz    target_real_backing = 'null-co://'
228298c6009SMax Reitz
229298c6009SMax Reitz
230298c6009SMax Reitzclass TestCommit(BaseClass):
231298c6009SMax Reitz    existing = False
232298c6009SMax Reitz
233298c6009SMax Reitz    def testCommit(self):
2341d701e0eSMax Reitz        result = self.vm.qmp('block-commit', job_id='commit-job',
2351d701e0eSMax Reitz                             device='source', base=back1_img)
236298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
237298c6009SMax Reitz
238298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_READY')
239298c6009SMax Reitz
2401d701e0eSMax Reitz        result = self.vm.qmp('block-job-complete', device='commit-job')
241298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
242298c6009SMax Reitz
243298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_COMPLETED')
244298c6009SMax Reitz
2452782bb75SMax Reitz        node = self.findBlockNode(None,
2462782bb75SMax Reitz                                  '/machine/peripheral/qdev0/virtio-backend')
247298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
248298c6009SMax Reitz                        back1_img)
249298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
250298c6009SMax Reitz                        back0_img)
251298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
252298c6009SMax Reitz                               '/filename')
253298c6009SMax Reitz
254298c6009SMax Reitz        self.assertIntactSourceBackingChain()
255298c6009SMax Reitz
256298c6009SMax Reitz
257298c6009SMax ReitzBaseClass = None
258298c6009SMax ReitzMirrorBaseClass = None
259298c6009SMax Reitz
260298c6009SMax Reitzif __name__ == '__main__':
261103cbc77SMax Reitz    iotests.main(supported_fmts=['qcow2'],
262103cbc77SMax Reitz                 supported_protocols=['file'])
263