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
8 # Copyright (C) 2016 Red Hat, Inc.
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.
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.
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/>.
26 from iotests
import qemu_img
28 back0_img
= os
.path
.join(iotests
.test_dir
, 'back0.' + iotests
.imgfmt
)
29 back1_img
= os
.path
.join(iotests
.test_dir
, 'back1.' + iotests
.imgfmt
)
30 back2_img
= os
.path
.join(iotests
.test_dir
, 'back2.' + iotests
.imgfmt
)
31 source_img
= os
.path
.join(iotests
.test_dir
, 'source.' + iotests
.imgfmt
)
32 target_img
= os
.path
.join(iotests
.test_dir
, 'target.' + iotests
.imgfmt
)
35 # Class variables for controlling its behavior:
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
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)
49 class BaseClass(iotests
.QMPTestCase
):
50 target_blockdev_backing
= None
51 target_real_backing
= None
54 qemu_img('create', '-f', iotests
.imgfmt
, back0_img
, '1M')
55 qemu_img('create', '-f', iotests
.imgfmt
, '-b', back0_img
, back1_img
)
56 qemu_img('create', '-f', iotests
.imgfmt
, '-b', back1_img
, back2_img
)
57 qemu_img('create', '-f', iotests
.imgfmt
, '-b', back2_img
, source_img
)
59 self
.vm
= iotests
.VM()
60 self
.vm
.add_drive(None, '', 'none')
63 # Add the BDS via blockdev-add so it stays around after the mirror block
64 # job has been completed
65 result
= self
.vm
.qmp('blockdev-add',
67 driver
=iotests
.imgfmt
,
68 file={'driver': 'file',
69 'filename': source_img
})
70 self
.assert_qmp(result
, 'return', {})
72 result
= self
.vm
.qmp('x-blockdev-insert-medium',
73 device
='drive0', node_name
='source')
74 self
.assert_qmp(result
, 'return', {})
76 self
.assertIntactSourceBackingChain()
79 if self
.target_backing
:
80 qemu_img('create', '-f', iotests
.imgfmt
,
81 '-b', self
.target_backing
, target_img
, '1M')
83 qemu_img('create', '-f', iotests
.imgfmt
, target_img
, '1M')
85 if self
.cmd
== 'blockdev-mirror':
86 options
= { 'node-name': 'target',
87 'driver': iotests
.imgfmt
,
88 'file': { 'driver': 'file',
89 'filename': target_img
} }
90 if self
.target_blockdev_backing
:
91 options
['backing'] = self
.target_blockdev_backing
93 result
= self
.vm
.qmp('blockdev-add', **options
)
94 self
.assert_qmp(result
, 'return', {})
103 os
.remove(target_img
)
107 def findBlockNode(self
, node_name
, id=None):
109 result
= self
.vm
.qmp('query-block')
110 for device
in result
['return']:
111 if device
['device'] == id:
113 self
.assert_qmp(device
, 'inserted/node-name', node_name
)
114 return device
['inserted']
116 result
= self
.vm
.qmp('query-named-block-nodes')
117 for node
in result
['return']:
118 if node
['node-name'] == node_name
:
121 self
.fail('Cannot find node %s/%s' % (id, node_name
))
123 def assertIntactSourceBackingChain(self
):
124 node
= self
.findBlockNode('source')
126 self
.assert_qmp(node
, 'image' + '/backing-image' * 0 + '/filename',
128 self
.assert_qmp(node
, 'image' + '/backing-image' * 1 + '/filename',
130 self
.assert_qmp(node
, 'image' + '/backing-image' * 2 + '/filename',
132 self
.assert_qmp(node
, 'image' + '/backing-image' * 3 + '/filename',
134 self
.assert_qmp_absent(node
, 'image' + '/backing-image' * 4)
136 def assertCorrectBackingImage(self
, node
, default_image
):
138 if self
.target_real_backing
:
139 image
= self
.target_real_backing
141 image
= self
.target_backing
143 image
= default_image
146 self
.assert_qmp(node
, 'image/backing-image/filename', image
)
148 self
.assert_qmp_absent(node
, 'image/backing-image')
151 # Class variables for controlling its behavior:
153 # cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
155 class MirrorBaseClass(BaseClass
):
156 def runMirror(self
, sync
):
157 if self
.cmd
== 'blockdev-mirror':
158 result
= self
.vm
.qmp(self
.cmd
, device
='drive0', sync
=sync
,
164 mode
= 'absolute-paths'
165 result
= self
.vm
.qmp(self
.cmd
, device
='drive0', sync
=sync
,
166 target
=target_img
, format
=iotests
.imgfmt
,
167 mode
=mode
, node_name
='target')
169 self
.assert_qmp(result
, 'return', {})
171 self
.vm
.event_wait('BLOCK_JOB_READY')
173 result
= self
.vm
.qmp('block-job-complete', device
='drive0')
174 self
.assert_qmp(result
, 'return', {})
176 self
.vm
.event_wait('BLOCK_JOB_COMPLETED')
179 self
.runMirror('full')
181 node
= self
.findBlockNode('target', 'drive0')
182 self
.assertCorrectBackingImage(node
, None)
183 self
.assertIntactSourceBackingChain()
186 self
.runMirror('top')
188 node
= self
.findBlockNode('target', 'drive0')
189 self
.assertCorrectBackingImage(node
, back2_img
)
190 self
.assertIntactSourceBackingChain()
193 self
.runMirror('none')
195 node
= self
.findBlockNode('target', 'drive0')
196 self
.assertCorrectBackingImage(node
, source_img
)
197 self
.assertIntactSourceBackingChain()
200 class TestDriveMirrorAbsolutePaths(MirrorBaseClass
):
204 class TestDriveMirrorExistingNoBacking(MirrorBaseClass
):
207 target_backing
= None
209 class TestDriveMirrorExistingBacking(MirrorBaseClass
):
212 target_backing
= 'null-co://'
214 class TestBlockdevMirrorNoBacking(MirrorBaseClass
):
215 cmd
= 'blockdev-mirror'
217 target_backing
= None
219 class TestBlockdevMirrorBacking(MirrorBaseClass
):
220 cmd
= 'blockdev-mirror'
222 target_backing
= 'null-co://'
224 class TestBlockdevMirrorForcedBacking(MirrorBaseClass
):
225 cmd
= 'blockdev-mirror'
227 target_backing
= None
228 target_blockdev_backing
= { 'driver': 'null-co' }
229 target_real_backing
= 'null-co://'
232 class TestCommit(BaseClass
):
235 def testCommit(self
):
236 result
= self
.vm
.qmp('block-commit', device
='drive0', base
=back1_img
)
237 self
.assert_qmp(result
, 'return', {})
239 self
.vm
.event_wait('BLOCK_JOB_READY')
241 result
= self
.vm
.qmp('block-job-complete', device
='drive0')
242 self
.assert_qmp(result
, 'return', {})
244 self
.vm
.event_wait('BLOCK_JOB_COMPLETED')
246 node
= self
.findBlockNode(None, 'drive0')
247 self
.assert_qmp(node
, 'image' + '/backing-image' * 0 + '/filename',
249 self
.assert_qmp(node
, 'image' + '/backing-image' * 1 + '/filename',
251 self
.assert_qmp_absent(node
, 'image' + '/backing-image' * 2 +
254 self
.assertIntactSourceBackingChain()
258 MirrorBaseClass
= None
260 if __name__
== '__main__':
261 iotests
.main(supported_fmts
=['qcow2'])