1#!/usr/bin/env python3
2# group: rw
3#
4# Test streaming with throttle nodes on top
5#
6# Copyright (C) 2022 Red Hat, Inc.
7#
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.
12#
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.
17#
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/>.
20#
21
22import asyncio
23import os
24from typing import List
25import iotests
26from iotests import qemu_img_create, qemu_io
27
28
29image_size = 256 * 1024 * 1024
30base_img = os.path.join(iotests.test_dir, 'base.img')
31top_img = os.path.join(iotests.test_dir, 'top.img')
32
33
34class TcgVM(iotests.VM):
35    '''
36    Variant of iotests.VM that uses -accel tcg.  Simply using
37    iotests.VM.add_args('-accel', 'tcg') is not sufficient, because that will
38    put -accel qtest before -accel tcg, and -accel arguments are prioritized in
39    the order they appear.
40    '''
41    @property
42    def _base_args(self) -> List[str]:
43        # Put -accel tcg first so it takes precedence
44        return ['-accel', 'tcg'] + super()._base_args
45
46
47class TestStreamWithThrottle(iotests.QMPTestCase):
48    def setUp(self) -> None:
49        '''
50        Create a simple backing chain between two images, write something to
51        the base image.  Attach them to the VM underneath two throttle nodes,
52        one of which has actually no limits set, but the other does.  Then put
53        a virtio-blk device on top.
54        This test configuration has been taken from
55        https://gitlab.com/qemu-project/qemu/-/issues/1215
56        '''
57        qemu_img_create('-f', iotests.imgfmt, base_img, str(image_size))
58        qemu_img_create('-f', iotests.imgfmt, '-b', base_img, '-F',
59                        iotests.imgfmt, top_img, str(image_size))
60
61        # Write something to stream
62        qemu_io(base_img, '-c', f'write 0 {image_size}')
63
64        blockdev = {
65            'driver': 'throttle',
66            'node-name': 'throttled-node',
67            'throttle-group': 'thrgr-limited',
68            'file': {
69                'driver': 'throttle',
70                'throttle-group': 'thrgr-unlimited',
71                'file': {
72                    'driver': iotests.imgfmt,
73                    'node-name': 'unthrottled-node',
74                    'file': {
75                        'driver': 'file',
76                        'filename': top_img
77                    }
78                }
79            }
80        }
81
82        # Issue 1215 is not reproducible in qtest mode, which is why we need to
83        # create an -accel tcg VM
84        self.vm = TcgVM()
85        self.vm.add_object('iothread,id=iothr0')
86        self.vm.add_object('throttle-group,id=thrgr-unlimited')
87        self.vm.add_object('throttle-group,id=thrgr-limited,'
88                           'x-iops-total=10000,x-bps-total=104857600')
89        self.vm.add_blockdev(self.vm.qmp_to_opts(blockdev))
90        self.vm.add_device('virtio-blk,iothread=iothr0,drive=throttled-node')
91        if iotests.qemu_default_machine == 's390-ccw-virtio':
92            self.vm.add_args('-no-shutdown')
93        self.vm.launch()
94
95    def tearDown(self) -> None:
96        self.vm.shutdown()
97        os.remove(top_img)
98        os.remove(base_img)
99
100    def test_stream(self) -> None:
101        '''
102        Do a simple stream beneath the two throttle nodes.  Should complete
103        with no problems.
104        '''
105        self.vm.cmd('block-stream',
106                    job_id='stream',
107                    device='unthrottled-node')
108
109        # Should succeed and not time out
110        try:
111            self.vm.run_job('stream')
112        except asyncio.TimeoutError:
113            # VM may be stuck, kill it before tearDown()
114            self.vm.kill()
115            raise
116
117
118if __name__ == '__main__':
119    # Must support backing images
120    iotests.main(supported_fmts=['qcow', 'qcow2', 'qed'],
121                 supported_protocols=['file'],
122                 required_fmts=['throttle'])
123