qom: Use return values to check for error where that's simpler
[qemu/ar7.git] / tests / qemu-iotests / 228
blobda0900fb82aa7626068ee6952fc097f5fa023c60
1 #!/usr/bin/env python3
3 # Test for when a backing file is considered overridden (thus, a
4 # json:{} filename is generated for the overlay) and when it is not
6 # Copyright (C) 2018 Red Hat, Inc.
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program.  If not, see <http://www.gnu.org/licenses/>.
21 # Creator/Owner: Max Reitz <mreitz@redhat.com>
23 import iotests
24 from iotests import log, qemu_img, filter_testfiles, filter_imgfmt, \
25         filter_qmp_testfiles, filter_qmp_imgfmt
27 # Need backing file and change-backing-file support
28 iotests.script_initialize(
29     supported_fmts=['qcow2', 'qed'],
30     supported_platforms=['linux'],
34 def log_node_info(node):
35     log('')
37     log('bs->filename: ' + node['image']['filename'],
38         filters=[filter_testfiles, filter_imgfmt])
39     log('bs->backing_file: ' + node['backing_file'],
40         filters=[filter_testfiles, filter_imgfmt])
42     if 'backing-image' in node['image']:
43         log('bs->backing->bs->filename: ' +
44             node['image']['backing-image']['filename'],
45             filters=[filter_testfiles, filter_imgfmt])
46     else:
47         log('bs->backing: (none)')
49     log('')
52 with iotests.FilePath('base.img') as base_img_path, \
53      iotests.FilePath('top.img') as top_img_path, \
54      iotests.VM() as vm:
56     assert qemu_img('create', '-f', iotests.imgfmt, base_img_path, '64M') == 0
57     # Choose a funny way to describe the backing filename
58     assert qemu_img('create', '-f', iotests.imgfmt, '-b',
59                     'file:' + base_img_path, top_img_path) == 0
61     vm.launch()
63     log('--- Implicit backing file ---')
64     log('')
66     vm.qmp_log('blockdev-add',
67                 node_name='node0',
68                 driver=iotests.imgfmt,
69                 file={
70                     'driver': 'file',
71                     'filename': top_img_path
72                 },
73                 filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
75     # Filename should be plain, and the backing filename should not
76     # contain the "file:" prefix
77     log_node_info(vm.node_info('node0'))
79     vm.qmp_log('blockdev-del', node_name='node0')
81     log('')
82     log('--- change-backing-file ---')
83     log('')
85     vm.qmp_log('blockdev-add',
86                node_name='node0',
87                driver=iotests.imgfmt,
88                file={
89                    'driver': 'file',
90                    'filename': top_img_path
91                },
92                filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
94     # Changing the backing file to a qemu-reported filename should
95     # result in qemu accepting the corresponding BDS as the implicit
96     # backing BDS (and thus not generate a json:{} filename).
97     # So, first, query the backing filename.
99     backing_filename = \
100         vm.node_info('node0')['image']['backing-image']['filename']
102     # Next, change the backing file to something different
104     vm.qmp_log('change-backing-file',
105                image_node_name='node0',
106                device='node0',
107                backing_file='null-co://',
108                filters=[filter_qmp_testfiles])
110     # Now, verify that we get a json:{} filename
111     # (Image header says "null-co://", actual backing file still is
112     # base_img_path)
114     log_node_info(vm.node_info('node0'))
116     # Change it back
117     # (To get header and backing file in sync)
119     vm.qmp_log('change-backing-file',
120                image_node_name='node0',
121                device='node0',
122                backing_file=backing_filename,
123                filters=[filter_qmp_testfiles])
125     # And verify that we get our original results
127     log_node_info(vm.node_info('node0'))
129     # Finally, try a "file:" prefix.  While this is actually what we
130     # originally had in the image header, qemu will not reopen the
131     # backing file here, so it cannot verify that this filename
132     # "resolves" to the actual backing BDS's filename and will thus
133     # consider both to be different.
134     # (This may be fixed in the future.)
136     vm.qmp_log('change-backing-file',
137                image_node_name='node0',
138                device='node0',
139                backing_file=('file:' + backing_filename),
140                filters=[filter_qmp_testfiles])
142     # So now we should get a json:{} filename
144     log_node_info(vm.node_info('node0'))
146     # Remove and re-attach so we can see that (as in our first try),
147     # opening the image anew helps qemu resolve the header backing
148     # filename.
150     vm.qmp_log('blockdev-del', node_name='node0')
152     vm.qmp_log('blockdev-add',
153                node_name='node0',
154                driver=iotests.imgfmt,
155                file={
156                    'driver': 'file',
157                    'filename': top_img_path
158                },
159                filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
161     log_node_info(vm.node_info('node0'))
163     vm.qmp_log('blockdev-del', node_name='node0')
165     log('')
166     log('--- Override backing file ---')
167     log('')
169     # For this test, we need the plain filename in the image header
170     # (because qemu cannot "canonicalize"/"resolve" the backing
171     # filename unless the backing file is opened implicitly with the
172     # overlay)
173     assert qemu_img('create', '-f', iotests.imgfmt, '-b', base_img_path,
174                     top_img_path) == 0
176     # You can only reliably override backing options by using a node
177     # reference (or by specifying file.filename, but, well...)
178     vm.qmp_log('blockdev-add', node_name='null', driver='null-co')
180     vm.qmp_log('blockdev-add',
181                node_name='node0',
182                driver=iotests.imgfmt,
183                file={
184                    'driver': 'file',
185                    'filename': top_img_path
186                },
187                backing='null',
188                filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
190     # Should get a json:{} filename (and bs->backing_file is
191     # null-co://, because that field actually has not much to do
192     # with the header backing filename (except that it is changed by
193     # change-backing-file))
195     log_node_info(vm.node_info('node0'))
197     # Detach the backing file by reopening the whole thing
199     vm.qmp_log('blockdev-del', node_name='node0')
200     vm.qmp_log('blockdev-del', node_name='null')
202     vm.qmp_log('blockdev-add',
203                node_name='node0',
204                driver=iotests.imgfmt,
205                file={
206                    'driver': 'file',
207                    'filename': top_img_path
208                },
209                backing=None,
210                filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
212     # Should get a json:{} filename (because we overrode the backing
213     # file to not be there)
215     log_node_info(vm.node_info('node0'))
217     # Open the original backing file
219     vm.qmp_log('blockdev-add',
220                node_name='original-backing',
221                driver=iotests.imgfmt,
222                file={
223                    'driver': 'file',
224                    'filename': base_img_path
225                },
226                filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
228     # Attach the original backing file to its overlay
230     vm.qmp_log('blockdev-snapshot',
231                node='original-backing',
232                overlay='node0')
234     # This should give us the original plain result
236     log_node_info(vm.node_info('node0'))
238     vm.qmp_log('blockdev-del', node_name='node0')
239     vm.qmp_log('blockdev-del', node_name='original-backing')
241     vm.shutdown()