1*823892d1SStefan Hajnoczi#!/usr/bin/env python3 29dd003a9SVladimir Sementsov-Ogievskiy# group: rw auto quick 3c78dc182SMax Reitz# 4c78dc182SMax Reitz# Test case for ejecting BDSs with block jobs still running on them 5c78dc182SMax Reitz# 6*823892d1SStefan Hajnoczi# Originally written in bash by Hanna Czenczek, ported to Python by Stefan 7*823892d1SStefan Hajnoczi# Hajnoczi. 8*823892d1SStefan Hajnoczi# 9*823892d1SStefan Hajnoczi# Copyright Red Hat 10c78dc182SMax Reitz# 11c78dc182SMax Reitz# This program is free software; you can redistribute it and/or modify 12c78dc182SMax Reitz# it under the terms of the GNU General Public License as published by 13c78dc182SMax Reitz# the Free Software Foundation; either version 2 of the License, or 14c78dc182SMax Reitz# (at your option) any later version. 15c78dc182SMax Reitz# 16c78dc182SMax Reitz# This program is distributed in the hope that it will be useful, 17c78dc182SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of 18c78dc182SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 19c78dc182SMax Reitz# GNU General Public License for more details. 20c78dc182SMax Reitz# 21c78dc182SMax Reitz# You should have received a copy of the GNU General Public License 22c78dc182SMax Reitz# along with this program. If not, see <http://www.gnu.org/licenses/>. 23c78dc182SMax Reitz# 24c78dc182SMax Reitz 25*823892d1SStefan Hajnocziimport iotests 26c78dc182SMax Reitz 27*823892d1SStefan Hajnoczi# Common filters to mask values that vary in the test output 28*823892d1SStefan HajnocziQMP_FILTERS = [iotests.filter_qmp_testfiles, \ 29*823892d1SStefan Hajnoczi iotests.filter_qmp_imgfmt] 30c78dc182SMax Reitz 31c78dc182SMax Reitz 32*823892d1SStefan Hajnocziclass TestCase: 33*823892d1SStefan Hajnoczi def __init__(self, name, vm, image_path, cancel_event): 34*823892d1SStefan Hajnoczi self.name = name 35*823892d1SStefan Hajnoczi self.vm = vm 36*823892d1SStefan Hajnoczi self.image_path = image_path 37*823892d1SStefan Hajnoczi self.cancel_event = cancel_event 38c78dc182SMax Reitz 39*823892d1SStefan Hajnoczi def __enter__(self): 40*823892d1SStefan Hajnoczi iotests.log(f'=== Testing {self.name} ===') 41*823892d1SStefan Hajnoczi self.vm.qmp_log('blockdev-add', \ 42*823892d1SStefan Hajnoczi node_name='drv0', \ 43*823892d1SStefan Hajnoczi driver=iotests.imgfmt, \ 44*823892d1SStefan Hajnoczi file={'driver': 'file', 'filename': self.image_path}, \ 45*823892d1SStefan Hajnoczi filters=QMP_FILTERS) 46c78dc182SMax Reitz 47*823892d1SStefan Hajnoczi def __exit__(self, *exc_details): 48*823892d1SStefan Hajnoczi # This is expected to fail because the job still exists 49*823892d1SStefan Hajnoczi self.vm.qmp_log('blockdev-del', node_name='drv0', \ 50*823892d1SStefan Hajnoczi filters=[iotests.filter_qmp_generated_node_ids]) 51c78dc182SMax Reitz 52*823892d1SStefan Hajnoczi self.vm.qmp_log('block-job-cancel', device='job0') 53*823892d1SStefan Hajnoczi event = self.vm.event_wait(self.cancel_event) 54*823892d1SStefan Hajnoczi iotests.log(event, filters=[iotests.filter_qmp_event]) 55c78dc182SMax Reitz 56*823892d1SStefan Hajnoczi # This time it succeeds 57*823892d1SStefan Hajnoczi self.vm.qmp_log('blockdev-del', node_name='drv0') 58*823892d1SStefan Hajnoczi 59*823892d1SStefan Hajnoczi # Separate test cases in output 60*823892d1SStefan Hajnoczi iotests.log('') 61c78dc182SMax Reitz 62c78dc182SMax Reitz 63*823892d1SStefan Hajnoczidef main() -> None: 64*823892d1SStefan Hajnoczi with iotests.FilePath('bottom', 'middle', 'top', 'target') as \ 65*823892d1SStefan Hajnoczi (bottom_path, middle_path, top_path, target_path), \ 66*823892d1SStefan Hajnoczi iotests.VM() as vm: 67c78dc182SMax Reitz 68*823892d1SStefan Hajnoczi iotests.log('Creating bottom <- middle <- top backing file chain...') 69*823892d1SStefan Hajnoczi IMAGE_SIZE='1M' 70*823892d1SStefan Hajnoczi iotests.qemu_img_create('-f', iotests.imgfmt, bottom_path, IMAGE_SIZE) 71*823892d1SStefan Hajnoczi iotests.qemu_img_create('-f', iotests.imgfmt, \ 72*823892d1SStefan Hajnoczi '-F', iotests.imgfmt, \ 73*823892d1SStefan Hajnoczi '-b', bottom_path, \ 74*823892d1SStefan Hajnoczi middle_path, \ 75*823892d1SStefan Hajnoczi IMAGE_SIZE) 76*823892d1SStefan Hajnoczi iotests.qemu_img_create('-f', iotests.imgfmt, \ 77*823892d1SStefan Hajnoczi '-F', iotests.imgfmt, \ 78*823892d1SStefan Hajnoczi '-b', middle_path, \ 79*823892d1SStefan Hajnoczi top_path, \ 80*823892d1SStefan Hajnoczi IMAGE_SIZE) 81c78dc182SMax Reitz 82*823892d1SStefan Hajnoczi iotests.log('Starting VM...') 83*823892d1SStefan Hajnoczi vm.add_args('-nodefaults') 84*823892d1SStefan Hajnoczi vm.launch() 85c78dc182SMax Reitz 86*823892d1SStefan Hajnoczi # drive-backup will not send BLOCK_JOB_READY by itself, and cancelling 87*823892d1SStefan Hajnoczi # the job will consequently result in BLOCK_JOB_CANCELLED being 88c78dc182SMax Reitz # emitted. 89*823892d1SStefan Hajnoczi with TestCase('drive-backup', vm, top_path, 'BLOCK_JOB_CANCELLED'): 90*823892d1SStefan Hajnoczi vm.qmp_log('drive-backup', \ 91*823892d1SStefan Hajnoczi job_id='job0', \ 92*823892d1SStefan Hajnoczi device='drv0', \ 93*823892d1SStefan Hajnoczi target=target_path, \ 94*823892d1SStefan Hajnoczi format=iotests.imgfmt, \ 95*823892d1SStefan Hajnoczi sync='none', \ 96*823892d1SStefan Hajnoczi filters=QMP_FILTERS) 97c78dc182SMax Reitz 98*823892d1SStefan Hajnoczi # drive-mirror will send BLOCK_JOB_READY basically immediately, and 99*823892d1SStefan Hajnoczi # cancelling the job will consequently result in BLOCK_JOB_COMPLETED 100*823892d1SStefan Hajnoczi # being emitted. 101*823892d1SStefan Hajnoczi with TestCase('drive-mirror', vm, top_path, 'BLOCK_JOB_COMPLETED'): 102*823892d1SStefan Hajnoczi vm.qmp_log('drive-mirror', \ 103*823892d1SStefan Hajnoczi job_id='job0', \ 104*823892d1SStefan Hajnoczi device='drv0', \ 105*823892d1SStefan Hajnoczi target=target_path, \ 106*823892d1SStefan Hajnoczi format=iotests.imgfmt, \ 107*823892d1SStefan Hajnoczi sync='none', \ 108*823892d1SStefan Hajnoczi filters=QMP_FILTERS) 109*823892d1SStefan Hajnoczi event = vm.event_wait('BLOCK_JOB_READY') 110*823892d1SStefan Hajnoczi assert event is not None # silence mypy 111*823892d1SStefan Hajnoczi iotests.log(event, filters=[iotests.filter_qmp_event]) 112c78dc182SMax Reitz 113*823892d1SStefan Hajnoczi # An active block-commit will send BLOCK_JOB_READY basically 114*823892d1SStefan Hajnoczi # immediately, and cancelling the job will consequently result in 115*823892d1SStefan Hajnoczi # BLOCK_JOB_COMPLETED being emitted. 116*823892d1SStefan Hajnoczi with TestCase('active block-commit', vm, top_path, \ 117*823892d1SStefan Hajnoczi 'BLOCK_JOB_COMPLETED'): 118*823892d1SStefan Hajnoczi vm.qmp_log('block-commit', \ 119*823892d1SStefan Hajnoczi job_id='job0', \ 120*823892d1SStefan Hajnoczi device='drv0') 121*823892d1SStefan Hajnoczi event = vm.event_wait('BLOCK_JOB_READY') 122*823892d1SStefan Hajnoczi assert event is not None # silence mypy 123*823892d1SStefan Hajnoczi iotests.log(event, filters=[iotests.filter_qmp_event]) 124c78dc182SMax Reitz 125c78dc182SMax Reitz # Give block-commit something to work on, otherwise it would be done 126*823892d1SStefan Hajnoczi # immediately, send a BLOCK_JOB_COMPLETED and ejecting the BDS would 127*823892d1SStefan Hajnoczi # work just fine without the block job still running. 128*823892d1SStefan Hajnoczi iotests.qemu_io(middle_path, '-c', f'write 0 {IMAGE_SIZE}') 129*823892d1SStefan Hajnoczi with TestCase('non-active block-commit', vm, top_path, \ 130*823892d1SStefan Hajnoczi 'BLOCK_JOB_CANCELLED'): 131*823892d1SStefan Hajnoczi vm.qmp_log('block-commit', \ 132*823892d1SStefan Hajnoczi job_id='job0', \ 133*823892d1SStefan Hajnoczi device='drv0', \ 134*823892d1SStefan Hajnoczi top=middle_path, \ 135*823892d1SStefan Hajnoczi speed=1, \ 136*823892d1SStefan Hajnoczi filters=[iotests.filter_qmp_testfiles]) 137c78dc182SMax Reitz 138c78dc182SMax Reitz # Give block-stream something to work on, otherwise it would be done 139*823892d1SStefan Hajnoczi # immediately, send a BLOCK_JOB_COMPLETED and ejecting the BDS would 140*823892d1SStefan Hajnoczi # work just fine without the block job still running. 141*823892d1SStefan Hajnoczi iotests.qemu_io(bottom_path, '-c', f'write 0 {IMAGE_SIZE}') 142*823892d1SStefan Hajnoczi with TestCase('block-stream', vm, top_path, 'BLOCK_JOB_CANCELLED'): 143*823892d1SStefan Hajnoczi vm.qmp_log('block-stream', \ 144*823892d1SStefan Hajnoczi job_id='job0', \ 145*823892d1SStefan Hajnoczi device='drv0', \ 146*823892d1SStefan Hajnoczi speed=1) 147c78dc182SMax Reitz 148*823892d1SStefan Hajnocziif __name__ == '__main__': 149*823892d1SStefan Hajnoczi iotests.script_main(main, supported_fmts=['qcow2', 'qed'], 150*823892d1SStefan Hajnoczi supported_protocols=['file']) 151