1#!/usr/bin/env python3 2# group: rw quick 3# 4# Test what happens when a stream job completes in a blk_drain(). 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 os 23import iotests 24from iotests import imgfmt, qemu_img_create, qemu_io_silent, QMPTestCase 25 26 27image_size = 1 * 1024 * 1024 28data_size = 64 * 1024 29base = os.path.join(iotests.test_dir, 'base.img') 30top = os.path.join(iotests.test_dir, 'top.img') 31 32 33# We want to test completing a stream job in a blk_drain(). 34# 35# The blk_drain() we are going to use is a virtio-scsi device resetting, 36# which we can trigger by resetting the system. 37# 38# In order to have the block job complete on drain, we (1) throttle its 39# base image so we can start the drain after it has begun, but before it 40# completes, and (2) make it encounter an I/O error on the ensuing write. 41# (If it completes regularly, the completion happens after the drain for 42# some reason.) 43 44class TestStreamErrorOnReset(QMPTestCase): 45 def setUp(self) -> None: 46 """ 47 Create two images: 48 - base image {base} with {data_size} bytes allocated 49 - top image {top} without any data allocated 50 51 And the following VM configuration: 52 - base image throttled to {data_size} 53 - top image with a blkdebug configuration so the first write access 54 to it will result in an error 55 - top image is attached to a virtio-scsi device 56 """ 57 assert qemu_img_create('-f', imgfmt, base, str(image_size)) == 0 58 assert qemu_io_silent('-c', f'write 0 {data_size}', base) == 0 59 assert qemu_img_create('-f', imgfmt, top, str(image_size)) == 0 60 61 self.vm = iotests.VM() 62 self.vm.add_args('-accel', 'tcg') # Make throttling work properly 63 self.vm.add_object(self.vm.qmp_to_opts({ 64 'qom-type': 'throttle-group', 65 'id': 'thrgr', 66 'x-bps-total': str(data_size) 67 })) 68 self.vm.add_blockdev(self.vm.qmp_to_opts({ 69 'driver': imgfmt, 70 'node-name': 'base', 71 'file': { 72 'driver': 'throttle', 73 'throttle-group': 'thrgr', 74 'file': { 75 'driver': 'file', 76 'filename': base 77 } 78 } 79 })) 80 self.vm.add_blockdev(self.vm.qmp_to_opts({ 81 'driver': imgfmt, 82 'node-name': 'top', 83 'file': { 84 'driver': 'blkdebug', 85 'node-name': 'top-blkdebug', 86 'inject-error': [{ 87 'event': 'pwritev', 88 'immediately': 'true', 89 'once': 'true' 90 }], 91 'image': { 92 'driver': 'file', 93 'filename': top 94 } 95 }, 96 'backing': 'base' 97 })) 98 self.vm.add_device(self.vm.qmp_to_opts({ 99 'driver': 'virtio-scsi', 100 'id': 'vscsi' 101 })) 102 self.vm.add_device(self.vm.qmp_to_opts({ 103 'driver': 'scsi-hd', 104 'bus': 'vscsi.0', 105 'drive': 'top' 106 })) 107 self.vm.launch() 108 109 def tearDown(self) -> None: 110 self.vm.shutdown() 111 os.remove(top) 112 os.remove(base) 113 114 def test_stream_error_on_reset(self) -> None: 115 # Launch a stream job, which will take at least a second to 116 # complete, because the base image is throttled (so we can 117 # get in between it having started and it having completed) 118 res = self.vm.qmp('block-stream', job_id='stream', device='top') 119 self.assert_qmp(res, 'return', {}) 120 121 while True: 122 ev = self.vm.event_wait('JOB_STATUS_CHANGE') 123 if ev['data']['status'] == 'running': 124 # Once the stream job is running, reset the system, which 125 # forces the virtio-scsi device to be reset, thus draining 126 # the stream job, and making it complete. Completing 127 # inside of that drain should not result in a segfault. 128 res = self.vm.qmp('system_reset') 129 self.assert_qmp(res, 'return', {}) 130 elif ev['data']['status'] == 'null': 131 # The test is done once the job is gone 132 break 133 134 135if __name__ == '__main__': 136 # Passes with any format with backing file support, but qed and 137 # qcow1 do not seem to exercise the used-to-be problematic code 138 # path, so there is no point in having them in this list 139 iotests.main(supported_fmts=['qcow2', 'vmdk'], 140 supported_protocols=['file']) 141