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