]>
Commit | Line | Data |
---|---|---|
903cb1bf | 1 | #!/usr/bin/env python3 |
298c6009 HR |
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 | ||
24 | import os | |
25 | import iotests | |
26 | from iotests import qemu_img | |
27 | ||
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) | |
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) | |
8bdee9f1 KW |
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. | |
6a5f6403 KW |
52 | # use_iothread: If True, an iothread is configured for the virtio-blk device |
53 | # that uses the image being mirrored | |
298c6009 HR |
54 | |
55 | class BaseClass(iotests.QMPTestCase): | |
56 | target_blockdev_backing = None | |
57 | target_real_backing = None | |
8bdee9f1 | 58 | target_open_with_backing = True |
6a5f6403 | 59 | use_iothread = False |
298c6009 HR |
60 | |
61 | def setUp(self): | |
1d701e0e | 62 | qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K') |
b66ff2c2 EB |
63 | qemu_img('create', '-f', iotests.imgfmt, '-b', back0_img, |
64 | '-F', iotests.imgfmt, back1_img) | |
65 | qemu_img('create', '-f', iotests.imgfmt, '-b', back1_img, | |
66 | '-F', iotests.imgfmt, back2_img) | |
67 | qemu_img('create', '-f', iotests.imgfmt, '-b', back2_img, | |
68 | '-F', iotests.imgfmt, source_img) | |
298c6009 HR |
69 | |
70 | self.vm = iotests.VM() | |
298c6009 HR |
71 | # Add the BDS via blockdev-add so it stays around after the mirror block |
72 | # job has been completed | |
1d701e0e HR |
73 | blockdev = {'node-name': 'source', |
74 | 'driver': iotests.imgfmt, | |
75 | 'file': {'driver': 'file', | |
76 | 'filename': source_img}} | |
62a94288 | 77 | self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev)) |
6a5f6403 KW |
78 | |
79 | if self.use_iothread: | |
80 | self.vm.add_object('iothread,id=iothread0') | |
81 | iothread = ",iothread=iothread0" | |
82 | else: | |
83 | iothread = "" | |
84 | ||
85 | self.vm.add_device('virtio-scsi%s' % iothread) | |
86 | self.vm.add_device('scsi-hd,id=qdev0,drive=source') | |
87 | ||
1d701e0e | 88 | self.vm.launch() |
298c6009 HR |
89 | |
90 | self.assertIntactSourceBackingChain() | |
91 | ||
92 | if self.existing: | |
93 | if self.target_backing: | |
94 | qemu_img('create', '-f', iotests.imgfmt, | |
b66ff2c2 EB |
95 | '-b', self.target_backing, '-F', 'raw', |
96 | target_img, '1440K') | |
298c6009 | 97 | else: |
1d701e0e | 98 | qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K') |
298c6009 HR |
99 | |
100 | if self.cmd == 'blockdev-mirror': | |
101 | options = { 'node-name': 'target', | |
102 | 'driver': iotests.imgfmt, | |
103 | 'file': { 'driver': 'file', | |
8bdee9f1 | 104 | 'node-name': 'target-file', |
298c6009 | 105 | 'filename': target_img } } |
8bdee9f1 KW |
106 | |
107 | if not self.target_open_with_backing: | |
108 | options['backing'] = None | |
109 | elif self.target_blockdev_backing: | |
110 | options['backing'] = self.target_blockdev_backing | |
298c6009 | 111 | |
0153d2f5 | 112 | result = self.vm.qmp('blockdev-add', **options) |
298c6009 HR |
113 | self.assert_qmp(result, 'return', {}) |
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 | ||
1d701e0e HR |
126 | def findBlockNode(self, node_name, qdev=None): |
127 | if qdev: | |
298c6009 HR |
128 | result = self.vm.qmp('query-block') |
129 | for device in result['return']: | |
1d701e0e | 130 | if device['qdev'] == qdev: |
298c6009 HR |
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 | ||
1d701e0e | 140 | self.fail('Cannot find node %s/%s' % (qdev, node_name)) |
298c6009 HR |
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 | ||
174 | class MirrorBaseClass(BaseClass): | |
8bdee9f1 KW |
175 | def openBacking(self): |
176 | pass | |
177 | ||
298c6009 HR |
178 | def runMirror(self, sync): |
179 | if self.cmd == 'blockdev-mirror': | |
1d701e0e | 180 | result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source', |
8bdee9f1 KW |
181 | sync=sync, target='target', |
182 | auto_finalize=False) | |
298c6009 HR |
183 | else: |
184 | if self.existing: | |
185 | mode = 'existing' | |
186 | else: | |
187 | mode = 'absolute-paths' | |
1d701e0e HR |
188 | result = self.vm.qmp(self.cmd, job_id='mirror-job', device='source', |
189 | sync=sync, target=target_img, | |
190 | format=iotests.imgfmt, mode=mode, | |
8bdee9f1 | 191 | node_name='target', auto_finalize=False) |
298c6009 HR |
192 | |
193 | self.assert_qmp(result, 'return', {}) | |
194 | ||
52ea799e | 195 | self.vm.run_job('mirror-job', auto_finalize=False, |
8bdee9f1 | 196 | pre_finalize=self.openBacking, auto_dismiss=True) |
298c6009 HR |
197 | |
198 | def testFull(self): | |
199 | self.runMirror('full') | |
200 | ||
6a5f6403 | 201 | node = self.findBlockNode('target', 'qdev0') |
298c6009 HR |
202 | self.assertCorrectBackingImage(node, None) |
203 | self.assertIntactSourceBackingChain() | |
204 | ||
205 | def testTop(self): | |
206 | self.runMirror('top') | |
207 | ||
6a5f6403 | 208 | node = self.findBlockNode('target', 'qdev0') |
298c6009 HR |
209 | self.assertCorrectBackingImage(node, back2_img) |
210 | self.assertIntactSourceBackingChain() | |
211 | ||
212 | def testNone(self): | |
213 | self.runMirror('none') | |
214 | ||
6a5f6403 | 215 | node = self.findBlockNode('target', 'qdev0') |
298c6009 HR |
216 | self.assertCorrectBackingImage(node, source_img) |
217 | self.assertIntactSourceBackingChain() | |
218 | ||
219 | ||
220 | class TestDriveMirrorAbsolutePaths(MirrorBaseClass): | |
221 | cmd = 'drive-mirror' | |
222 | existing = False | |
223 | ||
224 | class TestDriveMirrorExistingNoBacking(MirrorBaseClass): | |
225 | cmd = 'drive-mirror' | |
226 | existing = True | |
227 | target_backing = None | |
228 | ||
229 | class TestDriveMirrorExistingBacking(MirrorBaseClass): | |
230 | cmd = 'drive-mirror' | |
231 | existing = True | |
232 | target_backing = 'null-co://' | |
233 | ||
234 | class TestBlockdevMirrorNoBacking(MirrorBaseClass): | |
235 | cmd = 'blockdev-mirror' | |
236 | existing = True | |
237 | target_backing = None | |
238 | ||
239 | class TestBlockdevMirrorBacking(MirrorBaseClass): | |
240 | cmd = 'blockdev-mirror' | |
241 | existing = True | |
242 | target_backing = 'null-co://' | |
243 | ||
244 | class TestBlockdevMirrorForcedBacking(MirrorBaseClass): | |
245 | cmd = 'blockdev-mirror' | |
246 | existing = True | |
247 | target_backing = None | |
248 | target_blockdev_backing = { 'driver': 'null-co' } | |
249 | target_real_backing = 'null-co://' | |
250 | ||
8bdee9f1 KW |
251 | # Attach the backing chain only during completion, with blockdev-reopen |
252 | class TestBlockdevMirrorReopen(MirrorBaseClass): | |
253 | cmd = 'blockdev-mirror' | |
254 | existing = True | |
255 | target_backing = 'null-co://' | |
256 | target_open_with_backing = False | |
257 | ||
258 | def openBacking(self): | |
259 | if not self.target_open_with_backing: | |
260 | result = self.vm.qmp('blockdev-add', node_name="backing", | |
261 | driver="null-co") | |
262 | self.assert_qmp(result, 'return', {}) | |
263 | result = self.vm.qmp('x-blockdev-reopen', node_name="target", | |
264 | driver=iotests.imgfmt, file="target-file", | |
265 | backing="backing") | |
266 | self.assert_qmp(result, 'return', {}) | |
267 | ||
6a5f6403 KW |
268 | class TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen): |
269 | use_iothread = True | |
270 | ||
8bdee9f1 KW |
271 | # Attach the backing chain only during completion, with blockdev-snapshot |
272 | class TestBlockdevMirrorSnapshot(MirrorBaseClass): | |
273 | cmd = 'blockdev-mirror' | |
274 | existing = True | |
275 | target_backing = 'null-co://' | |
276 | target_open_with_backing = False | |
277 | ||
278 | def openBacking(self): | |
279 | if not self.target_open_with_backing: | |
280 | result = self.vm.qmp('blockdev-add', node_name="backing", | |
281 | driver="null-co") | |
282 | self.assert_qmp(result, 'return', {}) | |
283 | result = self.vm.qmp('blockdev-snapshot', node="backing", | |
284 | overlay="target") | |
285 | self.assert_qmp(result, 'return', {}) | |
298c6009 | 286 | |
6a5f6403 KW |
287 | class TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot): |
288 | use_iothread = True | |
289 | ||
298c6009 HR |
290 | class TestCommit(BaseClass): |
291 | existing = False | |
292 | ||
293 | def testCommit(self): | |
1d701e0e HR |
294 | result = self.vm.qmp('block-commit', job_id='commit-job', |
295 | device='source', base=back1_img) | |
298c6009 HR |
296 | self.assert_qmp(result, 'return', {}) |
297 | ||
298 | self.vm.event_wait('BLOCK_JOB_READY') | |
299 | ||
1d701e0e | 300 | result = self.vm.qmp('block-job-complete', device='commit-job') |
298c6009 HR |
301 | self.assert_qmp(result, 'return', {}) |
302 | ||
303 | self.vm.event_wait('BLOCK_JOB_COMPLETED') | |
304 | ||
6a5f6403 | 305 | node = self.findBlockNode(None, 'qdev0') |
298c6009 HR |
306 | self.assert_qmp(node, 'image' + '/backing-image' * 0 + '/filename', |
307 | back1_img) | |
308 | self.assert_qmp(node, 'image' + '/backing-image' * 1 + '/filename', | |
309 | back0_img) | |
310 | self.assert_qmp_absent(node, 'image' + '/backing-image' * 2 + | |
311 | '/filename') | |
312 | ||
313 | self.assertIntactSourceBackingChain() | |
314 | ||
315 | ||
316 | BaseClass = None | |
317 | MirrorBaseClass = None | |
318 | ||
319 | if __name__ == '__main__': | |
103cbc77 HR |
320 | iotests.main(supported_fmts=['qcow2'], |
321 | supported_protocols=['file']) |