xref: /openbmc/qemu/tests/qemu-iotests/155 (revision 0153d2f50bc2ad3f41810d838fcf66acbf10f07a)
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):
54298c6009SMax Reitz        qemu_img('create', '-f', iotests.imgfmt, back0_img, '1M')
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        self.vm.add_drive(None, '', 'none')
61298c6009SMax Reitz        self.vm.launch()
62298c6009SMax Reitz
63298c6009SMax Reitz        # Add the BDS via blockdev-add so it stays around after the mirror block
64298c6009SMax Reitz        # job has been completed
65298c6009SMax Reitz        result = self.vm.qmp('blockdev-add',
66*0153d2f5SKevin Wolf                             node_name='source',
67*0153d2f5SKevin Wolf                             driver=iotests.imgfmt,
68*0153d2f5SKevin Wolf                             file={'driver': 'file',
69*0153d2f5SKevin Wolf                                   'filename': source_img})
70298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
71298c6009SMax Reitz
72298c6009SMax Reitz        result = self.vm.qmp('x-blockdev-insert-medium',
73298c6009SMax Reitz                             device='drive0', node_name='source')
74298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
75298c6009SMax Reitz
76298c6009SMax Reitz        self.assertIntactSourceBackingChain()
77298c6009SMax Reitz
78298c6009SMax Reitz        if self.existing:
79298c6009SMax Reitz            if self.target_backing:
80298c6009SMax Reitz                qemu_img('create', '-f', iotests.imgfmt,
81298c6009SMax Reitz                         '-b', self.target_backing, target_img, '1M')
82298c6009SMax Reitz            else:
83298c6009SMax Reitz                qemu_img('create', '-f', iotests.imgfmt, target_img, '1M')
84298c6009SMax Reitz
85298c6009SMax Reitz            if self.cmd == 'blockdev-mirror':
86298c6009SMax Reitz                options = { 'node-name': 'target',
87298c6009SMax Reitz                            'driver': iotests.imgfmt,
88298c6009SMax Reitz                            'file': { 'driver': 'file',
89298c6009SMax Reitz                                      'filename': target_img } }
90298c6009SMax Reitz                if self.target_blockdev_backing:
91298c6009SMax Reitz                    options['backing'] = self.target_blockdev_backing
92298c6009SMax Reitz
93*0153d2f5SKevin Wolf                result = self.vm.qmp('blockdev-add', **options)
94298c6009SMax Reitz                self.assert_qmp(result, 'return', {})
95298c6009SMax Reitz
96298c6009SMax Reitz    def tearDown(self):
97298c6009SMax Reitz        self.vm.shutdown()
98298c6009SMax Reitz        os.remove(source_img)
99298c6009SMax Reitz        os.remove(back2_img)
100298c6009SMax Reitz        os.remove(back1_img)
101298c6009SMax Reitz        os.remove(back0_img)
102298c6009SMax Reitz        try:
103298c6009SMax Reitz            os.remove(target_img)
104298c6009SMax Reitz        except OSError:
105298c6009SMax Reitz            pass
106298c6009SMax Reitz
107298c6009SMax Reitz    def findBlockNode(self, node_name, id=None):
108298c6009SMax Reitz        if id:
109298c6009SMax Reitz            result = self.vm.qmp('query-block')
110298c6009SMax Reitz            for device in result['return']:
111298c6009SMax Reitz                if device['device'] == id:
112298c6009SMax Reitz                    if node_name:
113298c6009SMax Reitz                        self.assert_qmp(device, 'inserted/node-name', node_name)
114298c6009SMax Reitz                    return device['inserted']
115298c6009SMax Reitz        else:
116298c6009SMax Reitz            result = self.vm.qmp('query-named-block-nodes')
117298c6009SMax Reitz            for node in result['return']:
118298c6009SMax Reitz                if node['node-name'] == node_name:
119298c6009SMax Reitz                    return node
120298c6009SMax Reitz
121298c6009SMax Reitz        self.fail('Cannot find node %s/%s' % (id, node_name))
122298c6009SMax Reitz
123298c6009SMax Reitz    def assertIntactSourceBackingChain(self):
124298c6009SMax Reitz        node = self.findBlockNode('source')
125298c6009SMax Reitz
126298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
127298c6009SMax Reitz                        source_img)
128298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
129298c6009SMax Reitz                        back2_img)
130298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 2 + '/filename',
131298c6009SMax Reitz                        back1_img)
132298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 3 + '/filename',
133298c6009SMax Reitz                        back0_img)
134298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 4)
135298c6009SMax Reitz
136298c6009SMax Reitz    def assertCorrectBackingImage(self, node, default_image):
137298c6009SMax Reitz        if self.existing:
138298c6009SMax Reitz            if self.target_real_backing:
139298c6009SMax Reitz                image = self.target_real_backing
140298c6009SMax Reitz            else:
141298c6009SMax Reitz                image = self.target_backing
142298c6009SMax Reitz        else:
143298c6009SMax Reitz            image = default_image
144298c6009SMax Reitz
145298c6009SMax Reitz        if image:
146298c6009SMax Reitz            self.assert_qmp(node, 'image/backing-image/filename', image)
147298c6009SMax Reitz        else:
148298c6009SMax Reitz            self.assert_qmp_absent(node, 'image/backing-image')
149298c6009SMax Reitz
150298c6009SMax Reitz
151298c6009SMax Reitz# Class variables for controlling its behavior:
152298c6009SMax Reitz#
153298c6009SMax Reitz# cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
154298c6009SMax Reitz
155298c6009SMax Reitzclass MirrorBaseClass(BaseClass):
156298c6009SMax Reitz    def runMirror(self, sync):
157298c6009SMax Reitz        if self.cmd == 'blockdev-mirror':
158298c6009SMax Reitz            result = self.vm.qmp(self.cmd, device='drive0', sync=sync,
159298c6009SMax Reitz                                 target='target')
160298c6009SMax Reitz        else:
161298c6009SMax Reitz            if self.existing:
162298c6009SMax Reitz                mode = 'existing'
163298c6009SMax Reitz            else:
164298c6009SMax Reitz                mode = 'absolute-paths'
165298c6009SMax Reitz            result = self.vm.qmp(self.cmd, device='drive0', sync=sync,
166298c6009SMax Reitz                                 target=target_img, format=iotests.imgfmt,
167298c6009SMax Reitz                                 mode=mode, node_name='target')
168298c6009SMax Reitz
169298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
170298c6009SMax Reitz
171298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_READY')
172298c6009SMax Reitz
173298c6009SMax Reitz        result = self.vm.qmp('block-job-complete', device='drive0')
174298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
175298c6009SMax Reitz
176298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_COMPLETED')
177298c6009SMax Reitz
178298c6009SMax Reitz    def testFull(self):
179298c6009SMax Reitz        self.runMirror('full')
180298c6009SMax Reitz
181298c6009SMax Reitz        node = self.findBlockNode('target', 'drive0')
182298c6009SMax Reitz        self.assertCorrectBackingImage(node, None)
183298c6009SMax Reitz        self.assertIntactSourceBackingChain()
184298c6009SMax Reitz
185298c6009SMax Reitz    def testTop(self):
186298c6009SMax Reitz        self.runMirror('top')
187298c6009SMax Reitz
188298c6009SMax Reitz        node = self.findBlockNode('target', 'drive0')
189298c6009SMax Reitz        self.assertCorrectBackingImage(node, back2_img)
190298c6009SMax Reitz        self.assertIntactSourceBackingChain()
191298c6009SMax Reitz
192298c6009SMax Reitz    def testNone(self):
193298c6009SMax Reitz        self.runMirror('none')
194298c6009SMax Reitz
195298c6009SMax Reitz        node = self.findBlockNode('target', 'drive0')
196298c6009SMax Reitz        self.assertCorrectBackingImage(node, source_img)
197298c6009SMax Reitz        self.assertIntactSourceBackingChain()
198298c6009SMax Reitz
199298c6009SMax Reitz
200298c6009SMax Reitzclass TestDriveMirrorAbsolutePaths(MirrorBaseClass):
201298c6009SMax Reitz    cmd = 'drive-mirror'
202298c6009SMax Reitz    existing = False
203298c6009SMax Reitz
204298c6009SMax Reitzclass TestDriveMirrorExistingNoBacking(MirrorBaseClass):
205298c6009SMax Reitz    cmd = 'drive-mirror'
206298c6009SMax Reitz    existing = True
207298c6009SMax Reitz    target_backing = None
208298c6009SMax Reitz
209298c6009SMax Reitzclass TestDriveMirrorExistingBacking(MirrorBaseClass):
210298c6009SMax Reitz    cmd = 'drive-mirror'
211298c6009SMax Reitz    existing = True
212298c6009SMax Reitz    target_backing = 'null-co://'
213298c6009SMax Reitz
214298c6009SMax Reitzclass TestBlockdevMirrorNoBacking(MirrorBaseClass):
215298c6009SMax Reitz    cmd = 'blockdev-mirror'
216298c6009SMax Reitz    existing = True
217298c6009SMax Reitz    target_backing = None
218298c6009SMax Reitz
219298c6009SMax Reitzclass TestBlockdevMirrorBacking(MirrorBaseClass):
220298c6009SMax Reitz    cmd = 'blockdev-mirror'
221298c6009SMax Reitz    existing = True
222298c6009SMax Reitz    target_backing = 'null-co://'
223298c6009SMax Reitz
224298c6009SMax Reitzclass TestBlockdevMirrorForcedBacking(MirrorBaseClass):
225298c6009SMax Reitz    cmd = 'blockdev-mirror'
226298c6009SMax Reitz    existing = True
227298c6009SMax Reitz    target_backing = None
228298c6009SMax Reitz    target_blockdev_backing = { 'driver': 'null-co' }
229298c6009SMax Reitz    target_real_backing = 'null-co://'
230298c6009SMax Reitz
231298c6009SMax Reitz
232298c6009SMax Reitzclass TestCommit(BaseClass):
233298c6009SMax Reitz    existing = False
234298c6009SMax Reitz
235298c6009SMax Reitz    def testCommit(self):
236298c6009SMax Reitz        result = self.vm.qmp('block-commit', device='drive0', base=back1_img)
237298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
238298c6009SMax Reitz
239298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_READY')
240298c6009SMax Reitz
241298c6009SMax Reitz        result = self.vm.qmp('block-job-complete', device='drive0')
242298c6009SMax Reitz        self.assert_qmp(result, 'return', {})
243298c6009SMax Reitz
244298c6009SMax Reitz        self.vm.event_wait('BLOCK_JOB_COMPLETED')
245298c6009SMax Reitz
246298c6009SMax Reitz        node = self.findBlockNode(None, 'drive0')
247298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename',
248298c6009SMax Reitz                        back1_img)
249298c6009SMax Reitz        self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename',
250298c6009SMax Reitz                        back0_img)
251298c6009SMax Reitz        self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 +
252298c6009SMax Reitz                               '/filename')
253298c6009SMax Reitz
254298c6009SMax Reitz        self.assertIntactSourceBackingChain()
255298c6009SMax Reitz
256298c6009SMax Reitz
257298c6009SMax ReitzBaseClass = None
258298c6009SMax ReitzMirrorBaseClass = None
259298c6009SMax Reitz
260298c6009SMax Reitzif __name__ == '__main__':
261298c6009SMax Reitz    iotests.main(supported_fmts=['qcow2'])
262