xref: /openbmc/qemu/tests/qemu-iotests/260 (revision e6e03dcf)
1#!/usr/bin/env python
2#
3# Tests for temporary external snapshot when we have bitmaps.
4#
5# Copyright (c) 2019 Virtuozzo International GmbH. All rights reserved.
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
21import iotests
22from iotests import qemu_img_create, file_path, log, filter_qmp_event
23
24iotests.verify_image_format(supported_fmts=['qcow2'])
25
26base, top = file_path('base', 'top')
27size = 64 * 1024 * 3
28
29
30def print_bitmap(msg, vm):
31    result = vm.qmp('query-block')['return'][0]
32    if 'dirty-bitmaps' in result:
33        bitmap = result['dirty-bitmaps'][0]
34        log('{}: name={} dirty-clusters={}'.format(msg, bitmap['name'],
35            bitmap['count'] // 64 // 1024))
36    else:
37        log(msg + ': not found')
38
39
40def test(persistent, restart):
41    assert persistent or not restart
42    log("\nTestcase {}persistent {} restart\n".format(
43            '' if persistent else 'non-', 'with' if restart else 'without'))
44
45    qemu_img_create('-f', iotests.imgfmt, base, str(size))
46
47    vm = iotests.VM().add_drive(base)
48    vm.launch()
49
50    vm.qmp_log('block-dirty-bitmap-add', node='drive0', name='bitmap0',
51               persistent=persistent)
52    vm.hmp_qemu_io('drive0', 'write 0 64K')
53    print_bitmap('initial bitmap', vm)
54
55    vm.qmp_log('blockdev-snapshot-sync', device='drive0', snapshot_file=top,
56               format=iotests.imgfmt, filters=[iotests.filter_qmp_testfiles])
57    vm.hmp_qemu_io('drive0', 'write 64K 512')
58    print_bitmap('check that no bitmaps are in snapshot', vm)
59
60    if restart:
61        log("... Restart ...")
62        vm.shutdown()
63        vm = iotests.VM().add_drive(top)
64        vm.launch()
65
66    vm.qmp_log('block-commit', device='drive0', top=top,
67               filters=[iotests.filter_qmp_testfiles])
68    ev = vm.events_wait((('BLOCK_JOB_READY', None),
69                         ('BLOCK_JOB_COMPLETED', None)))
70    log(filter_qmp_event(ev))
71    if (ev['event'] == 'BLOCK_JOB_COMPLETED'):
72        vm.shutdown()
73        log(vm.get_log())
74        exit()
75
76    vm.qmp_log('block-job-complete', device='drive0')
77    ev = vm.event_wait('BLOCK_JOB_COMPLETED')
78    log(filter_qmp_event(ev))
79    print_bitmap('check bitmap after commit', vm)
80
81    vm.hmp_qemu_io('drive0', 'write 128K 64K')
82    print_bitmap('check updated bitmap', vm)
83
84    vm.shutdown()
85
86
87test(persistent=False, restart=False)
88test(persistent=True, restart=False)
89test(persistent=True, restart=True)
90