xref: /openbmc/qemu/tests/qemu-iotests/219 (revision d9efe9384ecb68dcb971b439099e9bead653f96b)
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
26*d9efe938SMax Reitzimg_size = 4 * 1024 * 1024
27*d9efe938SMax Reitz
28bdebdc71SKevin Wolfdef pause_wait(vm, job_id):
29bdebdc71SKevin Wolf    with iotests.Timeout(3, "Timeout waiting for job to pause"):
30bdebdc71SKevin Wolf        while True:
31bdebdc71SKevin Wolf            result = vm.qmp('query-jobs')
32bdebdc71SKevin Wolf            for job in result['return']:
33bdebdc71SKevin Wolf                if job['id'] == job_id and job['status'] in ['paused', 'standby']:
34bdebdc71SKevin Wolf                    return job
35bdebdc71SKevin Wolf
36bdebdc71SKevin Wolf# Test that block-job-pause/resume and job-pause/resume can be mixed
37bdebdc71SKevin Wolfdef test_pause_resume(vm):
38bdebdc71SKevin Wolf    for pause_cmd, pause_arg in [('block-job-pause', 'device'),
39bdebdc71SKevin Wolf                                 ('job-pause', 'id')]:
40bdebdc71SKevin Wolf        for resume_cmd, resume_arg in [('block-job-resume', 'device'),
41bdebdc71SKevin Wolf                                       ('job-resume', 'id')]:
42bdebdc71SKevin Wolf            iotests.log('=== Testing %s/%s ===' % (pause_cmd, resume_cmd))
43bdebdc71SKevin Wolf
44bdebdc71SKevin Wolf            iotests.log(vm.qmp(pause_cmd, **{pause_arg: 'job0'}))
45bdebdc71SKevin Wolf            pause_wait(vm, 'job0')
46bdebdc71SKevin Wolf            iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE')))
4783f90b53SMax Reitz            result = vm.qmp('query-jobs')
4883f90b53SMax Reitz            iotests.log(result)
4983f90b53SMax Reitz
5083f90b53SMax Reitz            old_progress = result['return'][0]['current-progress']
5183f90b53SMax Reitz            total_progress = result['return'][0]['total-progress']
52bdebdc71SKevin Wolf
53bdebdc71SKevin Wolf            iotests.log(vm.qmp(resume_cmd, **{resume_arg: 'job0'}))
54bdebdc71SKevin Wolf            iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE')))
5583f90b53SMax Reitz            if old_progress < total_progress:
5683f90b53SMax Reitz                # Wait for the job to advance
5783f90b53SMax Reitz                while result['return'][0]['current-progress'] == old_progress:
5883f90b53SMax Reitz                    result = vm.qmp('query-jobs')
5983f90b53SMax Reitz                iotests.log(result)
6083f90b53SMax Reitz            else:
6183f90b53SMax Reitz                # Already reached the end, so the job cannot advance
6283f90b53SMax Reitz                # any further; therefore, the query-jobs result can be
6383f90b53SMax Reitz                # logged immediately
64bdebdc71SKevin Wolf                iotests.log(vm.qmp('query-jobs'))
65bdebdc71SKevin Wolf
66bdebdc71SKevin Wolfdef test_job_lifecycle(vm, job, job_args, has_ready=False):
67*d9efe938SMax Reitz    global img_size
68*d9efe938SMax Reitz
69bdebdc71SKevin Wolf    iotests.log('')
70bdebdc71SKevin Wolf    iotests.log('')
71bdebdc71SKevin Wolf    iotests.log('Starting block job: %s (auto-finalize: %s; auto-dismiss: %s)' %
72bdebdc71SKevin Wolf                (job,
73bdebdc71SKevin Wolf                 job_args.get('auto-finalize', True),
74bdebdc71SKevin Wolf                 job_args.get('auto-dismiss', True)))
75bdebdc71SKevin Wolf    iotests.log(vm.qmp(job, job_id='job0', **job_args))
76bdebdc71SKevin Wolf
77bdebdc71SKevin Wolf    # Depending on the storage, the first request may or may not have completed
7883f90b53SMax Reitz    # yet (and the total progress may not have been fully determined yet), so
7983f90b53SMax Reitz    # filter out the progress. Later query-job calls don't need the filtering
8083f90b53SMax Reitz    # because the progress is made deterministic by the block job speed
81bdebdc71SKevin Wolf    result = vm.qmp('query-jobs')
82bdebdc71SKevin Wolf    for j in result['return']:
8383f90b53SMax Reitz        j['current-progress'] = 'FILTERED'
8483f90b53SMax Reitz        j['total-progress'] = 'FILTERED'
85bdebdc71SKevin Wolf    iotests.log(result)
86bdebdc71SKevin Wolf
87bdebdc71SKevin Wolf    # undefined -> created -> running
88bdebdc71SKevin Wolf    iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE')))
89bdebdc71SKevin Wolf    iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE')))
90bdebdc71SKevin Wolf
91*d9efe938SMax Reitz    # Wait for total-progress to stabilize
92*d9efe938SMax Reitz    while vm.qmp('query-jobs')['return'][0]['total-progress'] < img_size:
93*d9efe938SMax Reitz        pass
94*d9efe938SMax Reitz
95bdebdc71SKevin Wolf    # RUNNING state:
96bdebdc71SKevin Wolf    # pause/resume should work, complete/finalize/dismiss should error out
97bdebdc71SKevin Wolf    iotests.log('')
98bdebdc71SKevin Wolf    iotests.log('Pause/resume in RUNNING')
99bdebdc71SKevin Wolf    test_pause_resume(vm)
100bdebdc71SKevin Wolf
101bdebdc71SKevin Wolf    iotests.log(vm.qmp('job-complete', id='job0'))
102bdebdc71SKevin Wolf    iotests.log(vm.qmp('job-finalize', id='job0'))
103bdebdc71SKevin Wolf    iotests.log(vm.qmp('job-dismiss', id='job0'))
104bdebdc71SKevin Wolf
105bdebdc71SKevin Wolf    iotests.log(vm.qmp('block-job-complete', device='job0'))
106bdebdc71SKevin Wolf    iotests.log(vm.qmp('block-job-finalize', id='job0'))
107bdebdc71SKevin Wolf    iotests.log(vm.qmp('block-job-dismiss', id='job0'))
108bdebdc71SKevin Wolf
109bdebdc71SKevin Wolf    # Let the job complete (or transition to READY if it supports that)
110bdebdc71SKevin Wolf    iotests.log(vm.qmp('block-job-set-speed', device='job0', speed=0))
111bdebdc71SKevin Wolf    if has_ready:
112bdebdc71SKevin Wolf        iotests.log('')
113bdebdc71SKevin Wolf        iotests.log('Waiting for READY state...')
114bdebdc71SKevin Wolf        vm.event_wait('BLOCK_JOB_READY')
115bdebdc71SKevin Wolf        iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE')))
116bdebdc71SKevin Wolf        iotests.log(vm.qmp('query-jobs'))
117bdebdc71SKevin Wolf
118bdebdc71SKevin Wolf        # READY state:
119bdebdc71SKevin Wolf        # pause/resume/complete should work, finalize/dismiss should error out
120bdebdc71SKevin Wolf        iotests.log('')
121bdebdc71SKevin Wolf        iotests.log('Pause/resume in READY')
122bdebdc71SKevin Wolf        test_pause_resume(vm)
123bdebdc71SKevin Wolf
124bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-finalize', id='job0'))
125bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-dismiss', id='job0'))
126bdebdc71SKevin Wolf
127bdebdc71SKevin Wolf        iotests.log(vm.qmp('block-job-finalize', id='job0'))
128bdebdc71SKevin Wolf        iotests.log(vm.qmp('block-job-dismiss', id='job0'))
129bdebdc71SKevin Wolf
130bdebdc71SKevin Wolf        # Transition to WAITING
131bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-complete', id='job0'))
132bdebdc71SKevin Wolf
133bdebdc71SKevin Wolf    # Move to WAITING and PENDING state
134bdebdc71SKevin Wolf    iotests.log('')
135bdebdc71SKevin Wolf    iotests.log('Waiting for PENDING state...')
136bdebdc71SKevin Wolf    iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE')))
137bdebdc71SKevin Wolf    iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE')))
138bdebdc71SKevin Wolf
139bdebdc71SKevin Wolf    if not job_args.get('auto-finalize', True):
140bdebdc71SKevin Wolf        # PENDING state:
141bdebdc71SKevin Wolf        # finalize should work, pause/complete/dismiss should error out
142bdebdc71SKevin Wolf        iotests.log(vm.qmp('query-jobs'))
143bdebdc71SKevin Wolf
144bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-pause', id='job0'))
145bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-complete', id='job0'))
146bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-dismiss', id='job0'))
147bdebdc71SKevin Wolf
148bdebdc71SKevin Wolf        iotests.log(vm.qmp('block-job-pause', device='job0'))
149bdebdc71SKevin Wolf        iotests.log(vm.qmp('block-job-complete', device='job0'))
150bdebdc71SKevin Wolf        iotests.log(vm.qmp('block-job-dismiss', id='job0'))
151bdebdc71SKevin Wolf
152bdebdc71SKevin Wolf        # Transition to CONCLUDED
153bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-finalize', id='job0'))
154bdebdc71SKevin Wolf
155bdebdc71SKevin Wolf
156bdebdc71SKevin Wolf    # Move to CONCLUDED state
157bdebdc71SKevin Wolf    iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE')))
158bdebdc71SKevin Wolf
159bdebdc71SKevin Wolf    if not job_args.get('auto-dismiss', True):
160bdebdc71SKevin Wolf        # CONCLUDED state:
161bdebdc71SKevin Wolf        # dismiss should work, pause/complete/finalize should error out
162bdebdc71SKevin Wolf        iotests.log(vm.qmp('query-jobs'))
163bdebdc71SKevin Wolf
164bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-pause', id='job0'))
165bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-complete', id='job0'))
166bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-finalize', id='job0'))
167bdebdc71SKevin Wolf
168bdebdc71SKevin Wolf        iotests.log(vm.qmp('block-job-pause', device='job0'))
169bdebdc71SKevin Wolf        iotests.log(vm.qmp('block-job-complete', device='job0'))
170bdebdc71SKevin Wolf        iotests.log(vm.qmp('block-job-finalize', id='job0'))
171bdebdc71SKevin Wolf
172bdebdc71SKevin Wolf        # Transition to NULL
173bdebdc71SKevin Wolf        iotests.log(vm.qmp('job-dismiss', id='job0'))
174bdebdc71SKevin Wolf
175bdebdc71SKevin Wolf    # Move to NULL state
176bdebdc71SKevin Wolf    iotests.log(iotests.filter_qmp_event(vm.event_wait('JOB_STATUS_CHANGE')))
177bdebdc71SKevin Wolf    iotests.log(vm.qmp('query-jobs'))
178bdebdc71SKevin Wolf
179bdebdc71SKevin Wolf
180bdebdc71SKevin Wolfwith iotests.FilePath('disk.img') as disk_path, \
181bdebdc71SKevin Wolf     iotests.FilePath('copy.img') as copy_path, \
182bdebdc71SKevin Wolf     iotests.VM() as vm:
183bdebdc71SKevin Wolf
184*d9efe938SMax Reitz    iotests.qemu_img_create('-f', iotests.imgfmt, disk_path, str(img_size))
185*d9efe938SMax Reitz    iotests.qemu_io('-c', 'write 0 %i' % (img_size),
186bdebdc71SKevin Wolf                    '-f', iotests.imgfmt, disk_path)
187bdebdc71SKevin Wolf
188bdebdc71SKevin Wolf    iotests.log('Launching VM...')
189bdebdc71SKevin Wolf    vm.add_blockdev(vm.qmp_to_opts({
190bdebdc71SKevin Wolf        'driver': iotests.imgfmt,
191bdebdc71SKevin Wolf        'node-name': 'drive0-node',
192bdebdc71SKevin Wolf        'file': {
193bdebdc71SKevin Wolf            'driver': 'file',
194bdebdc71SKevin Wolf            'filename': disk_path,
195bdebdc71SKevin Wolf        },
196bdebdc71SKevin Wolf    }))
197bdebdc71SKevin Wolf    vm.launch()
198bdebdc71SKevin Wolf
199bdebdc71SKevin Wolf    # In order to keep things deterministic (especially progress in query-job,
200bdebdc71SKevin Wolf    # but related to this also automatic state transitions like job
201bdebdc71SKevin Wolf    # completion), but still get pause points often enough to avoid making this
202bdebdc71SKevin Wolf    # test very slow, it's important to have the right ratio between speed and
203bdebdc71SKevin Wolf    # buf_size.
204bdebdc71SKevin Wolf    #
205bdebdc71SKevin Wolf    # For backup, buf_size is hard-coded to the source image cluster size (64k),
206bdebdc71SKevin Wolf    # so we'll pick the same for mirror. The slice time, i.e. the granularity
207bdebdc71SKevin Wolf    # of the rate limiting is 100ms. With a speed of 256k per second, we can
208bdebdc71SKevin Wolf    # get four pause points per second. This gives us 250ms per iteration,
209bdebdc71SKevin Wolf    # which should be enough to stay deterministic.
210bdebdc71SKevin Wolf
211bdebdc71SKevin Wolf    test_job_lifecycle(vm, 'drive-mirror', has_ready=True, job_args={
212bdebdc71SKevin Wolf        'device': 'drive0-node',
213bdebdc71SKevin Wolf        'target': copy_path,
214bdebdc71SKevin Wolf        'sync': 'full',
215bdebdc71SKevin Wolf        'speed': 262144,
216bdebdc71SKevin Wolf        'buf_size': 65536,
217bdebdc71SKevin Wolf    })
218bdebdc71SKevin Wolf
219bdebdc71SKevin Wolf    for auto_finalize in [True, False]:
220bdebdc71SKevin Wolf        for auto_dismiss in [True, False]:
221bdebdc71SKevin Wolf            test_job_lifecycle(vm, 'drive-backup', job_args={
222bdebdc71SKevin Wolf                'device': 'drive0-node',
223bdebdc71SKevin Wolf                'target': copy_path,
224bdebdc71SKevin Wolf                'sync': 'full',
225bdebdc71SKevin Wolf                'speed': 262144,
226bdebdc71SKevin Wolf                'auto-finalize': auto_finalize,
227bdebdc71SKevin Wolf                'auto-dismiss': auto_dismiss,
228bdebdc71SKevin Wolf            })
229bdebdc71SKevin Wolf
230bdebdc71SKevin Wolf    vm.shutdown()
231