xref: /openbmc/qemu/tests/qemu-iotests/218 (revision b6aed193)
17c477526SPhilippe Mathieu-Daudé#!/usr/bin/env python3
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw quick
3dc885fffSMax Reitz#
4dc885fffSMax Reitz# This test covers what happens when a mirror block job is cancelled
5dc885fffSMax Reitz# in various phases of its existence.
6dc885fffSMax Reitz#
7dc885fffSMax Reitz# Note that this test only checks the emitted events (i.e.
8dc885fffSMax Reitz# BLOCK_JOB_COMPLETED vs. BLOCK_JOB_CANCELLED), it does not compare
9dc885fffSMax Reitz# whether the target is in sync with the source when the
10dc885fffSMax Reitz# BLOCK_JOB_COMPLETED event occurs.  This is covered by other tests
11dc885fffSMax Reitz# (such as 041).
12dc885fffSMax Reitz#
13dc885fffSMax Reitz# Copyright (C) 2018 Red Hat, Inc.
14dc885fffSMax Reitz#
15dc885fffSMax Reitz# This program is free software; you can redistribute it and/or modify
16dc885fffSMax Reitz# it under the terms of the GNU General Public License as published by
17dc885fffSMax Reitz# the Free Software Foundation; either version 2 of the License, or
18dc885fffSMax Reitz# (at your option) any later version.
19dc885fffSMax Reitz#
20dc885fffSMax Reitz# This program is distributed in the hope that it will be useful,
21dc885fffSMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
22dc885fffSMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
23dc885fffSMax Reitz# GNU General Public License for more details.
24dc885fffSMax Reitz#
25dc885fffSMax Reitz# You should have received a copy of the GNU General Public License
26dc885fffSMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
27dc885fffSMax Reitz#
2842a5009dSJohn Snow# Creator/Owner: Hanna Reitz <hreitz@redhat.com>
29dc885fffSMax Reitz
30dc885fffSMax Reitzimport iotests
3172cfb937SJohn Snowfrom iotests import log, qemu_img, qemu_io
32dc885fffSMax Reitz
337d814059SJohn Snowiotests.script_initialize(supported_fmts=['qcow2', 'raw'])
34dc885fffSMax Reitz
35dc885fffSMax Reitz
36dc885fffSMax Reitz# Launches the VM, adds two null-co nodes (source and target), and
37dc885fffSMax Reitz# starts a blockdev-mirror job on them.
38dc885fffSMax Reitz#
39dc885fffSMax Reitz# Either both or none of speed and buf_size must be given.
40dc885fffSMax Reitz
41dc885fffSMax Reitzdef start_mirror(vm, speed=None, buf_size=None):
42dc885fffSMax Reitz    vm.launch()
43dc885fffSMax Reitz
44*b6aed193SVladimir Sementsov-Ogievskiy    vm.cmd('blockdev-add',
45dc885fffSMax Reitz               node_name='source',
46dc885fffSMax Reitz               driver='null-co',
47dc885fffSMax Reitz               size=1048576)
48dc885fffSMax Reitz
49*b6aed193SVladimir Sementsov-Ogievskiy    vm.cmd('blockdev-add',
50dc885fffSMax Reitz               node_name='target',
51dc885fffSMax Reitz               driver='null-co',
52dc885fffSMax Reitz               size=1048576)
53dc885fffSMax Reitz
54dc885fffSMax Reitz    if speed is not None:
55*b6aed193SVladimir Sementsov-Ogievskiy        vm.cmd('blockdev-mirror',
56dc885fffSMax Reitz                   job_id='mirror',
57dc885fffSMax Reitz                   device='source',
58dc885fffSMax Reitz                   target='target',
59dc885fffSMax Reitz                   sync='full',
60dc885fffSMax Reitz                   speed=speed,
61dc885fffSMax Reitz                   buf_size=buf_size)
62dc885fffSMax Reitz    else:
63*b6aed193SVladimir Sementsov-Ogievskiy        vm.cmd('blockdev-mirror',
64dc885fffSMax Reitz                   job_id='mirror',
65dc885fffSMax Reitz                   device='source',
66dc885fffSMax Reitz                   target='target',
67dc885fffSMax Reitz                   sync='full')
68dc885fffSMax Reitz
69dc885fffSMax Reitz
70dc885fffSMax Reitzlog('')
71dc885fffSMax Reitzlog('=== Cancel mirror job before convergence ===')
72dc885fffSMax Reitzlog('')
73dc885fffSMax Reitz
74dc885fffSMax Reitzlog('--- force=false ---')
75dc885fffSMax Reitzlog('')
76dc885fffSMax Reitz
77dc885fffSMax Reitzwith iotests.VM() as vm:
78dc885fffSMax Reitz    # Low speed so it does not converge
79dc885fffSMax Reitz    start_mirror(vm, 65536, 65536)
80dc885fffSMax Reitz
81dc885fffSMax Reitz    log('Cancelling job')
82dc885fffSMax Reitz    log(vm.qmp('block-job-cancel', device='mirror', force=False))
83dc885fffSMax Reitz
84dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_CANCELLED'),
85dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
86dc885fffSMax Reitz
87dc885fffSMax Reitzlog('')
88dc885fffSMax Reitzlog('--- force=true ---')
89dc885fffSMax Reitzlog('')
90dc885fffSMax Reitz
91dc885fffSMax Reitzwith iotests.VM() as vm:
92dc885fffSMax Reitz    # Low speed so it does not converge
93dc885fffSMax Reitz    start_mirror(vm, 65536, 65536)
94dc885fffSMax Reitz
95dc885fffSMax Reitz    log('Cancelling job')
96dc885fffSMax Reitz    log(vm.qmp('block-job-cancel', device='mirror', force=True))
97dc885fffSMax Reitz
98dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_CANCELLED'),
99dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
100dc885fffSMax Reitz
101dc885fffSMax Reitz
102dc885fffSMax Reitzlog('')
103dc885fffSMax Reitzlog('=== Cancel mirror job after convergence ===')
104dc885fffSMax Reitzlog('')
105dc885fffSMax Reitz
106dc885fffSMax Reitzlog('--- force=false ---')
107dc885fffSMax Reitzlog('')
108dc885fffSMax Reitz
109dc885fffSMax Reitzwith iotests.VM() as vm:
110dc885fffSMax Reitz    start_mirror(vm)
111dc885fffSMax Reitz
112dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_READY'),
113dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
114dc885fffSMax Reitz
115dc885fffSMax Reitz    log('Cancelling job')
116dc885fffSMax Reitz    log(vm.qmp('block-job-cancel', device='mirror', force=False))
117dc885fffSMax Reitz
118dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_COMPLETED'),
119dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
120dc885fffSMax Reitz
121dc885fffSMax Reitzlog('')
122dc885fffSMax Reitzlog('--- force=true ---')
123dc885fffSMax Reitzlog('')
124dc885fffSMax Reitz
125dc885fffSMax Reitzwith iotests.VM() as vm:
126dc885fffSMax Reitz    start_mirror(vm)
127dc885fffSMax Reitz
128dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_READY'),
129dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
130dc885fffSMax Reitz
131dc885fffSMax Reitz    log('Cancelling job')
132dc885fffSMax Reitz    log(vm.qmp('block-job-cancel', device='mirror', force=True))
133dc885fffSMax Reitz
134dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_CANCELLED'),
135dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
13649278ec0SMax Reitz
13749278ec0SMax Reitzlog('')
13849278ec0SMax Reitzlog('=== Cancel mirror job from throttled node by quitting ===')
13949278ec0SMax Reitzlog('')
14049278ec0SMax Reitz
14149278ec0SMax Reitzwith iotests.VM() as vm, \
14249278ec0SMax Reitz     iotests.FilePath('src.img') as src_img_path:
14349278ec0SMax Reitz
144fc272d3cSJohn Snow    qemu_img('create', '-f', iotests.imgfmt, src_img_path, '64M')
14572cfb937SJohn Snow    qemu_io('-f', iotests.imgfmt, src_img_path, '-c', 'write -P 42 0M 64M')
14649278ec0SMax Reitz
14749278ec0SMax Reitz    vm.launch()
14849278ec0SMax Reitz
149*b6aed193SVladimir Sementsov-Ogievskiy    vm.cmd('object-add', qom_type='throttle-group', id='tg',
150fa818b2fSAlberto Garcia           limits={'bps-read': 4096})
15149278ec0SMax Reitz
152*b6aed193SVladimir Sementsov-Ogievskiy    vm.cmd('blockdev-add',
15349278ec0SMax Reitz           node_name='source',
15449278ec0SMax Reitz           driver=iotests.imgfmt,
15549278ec0SMax Reitz           file={
15649278ec0SMax Reitz               'driver': 'file',
15749278ec0SMax Reitz               'filename': src_img_path
15849278ec0SMax Reitz           })
15949278ec0SMax Reitz
160*b6aed193SVladimir Sementsov-Ogievskiy    vm.cmd('blockdev-add',
16149278ec0SMax Reitz           node_name='throttled-source',
16249278ec0SMax Reitz           driver='throttle',
16349278ec0SMax Reitz           throttle_group='tg',
16449278ec0SMax Reitz           file='source')
16549278ec0SMax Reitz
166*b6aed193SVladimir Sementsov-Ogievskiy    vm.cmd('blockdev-add',
16749278ec0SMax Reitz           node_name='target',
16849278ec0SMax Reitz           driver='null-co',
16949278ec0SMax Reitz           size=(64 * 1048576))
17049278ec0SMax Reitz
171*b6aed193SVladimir Sementsov-Ogievskiy    vm.cmd('blockdev-mirror',
17249278ec0SMax Reitz           job_id='mirror',
17349278ec0SMax Reitz           device='throttled-source',
17449278ec0SMax Reitz           target='target',
17549278ec0SMax Reitz           sync='full')
17649278ec0SMax Reitz
17749278ec0SMax Reitz    log(vm.qmp('quit'))
17849278ec0SMax Reitz
17949278ec0SMax Reitz    with iotests.Timeout(5, 'Timeout waiting for VM to quit'):
180b9420e4fSJohn Snow        vm.shutdown()
181