xref: /openbmc/qemu/tests/qemu-iotests/258 (revision a68694cd)
1#!/usr/bin/env python3
2#
3# Very specific tests for adjacent commit/stream block jobs
4#
5# Copyright (C) 2019 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20# Creator/Owner: Max Reitz <mreitz@redhat.com>
21
22import iotests
23from iotests import log, qemu_img, qemu_io_silent, \
24        filter_qmp_testfiles, filter_qmp_imgfmt
25
26# Returns a node for blockdev-add
27def node(node_name, path, backing=None, fmt=None, throttle=None):
28    if fmt is None:
29        fmt = iotests.imgfmt
30
31    res = {
32        'node-name': node_name,
33        'driver': fmt,
34        'file': {
35            'driver': 'file',
36            'filename': path
37        }
38    }
39
40    if backing is not None:
41        res['backing'] = backing
42
43    if throttle:
44        res['file'] = {
45            'driver': 'throttle',
46            'throttle-group': throttle,
47            'file': res['file']
48        }
49
50    return res
51
52# Finds a node in the debug block graph
53def find_graph_node(graph, node_id):
54    return next(node for node in graph['nodes'] if node['id'] == node_id)
55
56
57def test_concurrent_finish(write_to_stream_node):
58    log('')
59    log('=== Commit and stream finish concurrently (letting %s write) ===' % \
60        ('stream' if write_to_stream_node else 'commit'))
61    log('')
62
63    # All chosen in such a way that when the commit job wants to
64    # finish, it polls and thus makes stream finish concurrently --
65    # and the other way around, depending on whether the commit job
66    # is finalized before stream completes or not.
67
68    with iotests.FilePath('node4.img') as node4_path, \
69         iotests.FilePath('node3.img') as node3_path, \
70         iotests.FilePath('node2.img') as node2_path, \
71         iotests.FilePath('node1.img') as node1_path, \
72         iotests.FilePath('node0.img') as node0_path, \
73         iotests.VM() as vm:
74
75        # It is important to use raw for the base layer (so that
76        # permissions are just handed through to the protocol layer)
77        assert qemu_img('create', '-f', 'raw', node0_path, '64M') == 0
78
79        stream_throttle=None
80        commit_throttle=None
81
82        for path in [node1_path, node2_path, node3_path, node4_path]:
83            assert qemu_img('create', '-f', iotests.imgfmt, path, '64M') == 0
84
85        if write_to_stream_node:
86            # This is what (most of the time) makes commit finish
87            # earlier and then pull in stream
88            assert qemu_io_silent(node2_path,
89                                  '-c', 'write %iK 64K' % (65536 - 192),
90                                  '-c', 'write %iK 64K' % (65536 -  64)) == 0
91
92            stream_throttle='tg'
93        else:
94            # And this makes stream finish earlier
95            assert qemu_io_silent(node1_path,
96                                  '-c', 'write %iK 64K' % (65536 - 64)) == 0
97
98            commit_throttle='tg'
99
100        vm.launch()
101
102        vm.qmp_log('object-add',
103                   qom_type='throttle-group',
104                   id='tg',
105                   props={
106                       'x-iops-write': 1,
107                       'x-iops-write-max': 1
108                   })
109
110        vm.qmp_log('blockdev-add',
111                   filters=[filter_qmp_testfiles, filter_qmp_imgfmt],
112                   **node('node4', node4_path, throttle=stream_throttle,
113                     backing=node('node3', node3_path,
114                     backing=node('node2', node2_path,
115                     backing=node('node1', node1_path,
116                     backing=node('node0', node0_path, throttle=commit_throttle,
117                                  fmt='raw'))))))
118
119        vm.qmp_log('block-commit',
120                   job_id='commit',
121                   device='node4',
122                   filter_node_name='commit-filter',
123                   top_node='node1',
124                   base_node='node0',
125                   auto_finalize=False)
126
127        vm.qmp_log('block-stream',
128                   job_id='stream',
129                   device='node3',
130                   base_node='commit-filter')
131
132        if write_to_stream_node:
133            vm.run_job('commit', auto_finalize=False, auto_dismiss=True)
134            vm.run_job('stream', auto_finalize=True, auto_dismiss=True)
135        else:
136            # No, the jobs do not really finish concurrently here,
137            # the stream job does complete strictly before commit.
138            # But still, this is close enough for what we want to
139            # test.
140            vm.run_job('stream', auto_finalize=True, auto_dismiss=True)
141            vm.run_job('commit', auto_finalize=False, auto_dismiss=True)
142
143        # Assert that the backing node of node3 is node 0 now
144        graph = vm.qmp('x-debug-query-block-graph')['return']
145        for edge in graph['edges']:
146            if edge['name'] == 'backing' and \
147               find_graph_node(graph, edge['parent'])['name'] == 'node3':
148                assert find_graph_node(graph, edge['child'])['name'] == 'node0'
149                break
150
151
152def main():
153    log('Running tests:')
154    test_concurrent_finish(True)
155    test_concurrent_finish(False)
156
157if __name__ == '__main__':
158    # Need backing file and change-backing-file support
159    iotests.script_main(main,
160                        supported_fmts=['qcow2', 'qed'],
161                        supported_platforms=['linux'])
162