xref: /openbmc/qemu/tests/qemu-iotests/218 (revision 42a5009d8866f69945b708e98d81582bdbcd504f)
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#
28*42a5009dSJohn Snow# Creator/Owner: Hanna Reitz <hreitz@redhat.com>
29dc885fffSMax Reitz
30dc885fffSMax Reitzimport iotests
3149278ec0SMax Reitzfrom iotests import log, qemu_img, qemu_io_silent
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
44dc885fffSMax Reitz    ret = vm.qmp('blockdev-add',
45dc885fffSMax Reitz                     node_name='source',
46dc885fffSMax Reitz                     driver='null-co',
47dc885fffSMax Reitz                     size=1048576)
48dc885fffSMax Reitz    assert ret['return'] == {}
49dc885fffSMax Reitz
50dc885fffSMax Reitz    ret = vm.qmp('blockdev-add',
51dc885fffSMax Reitz                     node_name='target',
52dc885fffSMax Reitz                     driver='null-co',
53dc885fffSMax Reitz                     size=1048576)
54dc885fffSMax Reitz    assert ret['return'] == {}
55dc885fffSMax Reitz
56dc885fffSMax Reitz    if speed is not None:
57dc885fffSMax Reitz        ret = vm.qmp('blockdev-mirror',
58dc885fffSMax Reitz                         job_id='mirror',
59dc885fffSMax Reitz                         device='source',
60dc885fffSMax Reitz                         target='target',
61dc885fffSMax Reitz                         sync='full',
62dc885fffSMax Reitz                         speed=speed,
63dc885fffSMax Reitz                         buf_size=buf_size)
64dc885fffSMax Reitz    else:
65dc885fffSMax Reitz        ret = vm.qmp('blockdev-mirror',
66dc885fffSMax Reitz                         job_id='mirror',
67dc885fffSMax Reitz                         device='source',
68dc885fffSMax Reitz                         target='target',
69dc885fffSMax Reitz                         sync='full')
70dc885fffSMax Reitz
71dc885fffSMax Reitz    assert ret['return'] == {}
72dc885fffSMax Reitz
73dc885fffSMax Reitz
74dc885fffSMax Reitzlog('')
75dc885fffSMax Reitzlog('=== Cancel mirror job before convergence ===')
76dc885fffSMax Reitzlog('')
77dc885fffSMax Reitz
78dc885fffSMax Reitzlog('--- force=false ---')
79dc885fffSMax Reitzlog('')
80dc885fffSMax Reitz
81dc885fffSMax Reitzwith iotests.VM() as vm:
82dc885fffSMax Reitz    # Low speed so it does not converge
83dc885fffSMax Reitz    start_mirror(vm, 65536, 65536)
84dc885fffSMax Reitz
85dc885fffSMax Reitz    log('Cancelling job')
86dc885fffSMax Reitz    log(vm.qmp('block-job-cancel', device='mirror', force=False))
87dc885fffSMax Reitz
88dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_CANCELLED'),
89dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
90dc885fffSMax Reitz
91dc885fffSMax Reitzlog('')
92dc885fffSMax Reitzlog('--- force=true ---')
93dc885fffSMax Reitzlog('')
94dc885fffSMax Reitz
95dc885fffSMax Reitzwith iotests.VM() as vm:
96dc885fffSMax Reitz    # Low speed so it does not converge
97dc885fffSMax Reitz    start_mirror(vm, 65536, 65536)
98dc885fffSMax Reitz
99dc885fffSMax Reitz    log('Cancelling job')
100dc885fffSMax Reitz    log(vm.qmp('block-job-cancel', device='mirror', force=True))
101dc885fffSMax Reitz
102dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_CANCELLED'),
103dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
104dc885fffSMax Reitz
105dc885fffSMax Reitz
106dc885fffSMax Reitzlog('')
107dc885fffSMax Reitzlog('=== Cancel mirror job after convergence ===')
108dc885fffSMax Reitzlog('')
109dc885fffSMax Reitz
110dc885fffSMax Reitzlog('--- force=false ---')
111dc885fffSMax Reitzlog('')
112dc885fffSMax Reitz
113dc885fffSMax Reitzwith iotests.VM() as vm:
114dc885fffSMax Reitz    start_mirror(vm)
115dc885fffSMax Reitz
116dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_READY'),
117dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
118dc885fffSMax Reitz
119dc885fffSMax Reitz    log('Cancelling job')
120dc885fffSMax Reitz    log(vm.qmp('block-job-cancel', device='mirror', force=False))
121dc885fffSMax Reitz
122dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_COMPLETED'),
123dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
124dc885fffSMax Reitz
125dc885fffSMax Reitzlog('')
126dc885fffSMax Reitzlog('--- force=true ---')
127dc885fffSMax Reitzlog('')
128dc885fffSMax Reitz
129dc885fffSMax Reitzwith iotests.VM() as vm:
130dc885fffSMax Reitz    start_mirror(vm)
131dc885fffSMax Reitz
132dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_READY'),
133dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
134dc885fffSMax Reitz
135dc885fffSMax Reitz    log('Cancelling job')
136dc885fffSMax Reitz    log(vm.qmp('block-job-cancel', device='mirror', force=True))
137dc885fffSMax Reitz
138dc885fffSMax Reitz    log(vm.event_wait('BLOCK_JOB_CANCELLED'),
139dc885fffSMax Reitz        filters=[iotests.filter_qmp_event])
14049278ec0SMax Reitz
14149278ec0SMax Reitzlog('')
14249278ec0SMax Reitzlog('=== Cancel mirror job from throttled node by quitting ===')
14349278ec0SMax Reitzlog('')
14449278ec0SMax Reitz
14549278ec0SMax Reitzwith iotests.VM() as vm, \
14649278ec0SMax Reitz     iotests.FilePath('src.img') as src_img_path:
14749278ec0SMax Reitz
148fc272d3cSJohn Snow    qemu_img('create', '-f', iotests.imgfmt, src_img_path, '64M')
14949278ec0SMax Reitz    assert qemu_io_silent('-f', iotests.imgfmt, src_img_path,
15049278ec0SMax Reitz                          '-c', 'write -P 42 0M 64M') == 0
15149278ec0SMax Reitz
15249278ec0SMax Reitz    vm.launch()
15349278ec0SMax Reitz
15449278ec0SMax Reitz    ret = vm.qmp('object-add', qom_type='throttle-group', id='tg',
155fa818b2fSAlberto Garcia                 limits={'bps-read': 4096})
15649278ec0SMax Reitz    assert ret['return'] == {}
15749278ec0SMax Reitz
15849278ec0SMax Reitz    ret = vm.qmp('blockdev-add',
15949278ec0SMax Reitz                 node_name='source',
16049278ec0SMax Reitz                 driver=iotests.imgfmt,
16149278ec0SMax Reitz                 file={
16249278ec0SMax Reitz                     'driver': 'file',
16349278ec0SMax Reitz                     'filename': src_img_path
16449278ec0SMax Reitz                 })
16549278ec0SMax Reitz    assert ret['return'] == {}
16649278ec0SMax Reitz
16749278ec0SMax Reitz    ret = vm.qmp('blockdev-add',
16849278ec0SMax Reitz                 node_name='throttled-source',
16949278ec0SMax Reitz                 driver='throttle',
17049278ec0SMax Reitz                 throttle_group='tg',
17149278ec0SMax Reitz                 file='source')
17249278ec0SMax Reitz    assert ret['return'] == {}
17349278ec0SMax Reitz
17449278ec0SMax Reitz    ret = vm.qmp('blockdev-add',
17549278ec0SMax Reitz                 node_name='target',
17649278ec0SMax Reitz                 driver='null-co',
17749278ec0SMax Reitz                 size=(64 * 1048576))
17849278ec0SMax Reitz    assert ret['return'] == {}
17949278ec0SMax Reitz
18049278ec0SMax Reitz    ret = vm.qmp('blockdev-mirror',
18149278ec0SMax Reitz                 job_id='mirror',
18249278ec0SMax Reitz                 device='throttled-source',
18349278ec0SMax Reitz                 target='target',
18449278ec0SMax Reitz                 sync='full')
18549278ec0SMax Reitz    assert ret['return'] == {}
18649278ec0SMax Reitz
18749278ec0SMax Reitz    log(vm.qmp('quit'))
18849278ec0SMax Reitz
18949278ec0SMax Reitz    with iotests.Timeout(5, 'Timeout waiting for VM to quit'):
190b9420e4fSJohn Snow        vm.shutdown()
191