xref: /qemu/tests/qemu-iotests/141 (revision b9c4a2018aa9c89233b8fc68ce26faf8e4ce1c78)
1*9ee2dd4cSStefan Hajnoczi#!/usr/bin/env python3
29dd003a9SVladimir Sementsov-Ogievskiy# group: rw auto quick
3c78dc182SMax Reitz#
4c78dc182SMax Reitz# Test case for ejecting BDSs with block jobs still running on them
5c78dc182SMax Reitz#
6*9ee2dd4cSStefan Hajnoczi# Originally written in bash by Hanna Czenczek, ported to Python by Stefan
7*9ee2dd4cSStefan Hajnoczi# Hajnoczi.
8*9ee2dd4cSStefan Hajnoczi#
9*9ee2dd4cSStefan Hajnoczi# Copyright Red Hat
10c78dc182SMax Reitz#
11c78dc182SMax Reitz# This program is free software; you can redistribute it and/or modify
12c78dc182SMax Reitz# it under the terms of the GNU General Public License as published by
13c78dc182SMax Reitz# the Free Software Foundation; either version 2 of the License, or
14c78dc182SMax Reitz# (at your option) any later version.
15c78dc182SMax Reitz#
16c78dc182SMax Reitz# This program is distributed in the hope that it will be useful,
17c78dc182SMax Reitz# but WITHOUT ANY WARRANTY; without even the implied warranty of
18c78dc182SMax Reitz# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
19c78dc182SMax Reitz# GNU General Public License for more details.
20c78dc182SMax Reitz#
21c78dc182SMax Reitz# You should have received a copy of the GNU General Public License
22c78dc182SMax Reitz# along with this program.  If not, see <http://www.gnu.org/licenses/>.
23c78dc182SMax Reitz#
24c78dc182SMax Reitz
25*9ee2dd4cSStefan Hajnocziimport iotests
26c78dc182SMax Reitz
27*9ee2dd4cSStefan Hajnoczi# Common filters to mask values that vary in the test output
28*9ee2dd4cSStefan HajnocziQMP_FILTERS = [iotests.filter_qmp_testfiles, \
29*9ee2dd4cSStefan Hajnoczi               iotests.filter_qmp_imgfmt]
30c78dc182SMax Reitz
31c78dc182SMax Reitz
32*9ee2dd4cSStefan Hajnocziclass TestCase:
33*9ee2dd4cSStefan Hajnoczi    def __init__(self, name, vm, image_path, cancel_event):
34*9ee2dd4cSStefan Hajnoczi        self.name = name
35*9ee2dd4cSStefan Hajnoczi        self.vm = vm
36*9ee2dd4cSStefan Hajnoczi        self.image_path = image_path
37*9ee2dd4cSStefan Hajnoczi        self.cancel_event = cancel_event
38c78dc182SMax Reitz
39*9ee2dd4cSStefan Hajnoczi    def __enter__(self):
40*9ee2dd4cSStefan Hajnoczi        iotests.log(f'=== Testing {self.name} ===')
41*9ee2dd4cSStefan Hajnoczi        self.vm.qmp_log('blockdev-add', \
42*9ee2dd4cSStefan Hajnoczi                        node_name='drv0', \
43*9ee2dd4cSStefan Hajnoczi                        driver=iotests.imgfmt, \
44*9ee2dd4cSStefan Hajnoczi                        file={'driver': 'file', 'filename': self.image_path}, \
45*9ee2dd4cSStefan Hajnoczi                        filters=QMP_FILTERS)
46c78dc182SMax Reitz
47*9ee2dd4cSStefan Hajnoczi    def __exit__(self, *exc_details):
48*9ee2dd4cSStefan Hajnoczi        # This is expected to fail because the job still exists
49*9ee2dd4cSStefan Hajnoczi        self.vm.qmp_log('blockdev-del', node_name='drv0', \
50*9ee2dd4cSStefan Hajnoczi                        filters=[iotests.filter_qmp_generated_node_ids])
51c78dc182SMax Reitz
52*9ee2dd4cSStefan Hajnoczi        self.vm.qmp_log('block-job-cancel', device='job0')
53*9ee2dd4cSStefan Hajnoczi        event = self.vm.event_wait(self.cancel_event)
54*9ee2dd4cSStefan Hajnoczi        iotests.log(event, filters=[iotests.filter_qmp_event])
55c78dc182SMax Reitz
56*9ee2dd4cSStefan Hajnoczi        # This time it succeeds
57*9ee2dd4cSStefan Hajnoczi        self.vm.qmp_log('blockdev-del', node_name='drv0')
58*9ee2dd4cSStefan Hajnoczi
59*9ee2dd4cSStefan Hajnoczi        # Separate test cases in output
60*9ee2dd4cSStefan Hajnoczi        iotests.log('')
61c78dc182SMax Reitz
62c78dc182SMax Reitz
63*9ee2dd4cSStefan Hajnoczidef main() -> None:
64*9ee2dd4cSStefan Hajnoczi    with iotests.FilePath('bottom', 'middle', 'top', 'target') as \
65*9ee2dd4cSStefan Hajnoczi            (bottom_path, middle_path, top_path, target_path), \
66*9ee2dd4cSStefan Hajnoczi         iotests.VM() as vm:
67c78dc182SMax Reitz
68*9ee2dd4cSStefan Hajnoczi        iotests.log('Creating bottom <- middle <- top backing file chain...')
69*9ee2dd4cSStefan Hajnoczi        IMAGE_SIZE='1M'
70*9ee2dd4cSStefan Hajnoczi        iotests.qemu_img_create('-f', iotests.imgfmt, bottom_path, IMAGE_SIZE)
71*9ee2dd4cSStefan Hajnoczi        iotests.qemu_img_create('-f', iotests.imgfmt, \
72*9ee2dd4cSStefan Hajnoczi                                '-F', iotests.imgfmt, \
73*9ee2dd4cSStefan Hajnoczi                                '-b', bottom_path, \
74*9ee2dd4cSStefan Hajnoczi                                middle_path, \
75*9ee2dd4cSStefan Hajnoczi                                IMAGE_SIZE)
76*9ee2dd4cSStefan Hajnoczi        iotests.qemu_img_create('-f', iotests.imgfmt, \
77*9ee2dd4cSStefan Hajnoczi                                '-F', iotests.imgfmt, \
78*9ee2dd4cSStefan Hajnoczi                                '-b', middle_path, \
79*9ee2dd4cSStefan Hajnoczi                                top_path, \
80*9ee2dd4cSStefan Hajnoczi                                IMAGE_SIZE)
81c78dc182SMax Reitz
82*9ee2dd4cSStefan Hajnoczi        iotests.log('Starting VM...')
83*9ee2dd4cSStefan Hajnoczi        vm.add_args('-nodefaults')
84*9ee2dd4cSStefan Hajnoczi        vm.launch()
85c78dc182SMax Reitz
86*9ee2dd4cSStefan Hajnoczi        # drive-backup will not send BLOCK_JOB_READY by itself, and cancelling
87*9ee2dd4cSStefan Hajnoczi        # the job will consequently result in BLOCK_JOB_CANCELLED being
88c78dc182SMax Reitz        # emitted.
89*9ee2dd4cSStefan Hajnoczi        with TestCase('drive-backup', vm, top_path, 'BLOCK_JOB_CANCELLED'):
90*9ee2dd4cSStefan Hajnoczi            vm.qmp_log('drive-backup', \
91*9ee2dd4cSStefan Hajnoczi                       job_id='job0', \
92*9ee2dd4cSStefan Hajnoczi                       device='drv0', \
93*9ee2dd4cSStefan Hajnoczi                       target=target_path, \
94*9ee2dd4cSStefan Hajnoczi                       format=iotests.imgfmt, \
95*9ee2dd4cSStefan Hajnoczi                       sync='none', \
96*9ee2dd4cSStefan Hajnoczi                       filters=QMP_FILTERS)
97c78dc182SMax Reitz
98*9ee2dd4cSStefan Hajnoczi        # drive-mirror will send BLOCK_JOB_READY basically immediately, and
99*9ee2dd4cSStefan Hajnoczi        # cancelling the job will consequently result in BLOCK_JOB_COMPLETED
100*9ee2dd4cSStefan Hajnoczi        # being emitted.
101*9ee2dd4cSStefan Hajnoczi        with TestCase('drive-mirror', vm, top_path, 'BLOCK_JOB_COMPLETED'):
102*9ee2dd4cSStefan Hajnoczi            vm.qmp_log('drive-mirror', \
103*9ee2dd4cSStefan Hajnoczi                       job_id='job0', \
104*9ee2dd4cSStefan Hajnoczi                       device='drv0', \
105*9ee2dd4cSStefan Hajnoczi                       target=target_path, \
106*9ee2dd4cSStefan Hajnoczi                       format=iotests.imgfmt, \
107*9ee2dd4cSStefan Hajnoczi                       sync='none', \
108*9ee2dd4cSStefan Hajnoczi                       filters=QMP_FILTERS)
109*9ee2dd4cSStefan Hajnoczi            event = vm.event_wait('BLOCK_JOB_READY')
110*9ee2dd4cSStefan Hajnoczi            assert event is not None # silence mypy
111*9ee2dd4cSStefan Hajnoczi            iotests.log(event, filters=[iotests.filter_qmp_event])
112c78dc182SMax Reitz
113*9ee2dd4cSStefan Hajnoczi        # An active block-commit will send BLOCK_JOB_READY basically
114*9ee2dd4cSStefan Hajnoczi        # immediately, and cancelling the job will consequently result in
115*9ee2dd4cSStefan Hajnoczi        # BLOCK_JOB_COMPLETED being emitted.
116*9ee2dd4cSStefan Hajnoczi        with TestCase('active block-commit', vm, top_path, \
117*9ee2dd4cSStefan Hajnoczi                      'BLOCK_JOB_COMPLETED'):
118*9ee2dd4cSStefan Hajnoczi            vm.qmp_log('block-commit', \
119*9ee2dd4cSStefan Hajnoczi                       job_id='job0', \
120*9ee2dd4cSStefan Hajnoczi                       device='drv0')
121*9ee2dd4cSStefan Hajnoczi            event = vm.event_wait('BLOCK_JOB_READY')
122*9ee2dd4cSStefan Hajnoczi            assert event is not None # silence mypy
123*9ee2dd4cSStefan Hajnoczi            iotests.log(event, filters=[iotests.filter_qmp_event])
124c78dc182SMax Reitz
125c78dc182SMax Reitz        # Give block-commit something to work on, otherwise it would be done
126*9ee2dd4cSStefan Hajnoczi        # immediately, send a BLOCK_JOB_COMPLETED and ejecting the BDS would
127*9ee2dd4cSStefan Hajnoczi        # work just fine without the block job still running.
128*9ee2dd4cSStefan Hajnoczi        iotests.qemu_io(middle_path, '-c', f'write 0 {IMAGE_SIZE}')
129*9ee2dd4cSStefan Hajnoczi        with TestCase('non-active block-commit', vm, top_path, \
130*9ee2dd4cSStefan Hajnoczi                      'BLOCK_JOB_CANCELLED'):
131*9ee2dd4cSStefan Hajnoczi            vm.qmp_log('block-commit', \
132*9ee2dd4cSStefan Hajnoczi                       job_id='job0', \
133*9ee2dd4cSStefan Hajnoczi                       device='drv0', \
134*9ee2dd4cSStefan Hajnoczi                       top=middle_path, \
135*9ee2dd4cSStefan Hajnoczi                       speed=1, \
136*9ee2dd4cSStefan Hajnoczi                       filters=[iotests.filter_qmp_testfiles])
137c78dc182SMax Reitz
138c78dc182SMax Reitz        # Give block-stream something to work on, otherwise it would be done
139*9ee2dd4cSStefan Hajnoczi        # immediately, send a BLOCK_JOB_COMPLETED and ejecting the BDS would
140*9ee2dd4cSStefan Hajnoczi        # work just fine without the block job still running.
141*9ee2dd4cSStefan Hajnoczi        iotests.qemu_io(bottom_path, '-c', f'write 0 {IMAGE_SIZE}')
142*9ee2dd4cSStefan Hajnoczi        with TestCase('block-stream', vm, top_path, 'BLOCK_JOB_CANCELLED'):
143*9ee2dd4cSStefan Hajnoczi            vm.qmp_log('block-stream', \
144*9ee2dd4cSStefan Hajnoczi                       job_id='job0', \
145*9ee2dd4cSStefan Hajnoczi                       device='drv0', \
146*9ee2dd4cSStefan Hajnoczi                       speed=1)
147c78dc182SMax Reitz
148*9ee2dd4cSStefan Hajnocziif __name__ == '__main__':
149*9ee2dd4cSStefan Hajnoczi    iotests.script_main(main, supported_fmts=['qcow2', 'qed'],
150*9ee2dd4cSStefan Hajnoczi                        supported_protocols=['file'])
151