xref: /qemu/tests/qemu-iotests/141 (revision 52ea63de)
1#!/bin/bash
2#
3# Test case for ejecting BDSs with block jobs still running on them
4#
5# Copyright (C) 2016 Red Hat, Inc.
6#
7# This program is free software; you can redistribute it and/or modify
8# it under the terms of the GNU General Public License as published by
9# the Free Software Foundation; either version 2 of the License, or
10# (at your option) any later version.
11#
12# This program is distributed in the hope that it will be useful,
13# but WITHOUT ANY WARRANTY; without even the implied warranty of
14# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15# GNU General Public License for more details.
16#
17# You should have received a copy of the GNU General Public License
18# along with this program.  If not, see <http://www.gnu.org/licenses/>.
19#
20
21# creator
22owner=mreitz@redhat.com
23
24seq="$(basename $0)"
25echo "QA output created by $seq"
26
27here="$PWD"
28status=1	# failure is the default!
29
30_cleanup()
31{
32    _cleanup_test_img
33    rm -f "$TEST_DIR/{b,m,o}.$IMGFMT"
34}
35trap "_cleanup; exit \$status" 0 1 2 3 15
36
37# get standard environment, filters and checks
38. ./common.rc
39. ./common.filter
40. ./common.qemu
41
42# Needs backing file and backing format support
43_supported_fmt qcow2 qed
44_supported_proto file
45_supported_os Linux
46
47
48test_blockjob()
49{
50    _send_qemu_cmd $QEMU_HANDLE \
51        "{'execute': 'blockdev-add',
52          'arguments': {
53              'options': {
54                  'id': 'drv0',
55                  'driver': '$IMGFMT',
56                  'file': {
57                      'driver': 'file',
58                      'filename': '$TEST_IMG'
59                  }}}}" \
60        'return'
61
62    _send_qemu_cmd $QEMU_HANDLE \
63        "$1" \
64        "$2" \
65        | _filter_img_create
66
67    # We want this to return an error because the block job is still running
68    _send_qemu_cmd $QEMU_HANDLE \
69        "{'execute': 'x-blockdev-remove-medium',
70          'arguments': {'device': 'drv0'}}" \
71        'error'
72
73    _send_qemu_cmd $QEMU_HANDLE \
74        "{'execute': 'block-job-cancel',
75          'arguments': {'device': 'drv0'}}" \
76        "$3"
77
78    _send_qemu_cmd $QEMU_HANDLE \
79        "{'execute': 'x-blockdev-del',
80          'arguments': {'id': 'drv0'}}" \
81        'return'
82}
83
84
85TEST_IMG="$TEST_DIR/b.$IMGFMT" _make_test_img 1M
86TEST_IMG="$TEST_DIR/m.$IMGFMT" _make_test_img -b "$TEST_DIR/b.$IMGFMT" 1M
87_make_test_img -b "$TEST_DIR/m.$IMGFMT" 1M
88
89_launch_qemu -nodefaults
90
91_send_qemu_cmd $QEMU_HANDLE \
92    "{'execute': 'qmp_capabilities'}" \
93    'return'
94
95echo
96echo '=== Testing drive-backup ==='
97echo
98
99# drive-backup will not send BLOCK_JOB_READY by itself, and cancelling the job
100# will consequently result in BLOCK_JOB_CANCELLED being emitted.
101
102test_blockjob \
103    "{'execute': 'drive-backup',
104      'arguments': {'device': 'drv0',
105                    'target': '$TEST_DIR/o.$IMGFMT',
106                    'format': '$IMGFMT',
107                    'sync': 'none'}}" \
108    'return' \
109    'BLOCK_JOB_CANCELLED'
110
111echo
112echo '=== Testing drive-mirror ==='
113echo
114
115# drive-mirror will send BLOCK_JOB_READY basically immediately, and cancelling
116# the job will consequently result in BLOCK_JOB_COMPLETED being emitted.
117
118test_blockjob \
119    "{'execute': 'drive-mirror',
120      'arguments': {'device': 'drv0',
121                    'target': '$TEST_DIR/o.$IMGFMT',
122                    'format': '$IMGFMT',
123                    'sync': 'none'}}" \
124    'BLOCK_JOB_READY' \
125    'BLOCK_JOB_COMPLETED'
126
127echo
128echo '=== Testing active block-commit ==='
129echo
130
131# An active block-commit will send BLOCK_JOB_READY basically immediately, and
132# cancelling the job will consequently result in BLOCK_JOB_COMPLETED being
133# emitted.
134
135test_blockjob \
136    "{'execute': 'block-commit',
137      'arguments': {'device': 'drv0'}}" \
138    'BLOCK_JOB_READY' \
139    'BLOCK_JOB_COMPLETED'
140
141echo
142echo '=== Testing non-active block-commit ==='
143echo
144
145# Give block-commit something to work on, otherwise it would be done
146# immediately, send a BLOCK_JOB_COMPLETED and ejecting the BDS would work just
147# fine without the block job still running.
148
149$QEMU_IO -c 'write 0 1M' "$TEST_DIR/m.$IMGFMT" | _filter_qemu_io
150
151test_blockjob \
152    "{'execute': 'block-commit',
153      'arguments': {'device': 'drv0',
154                    'top':    '$TEST_DIR/m.$IMGFMT',
155                    'speed':  1}}" \
156    'return' \
157    'BLOCK_JOB_CANCELLED'
158
159echo
160echo '=== Testing block-stream ==='
161echo
162
163# Give block-stream something to work on, otherwise it would be done
164# immediately, send a BLOCK_JOB_COMPLETED and ejecting the BDS would work just
165# fine without the block job still running.
166
167$QEMU_IO -c 'write 0 1M' "$TEST_DIR/b.$IMGFMT" | _filter_qemu_io
168
169# With some data to stream (and @speed set to 1), block-stream will not complete
170# until we send the block-job-cancel command. Therefore, no event other than
171# BLOCK_JOB_CANCELLED will be emitted.
172
173test_blockjob \
174    "{'execute': 'block-stream',
175      'arguments': {'device': 'drv0',
176                    'speed': 1}}" \
177    'return' \
178    'BLOCK_JOB_CANCELLED'
179
180_cleanup_qemu
181
182# success, all done
183echo "*** done"
184rm -f $seq.full
185status=0
186