xref: /qemu/tests/qemu-iotests/155 (revision 52ea799e)
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)
488bdee9f1SKevin Wolf# target_open_with_backing: If True, the target image is added with its backing
498bdee9f1SKevin Wolf#                           chain opened right away. If False, blockdev-add
508bdee9f1SKevin Wolf#                           opens it without a backing file and job completion
518bdee9f1SKevin Wolf#                           is supposed to open the backing chain.
526a5f6403SKevin Wolf# use_iothread: If True, an iothread is configured for the virtio-blk device
536a5f6403SKevin Wolf#               that uses the image being mirrored
54298c6009SMax Reitz
55298c6009SMax Reitzclass BaseClass(iotests.QMPTestCase):
56298c6009SMax Reitz    target_blockdev_backing = None
57298c6009SMax Reitz    target_real_backing = None
588bdee9f1SKevin Wolf    target_open_with_backing = True
596a5f6403SKevin Wolf    use_iothread = False
60298c6009SMax Reitz
61298c6009SMax Reitz    def setUp(self):
621d701e0eSMax Reitz        qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K')
63298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img, back1_img)
64298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img, back2_img)
65298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img, source_img)
66298c6009SMax Reitz
67298c6009SMax Reitz        self.vm = iotests.VM()
68298c6009SMax Reitz        # Add the BDS via blockdev-add so it stays around after the mirror block
69298c6009SMax Reitz        # job has been completed
701d701e0eSMax Reitz        blockdev = {'node-name': 'source',
711d701e0eSMax Reitz                    'driver': iotests.imgfmt,
721d701e0eSMax Reitz                    'file': {'driver': 'file',
731d701e0eSMax Reitz                             'filename': source_img}}
7462a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
756a5f6403SKevin Wolf
766a5f6403SKevin Wolf        if self.use_iothread:
776a5f6403SKevin Wolf            self.vm.add_object('iothread,id=iothread0')
786a5f6403SKevin Wolf            iothread = ",iothread=iothread0"
796a5f6403SKevin Wolf        else:
806a5f6403SKevin Wolf            iothread = ""
816a5f6403SKevin Wolf
826a5f6403SKevin Wolf        self.vm.add_device('virtio-scsi%s' % iothread)
836a5f6403SKevin Wolf        self.vm.add_device('scsi-hd,id=qdev0,drive=source')
846a5f6403SKevin Wolf
851d701e0eSMax Reitz        self.vm.launch()
86298c6009SMax Reitz
87298c6009SMax Reitz        self.assertIntactSourceBackingChain()
88298c6009SMax Reitz
89298c6009SMax Reitz        if self.existing:
90298c6009SMax Reitz            if self.target_backing:
91298c6009SMax Reitz                qemu_img('create', '-f', iotests.imgfmt,
921d701e0eSMax Reitz                         '-b', self.target_backing, target_img, '1440K')
93298c6009SMax Reitz            else:
941d701e0eSMax Reitz                qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
95298c6009SMax Reitz
96298c6009SMax Reitz            if self.cmd == 'blockdev-mirror':
97298c6009SMax Reitz                options = { 'node-name': 'target',
98298c6009SMax Reitz                            'driver': iotests.imgfmt,
99298c6009SMax Reitz                            'file': { 'driver': 'file',
1008bdee9f1SKevin Wolf                                      'node-name': 'target-file',
101298c6009SMax Reitz                                      'filename': target_img } }
1028bdee9f1SKevin Wolf
1038bdee9f1SKevin Wolf                if not self.target_open_with_backing:
1048bdee9f1SKevin Wolf                        options['backing'] = None
1058bdee9f1SKevin Wolf                elif self.target_blockdev_backing:
106298c6009SMax Reitz                        options['backing'] = self.target_blockdev_backing
107298c6009SMax Reitz
1080153d2f5SKevin Wolf                result = self.vm.qmp('blockdev-add', **options)
109298c6009SMax Reitz                self.assert_qmp(result, 'return', {})
110298c6009SMax Reitz
111298c6009SMax Reitz    def tearDown(self):
112298c6009SMax Reitz        self.vm.shutdown()
113298c6009SMax Reitz        os.remove(source_img)
114298c6009SMax Reitz        os.remove(back2_img)
115298c6009SMax Reitz        os.remove(back1_img)
116298c6009SMax Reitz        os.remove(back0_img)
117298c6009SMax Reitz        try:
118298c6009SMax Reitz            os.remove(target_img)
119298c6009SMax Reitz        except OSError:
120298c6009SMax Reitz            pass
121298c6009SMax Reitz
1221d701e0eSMax Reitz    def findBlockNode(self, node_name, qdev=None):
1231d701e0eSMax Reitz        if qdev:
124298c6009SMax Reitz            result = self.vm.qmp('query-block')
125298c6009SMax Reitz            for device in result['return']:
1261d701e0eSMax Reitz                if device['qdev'] == qdev:
127298c6009SMax Reitz                    if node_name:
128298c6009SMax Reitz                        self.assert_qmp(device, 'inserted/node-name', node_name)
129298c6009SMax Reitz                    return device['inserted']
130298c6009SMax Reitz        else:
131298c6009SMax Reitz            result = self.vm.qmp('query-named-block-nodes')
132298c6009SMax Reitz            for node in result['return']:
133298c6009SMax Reitz                if node['node-name'] == node_name:
134298c6009SMax Reitz                    return node
135298c6009SMax Reitz
1361d701e0eSMax Reitz        self.fail('Cannot find node %s/%s' % (qdev, node_name))
137298c6009SMax Reitz
138298c6009SMax Reitz    def assertIntactSourceBackingChain(self):
139298c6009SMax Reitz        node = self.findBlockNode('source')
140298c6009SMax Reitz
141298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
142298c6009SMax Reitz                        source_img)
143298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
144298c6009SMax Reitz                        back2_img)
145298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
146298c6009SMax Reitz                        back1_img)
147298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
148298c6009SMax Reitz                        back0_img)
149298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
150298c6009SMax Reitz
151298c6009SMax Reitz    def assertCorrectBackingImage(self, node, default_image):
152298c6009SMax Reitz        if self.existing:
153298c6009SMax Reitz            if self.target_real_backing:
154298c6009SMax Reitz                image = self.target_real_backing
155298c6009SMax Reitz            else:
156298c6009SMax Reitz                image = self.target_backing
157298c6009SMax Reitz        else:
158298c6009SMax Reitz            image = default_image
159298c6009SMax Reitz
160298c6009SMax Reitz        if image:
161298c6009SMax Reitz            self.assert_qmp(node, 'image/backing-image/filename', image)
162298c6009SMax Reitz        else:
163298c6009SMax Reitz            self.assert_qmp_absent(node, 'image/backing-image')
164298c6009SMax Reitz
165298c6009SMax Reitz
166298c6009SMax Reitz# Class variables for controlling its behavior:
167298c6009SMax Reitz#
168298c6009SMax Reitz# cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
169298c6009SMax Reitz
170298c6009SMax Reitzclass MirrorBaseClass(BaseClass):
1718bdee9f1SKevin Wolf    def openBacking(self):
1728bdee9f1SKevin Wolf        pass
1738bdee9f1SKevin Wolf
174298c6009SMax Reitz    def runMirror(self, sync):
175298c6009SMax Reitz        if self.cmd == 'blockdev-mirror':
1761d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
1778bdee9f1SKevin Wolf                                 sync=sync, target='target',
1788bdee9f1SKevin Wolf                                 auto_finalize=False)
179298c6009SMax Reitz        else:
180298c6009SMax Reitz            if self.existing:
181298c6009SMax Reitz                mode = 'existing'
182298c6009SMax Reitz            else:
183298c6009SMax Reitz                mode = 'absolute-paths'
1841d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
1851d701e0eSMax Reitz                                 sync=sync, target=target_img,
1861d701e0eSMax Reitz                                 format=iotests.imgfmt, mode=mode,
1878bdee9f1SKevin Wolf                                 node_name='target', auto_finalize=False)
188298c6009SMax Reitz
189298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
190298c6009SMax Reitz
191*52ea799eSJohn Snow        self.vm.run_job('mirror-job', auto_finalize=False,
1928bdee9f1SKevin Wolf                        pre_finalize=self.openBacking, auto_dismiss=True)
193298c6009SMax Reitz
194298c6009SMax Reitz    def testFull(self):
195298c6009SMax Reitz        self.runMirror('full')
196298c6009SMax Reitz
1976a5f6403SKevin Wolf        node = self.findBlockNode('target', 'qdev0')
198298c6009SMax Reitz        self.assertCorrectBackingImage(node, None)
199298c6009SMax Reitz        self.assertIntactSourceBackingChain()
200298c6009SMax Reitz
201298c6009SMax Reitz    def testTop(self):
202298c6009SMax Reitz        self.runMirror('top')
203298c6009SMax Reitz
2046a5f6403SKevin Wolf        node = self.findBlockNode('target', 'qdev0')
205298c6009SMax Reitz        self.assertCorrectBackingImage(node, back2_img)
206298c6009SMax Reitz        self.assertIntactSourceBackingChain()
207298c6009SMax Reitz
208298c6009SMax Reitz    def testNone(self):
209298c6009SMax Reitz        self.runMirror('none')
210298c6009SMax Reitz
2116a5f6403SKevin Wolf        node = self.findBlockNode('target', 'qdev0')
212298c6009SMax Reitz        self.assertCorrectBackingImage(node, source_img)
213298c6009SMax Reitz        self.assertIntactSourceBackingChain()
214298c6009SMax Reitz
215298c6009SMax Reitz
216298c6009SMax Reitzclass TestDriveMirrorAbsolutePaths(MirrorBaseClass):
217298c6009SMax Reitz    cmd = 'drive-mirror'
218298c6009SMax Reitz    existing = False
219298c6009SMax Reitz
220298c6009SMax Reitzclass TestDriveMirrorExistingNoBacking(MirrorBaseClass):
221298c6009SMax Reitz    cmd = 'drive-mirror'
222298c6009SMax Reitz    existing = True
223298c6009SMax Reitz    target_backing = None
224298c6009SMax Reitz
225298c6009SMax Reitzclass TestDriveMirrorExistingBacking(MirrorBaseClass):
226298c6009SMax Reitz    cmd = 'drive-mirror'
227298c6009SMax Reitz    existing = True
228298c6009SMax Reitz    target_backing = 'null-co://'
229298c6009SMax Reitz
230298c6009SMax Reitzclass TestBlockdevMirrorNoBacking(MirrorBaseClass):
231298c6009SMax Reitz    cmd = 'blockdev-mirror'
232298c6009SMax Reitz    existing = True
233298c6009SMax Reitz    target_backing = None
234298c6009SMax Reitz
235298c6009SMax Reitzclass TestBlockdevMirrorBacking(MirrorBaseClass):
236298c6009SMax Reitz    cmd = 'blockdev-mirror'
237298c6009SMax Reitz    existing = True
238298c6009SMax Reitz    target_backing = 'null-co://'
239298c6009SMax Reitz
240298c6009SMax Reitzclass TestBlockdevMirrorForcedBacking(MirrorBaseClass):
241298c6009SMax Reitz    cmd = 'blockdev-mirror'
242298c6009SMax Reitz    existing = True
243298c6009SMax Reitz    target_backing = None
244298c6009SMax Reitz    target_blockdev_backing = { 'driver': 'null-co' }
245298c6009SMax Reitz    target_real_backing = 'null-co://'
246298c6009SMax Reitz
2478bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-reopen
2488bdee9f1SKevin Wolfclass TestBlockdevMirrorReopen(MirrorBaseClass):
2498bdee9f1SKevin Wolf    cmd = 'blockdev-mirror'
2508bdee9f1SKevin Wolf    existing = True
2518bdee9f1SKevin Wolf    target_backing = 'null-co://'
2528bdee9f1SKevin Wolf    target_open_with_backing = False
2538bdee9f1SKevin Wolf
2548bdee9f1SKevin Wolf    def openBacking(self):
2558bdee9f1SKevin Wolf        if not self.target_open_with_backing:
2568bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-add', node_name="backing",
2578bdee9f1SKevin Wolf                                 driver="null-co")
2588bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
2598bdee9f1SKevin Wolf            result = self.vm.qmp('x-blockdev-reopen', node_name="target",
2608bdee9f1SKevin Wolf                                 driver=iotests.imgfmt, file="target-file",
2618bdee9f1SKevin Wolf                                 backing="backing")
2628bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
2638bdee9f1SKevin Wolf
2646a5f6403SKevin Wolfclass TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen):
2656a5f6403SKevin Wolf    use_iothread = True
2666a5f6403SKevin Wolf
2678bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-snapshot
2688bdee9f1SKevin Wolfclass TestBlockdevMirrorSnapshot(MirrorBaseClass):
2698bdee9f1SKevin Wolf    cmd = 'blockdev-mirror'
2708bdee9f1SKevin Wolf    existing = True
2718bdee9f1SKevin Wolf    target_backing = 'null-co://'
2728bdee9f1SKevin Wolf    target_open_with_backing = False
2738bdee9f1SKevin Wolf
2748bdee9f1SKevin Wolf    def openBacking(self):
2758bdee9f1SKevin Wolf        if not self.target_open_with_backing:
2768bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-add', node_name="backing",
2778bdee9f1SKevin Wolf                                 driver="null-co")
2788bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
2798bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-snapshot', node="backing",
2808bdee9f1SKevin Wolf                                 overlay="target")
2818bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
282298c6009SMax Reitz
2836a5f6403SKevin Wolfclass TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot):
2846a5f6403SKevin Wolf    use_iothread = True
2856a5f6403SKevin Wolf
286298c6009SMax Reitzclass TestCommit(BaseClass):
287298c6009SMax Reitz    existing = False
288298c6009SMax Reitz
289298c6009SMax Reitz    def testCommit(self):
2901d701e0eSMax Reitz        result = self.vm.qmp('block-commit', job_id='commit-job',
2911d701e0eSMax Reitz                             device='source', base=back1_img)
292298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
293298c6009SMax Reitz
294298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_READY')
295298c6009SMax Reitz
2961d701e0eSMax Reitz        result = self.vm.qmp('block-job-complete', device='commit-job')
297298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
298298c6009SMax Reitz
299298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_COMPLETED')
300298c6009SMax Reitz
3016a5f6403SKevin Wolf        node = self.findBlockNode(None, 'qdev0')
302298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
303298c6009SMax Reitz                        back1_img)
304298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
305298c6009SMax Reitz                        back0_img)
306298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
307298c6009SMax Reitz                               '/filename')
308298c6009SMax Reitz
309298c6009SMax Reitz        self.assertIntactSourceBackingChain()
310298c6009SMax Reitz
311298c6009SMax Reitz
312298c6009SMax ReitzBaseClass = None
313298c6009SMax ReitzMirrorBaseClass = None
314298c6009SMax Reitz
315298c6009SMax Reitzif __name__ == '__main__':
316103cbc77SMax Reitz    iotests.main(supported_fmts=['qcow2'],
317103cbc77SMax Reitz                 supported_protocols=['file'])
318