1bdebdc71SKevin Wolf#!/usr/bin/env python 2bdebdc71SKevin Wolf# 3bdebdc71SKevin Wolf# Copyright (C) 2018 Red Hat, Inc. 4bdebdc71SKevin Wolf# 5bdebdc71SKevin Wolf# This program is free software; you can redistribute it and/or modify 6bdebdc71SKevin Wolf# it under the terms of the GNU General Public License as published by 7bdebdc71SKevin Wolf# the Free Software Foundation; either version 2 of the License, or 8bdebdc71SKevin Wolf# (at your option) any later version. 9bdebdc71SKevin Wolf# 10bdebdc71SKevin Wolf# This program is distributed in the hope that it will be useful, 11bdebdc71SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of 12bdebdc71SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 13bdebdc71SKevin Wolf# GNU General Public License for more details. 14bdebdc71SKevin Wolf# 15bdebdc71SKevin Wolf# You should have received a copy of the GNU General Public License 16bdebdc71SKevin Wolf# along with this program. If not, see <http://www.gnu.org/licenses/>. 17bdebdc71SKevin Wolf# 18bdebdc71SKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com> 19bdebdc71SKevin Wolf# 20bdebdc71SKevin Wolf# Check using the job-* QMP commands with block jobs 21bdebdc71SKevin Wolf 22bdebdc71SKevin Wolfimport iotests 23bdebdc71SKevin Wolf 24bdebdc71SKevin Wolfiotests.verify_image_format(supported_fmts=['qcow2']) 25bdebdc71SKevin Wolf 26bdebdc71SKevin Wolfdef pause_wait(vm, job_id): 27bdebdc71SKevin Wolf with iotests.Timeout(3, "Timeout waiting for job to pause"): 28bdebdc71SKevin Wolf while True: 29bdebdc71SKevin Wolf result = vm.qmp('query-jobs') 30bdebdc71SKevin Wolf for job in result['return']: 31bdebdc71SKevin Wolf if job['id'] == job_id and job['status'] in ['paused', 'standby']: 32bdebdc71SKevin Wolf return job 33bdebdc71SKevin Wolf 34bdebdc71SKevin Wolf# Test that block-job-pause/resume and job-pause/resume can be mixed 35bdebdc71SKevin Wolfdef test_pause_resume(vm): 36bdebdc71SKevin Wolf for pause_cmd, pause_arg in [('block-job-pause', 'device'), 37bdebdc71SKevin Wolf ('job-pause', 'id')]: 38bdebdc71SKevin Wolf for resume_cmd, resume_arg in [('block-job-resume', 'device'), 39bdebdc71SKevin Wolf ('job-resume', 'id')]: 40bdebdc71SKevin Wolf iotests.log('=== Testing %s/%s ===' % (pause_cmd, resume_cmd)) 41bdebdc71SKevin Wolf 42bdebdc71SKevin Wolf iotests.log(vm.qmp(pause_cmd, **{pause_arg: 'job0'})) 43bdebdc71SKevin Wolf pause_wait(vm, 'job0') 44bdebdc71SKevin Wolf iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE'))) 45*83f90b53SMax Reitz result = vm.qmp('query-jobs') 46*83f90b53SMax Reitz iotests.log(result) 47*83f90b53SMax Reitz 48*83f90b53SMax Reitz old_progress = result['return'][0]['current-progress'] 49*83f90b53SMax Reitz total_progress = result['return'][0]['total-progress'] 50bdebdc71SKevin Wolf 51bdebdc71SKevin Wolf iotests.log(vm.qmp(resume_cmd, **{resume_arg: 'job0'})) 52bdebdc71SKevin Wolf iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE'))) 53*83f90b53SMax Reitz if old_progress < total_progress: 54*83f90b53SMax Reitz # Wait for the job to advance 55*83f90b53SMax Reitz while result['return'][0]['current-progress'] == old_progress: 56*83f90b53SMax Reitz result = vm.qmp('query-jobs') 57*83f90b53SMax Reitz iotests.log(result) 58*83f90b53SMax Reitz else: 59*83f90b53SMax Reitz # Already reached the end, so the job cannot advance 60*83f90b53SMax Reitz # any further; therefore, the query-jobs result can be 61*83f90b53SMax Reitz # logged immediately 62bdebdc71SKevin Wolf iotests.log(vm.qmp('query-jobs')) 63bdebdc71SKevin Wolf 64bdebdc71SKevin Wolfdef test_job_lifecycle(vm, job, job_args, has_ready=False): 65bdebdc71SKevin Wolf iotests.log('') 66bdebdc71SKevin Wolf iotests.log('') 67bdebdc71SKevin Wolf iotests.log('Starting block job: %s (auto-finalize: %s; auto-dismiss: %s)' % 68bdebdc71SKevin Wolf (job, 69bdebdc71SKevin Wolf job_args.get('auto-finalize', True), 70bdebdc71SKevin Wolf job_args.get('auto-dismiss', True))) 71bdebdc71SKevin Wolf iotests.log(vm.qmp(job, job_id='job0', **job_args)) 72bdebdc71SKevin Wolf 73bdebdc71SKevin Wolf # Depending on the storage, the first request may or may not have completed 74*83f90b53SMax Reitz # yet (and the total progress may not have been fully determined yet), so 75*83f90b53SMax Reitz # filter out the progress. Later query-job calls don't need the filtering 76*83f90b53SMax Reitz # because the progress is made deterministic by the block job speed 77bdebdc71SKevin Wolf result = vm.qmp('query-jobs') 78bdebdc71SKevin Wolf for j in result['return']: 79*83f90b53SMax Reitz j['current-progress'] = 'FILTERED' 80*83f90b53SMax Reitz j['total-progress'] = 'FILTERED' 81bdebdc71SKevin Wolf iotests.log(result) 82bdebdc71SKevin Wolf 83bdebdc71SKevin Wolf # undefined -> created -> running 84bdebdc71SKevin Wolf iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE'))) 85bdebdc71SKevin Wolf iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE'))) 86bdebdc71SKevin Wolf 87bdebdc71SKevin Wolf # RUNNING state: 88bdebdc71SKevin Wolf # pause/resume should work, complete/finalize/dismiss should error out 89bdebdc71SKevin Wolf iotests.log('') 90bdebdc71SKevin Wolf iotests.log('Pause/resume in RUNNING') 91bdebdc71SKevin Wolf test_pause_resume(vm) 92bdebdc71SKevin Wolf 93bdebdc71SKevin Wolf iotests.log(vm.qmp('job-complete', id='job0')) 94bdebdc71SKevin Wolf iotests.log(vm.qmp('job-finalize', id='job0')) 95bdebdc71SKevin Wolf iotests.log(vm.qmp('job-dismiss', id='job0')) 96bdebdc71SKevin Wolf 97bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-complete', device='job0')) 98bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-finalize', id='job0')) 99bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-dismiss', id='job0')) 100bdebdc71SKevin Wolf 101bdebdc71SKevin Wolf # Let the job complete (or transition to READY if it supports that) 102bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-set-speed', device='job0', speed=0)) 103bdebdc71SKevin Wolf if has_ready: 104bdebdc71SKevin Wolf iotests.log('') 105bdebdc71SKevin Wolf iotests.log('Waiting for READY state...') 106bdebdc71SKevin Wolf vm.event_wait('BLOCK_JOB_READY') 107bdebdc71SKevin Wolf iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE'))) 108bdebdc71SKevin Wolf iotests.log(vm.qmp('query-jobs')) 109bdebdc71SKevin Wolf 110bdebdc71SKevin Wolf # READY state: 111bdebdc71SKevin Wolf # pause/resume/complete should work, finalize/dismiss should error out 112bdebdc71SKevin Wolf iotests.log('') 113bdebdc71SKevin Wolf iotests.log('Pause/resume in READY') 114bdebdc71SKevin Wolf test_pause_resume(vm) 115bdebdc71SKevin Wolf 116bdebdc71SKevin Wolf iotests.log(vm.qmp('job-finalize', id='job0')) 117bdebdc71SKevin Wolf iotests.log(vm.qmp('job-dismiss', id='job0')) 118bdebdc71SKevin Wolf 119bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-finalize', id='job0')) 120bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-dismiss', id='job0')) 121bdebdc71SKevin Wolf 122bdebdc71SKevin Wolf # Transition to WAITING 123bdebdc71SKevin Wolf iotests.log(vm.qmp('job-complete', id='job0')) 124bdebdc71SKevin Wolf 125bdebdc71SKevin Wolf # Move to WAITING and PENDING state 126bdebdc71SKevin Wolf iotests.log('') 127bdebdc71SKevin Wolf iotests.log('Waiting for PENDING state...') 128bdebdc71SKevin Wolf iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE'))) 129bdebdc71SKevin Wolf iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE'))) 130bdebdc71SKevin Wolf 131bdebdc71SKevin Wolf if not job_args.get('auto-finalize', True): 132bdebdc71SKevin Wolf # PENDING state: 133bdebdc71SKevin Wolf # finalize should work, pause/complete/dismiss should error out 134bdebdc71SKevin Wolf iotests.log(vm.qmp('query-jobs')) 135bdebdc71SKevin Wolf 136bdebdc71SKevin Wolf iotests.log(vm.qmp('job-pause', id='job0')) 137bdebdc71SKevin Wolf iotests.log(vm.qmp('job-complete', id='job0')) 138bdebdc71SKevin Wolf iotests.log(vm.qmp('job-dismiss', id='job0')) 139bdebdc71SKevin Wolf 140bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-pause', device='job0')) 141bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-complete', device='job0')) 142bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-dismiss', id='job0')) 143bdebdc71SKevin Wolf 144bdebdc71SKevin Wolf # Transition to CONCLUDED 145bdebdc71SKevin Wolf iotests.log(vm.qmp('job-finalize', id='job0')) 146bdebdc71SKevin Wolf 147bdebdc71SKevin Wolf 148bdebdc71SKevin Wolf # Move to CONCLUDED state 149bdebdc71SKevin Wolf iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE'))) 150bdebdc71SKevin Wolf 151bdebdc71SKevin Wolf if not job_args.get('auto-dismiss', True): 152bdebdc71SKevin Wolf # CONCLUDED state: 153bdebdc71SKevin Wolf # dismiss should work, pause/complete/finalize should error out 154bdebdc71SKevin Wolf iotests.log(vm.qmp('query-jobs')) 155bdebdc71SKevin Wolf 156bdebdc71SKevin Wolf iotests.log(vm.qmp('job-pause', id='job0')) 157bdebdc71SKevin Wolf iotests.log(vm.qmp('job-complete', id='job0')) 158bdebdc71SKevin Wolf iotests.log(vm.qmp('job-finalize', id='job0')) 159bdebdc71SKevin Wolf 160bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-pause', device='job0')) 161bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-complete', device='job0')) 162bdebdc71SKevin Wolf iotests.log(vm.qmp('block-job-finalize', id='job0')) 163bdebdc71SKevin Wolf 164bdebdc71SKevin Wolf # Transition to NULL 165bdebdc71SKevin Wolf iotests.log(vm.qmp('job-dismiss', id='job0')) 166bdebdc71SKevin Wolf 167bdebdc71SKevin Wolf # Move to NULL state 168bdebdc71SKevin Wolf iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE'))) 169bdebdc71SKevin Wolf iotests.log(vm.qmp('query-jobs')) 170bdebdc71SKevin Wolf 171bdebdc71SKevin Wolf 172bdebdc71SKevin Wolfwith iotests.FilePath('disk.img') as disk_path, \ 173bdebdc71SKevin Wolf iotests.FilePath('copy.img') as copy_path, \ 174bdebdc71SKevin Wolf iotests.VM() as vm: 175bdebdc71SKevin Wolf 176bdebdc71SKevin Wolf img_size = '4M' 177bdebdc71SKevin Wolf iotests.qemu_img_create('-f', iotests.imgfmt, disk_path, img_size) 178bdebdc71SKevin Wolf iotests.qemu_io('-c', 'write 0 %s' % (img_size), 179bdebdc71SKevin Wolf '-f', iotests.imgfmt, disk_path) 180bdebdc71SKevin Wolf 181bdebdc71SKevin Wolf iotests.log('Launching VM...') 182bdebdc71SKevin Wolf vm.add_blockdev(vm.qmp_to_opts({ 183bdebdc71SKevin Wolf 'driver': iotests.imgfmt, 184bdebdc71SKevin Wolf 'node-name': 'drive0-node', 185bdebdc71SKevin Wolf 'file': { 186bdebdc71SKevin Wolf 'driver': 'file', 187bdebdc71SKevin Wolf 'filename': disk_path, 188bdebdc71SKevin Wolf }, 189bdebdc71SKevin Wolf })) 190bdebdc71SKevin Wolf vm.launch() 191bdebdc71SKevin Wolf 192bdebdc71SKevin Wolf # In order to keep things deterministic (especially progress in query-job, 193bdebdc71SKevin Wolf # but related to this also automatic state transitions like job 194bdebdc71SKevin Wolf # completion), but still get pause points often enough to avoid making this 195bdebdc71SKevin Wolf # test very slow, it's important to have the right ratio between speed and 196bdebdc71SKevin Wolf # buf_size. 197bdebdc71SKevin Wolf # 198bdebdc71SKevin Wolf # For backup, buf_size is hard-coded to the source image cluster size (64k), 199bdebdc71SKevin Wolf # so we'll pick the same for mirror. The slice time, i.e. the granularity 200bdebdc71SKevin Wolf # of the rate limiting is 100ms. With a speed of 256k per second, we can 201bdebdc71SKevin Wolf # get four pause points per second. This gives us 250ms per iteration, 202bdebdc71SKevin Wolf # which should be enough to stay deterministic. 203bdebdc71SKevin Wolf 204bdebdc71SKevin Wolf test_job_lifecycle(vm, 'drive-mirror', has_ready=True, job_args={ 205bdebdc71SKevin Wolf 'device': 'drive0-node', 206bdebdc71SKevin Wolf 'target': copy_path, 207bdebdc71SKevin Wolf 'sync': 'full', 208bdebdc71SKevin Wolf 'speed': 262144, 209bdebdc71SKevin Wolf 'buf_size': 65536, 210bdebdc71SKevin Wolf }) 211bdebdc71SKevin Wolf 212bdebdc71SKevin Wolf for auto_finalize in [True, False]: 213bdebdc71SKevin Wolf for auto_dismiss in [True, False]: 214bdebdc71SKevin Wolf test_job_lifecycle(vm, 'drive-backup', job_args={ 215bdebdc71SKevin Wolf 'device': 'drive0-node', 216bdebdc71SKevin Wolf 'target': copy_path, 217bdebdc71SKevin Wolf 'sync': 'full', 218bdebdc71SKevin Wolf 'speed': 262144, 219bdebdc71SKevin Wolf 'auto-finalize': auto_finalize, 220bdebdc71SKevin Wolf 'auto-dismiss': auto_dismiss, 221bdebdc71SKevin Wolf }) 222bdebdc71SKevin Wolf 223bdebdc71SKevin Wolf vm.shutdown() 224