xref: /qemu/tests/qemu-iotests/155 (revision 1d701e0e)
1298c6009SMax Reitz#!/usr/bin/env python
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):
54*1d701e0eSMax 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
62*1d701e0eSMax Reitz        blockdev = {'node-name': 'source',
63*1d701e0eSMax Reitz                    'driver': iotests.imgfmt,
64*1d701e0eSMax Reitz                    'file': {'driver': 'file',
65*1d701e0eSMax Reitz                             'filename': source_img}}
66*1d701e0eSMax Reitz        self.vm.add_blockdev(self.qmp_to_opts(blockdev))
67*1d701e0eSMax Reitz        self.vm.add_device('floppy,id=qdev0,drive=source')
68*1d701e0eSMax 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,
75*1d701e0eSMax Reitz                         '-b', self.target_backing, target_img, '1440K')
76298c6009SMax Reitz            else:
77*1d701e0eSMax 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
101*1d701e0eSMax Reitz    def findBlockNode(self, node_name, qdev=None):
102*1d701e0eSMax Reitz        if qdev:
103298c6009SMax Reitz            result = self.vm.qmp('query-block')
104298c6009SMax Reitz            for device in result['return']:
105*1d701e0eSMax 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
115*1d701e0eSMax 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':
152*1d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
153*1d701e0eSMax 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'
159*1d701e0eSMax Reitz            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
160*1d701e0eSMax Reitz                                 sync=sync, target=target_img,
161*1d701e0eSMax Reitz                                 format=iotests.imgfmt, mode=mode,
162*1d701e0eSMax 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
168*1d701e0eSMax 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
176*1d701e0eSMax Reitz        node = self.findBlockNode('target', 'qdev0')
177298c6009SMax Reitz        self.assertCorrectBackingImage(node, None)
178298c6009SMax Reitz        self.assertIntactSourceBackingChain()
179298c6009SMax Reitz
180298c6009SMax Reitz    def testTop(self):
181298c6009SMax Reitz        self.runMirror('top')
182298c6009SMax Reitz
183*1d701e0eSMax Reitz        node = self.findBlockNode('target', 'qdev0')
184298c6009SMax Reitz        self.assertCorrectBackingImage(node, back2_img)
185298c6009SMax Reitz        self.assertIntactSourceBackingChain()
186298c6009SMax Reitz
187298c6009SMax Reitz    def testNone(self):
188298c6009SMax Reitz        self.runMirror('none')
189298c6009SMax Reitz
190*1d701e0eSMax Reitz        node = self.findBlockNode('target', 'qdev0')
191298c6009SMax Reitz        self.assertCorrectBackingImage(node, source_img)
192298c6009SMax Reitz        self.assertIntactSourceBackingChain()
193298c6009SMax Reitz
194298c6009SMax Reitz
195298c6009SMax Reitzclass TestDriveMirrorAbsolutePaths(MirrorBaseClass):
196298c6009SMax Reitz    cmd = 'drive-mirror'
197298c6009SMax Reitz    existing = False
198298c6009SMax Reitz
199298c6009SMax Reitzclass TestDriveMirrorExistingNoBacking(MirrorBaseClass):
200298c6009SMax Reitz    cmd = 'drive-mirror'
201298c6009SMax Reitz    existing = True
202298c6009SMax Reitz    target_backing = None
203298c6009SMax Reitz
204298c6009SMax Reitzclass TestDriveMirrorExistingBacking(MirrorBaseClass):
205298c6009SMax Reitz    cmd = 'drive-mirror'
206298c6009SMax Reitz    existing = True
207298c6009SMax Reitz    target_backing = 'null-co://'
208298c6009SMax Reitz
209298c6009SMax Reitzclass TestBlockdevMirrorNoBacking(MirrorBaseClass):
210298c6009SMax Reitz    cmd = 'blockdev-mirror'
211298c6009SMax Reitz    existing = True
212298c6009SMax Reitz    target_backing = None
213298c6009SMax Reitz
214298c6009SMax Reitzclass TestBlockdevMirrorBacking(MirrorBaseClass):
215298c6009SMax Reitz    cmd = 'blockdev-mirror'
216298c6009SMax Reitz    existing = True
217298c6009SMax Reitz    target_backing = 'null-co://'
218298c6009SMax Reitz
219298c6009SMax Reitzclass TestBlockdevMirrorForcedBacking(MirrorBaseClass):
220298c6009SMax Reitz    cmd = 'blockdev-mirror'
221298c6009SMax Reitz    existing = True
222298c6009SMax Reitz    target_backing = None
223298c6009SMax Reitz    target_blockdev_backing = { 'driver': 'null-co' }
224298c6009SMax Reitz    target_real_backing = 'null-co://'
225298c6009SMax Reitz
226298c6009SMax Reitz
227298c6009SMax Reitzclass TestCommit(BaseClass):
228298c6009SMax Reitz    existing = False
229298c6009SMax Reitz
230298c6009SMax Reitz    def testCommit(self):
231*1d701e0eSMax Reitz        result = self.vm.qmp('block-commit', job_id='commit-job',
232*1d701e0eSMax Reitz                             device='source', base=back1_img)
233298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
234298c6009SMax Reitz
235298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_READY')
236298c6009SMax Reitz
237*1d701e0eSMax Reitz        result = self.vm.qmp('block-job-complete', device='commit-job')
238298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
239298c6009SMax Reitz
240298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_COMPLETED')
241298c6009SMax Reitz
242*1d701e0eSMax Reitz        node = self.findBlockNode(None, 'qdev0')
243298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
244298c6009SMax Reitz                        back1_img)
245298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
246298c6009SMax Reitz                        back0_img)
247298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
248298c6009SMax Reitz                               '/filename')
249298c6009SMax Reitz
250298c6009SMax Reitz        self.assertIntactSourceBackingChain()
251298c6009SMax Reitz
252298c6009SMax Reitz
253298c6009SMax ReitzBaseClass = None
254298c6009SMax ReitzMirrorBaseClass = None
255298c6009SMax Reitz
256298c6009SMax Reitzif __name__ == '__main__':
257298c6009SMax Reitz    iotests.main(supported_fmts=['qcow2'])
258