1ac6fb43eSKevin Wolf#!/usr/bin/env python 2ac6fb43eSKevin Wolf# 3ac6fb43eSKevin Wolf# Test commit job graph modifications while requests are active 4ac6fb43eSKevin Wolf# 5ac6fb43eSKevin Wolf# Copyright (C) 2019 Red Hat, Inc. 6ac6fb43eSKevin Wolf# 7ac6fb43eSKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 8ac6fb43eSKevin Wolf# 9ac6fb43eSKevin Wolf# This program is free software; you can redistribute it and/or modify 10ac6fb43eSKevin Wolf# it under the terms of the GNU General Public License as published by 11ac6fb43eSKevin Wolf# the Free Software Foundation; either version 2 of the License, or 12ac6fb43eSKevin Wolf# (at your option) any later version. 13ac6fb43eSKevin Wolf# 14ac6fb43eSKevin Wolf# This program is distributed in the hope that it will be useful, 15ac6fb43eSKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 16ac6fb43eSKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 17ac6fb43eSKevin Wolf# GNU General Public License for more details. 18ac6fb43eSKevin Wolf# 19ac6fb43eSKevin Wolf# You should have received a copy of the GNU General Public License 20ac6fb43eSKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 21ac6fb43eSKevin Wolf# 22ac6fb43eSKevin Wolf 23ac6fb43eSKevin Wolfimport iotests 24ac6fb43eSKevin Wolffrom iotests import imgfmt 25ac6fb43eSKevin Wolf 26ac6fb43eSKevin Wolfiotests.verify_image_format(supported_fmts=['qcow2']) 27ac6fb43eSKevin Wolf 28ac6fb43eSKevin Wolfdef blockdev_create(vm, options): 29ac6fb43eSKevin Wolf result = vm.qmp_log('blockdev-create', 30ac6fb43eSKevin Wolf filters=[iotests.filter_qmp_testfiles], 31ac6fb43eSKevin Wolf job_id='job0', options=options) 32ac6fb43eSKevin Wolf 33ac6fb43eSKevin Wolf if 'return' in result: 34ac6fb43eSKevin Wolf assert result['return'] == {} 35ac6fb43eSKevin Wolf vm.run_job('job0') 36ac6fb43eSKevin Wolf iotests.log("") 37ac6fb43eSKevin Wolf 3874f27eadSMax Reitziotests.log('Finishing a commit job with background reads') 3974f27eadSMax Reitziotests.log('============================================') 4074f27eadSMax Reitziotests.log('') 4174f27eadSMax Reitz 42ac6fb43eSKevin Wolfwith iotests.FilePath('t.qcow2') as disk_path, \ 43ac6fb43eSKevin Wolf iotests.FilePath('t.qcow2.mid') as mid_path, \ 44ac6fb43eSKevin Wolf iotests.FilePath('t.qcow2.base') as base_path, \ 45ac6fb43eSKevin Wolf iotests.VM() as vm: 46ac6fb43eSKevin Wolf 47ac6fb43eSKevin Wolf iotests.log("=== Create backing chain and start VM ===") 48ac6fb43eSKevin Wolf iotests.log("") 49ac6fb43eSKevin Wolf 50ac6fb43eSKevin Wolf size = 128 * 1024 * 1024 51ac6fb43eSKevin Wolf size_str = str(size) 52ac6fb43eSKevin Wolf 53ac6fb43eSKevin Wolf iotests.create_image(base_path, size) 54ac6fb43eSKevin Wolf iotests.qemu_img_log('create', '-f', iotests.imgfmt, mid_path, size_str) 55ac6fb43eSKevin Wolf iotests.qemu_img_log('create', '-f', iotests.imgfmt, disk_path, size_str) 56ac6fb43eSKevin Wolf 57ac6fb43eSKevin Wolf # Create a backing chain like this: 58ac6fb43eSKevin Wolf # base <- [throttled: bps-read=4096] <- mid <- overlay 59ac6fb43eSKevin Wolf 60ac6fb43eSKevin Wolf vm.add_object('throttle-group,x-bps-read=4096,id=throttle0') 61ac6fb43eSKevin Wolf vm.add_blockdev('file,filename=%s,node-name=base' % (base_path)) 62ac6fb43eSKevin Wolf vm.add_blockdev('throttle,throttle-group=throttle0,file=base,node-name=throttled') 63ac6fb43eSKevin Wolf vm.add_blockdev('file,filename=%s,node-name=mid-file' % (mid_path)) 64ac6fb43eSKevin Wolf vm.add_blockdev('qcow2,file=mid-file,node-name=mid,backing=throttled') 65ac6fb43eSKevin Wolf vm.add_drive_raw('if=none,id=overlay,driver=qcow2,file=%s,backing=mid' % (disk_path)) 66ac6fb43eSKevin Wolf 67ac6fb43eSKevin Wolf vm.launch() 68ac6fb43eSKevin Wolf 69ac6fb43eSKevin Wolf iotests.log("=== Start background read requests ===") 70ac6fb43eSKevin Wolf iotests.log("") 71ac6fb43eSKevin Wolf 72ac6fb43eSKevin Wolf def start_requests(): 73ac6fb43eSKevin Wolf vm.hmp_qemu_io('overlay', 'aio_read 0 4k') 74ac6fb43eSKevin Wolf vm.hmp_qemu_io('overlay', 'aio_read 0 4k') 75ac6fb43eSKevin Wolf 76ac6fb43eSKevin Wolf start_requests() 77ac6fb43eSKevin Wolf 78ac6fb43eSKevin Wolf iotests.log("=== Run a commit job ===") 79ac6fb43eSKevin Wolf iotests.log("") 80ac6fb43eSKevin Wolf 81ac6fb43eSKevin Wolf result = vm.qmp_log('block-commit', job_id='job0', auto_finalize=False, 82ac6fb43eSKevin Wolf device='overlay', top_node='mid') 83ac6fb43eSKevin Wolf 84ac6fb43eSKevin Wolf vm.run_job('job0', auto_finalize=False, pre_finalize=start_requests, 85ac6fb43eSKevin Wolf auto_dismiss=True) 86ac6fb43eSKevin Wolf 87ac6fb43eSKevin Wolf vm.shutdown() 8874f27eadSMax Reitz 8974f27eadSMax Reitziotests.log('') 9074f27eadSMax Reitziotests.log('Closing the VM while a job is being cancelled') 9174f27eadSMax Reitziotests.log('=============================================') 9274f27eadSMax Reitziotests.log('') 9374f27eadSMax Reitz 9474f27eadSMax Reitzwith iotests.FilePath('src.qcow2') as src_path, \ 9574f27eadSMax Reitz iotests.FilePath('dst.qcow2') as dst_path, \ 9674f27eadSMax Reitz iotests.VM() as vm: 9774f27eadSMax Reitz 9874f27eadSMax Reitz iotests.log('=== Create images and start VM ===') 9974f27eadSMax Reitz iotests.log('') 10074f27eadSMax Reitz 10174f27eadSMax Reitz size = 128 * 1024 * 1024 10274f27eadSMax Reitz size_str = str(size) 10374f27eadSMax Reitz 10474f27eadSMax Reitz iotests.qemu_img_log('create', '-f', iotests.imgfmt, src_path, size_str) 10574f27eadSMax Reitz iotests.qemu_img_log('create', '-f', iotests.imgfmt, dst_path, size_str) 10674f27eadSMax Reitz 10774f27eadSMax Reitz iotests.log(iotests.qemu_io('-f', iotests.imgfmt, '-c', 'write 0 1M', 10874f27eadSMax Reitz src_path), 10974f27eadSMax Reitz filters=[iotests.filter_test_dir, iotests.filter_qemu_io]) 11074f27eadSMax Reitz 11174f27eadSMax Reitz vm.add_object('throttle-group,x-bps-read=4096,id=throttle0') 11274f27eadSMax Reitz 11374f27eadSMax Reitz vm.add_blockdev('file,node-name=src-file,filename=%s' % (src_path)) 11474f27eadSMax Reitz vm.add_blockdev('%s,node-name=src,file=src-file' % (iotests.imgfmt)) 11574f27eadSMax Reitz 11674f27eadSMax Reitz vm.add_blockdev('file,node-name=dst-file,filename=%s' % (dst_path)) 11774f27eadSMax Reitz vm.add_blockdev('%s,node-name=dst,file=dst-file' % (iotests.imgfmt)) 11874f27eadSMax Reitz 11974f27eadSMax Reitz vm.add_blockdev('throttle,node-name=src-throttled,' + 12074f27eadSMax Reitz 'throttle-group=throttle0,file=src') 12174f27eadSMax Reitz 12274f27eadSMax Reitz vm.add_device('virtio-blk,drive=src-throttled') 12374f27eadSMax Reitz 12474f27eadSMax Reitz vm.launch() 12574f27eadSMax Reitz 12674f27eadSMax Reitz iotests.log('=== Start a mirror job ===') 12774f27eadSMax Reitz iotests.log('') 12874f27eadSMax Reitz 12974f27eadSMax Reitz vm.qmp_log('blockdev-mirror', job_id='job0', device='src-throttled', 13074f27eadSMax Reitz target='dst', sync='full') 13174f27eadSMax Reitz 13274f27eadSMax Reitz vm.qmp_log('block-job-cancel', device='job0') 13374f27eadSMax Reitz vm.qmp_log('quit') 13474f27eadSMax Reitz 135*4687133bSMax Reitz vm.shutdown(has_quit=True) 136