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 self.vm.launch() 92 93 def tearDown(self) -> None: 94 self.vm.shutdown() 95 os.remove(top_img) 96 os.remove(base_img) 97 98 def test_stream(self) -> None: 99 ''' 100 Do a simple stream beneath the two throttle nodes. Should complete 101 with no problems. 102 ''' 103 result = self.vm.qmp('block-stream', 104 job_id='stream', 105 device='unthrottled-node') 106 self.assert_qmp(result, 'return', {}) 107 108 # Should succeed and not time out 109 try: 110 self.vm.run_job('stream') 111 except asyncio.TimeoutError: 112 # VM may be stuck, kill it before tearDown() 113 self.vm.kill() 114 raise 115 116 117if __name__ == '__main__': 118 # Must support backing images 119 iotests.main(supported_fmts=['qcow', 'qcow2', 'qed'], 120 supported_protocols=['file'], 121 required_fmts=['throttle']) 122