xref: /qemu/tests/qemu-iotests/155 (revision b66ff2c2)
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')
63*b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img,
64*b66ff2c2SEric Blake                 '-F', iotests.imgfmt, back1_img)
65*b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img,
66*b66ff2c2SEric Blake                 '-F', iotests.imgfmt, back2_img)
67*b66ff2c2SEric Blake        qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img,
68*b66ff2c2SEric Blake                 '-F', iotests.imgfmt, source_img)
69298c6009SMax Reitz
70298c6009SMax Reitz        self.vm = iotests.VM()
71298c6009SMax Reitz        # Add the BDS via blockdev-add so it stays around after the mirror block
72298c6009SMax Reitz        # job has been completed
731d701e0eSMax Reitz        blockdev = {'node-name': 'source',
741d701e0eSMax Reitz                    'driver': iotests.imgfmt,
751d701e0eSMax Reitz                    'file': {'driver': 'file',
761d701e0eSMax Reitz                             'filename': source_img}}
7762a94288SKevin Wolf        self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
786a5f6403SKevin Wolf
796a5f6403SKevin Wolf        if self.use_iothread:
806a5f6403SKevin Wolf            self.vm.add_object('iothread,id=iothread0')
816a5f6403SKevin Wolf            iothread = ",iothread=iothread0"
826a5f6403SKevin Wolf        else:
836a5f6403SKevin Wolf            iothread = ""
846a5f6403SKevin Wolf
856a5f6403SKevin Wolf        self.vm.add_device('virtio-scsi%s' % iothread)
866a5f6403SKevin Wolf        self.vm.add_device('scsi-hd,id=qdev0,drive=source')
876a5f6403SKevin Wolf
881d701e0eSMax Reitz        self.vm.launch()
89298c6009SMax Reitz
90298c6009SMax Reitz        self.assertIntactSourceBackingChain()
91298c6009SMax Reitz
92298c6009SMax Reitz        if self.existing:
93298c6009SMax Reitz            if self.target_backing:
94298c6009SMax Reitz                qemu_img('create', '-f', iotests.imgfmt,
95*b66ff2c2SEric Blake                         '-b', self.target_backing, '-F', 'raw',
96*b66ff2c2SEric Blake                         target_img, '1440K')
97298c6009SMax Reitz            else:
981d701e0eSMax Reitz                qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
99298c6009SMax Reitz
100298c6009SMax Reitz            if self.cmd == 'blockdev-mirror':
101298c6009SMax Reitz                options = { 'node-name': 'target',
102298c6009SMax Reitz                            'driver': iotests.imgfmt,
103298c6009SMax Reitz                            'file': { 'driver': 'file',
1048bdee9f1SKevin Wolf                                      'node-name': 'target-file',
105298c6009SMax Reitz                                      'filename': target_img } }
1068bdee9f1SKevin Wolf
1078bdee9f1SKevin Wolf                if not self.target_open_with_backing:
1088bdee9f1SKevin Wolf                        options['backing'] = None
1098bdee9f1SKevin Wolf                elif self.target_blockdev_backing:
110298c6009SMax Reitz                        options['backing'] = self.target_blockdev_backing
111298c6009SMax Reitz
1120153d2f5SKevin Wolf                result = self.vm.qmp('blockdev-add', **options)
113298c6009SMax Reitz                self.assert_qmp(result, 'return', {})
114298c6009SMax Reitz
115298c6009SMax Reitz    def tearDown(self):
116298c6009SMax Reitz        self.vm.shutdown()
117298c6009SMax Reitz        os.remove(source_img)
118298c6009SMax Reitz        os.remove(back2_img)
119298c6009SMax Reitz        os.remove(back1_img)
120298c6009SMax Reitz        os.remove(back0_img)
121298c6009SMax Reitz        try:
122298c6009SMax Reitz            os.remove(target_img)
123298c6009SMax Reitz        except OSError:
124298c6009SMax Reitz            pass
125298c6009SMax Reitz
1261d701e0eSMax Reitz    def findBlockNode(self, node_name, qdev=None):
1271d701e0eSMax Reitz        if qdev:
128298c6009SMax Reitz            result = self.vm.qmp('query-block')
129298c6009SMax Reitz            for device in result['return']:
1301d701e0eSMax Reitz                if device['qdev'] == qdev:
131298c6009SMax Reitz                    if node_name:
132298c6009SMax Reitz                        self.assert_qmp(device, 'inserted/node-name', node_name)
133298c6009SMax Reitz                    return device['inserted']
134298c6009SMax Reitz        else:
135298c6009SMax Reitz            result = self.vm.qmp('query-named-block-nodes')
136298c6009SMax Reitz            for node in result['return']:
137298c6009SMax Reitz                if node['node-name'] == node_name:
138298c6009SMax Reitz                    return node
139298c6009SMax Reitz
1401d701e0eSMax Reitz        self.fail('Cannot find node %s/%s' % (qdev, node_name))
141298c6009SMax Reitz
142298c6009SMax Reitz    def assertIntactSourceBackingChain(self):
143298c6009SMax Reitz        node = self.findBlockNode('source')
144298c6009SMax Reitz
145298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
146298c6009SMax Reitz                        source_img)
147298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
148298c6009SMax Reitz                        back2_img)
149298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
150298c6009SMax Reitz                        back1_img)
151298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
152298c6009SMax Reitz                        back0_img)
153298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
154298c6009SMax Reitz
155298c6009SMax Reitz    def assertCorrectBackingImage(self, node, default_image):
156298c6009SMax Reitz        if self.existing:
157298c6009SMax Reitz            if self.target_real_backing:
158298c6009SMax Reitz                image = self.target_real_backing
159298c6009SMax Reitz            else:
160298c6009SMax Reitz                image = self.target_backing
161298c6009SMax Reitz        else:
162298c6009SMax Reitz            image = default_image
163298c6009SMax Reitz
164298c6009SMax Reitz        if image:
165298c6009SMax Reitz            self.assert_qmp(node, 'image/backing-image/filename', image)
166298c6009SMax Reitz        else:
167298c6009SMax Reitz            self.assert_qmp_absent(node, 'image/backing-image')
168298c6009SMax Reitz
169298c6009SMax Reitz
170298c6009SMax Reitz# Class variables for controlling its behavior:
171298c6009SMax Reitz#
172298c6009SMax Reitz# cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
173298c6009SMax Reitz
174298c6009SMax Reitzclass MirrorBaseClass(BaseClass):
1758bdee9f1SKevin Wolf    def openBacking(self):
1768bdee9f1SKevin Wolf        pass
1778bdee9f1SKevin Wolf
178298c6009SMax Reitz    def runMirror(self, sync):
179298c6009SMax Reitz        if self.cmd == 'blockdev-mirror':
1801d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
1818bdee9f1SKevin Wolf                                 sync=sync, target='target',
1828bdee9f1SKevin Wolf                                 auto_finalize=False)
183298c6009SMax Reitz        else:
184298c6009SMax Reitz            if self.existing:
185298c6009SMax Reitz                mode = 'existing'
186298c6009SMax Reitz            else:
187298c6009SMax Reitz                mode = 'absolute-paths'
1881d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
1891d701e0eSMax Reitz                                 sync=sync, target=target_img,
1901d701e0eSMax Reitz                                 format=iotests.imgfmt, mode=mode,
1918bdee9f1SKevin Wolf                                 node_name='target', auto_finalize=False)
192298c6009SMax Reitz
193298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
194298c6009SMax Reitz
19552ea799eSJohn Snow        self.vm.run_job('mirror-job', auto_finalize=False,
1968bdee9f1SKevin Wolf                        pre_finalize=self.openBacking, auto_dismiss=True)
197298c6009SMax Reitz
198298c6009SMax Reitz    def testFull(self):
199298c6009SMax Reitz        self.runMirror('full')
200298c6009SMax Reitz
2016a5f6403SKevin Wolf        node = self.findBlockNode('target', 'qdev0')
202298c6009SMax Reitz        self.assertCorrectBackingImage(node, None)
203298c6009SMax Reitz        self.assertIntactSourceBackingChain()
204298c6009SMax Reitz
205298c6009SMax Reitz    def testTop(self):
206298c6009SMax Reitz        self.runMirror('top')
207298c6009SMax Reitz
2086a5f6403SKevin Wolf        node = self.findBlockNode('target', 'qdev0')
209298c6009SMax Reitz        self.assertCorrectBackingImage(node, back2_img)
210298c6009SMax Reitz        self.assertIntactSourceBackingChain()
211298c6009SMax Reitz
212298c6009SMax Reitz    def testNone(self):
213298c6009SMax Reitz        self.runMirror('none')
214298c6009SMax Reitz
2156a5f6403SKevin Wolf        node = self.findBlockNode('target', 'qdev0')
216298c6009SMax Reitz        self.assertCorrectBackingImage(node, source_img)
217298c6009SMax Reitz        self.assertIntactSourceBackingChain()
218298c6009SMax Reitz
219298c6009SMax Reitz
220298c6009SMax Reitzclass TestDriveMirrorAbsolutePaths(MirrorBaseClass):
221298c6009SMax Reitz    cmd = 'drive-mirror'
222298c6009SMax Reitz    existing = False
223298c6009SMax Reitz
224298c6009SMax Reitzclass TestDriveMirrorExistingNoBacking(MirrorBaseClass):
225298c6009SMax Reitz    cmd = 'drive-mirror'
226298c6009SMax Reitz    existing = True
227298c6009SMax Reitz    target_backing = None
228298c6009SMax Reitz
229298c6009SMax Reitzclass TestDriveMirrorExistingBacking(MirrorBaseClass):
230298c6009SMax Reitz    cmd = 'drive-mirror'
231298c6009SMax Reitz    existing = True
232298c6009SMax Reitz    target_backing = 'null-co://'
233298c6009SMax Reitz
234298c6009SMax Reitzclass TestBlockdevMirrorNoBacking(MirrorBaseClass):
235298c6009SMax Reitz    cmd = 'blockdev-mirror'
236298c6009SMax Reitz    existing = True
237298c6009SMax Reitz    target_backing = None
238298c6009SMax Reitz
239298c6009SMax Reitzclass TestBlockdevMirrorBacking(MirrorBaseClass):
240298c6009SMax Reitz    cmd = 'blockdev-mirror'
241298c6009SMax Reitz    existing = True
242298c6009SMax Reitz    target_backing = 'null-co://'
243298c6009SMax Reitz
244298c6009SMax Reitzclass TestBlockdevMirrorForcedBacking(MirrorBaseClass):
245298c6009SMax Reitz    cmd = 'blockdev-mirror'
246298c6009SMax Reitz    existing = True
247298c6009SMax Reitz    target_backing = None
248298c6009SMax Reitz    target_blockdev_backing = { 'driver': 'null-co' }
249298c6009SMax Reitz    target_real_backing = 'null-co://'
250298c6009SMax Reitz
2518bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-reopen
2528bdee9f1SKevin Wolfclass TestBlockdevMirrorReopen(MirrorBaseClass):
2538bdee9f1SKevin Wolf    cmd = 'blockdev-mirror'
2548bdee9f1SKevin Wolf    existing = True
2558bdee9f1SKevin Wolf    target_backing = 'null-co://'
2568bdee9f1SKevin Wolf    target_open_with_backing = False
2578bdee9f1SKevin Wolf
2588bdee9f1SKevin Wolf    def openBacking(self):
2598bdee9f1SKevin Wolf        if not self.target_open_with_backing:
2608bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-add', node_name="backing",
2618bdee9f1SKevin Wolf                                 driver="null-co")
2628bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
2638bdee9f1SKevin Wolf            result = self.vm.qmp('x-blockdev-reopen', node_name="target",
2648bdee9f1SKevin Wolf                                 driver=iotests.imgfmt, file="target-file",
2658bdee9f1SKevin Wolf                                 backing="backing")
2668bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
2678bdee9f1SKevin Wolf
2686a5f6403SKevin Wolfclass TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen):
2696a5f6403SKevin Wolf    use_iothread = True
2706a5f6403SKevin Wolf
2718bdee9f1SKevin Wolf# Attach the backing chain only during completion, with blockdev-snapshot
2728bdee9f1SKevin Wolfclass TestBlockdevMirrorSnapshot(MirrorBaseClass):
2738bdee9f1SKevin Wolf    cmd = 'blockdev-mirror'
2748bdee9f1SKevin Wolf    existing = True
2758bdee9f1SKevin Wolf    target_backing = 'null-co://'
2768bdee9f1SKevin Wolf    target_open_with_backing = False
2778bdee9f1SKevin Wolf
2788bdee9f1SKevin Wolf    def openBacking(self):
2798bdee9f1SKevin Wolf        if not self.target_open_with_backing:
2808bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-add', node_name="backing",
2818bdee9f1SKevin Wolf                                 driver="null-co")
2828bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
2838bdee9f1SKevin Wolf            result = self.vm.qmp('blockdev-snapshot', node="backing",
2848bdee9f1SKevin Wolf                                 overlay="target")
2858bdee9f1SKevin Wolf            self.assert_qmp(result, 'return', {})
286298c6009SMax Reitz
2876a5f6403SKevin Wolfclass TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot):
2886a5f6403SKevin Wolf    use_iothread = True
2896a5f6403SKevin Wolf
290298c6009SMax Reitzclass TestCommit(BaseClass):
291298c6009SMax Reitz    existing = False
292298c6009SMax Reitz
293298c6009SMax Reitz    def testCommit(self):
2941d701e0eSMax Reitz        result = self.vm.qmp('block-commit', job_id='commit-job',
2951d701e0eSMax Reitz                             device='source', base=back1_img)
296298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
297298c6009SMax Reitz
298298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_READY')
299298c6009SMax Reitz
3001d701e0eSMax Reitz        result = self.vm.qmp('block-job-complete', device='commit-job')
301298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
302298c6009SMax Reitz
303298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_COMPLETED')
304298c6009SMax Reitz
3056a5f6403SKevin Wolf        node = self.findBlockNode(None, 'qdev0')
306298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
307298c6009SMax Reitz                        back1_img)
308298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
309298c6009SMax Reitz                        back0_img)
310298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
311298c6009SMax Reitz                               '/filename')
312298c6009SMax Reitz
313298c6009SMax Reitz        self.assertIntactSourceBackingChain()
314298c6009SMax Reitz
315298c6009SMax Reitz
316298c6009SMax ReitzBaseClass = None
317298c6009SMax ReitzMirrorBaseClass = None
318298c6009SMax Reitz
319298c6009SMax Reitzif __name__ == '__main__':
320103cbc77SMax Reitz    iotests.main(supported_fmts=['qcow2'],
321103cbc77SMax Reitz                 supported_protocols=['file'])
322