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