xref: /openbmc/qemu/tests/qemu-iotests/224 (revision 72cfb937)
17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw quick
37b14f231SMax Reitz#
47b14f231SMax Reitz# Test json:{} filenames with qemu-internal BDSs
57b14f231SMax Reitz# (the one of commit, to be precise)
67b14f231SMax Reitz#
77b14f231SMax Reitz# Copyright (C) 2018 Red Hat, Inc.
87b14f231SMax Reitz#
97b14f231SMax Reitz# This program is free software; you can redistribute it and/or modify
107b14f231SMax Reitz# it under the terms of the GNU General Public License as published by
117b14f231SMax Reitz# the Free Software Foundation; either version 2 of the License, or
127b14f231SMax Reitz# (at your option) any later version.
137b14f231SMax Reitz#
147b14f231SMax Reitz# This program is distributed in the hope that it will be useful,
157b14f231SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
167b14f231SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
177b14f231SMax Reitz# GNU General Public License for more details.
187b14f231SMax Reitz#
197b14f231SMax Reitz# You should have received a copy of the GNU General Public License
207b14f231SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
217b14f231SMax Reitz#
2242a5009dSJohn Snow# Creator/Owner: Hanna Reitz <hreitz@redhat.com>
237b14f231SMax Reitz
247b14f231SMax Reitzimport iotests
25*72cfb937SJohn Snowfrom iotests import log, qemu_img, qemu_io, filter_qmp_testfiles, \
267b14f231SMax Reitz                    filter_qmp_imgfmt
277b14f231SMax Reitzimport json
287b14f231SMax Reitz
297b14f231SMax Reitz# Need backing file support (for arbitrary backing formats)
307d814059SJohn Snowiotests.script_initialize(supported_fmts=['qcow2', 'qcow', 'qed'],
317d814059SJohn Snow                          supported_platforms=['linux'])
327b14f231SMax Reitz
337b14f231SMax Reitz
347b14f231SMax Reitz# There are two variations of this test:
357b14f231SMax Reitz# (1) We do not set filter_node_name.  In that case, the commit_top
367b14f231SMax Reitz#     driver should not appear anywhere.
377b14f231SMax Reitz# (2) We do set filter_node_name.  In that case, it should appear.
387b14f231SMax Reitz#
397b14f231SMax Reitz# This for loop executes both.
407b14f231SMax Reitzfor filter_node_name in False, True:
417b14f231SMax Reitz    log('')
427b14f231SMax Reitz    log('--- filter_node_name: %s ---' % filter_node_name)
437b14f231SMax Reitz    log('')
447b14f231SMax Reitz
457b14f231SMax Reitz    with iotests.FilePath('base.img') as base_img_path, \
467b14f231SMax Reitz         iotests.FilePath('mid.img') as mid_img_path, \
477b14f231SMax Reitz         iotests.FilePath('top.img') as top_img_path, \
487b14f231SMax Reitz         iotests.VM() as vm:
497b14f231SMax Reitz
50fc272d3cSJohn Snow        qemu_img('create', '-f', iotests.imgfmt, base_img_path, '64M')
51fc272d3cSJohn Snow        qemu_img('create', '-f', iotests.imgfmt, '-b', base_img_path,
52fc272d3cSJohn Snow                 '-F', iotests.imgfmt, mid_img_path)
53fc272d3cSJohn Snow        qemu_img('create', '-f', iotests.imgfmt, '-b', mid_img_path,
54fc272d3cSJohn Snow                 '-F', iotests.imgfmt, top_img_path)
557b14f231SMax Reitz
567b14f231SMax Reitz        # Something to commit
57*72cfb937SJohn Snow        qemu_io(mid_img_path, '-c', 'write -P 1 0 1M')
587b14f231SMax Reitz
597b14f231SMax Reitz        vm.launch()
607b14f231SMax Reitz
617b14f231SMax Reitz        # Change the bottom-most image's backing file (to null-co://)
627b14f231SMax Reitz        # to enforce json:{} filenames
637b14f231SMax Reitz        vm.qmp_log('blockdev-add',
647b14f231SMax Reitz                    node_name='top',
657b14f231SMax Reitz                    driver=iotests.imgfmt,
667b14f231SMax Reitz                    file={
677b14f231SMax Reitz                        'driver': 'file',
687b14f231SMax Reitz                        'filename': top_img_path
697b14f231SMax Reitz                    },
707b14f231SMax Reitz                    backing={
717b14f231SMax Reitz                        'node-name': 'mid',
727b14f231SMax Reitz                        'driver': iotests.imgfmt,
737b14f231SMax Reitz                        'file': {
747b14f231SMax Reitz                            'driver': 'file',
757b14f231SMax Reitz                            'filename': mid_img_path
767b14f231SMax Reitz                        },
777b14f231SMax Reitz                        'backing': {
787b14f231SMax Reitz                            'node-name': 'base',
797b14f231SMax Reitz                            'driver': iotests.imgfmt,
807b14f231SMax Reitz                            'file': {
817b14f231SMax Reitz                                'driver': 'file',
827b14f231SMax Reitz                                'filename': base_img_path
837b14f231SMax Reitz                            },
847b14f231SMax Reitz                            'backing': {
857b14f231SMax Reitz                                'driver': 'null-co'
867b14f231SMax Reitz                            }
877b14f231SMax Reitz                        }
887b14f231SMax Reitz                    },
897b14f231SMax Reitz                    filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
907b14f231SMax Reitz
917b14f231SMax Reitz        # As long as block-commit does not accept node names, we have to
927b14f231SMax Reitz        # get our mid/base filenames here
937b14f231SMax Reitz        mid_name = vm.node_info('mid')['image']['filename']
947b14f231SMax Reitz        base_name = vm.node_info('base')['image']['filename']
957b14f231SMax Reitz
967b14f231SMax Reitz        assert mid_name[:5] == 'json:'
977b14f231SMax Reitz        assert base_name[:5] == 'json:'
987b14f231SMax Reitz
997b14f231SMax Reitz        # Start the block job
1007b14f231SMax Reitz        if filter_node_name:
1017b14f231SMax Reitz            vm.qmp_log('block-commit',
1027b14f231SMax Reitz                        job_id='commit',
1037b14f231SMax Reitz                        device='top',
1047b14f231SMax Reitz                        filter_node_name='filter_node',
1057b14f231SMax Reitz                        top=mid_name,
1067b14f231SMax Reitz                        base=base_name,
1077b14f231SMax Reitz                        speed=1,
1087b14f231SMax Reitz                        filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
1097b14f231SMax Reitz        else:
1107b14f231SMax Reitz            vm.qmp_log('block-commit',
1117b14f231SMax Reitz                        job_id='commit',
1127b14f231SMax Reitz                        device='top',
1137b14f231SMax Reitz                        top=mid_name,
1147b14f231SMax Reitz                        base=base_name,
1157b14f231SMax Reitz                        speed=1,
1167b14f231SMax Reitz                        filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
1177b14f231SMax Reitz
1187b14f231SMax Reitz        vm.qmp_log('job-pause', id='commit')
1197b14f231SMax Reitz
1207b14f231SMax Reitz        # Get and parse top's json:{} filename
1217b14f231SMax Reitz        top_name = vm.node_info('top')['image']['filename']
1227b14f231SMax Reitz
1237b14f231SMax Reitz        vm.shutdown()
1247b14f231SMax Reitz
1257b14f231SMax Reitz        assert top_name[:5] == 'json:'
1267b14f231SMax Reitz        top_options = json.loads(top_name[5:])
1277b14f231SMax Reitz
1287b14f231SMax Reitz        if filter_node_name:
1297b14f231SMax Reitz            # This should be present and set
1307b14f231SMax Reitz            assert top_options['backing']['driver'] == 'commit_top'
1317b14f231SMax Reitz            # And the mid image is commit_top's backing image
1327b14f231SMax Reitz            mid_options = top_options['backing']['backing']
1337b14f231SMax Reitz        else:
1347b14f231SMax Reitz            # The mid image should appear as the immediate backing BDS
1357b14f231SMax Reitz            # of top
1367b14f231SMax Reitz            mid_options = top_options['backing']
1377b14f231SMax Reitz
1387b14f231SMax Reitz        assert mid_options['driver'] == iotests.imgfmt
1397b14f231SMax Reitz        assert mid_options['file']['filename'] == mid_img_path
140