xref: /openbmc/qemu/tests/qemu-iotests/255 (revision f6476697)
1#!/usr/bin/env python3
2# group: rw quick
3#
4# Test commit job graph modifications while requests are active
5#
6# Copyright (C) 2019 Red Hat, Inc.
7#
8# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
9#
10# This program is free software; you can redistribute it and/or modify
11# it under the terms of the GNU General Public License as published by
12# the Free Software Foundation; either version 2 of the License, or
13# (at your option) any later version.
14#
15# This program is distributed in the hope that it will be useful,
16# but WITHOUT ANY WARRANTY; without even the implied warranty of
17# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18# GNU General Public License for more details.
19#
20# You should have received a copy of the GNU General Public License
21# along with this program.  If not, see <http://www.gnu.org/licenses/>.
22#
23
24import iotests
25from iotests import imgfmt
26
27iotests.script_initialize(supported_fmts=['qcow2'])
28
29iotests.log('Finishing a commit job with background reads')
30iotests.log('============================================')
31iotests.log('')
32
33with iotests.FilePath('t.qcow2') as disk_path, \
34     iotests.FilePath('t.qcow2.mid') as mid_path, \
35     iotests.FilePath('t.qcow2.base') as base_path, \
36     iotests.VM() as vm:
37
38    iotests.log("=== Create backing chain and start VM ===")
39    iotests.log("")
40
41    size = 128 * 1024 * 1024
42    size_str = str(size)
43
44    iotests.create_image(base_path, size)
45    iotests.qemu_img_log('create', '-f', iotests.imgfmt, mid_path, size_str)
46    iotests.qemu_img_log('create', '-f', iotests.imgfmt, disk_path, size_str)
47
48    # Create a backing chain like this:
49    # base <- [throttled: bps-read=4096] <- mid <- overlay
50
51    vm.add_object('throttle-group,x-bps-read=4096,id=throttle0')
52    vm.add_blockdev('file,filename=%s,node-name=base' % (base_path))
53    vm.add_blockdev('throttle,throttle-group=throttle0,file=base,node-name=throttled')
54    vm.add_blockdev('file,filename=%s,node-name=mid-file' % (mid_path))
55    vm.add_blockdev('qcow2,file=mid-file,node-name=mid,backing=throttled')
56    vm.add_drive_raw('if=none,id=overlay,driver=qcow2,file=%s,backing=mid' % (disk_path))
57
58    vm.launch()
59
60    iotests.log("=== Start background read requests ===")
61    iotests.log("")
62
63    def start_requests():
64        vm.hmp_qemu_io('overlay', 'aio_read 0 4k')
65        vm.hmp_qemu_io('overlay', 'aio_read 0 4k')
66
67    start_requests()
68
69    iotests.log("=== Run a commit job ===")
70    iotests.log("")
71
72    result = vm.qmp_log('block-commit', job_id='job0', auto_finalize=False,
73                        device='overlay', top_node='mid')
74
75    vm.run_job('job0', auto_finalize=False, pre_finalize=start_requests,
76                auto_dismiss=True)
77
78    vm.shutdown()
79
80iotests.log('')
81iotests.log('Closing the VM while a job is being cancelled')
82iotests.log('=============================================')
83iotests.log('')
84
85with iotests.FilePath('src.qcow2') as src_path, \
86     iotests.FilePath('dst.qcow2') as dst_path, \
87     iotests.VM() as vm:
88
89    iotests.log('=== Create images and start VM ===')
90    iotests.log('')
91
92    size = 128 * 1024 * 1024
93    size_str = str(size)
94
95    iotests.qemu_img_log('create', '-f', iotests.imgfmt, src_path, size_str)
96    iotests.qemu_img_log('create', '-f', iotests.imgfmt, dst_path, size_str)
97
98    iotests.log(iotests.qemu_io('-f', iotests.imgfmt, '-c', 'write 0 1M',
99                                src_path),
100                filters=[iotests.filter_test_dir, iotests.filter_qemu_io])
101
102    vm.add_object('throttle-group,x-bps-read=4096,id=throttle0')
103
104    vm.add_blockdev('file,node-name=src-file,filename=%s' % (src_path))
105    vm.add_blockdev('%s,node-name=src,file=src-file' % (iotests.imgfmt))
106
107    vm.add_blockdev('file,node-name=dst-file,filename=%s' % (dst_path))
108    vm.add_blockdev('%s,node-name=dst,file=dst-file' % (iotests.imgfmt))
109
110    vm.add_blockdev('throttle,node-name=src-throttled,' +
111                    'throttle-group=throttle0,file=src')
112
113    vm.add_device('virtio-blk,drive=src-throttled')
114
115    vm.launch()
116
117    iotests.log('=== Start a mirror job ===')
118    iotests.log('')
119
120    vm.qmp_log('blockdev-mirror', job_id='job0', device='src-throttled',
121                                  target='dst', sync='full')
122
123    vm.qmp_log('block-job-cancel', device='job0')
124    vm.qmp_log('quit')
125
126    vm.shutdown(has_quit=True)
127