xref: /qemu/tests/qemu-iotests/224 (revision 727385c4)
1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test json:{} filenames with qemu-internal BDSs
5# (the one of commit, to be precise)
6#
7# Copyright (C) 2018 Red Hat, Inc.
8#
9# This program is free software; you can redistribute it and/or modify
10# it under the terms of the GNU General Public License as published by
11# the Free Software Foundation; either version 2 of the License, or
12# (at your option) any later version.
13#
14# This program is distributed in the hope that it will be useful,
15# but WITHOUT ANY WARRANTY; without even the implied warranty of
16# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17# GNU General Public License for more details.
18#
19# You should have received a copy of the GNU General Public License
20# along with this program.  If not, see <http://www.gnu.org/licenses/>.
21#
22# Creator/Owner: Max Reitz <mreitz@redhat.com>
23
24import iotests
25from iotests import log, qemu_img, qemu_io_silent, filter_qmp_testfiles, \
26                    filter_qmp_imgfmt
27import json
28
29# Need backing file support (for arbitrary backing formats)
30iotests.script_initialize(supported_fmts=['qcow2', 'qcow', 'qed'],
31                          supported_platforms=['linux'])
32
33
34# There are two variations of this test:
35# (1) We do not set filter_node_name.  In that case, the commit_top
36#     driver should not appear anywhere.
37# (2) We do set filter_node_name.  In that case, it should appear.
38#
39# This for loop executes both.
40for filter_node_name in False, True:
41    log('')
42    log('--- filter_node_name: %s ---' % filter_node_name)
43    log('')
44
45    with iotests.FilePath('base.img') as base_img_path, \
46         iotests.FilePath('mid.img') as mid_img_path, \
47         iotests.FilePath('top.img') as top_img_path, \
48         iotests.VM() as vm:
49
50        assert qemu_img('create', '-f', iotests.imgfmt,
51                        base_img_path, '64M') == 0
52        assert qemu_img('create', '-f', iotests.imgfmt, '-b', base_img_path,
53                        '-F', iotests.imgfmt, mid_img_path) == 0
54        assert qemu_img('create', '-f', iotests.imgfmt, '-b', mid_img_path,
55                        '-F', iotests.imgfmt, top_img_path) == 0
56
57        # Something to commit
58        assert qemu_io_silent(mid_img_path, '-c', 'write -P 1 0 1M') == 0
59
60        vm.launch()
61
62        # Change the bottom-most image's backing file (to null-co://)
63        # to enforce json:{} filenames
64        vm.qmp_log('blockdev-add',
65                    node_name='top',
66                    driver=iotests.imgfmt,
67                    file={
68                        'driver': 'file',
69                        'filename': top_img_path
70                    },
71                    backing={
72                        'node-name': 'mid',
73                        'driver': iotests.imgfmt,
74                        'file': {
75                            'driver': 'file',
76                            'filename': mid_img_path
77                        },
78                        'backing': {
79                            'node-name': 'base',
80                            'driver': iotests.imgfmt,
81                            'file': {
82                                'driver': 'file',
83                                'filename': base_img_path
84                            },
85                            'backing': {
86                                'driver': 'null-co'
87                            }
88                        }
89                    },
90                    filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
91
92        # As long as block-commit does not accept node names, we have to
93        # get our mid/base filenames here
94        mid_name = vm.node_info('mid')['image']['filename']
95        base_name = vm.node_info('base')['image']['filename']
96
97        assert mid_name[:5] == 'json:'
98        assert base_name[:5] == 'json:'
99
100        # Start the block job
101        if filter_node_name:
102            vm.qmp_log('block-commit',
103                        job_id='commit',
104                        device='top',
105                        filter_node_name='filter_node',
106                        top=mid_name,
107                        base=base_name,
108                        speed=1,
109                        filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
110        else:
111            vm.qmp_log('block-commit',
112                        job_id='commit',
113                        device='top',
114                        top=mid_name,
115                        base=base_name,
116                        speed=1,
117                        filters=[filter_qmp_testfiles, filter_qmp_imgfmt])
118
119        vm.qmp_log('job-pause', id='commit')
120
121        # Get and parse top's json:{} filename
122        top_name = vm.node_info('top')['image']['filename']
123
124        vm.shutdown()
125
126        assert top_name[:5] == 'json:'
127        top_options = json.loads(top_name[5:])
128
129        if filter_node_name:
130            # This should be present and set
131            assert top_options['backing']['driver'] == 'commit_top'
132            # And the mid image is commit_top's backing image
133            mid_options = top_options['backing']['backing']
134        else:
135            # The mid image should appear as the immediate backing BDS
136            # of top
137            mid_options = top_options['backing']
138
139        assert mid_options['driver'] == iotests.imgfmt
140        assert mid_options['file']['filename'] == mid_img_path
141