xref: /openbmc/qemu/tests/qemu-iotests/155 (revision 8a49b300)
1#!/usr/bin/env python3
2#
3# Test whether the backing BDSs are correct after completion of a
4# mirror block job; in "existing" modes (drive-mirror with
5# mode=existing and blockdev-mirror) the backing chain should not be
6# overridden.
7#
8# Copyright (C) 2016 Red Hat, Inc.
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22#
23
24import os
25import iotests
26from iotests import qemu_img
27
28back0_img = os.path.join(iotests.test_dir, 'back0.' + iotests.imgfmt)
29back1_img = os.path.join(iotests.test_dir, 'back1.' + iotests.imgfmt)
30back2_img = os.path.join(iotests.test_dir, 'back2.' + iotests.imgfmt)
31source_img = os.path.join(iotests.test_dir, 'source.' + iotests.imgfmt)
32target_img = os.path.join(iotests.test_dir, 'target.' + iotests.imgfmt)
33
34
35# Class variables for controlling its behavior:
36#
37# existing: If True, explicitly create the target image and blockdev-add it
38# target_backing: If existing is True: Use this filename as the backing file
39#                 of the target image
40#                 (None: no backing file)
41# target_blockdev_backing: If existing is True: Pass this dict as "backing"
42#                          for the blockdev-add command
43#                          (None: do not pass "backing")
44# target_real_backing: If existing is True: The real filename of the backing
45#                      image during runtime, only makes sense if
46#                      target_blockdev_backing is not None
47#                      (None: same as target_backing)
48# target_open_with_backing: If True, the target image is added with its backing
49#                           chain opened right away. If False, blockdev-add
50#                           opens it without a backing file and job completion
51#                           is supposed to open the backing chain.
52# use_iothread: If True, an iothread is configured for the virtio-blk device
53#               that uses the image being mirrored
54
55class BaseClass(iotests.QMPTestCase):
56    target_blockdev_backing = None
57    target_real_backing = None
58    target_open_with_backing = True
59    use_iothread = False
60
61    def setUp(self):
62        qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K')
63        qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img, back1_img)
64        qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img, back2_img)
65        qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img, source_img)
66
67        self.vm = iotests.VM()
68        # Add the BDS via blockdev-add so it stays around after the mirror block
69        # job has been completed
70        blockdev = {'node-name': 'source',
71                    'driver': iotests.imgfmt,
72                    'file': {'driver': 'file',
73                             'filename': source_img}}
74        self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
75
76        if self.use_iothread:
77            self.vm.add_object('iothread,id=iothread0')
78            iothread = ",iothread=iothread0"
79        else:
80            iothread = ""
81
82        self.vm.add_device('virtio-scsi%s' % iothread)
83        self.vm.add_device('scsi-hd,id=qdev0,drive=source')
84
85        self.vm.launch()
86
87        self.assertIntactSourceBackingChain()
88
89        if self.existing:
90            if self.target_backing:
91                qemu_img('create', '-f', iotests.imgfmt,
92                         '-b', self.target_backing, target_img, '1440K')
93            else:
94                qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
95
96            if self.cmd == 'blockdev-mirror':
97                options = { 'node-name': 'target',
98                            'driver': iotests.imgfmt,
99                            'file': { 'driver': 'file',
100                                      'node-name': 'target-file',
101                                      'filename': target_img } }
102
103                if not self.target_open_with_backing:
104                        options['backing'] = None
105                elif self.target_blockdev_backing:
106                        options['backing'] = self.target_blockdev_backing
107
108                result = self.vm.qmp('blockdev-add', **options)
109                self.assert_qmp(result, 'return', {})
110
111    def tearDown(self):
112        self.vm.shutdown()
113        os.remove(source_img)
114        os.remove(back2_img)
115        os.remove(back1_img)
116        os.remove(back0_img)
117        try:
118            os.remove(target_img)
119        except OSError:
120            pass
121
122    def findBlockNode(self, node_name, qdev=None):
123        if qdev:
124            result = self.vm.qmp('query-block')
125            for device in result['return']:
126                if device['qdev'] == qdev:
127                    if node_name:
128                        self.assert_qmp(device, 'inserted/node-name', node_name)
129                    return device['inserted']
130        else:
131            result = self.vm.qmp('query-named-block-nodes')
132            for node in result['return']:
133                if node['node-name'] == node_name:
134                    return node
135
136        self.fail('Cannot find node %s/%s' % (qdev, node_name))
137
138    def assertIntactSourceBackingChain(self):
139        node = self.findBlockNode('source')
140
141        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
142                        source_img)
143        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
144                        back2_img)
145        self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
146                        back1_img)
147        self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
148                        back0_img)
149        self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
150
151    def assertCorrectBackingImage(self, node, default_image):
152        if self.existing:
153            if self.target_real_backing:
154                image = self.target_real_backing
155            else:
156                image = self.target_backing
157        else:
158            image = default_image
159
160        if image:
161            self.assert_qmp(node, 'image/backing-image/filename', image)
162        else:
163            self.assert_qmp_absent(node, 'image/backing-image')
164
165
166# Class variables for controlling its behavior:
167#
168# cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
169
170class MirrorBaseClass(BaseClass):
171    def openBacking(self):
172        pass
173
174    def runMirror(self, sync):
175        if self.cmd == 'blockdev-mirror':
176            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
177                                 sync=sync, target='target',
178                                 auto_finalize=False)
179        else:
180            if self.existing:
181                mode = 'existing'
182            else:
183                mode = 'absolute-paths'
184            result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source',
185                                 sync=sync, target=target_img,
186                                 format=iotests.imgfmt, mode=mode,
187                                 node_name='target', auto_finalize=False)
188
189        self.assert_qmp(result, 'return', {})
190
191        self.vm.run_job('mirror-job', auto_finalize=False,
192                        pre_finalize=self.openBacking, auto_dismiss=True)
193
194    def testFull(self):
195        self.runMirror('full')
196
197        node = self.findBlockNode('target', 'qdev0')
198        self.assertCorrectBackingImage(node, None)
199        self.assertIntactSourceBackingChain()
200
201    def testTop(self):
202        self.runMirror('top')
203
204        node = self.findBlockNode('target', 'qdev0')
205        self.assertCorrectBackingImage(node, back2_img)
206        self.assertIntactSourceBackingChain()
207
208    def testNone(self):
209        self.runMirror('none')
210
211        node = self.findBlockNode('target', 'qdev0')
212        self.assertCorrectBackingImage(node, source_img)
213        self.assertIntactSourceBackingChain()
214
215
216class TestDriveMirrorAbsolutePaths(MirrorBaseClass):
217    cmd = 'drive-mirror'
218    existing = False
219
220class TestDriveMirrorExistingNoBacking(MirrorBaseClass):
221    cmd = 'drive-mirror'
222    existing = True
223    target_backing = None
224
225class TestDriveMirrorExistingBacking(MirrorBaseClass):
226    cmd = 'drive-mirror'
227    existing = True
228    target_backing = 'null-co://'
229
230class TestBlockdevMirrorNoBacking(MirrorBaseClass):
231    cmd = 'blockdev-mirror'
232    existing = True
233    target_backing = None
234
235class TestBlockdevMirrorBacking(MirrorBaseClass):
236    cmd = 'blockdev-mirror'
237    existing = True
238    target_backing = 'null-co://'
239
240class TestBlockdevMirrorForcedBacking(MirrorBaseClass):
241    cmd = 'blockdev-mirror'
242    existing = True
243    target_backing = None
244    target_blockdev_backing = { 'driver': 'null-co' }
245    target_real_backing = 'null-co://'
246
247# Attach the backing chain only during completion, with blockdev-reopen
248class TestBlockdevMirrorReopen(MirrorBaseClass):
249    cmd = 'blockdev-mirror'
250    existing = True
251    target_backing = 'null-co://'
252    target_open_with_backing = False
253
254    def openBacking(self):
255        if not self.target_open_with_backing:
256            result = self.vm.qmp('blockdev-add', node_name="backing",
257                                 driver="null-co")
258            self.assert_qmp(result, 'return', {})
259            result = self.vm.qmp('x-blockdev-reopen', node_name="target",
260                                 driver=iotests.imgfmt, file="target-file",
261                                 backing="backing")
262            self.assert_qmp(result, 'return', {})
263
264class TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen):
265    use_iothread = True
266
267# Attach the backing chain only during completion, with blockdev-snapshot
268class TestBlockdevMirrorSnapshot(MirrorBaseClass):
269    cmd = 'blockdev-mirror'
270    existing = True
271    target_backing = 'null-co://'
272    target_open_with_backing = False
273
274    def openBacking(self):
275        if not self.target_open_with_backing:
276            result = self.vm.qmp('blockdev-add', node_name="backing",
277                                 driver="null-co")
278            self.assert_qmp(result, 'return', {})
279            result = self.vm.qmp('blockdev-snapshot', node="backing",
280                                 overlay="target")
281            self.assert_qmp(result, 'return', {})
282
283class TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot):
284    use_iothread = True
285
286class TestCommit(BaseClass):
287    existing = False
288
289    def testCommit(self):
290        result = self.vm.qmp('block-commit', job_id='commit-job',
291                             device='source', base=back1_img)
292        self.assert_qmp(result, 'return', {})
293
294        self.vm.event_wait('BLOCK_JOB_READY')
295
296        result = self.vm.qmp('block-job-complete', device='commit-job')
297        self.assert_qmp(result, 'return', {})
298
299        self.vm.event_wait('BLOCK_JOB_COMPLETED')
300
301        node = self.findBlockNode(None, 'qdev0')
302        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
303                        back1_img)
304        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
305                        back0_img)
306        self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
307                               '/filename')
308
309        self.assertIntactSourceBackingChain()
310
311
312BaseClass = None
313MirrorBaseClass = None
314
315if __name__ == '__main__':
316    iotests.main(supported_fmts=['qcow2'],
317                 supported_protocols=['file'])
318