xref: /qemu/tests/qemu-iotests/155 (revision 8bdee9f1)
1903cb1bfSPhilippe 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)
48*8bdee9f1SKevin Wolf# target_open_with_backing: If True, the target image is added with its backing
49*8bdee9f1SKevin Wolf#                           chain opened right away. If False, blockdev-add
50*8bdee9f1SKevin Wolf#                           opens it without a backing file and job completion
51*8bdee9f1SKevin Wolf#                           is supposed to open the backing chain.
52298c6009SMax Reitz
53298c6009SMax Reitzclass BaseClass(iotests.QMPTestCase):
54298c6009SMax Reitz    target_blockdev_backing = None
55298c6009SMax Reitz    target_real_backing = None
56*8bdee9f1SKevin Wolf    target_open_with_backing = True
57298c6009SMax Reitz
58298c6009SMax Reitz    def setUp(self):
591d701e0eSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K')
60298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img, back1_img)
61298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img, back2_img)
62298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img, source_img)
63298c6009SMax Reitz
64298c6009SMax Reitz        self.vm = iotests.VM()
65298c6009SMax Reitz        # Add the BDS via blockdev-add so it stays around after the mirror block
66298c6009SMax Reitz        # job has been completed
671d701e0eSMax Reitz        blockdev = {'node-name': 'source',
681d701e0eSMax Reitz                    'driver': iotests.imgfmt,
691d701e0eSMax Reitz                    'file': {'driver': 'file',
701d701e0eSMax Reitz                             'filename': source_img}}
7162a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
722782bb75SMax Reitz        self.vm.add_device('virtio-blk,id=qdev0,drive=source')
731d701e0eSMax Reitz        self.vm.launch()
74298c6009SMax Reitz
75298c6009SMax Reitz        self.assertIntactSourceBackingChain()
76298c6009SMax Reitz
77298c6009SMax Reitz        if self.existing:
78298c6009SMax Reitz            if self.target_backing:
79298c6009SMax Reitz                qemu_img('create', '-f', iotests.imgfmt,
801d701e0eSMax Reitz                         '-b', self.target_backing, target_img, '1440K')
81298c6009SMax Reitz            else:
821d701e0eSMax Reitz                qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
83298c6009SMax Reitz
84298c6009SMax Reitz            if self.cmd == 'blockdev-mirror':
85298c6009SMax Reitz                options = { 'node-name': 'target',
86298c6009SMax Reitz                            'driver': iotests.imgfmt,
87298c6009SMax Reitz                            'file': { 'driver': 'file',
88*8bdee9f1SKevin Wolf                                      'node-name': 'target-file',
89298c6009SMax Reitz                                      'filename': target_img } }
90*8bdee9f1SKevin Wolf
91*8bdee9f1SKevin Wolf                if not self.target_open_with_backing:
92*8bdee9f1SKevin Wolf                        options['backing'] = None
93*8bdee9f1SKevin Wolf                elif self.target_blockdev_backing:
94298c6009SMax Reitz                        options['backing'] = self.target_blockdev_backing
95298c6009SMax Reitz
960153d2f5SKevin Wolf                result = self.vm.qmp('blockdev-add', **options)
97298c6009SMax Reitz                self.assert_qmp(result, 'return', {})
98298c6009SMax Reitz
99298c6009SMax Reitz    def tearDown(self):
100298c6009SMax Reitz        self.vm.shutdown()
101298c6009SMax Reitz        os.remove(source_img)
102298c6009SMax Reitz        os.remove(back2_img)
103298c6009SMax Reitz        os.remove(back1_img)
104298c6009SMax Reitz        os.remove(back0_img)
105298c6009SMax Reitz        try:
106298c6009SMax Reitz            os.remove(target_img)
107298c6009SMax Reitz        except OSError:
108298c6009SMax Reitz            pass
109298c6009SMax Reitz
1101d701e0eSMax Reitz    def findBlockNode(self, node_name, qdev=None):
1111d701e0eSMax Reitz        if qdev:
112298c6009SMax Reitz            result = self.vm.qmp('query-block')
113298c6009SMax Reitz            for device in result['return']:
1141d701e0eSMax Reitz                if device['qdev'] == qdev:
115298c6009SMax Reitz                    if node_name:
116298c6009SMax Reitz                        self.assert_qmp(device, 'inserted/node-name', node_name)
117298c6009SMax Reitz                    return device['inserted']
118298c6009SMax Reitz        else:
119298c6009SMax Reitz            result = self.vm.qmp('query-named-block-nodes')
120298c6009SMax Reitz            for node in result['return']:
121298c6009SMax Reitz                if node['node-name'] == node_name:
122298c6009SMax Reitz                    return node
123298c6009SMax Reitz
1241d701e0eSMax Reitz        self.fail('Cannot find node %s/%s' % (qdev, node_name))
125298c6009SMax Reitz
126298c6009SMax Reitz    def assertIntactSourceBackingChain(self):
127298c6009SMax Reitz        node = self.findBlockNode('source')
128298c6009SMax Reitz
129298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
130298c6009SMax Reitz                        source_img)
131298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
132298c6009SMax Reitz                        back2_img)
133298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
134298c6009SMax Reitz                        back1_img)
135298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
136298c6009SMax Reitz                        back0_img)
137298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
138298c6009SMax Reitz
139298c6009SMax Reitz    def assertCorrectBackingImage(self, node, default_image):
140298c6009SMax Reitz        if self.existing:
141298c6009SMax Reitz            if self.target_real_backing:
142298c6009SMax Reitz                image = self.target_real_backing
143298c6009SMax Reitz            else:
144298c6009SMax Reitz                image = self.target_backing
145298c6009SMax Reitz        else:
146298c6009SMax Reitz            image = default_image
147298c6009SMax Reitz
148298c6009SMax Reitz        if image:
149298c6009SMax Reitz            self.assert_qmp(node, 'image/backing-image/filename', image)
150298c6009SMax Reitz        else:
151298c6009SMax Reitz            self.assert_qmp_absent(node, 'image/backing-image')
152298c6009SMax Reitz
153298c6009SMax Reitz
154298c6009SMax Reitz# Class variables for controlling its behavior:
155298c6009SMax Reitz#
156298c6009SMax Reitz# cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
157298c6009SMax Reitz
158298c6009SMax Reitzclass MirrorBaseClass(BaseClass):
159*8bdee9f1SKevin Wolf    def openBacking(self):
160*8bdee9f1SKevin Wolf        pass
161*8bdee9f1SKevin Wolf
162298c6009SMax Reitz    def runMirror(self, sync):
163298c6009SMax Reitz        if self.cmd == 'blockdev-mirror':
1641d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
165*8bdee9f1SKevin Wolf                                 sync=sync, target='target',
166*8bdee9f1SKevin Wolf                                 auto_finalize=False)
167298c6009SMax Reitz        else:
168298c6009SMax Reitz            if self.existing:
169298c6009SMax Reitz                mode = 'existing'
170298c6009SMax Reitz            else:
171298c6009SMax Reitz                mode = 'absolute-paths'
1721d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
1731d701e0eSMax Reitz                                 sync=sync, target=target_img,
1741d701e0eSMax Reitz                                 format=iotests.imgfmt, mode=mode,
175*8bdee9f1SKevin Wolf                                 node_name='target', auto_finalize=False)
176298c6009SMax Reitz
177298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
178298c6009SMax Reitz
179*8bdee9f1SKevin Wolf        self.vm.run_job('mirror-job', use_log=False, auto_finalize=False,
180*8bdee9f1SKevin Wolf                        pre_finalize=self.openBacking, auto_dismiss=True)
181298c6009SMax Reitz
182298c6009SMax Reitz    def testFull(self):
183298c6009SMax Reitz        self.runMirror('full')
184298c6009SMax Reitz
1852782bb75SMax Reitz        node = self.findBlockNode('target',
1862782bb75SMax Reitz                                  '/machine/peripheral/qdev0/virtio-backend')
187298c6009SMax Reitz        self.assertCorrectBackingImage(node, None)
188298c6009SMax Reitz        self.assertIntactSourceBackingChain()
189298c6009SMax Reitz
190298c6009SMax Reitz    def testTop(self):
191298c6009SMax Reitz        self.runMirror('top')
192298c6009SMax Reitz
1932782bb75SMax Reitz        node = self.findBlockNode('target',
1942782bb75SMax Reitz                                  '/machine/peripheral/qdev0/virtio-backend')
195298c6009SMax Reitz        self.assertCorrectBackingImage(node, back2_img)
196298c6009SMax Reitz        self.assertIntactSourceBackingChain()
197298c6009SMax Reitz
198298c6009SMax Reitz    def testNone(self):
199298c6009SMax Reitz        self.runMirror('none')
200298c6009SMax Reitz
2012782bb75SMax Reitz        node = self.findBlockNode('target',
2022782bb75SMax Reitz                                  '/machine/peripheral/qdev0/virtio-backend')
203298c6009SMax Reitz        self.assertCorrectBackingImage(node, source_img)
204298c6009SMax Reitz        self.assertIntactSourceBackingChain()
205298c6009SMax Reitz
206298c6009SMax Reitz
207298c6009SMax Reitzclass TestDriveMirrorAbsolutePaths(MirrorBaseClass):
208298c6009SMax Reitz    cmd = 'drive-mirror'
209298c6009SMax Reitz    existing = False
210298c6009SMax Reitz
211298c6009SMax Reitzclass TestDriveMirrorExistingNoBacking(MirrorBaseClass):
212298c6009SMax Reitz    cmd = 'drive-mirror'
213298c6009SMax Reitz    existing = True
214298c6009SMax Reitz    target_backing = None
215298c6009SMax Reitz
216298c6009SMax Reitzclass TestDriveMirrorExistingBacking(MirrorBaseClass):
217298c6009SMax Reitz    cmd = 'drive-mirror'
218298c6009SMax Reitz    existing = True
219298c6009SMax Reitz    target_backing = 'null-co://'
220298c6009SMax Reitz
221298c6009SMax Reitzclass TestBlockdevMirrorNoBacking(MirrorBaseClass):
222298c6009SMax Reitz    cmd = 'blockdev-mirror'
223298c6009SMax Reitz    existing = True
224298c6009SMax Reitz    target_backing = None
225298c6009SMax Reitz
226298c6009SMax Reitzclass TestBlockdevMirrorBacking(MirrorBaseClass):
227298c6009SMax Reitz    cmd = 'blockdev-mirror'
228298c6009SMax Reitz    existing = True
229298c6009SMax Reitz    target_backing = 'null-co://'
230298c6009SMax Reitz
231298c6009SMax Reitzclass TestBlockdevMirrorForcedBacking(MirrorBaseClass):
232298c6009SMax Reitz    cmd = 'blockdev-mirror'
233298c6009SMax Reitz    existing = True
234298c6009SMax Reitz    target_backing = None
235298c6009SMax Reitz    target_blockdev_backing = { 'driver': 'null-co' }
236298c6009SMax Reitz    target_real_backing = 'null-co://'
237298c6009SMax Reitz
238*8bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-reopen
239*8bdee9f1SKevin Wolfclass TestBlockdevMirrorReopen(MirrorBaseClass):
240*8bdee9f1SKevin Wolf    cmd = 'blockdev-mirror'
241*8bdee9f1SKevin Wolf    existing = True
242*8bdee9f1SKevin Wolf    target_backing = 'null-co://'
243*8bdee9f1SKevin Wolf    target_open_with_backing = False
244*8bdee9f1SKevin Wolf
245*8bdee9f1SKevin Wolf    def openBacking(self):
246*8bdee9f1SKevin Wolf        if not self.target_open_with_backing:
247*8bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-add', node_name="backing",
248*8bdee9f1SKevin Wolf                                 driver="null-co")
249*8bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
250*8bdee9f1SKevin Wolf            result = self.vm.qmp('x-blockdev-reopen', node_name="target",
251*8bdee9f1SKevin Wolf                                 driver=iotests.imgfmt, file="target-file",
252*8bdee9f1SKevin Wolf                                 backing="backing")
253*8bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
254*8bdee9f1SKevin Wolf
255*8bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-snapshot
256*8bdee9f1SKevin Wolfclass TestBlockdevMirrorSnapshot(MirrorBaseClass):
257*8bdee9f1SKevin Wolf    cmd = 'blockdev-mirror'
258*8bdee9f1SKevin Wolf    existing = True
259*8bdee9f1SKevin Wolf    target_backing = 'null-co://'
260*8bdee9f1SKevin Wolf    target_open_with_backing = False
261*8bdee9f1SKevin Wolf
262*8bdee9f1SKevin Wolf    def openBacking(self):
263*8bdee9f1SKevin Wolf        if not self.target_open_with_backing:
264*8bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-add', node_name="backing",
265*8bdee9f1SKevin Wolf                                 driver="null-co")
266*8bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
267*8bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-snapshot', node="backing",
268*8bdee9f1SKevin Wolf                                 overlay="target")
269*8bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
270298c6009SMax Reitz
271298c6009SMax Reitzclass TestCommit(BaseClass):
272298c6009SMax Reitz    existing = False
273298c6009SMax Reitz
274298c6009SMax Reitz    def testCommit(self):
2751d701e0eSMax Reitz        result = self.vm.qmp('block-commit', job_id='commit-job',
2761d701e0eSMax Reitz                             device='source', base=back1_img)
277298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
278298c6009SMax Reitz
279298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_READY')
280298c6009SMax Reitz
2811d701e0eSMax Reitz        result = self.vm.qmp('block-job-complete', device='commit-job')
282298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
283298c6009SMax Reitz
284298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_COMPLETED')
285298c6009SMax Reitz
2862782bb75SMax Reitz        node = self.findBlockNode(None,
2872782bb75SMax Reitz                                  '/machine/peripheral/qdev0/virtio-backend')
288298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
289298c6009SMax Reitz                        back1_img)
290298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
291298c6009SMax Reitz                        back0_img)
292298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
293298c6009SMax Reitz                               '/filename')
294298c6009SMax Reitz
295298c6009SMax Reitz        self.assertIntactSourceBackingChain()
296298c6009SMax Reitz
297298c6009SMax Reitz
298298c6009SMax ReitzBaseClass = None
299298c6009SMax ReitzMirrorBaseClass = None
300298c6009SMax Reitz
301298c6009SMax Reitzif __name__ == '__main__':
302103cbc77SMax Reitz    iotests.main(supported_fmts=['qcow2'],
303103cbc77SMax Reitz                 supported_protocols=['file'])
304