acceptance: use stable URLs for the Debian and Ubuntu installer
[qemu/ar7.git] / tests / qemu-iotests / 155
blob988f9861447c2ff8feb9d92f5b25044b466bb9f3
1 #!/usr/bin/env python3
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.
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/>.
24 import os
25 import iotests
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
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
55 class BaseClass(iotests.QMPTestCase):
56     target_blockdev_backing = None
57     target_real_backing = None
58     target_open_with_backing = True
59     use_iothread = False
61     def setUp(self):
62         qemu_img('create', '-f', iotests.imgfmt, back0_img, '1440K')
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)
70         self.vm = iotests.VM()
71         # Add the BDS via blockdev-add so it stays around after the mirror block
72         # job has been completed
73         blockdev = {'node-name': 'source',
74                     'driver': iotests.imgfmt,
75                     'file': {'driver': 'file',
76                              'filename': source_img}}
77         self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
79         if self.use_iothread:
80             self.vm.add_object('iothread,id=iothread0')
81             iothread = ",iothread=iothread0"
82         else:
83             iothread = ""
85         self.vm.add_device('virtio-scsi%s' % iothread)
86         self.vm.add_device('scsi-hd,id=qdev0,drive=source')
88         self.vm.launch()
90         self.assertIntactSourceBackingChain()
92         if self.existing:
93             if self.target_backing:
94                 qemu_img('create', '-f', iotests.imgfmt,
95                          '-b', self.target_backing, '-F', 'raw',
96                          target_img, '1440K')
97             else:
98                 qemu_img('create', '-f', iotests.imgfmt, target_img, '1440K')
100             if self.cmd == 'blockdev-mirror':
101                 options = { 'node-name': 'target',
102                             'driver': iotests.imgfmt,
103                             'file': { 'driver': 'file',
104                                       'node-name': 'target-file',
105                                       'filename': target_img } }
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
112                 result = self.vm.qmp('blockdev-add', **options)
113                 self.assert_qmp(result, 'return', {})
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
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
140         self.fail('Cannot find node %s/%s' % (qdev, node_name))
142     def assertIntactSourceBackingChain(self):
143         node = self.findBlockNode('source')
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)
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
164         if image:
165             self.assert_qmp(node, 'image/backing-image/filename', image)
166         else:
167             self.assert_qmp_absent(node, 'image/backing-image')
170 # Class variables for controlling its behavior:
172 # cmd: Mirroring command to execute, either drive-mirror or blockdev-mirror
174 class MirrorBaseClass(BaseClass):
175     def openBacking(self):
176         pass
178     def runMirror(self, sync):
179         if self.cmd == 'blockdev-mirror':
180             result = self.vm.qmp(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             result = self.vm.qmp(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)
193         self.assert_qmp(result, 'return', {})
195         self.vm.run_job('mirror-job', auto_finalize=False,
196                         pre_finalize=self.openBacking, auto_dismiss=True)
198     def testFull(self):
199         self.runMirror('full')
201         node = self.findBlockNode('target', 'qdev0')
202         self.assertCorrectBackingImage(node, None)
203         self.assertIntactSourceBackingChain()
205     def testTop(self):
206         self.runMirror('top')
208         node = self.findBlockNode('target', 'qdev0')
209         self.assertCorrectBackingImage(node, back2_img)
210         self.assertIntactSourceBackingChain()
212     def testNone(self):
213         self.runMirror('none')
215         node = self.findBlockNode('target', 'qdev0')
216         self.assertCorrectBackingImage(node, source_img)
217         self.assertIntactSourceBackingChain()
220 class TestDriveMirrorAbsolutePaths(MirrorBaseClass):
221     cmd = 'drive-mirror'
222     existing = False
224 class TestDriveMirrorExistingNoBacking(MirrorBaseClass):
225     cmd = 'drive-mirror'
226     existing = True
227     target_backing = None
229 class TestDriveMirrorExistingBacking(MirrorBaseClass):
230     cmd = 'drive-mirror'
231     existing = True
232     target_backing = 'null-co://'
234 class TestBlockdevMirrorNoBacking(MirrorBaseClass):
235     cmd = 'blockdev-mirror'
236     existing = True
237     target_backing = None
239 class TestBlockdevMirrorBacking(MirrorBaseClass):
240     cmd = 'blockdev-mirror'
241     existing = True
242     target_backing = 'null-co://'
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://'
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
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', {})
268 class TestBlockdevMirrorReopenIothread(TestBlockdevMirrorReopen):
269     use_iothread = True
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
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', {})
287 class TestBlockdevMirrorSnapshotIothread(TestBlockdevMirrorSnapshot):
288     use_iothread = True
290 class TestCommit(BaseClass):
291     existing = False
293     def testCommit(self):
294         result = self.vm.qmp('block-commit', job_id='commit-job',
295                              device='source', base=back1_img)
296         self.assert_qmp(result, 'return', {})
298         self.vm.event_wait('BLOCK_JOB_READY')
300         result = self.vm.qmp('block-job-complete', device='commit-job')
301         self.assert_qmp(result, 'return', {})
303         self.vm.event_wait('BLOCK_JOB_COMPLETED')
305         node = self.findBlockNode(None, 'qdev0')
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')
313         self.assertIntactSourceBackingChain()
316 BaseClass = None
317 MirrorBaseClass = None
319 if __name__ == '__main__':
320     iotests.main(supported_fmts=['qcow2'],
321                  supported_protocols=['file'])