xref: /qemu/tests/qemu-iotests/056 (revision 96c04212)
1#!/usr/bin/env python
2#
3# Tests for drive-backup
4#
5# Copyright (C) 2013 Red Hat, Inc.
6#
7# Based on 041.
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 time
24import os
25import iotests
26from iotests import qemu_img, qemu_io, create_image
27
28backing_img = os.path.join(iotests.test_dir, 'backing.img')
29test_img = os.path.join(iotests.test_dir, 'test.img')
30target_img = os.path.join(iotests.test_dir, 'target.img')
31
32class TestSyncModesNoneAndTop(iotests.QMPTestCase):
33    image_len = 64 * 1024 * 1024 # MB
34
35    def setUp(self):
36        create_image(backing_img, TestSyncModesNoneAndTop.image_len)
37        qemu_img('create', '-f', iotests.imgfmt, '-o', 'backing_file=%s' % backing_img, test_img)
38        qemu_io('-c', 'write -P0x41 0 512', test_img)
39        qemu_io('-c', 'write -P0xd5 1M 32k', test_img)
40        qemu_io('-c', 'write -P0xdc 32M 124k', test_img)
41        qemu_io('-c', 'write -P0xdc 67043328 64k', test_img)
42        self.vm = iotests.VM().add_drive(test_img)
43        self.vm.launch()
44
45    def tearDown(self):
46        self.vm.shutdown()
47        os.remove(test_img)
48        os.remove(backing_img)
49        try:
50            os.remove(target_img)
51        except OSError:
52            pass
53
54    def test_complete_top(self):
55        self.assert_no_active_block_jobs()
56        result = self.vm.qmp('drive-backup', device='drive0', sync='top',
57                             format=iotests.imgfmt, target=target_img)
58        self.assert_qmp(result, 'return', {})
59
60        # Custom completed check as we are not copying all data.
61        completed = False
62        while not completed:
63            for event in self.vm.get_qmp_events(wait=True):
64                if event['event'] == 'BLOCK_JOB_COMPLETED':
65                    self.assert_qmp(event, 'data/device', 'drive0')
66                    self.assert_qmp_absent(event, 'data/error')
67                    completed = True
68
69        self.assert_no_active_block_jobs()
70        self.vm.shutdown()
71        self.assertTrue(iotests.compare_images(test_img, target_img),
72                        'target image does not match source after backup')
73
74    def test_cancel_sync_none(self):
75        self.assert_no_active_block_jobs()
76
77        result = self.vm.qmp('drive-backup', device='drive0',
78                             sync='none', target=target_img)
79        self.assert_qmp(result, 'return', {})
80        time.sleep(1)
81        self.vm.hmp_qemu_io('drive0', 'write -P0x5e 0 512')
82        self.vm.hmp_qemu_io('drive0', 'aio_flush')
83        # Verify that the original contents exist in the target image.
84
85        event = self.cancel_and_wait()
86        self.assert_qmp(event, 'data/type', 'backup')
87
88        self.vm.shutdown()
89        time.sleep(1)
90        self.assertEqual(-1, qemu_io('-c', 'read -P0x41 0 512', target_img).find("verification failed"))
91
92
93if __name__ == '__main__':
94    iotests.main(supported_fmts=['qcow2', 'qed'])
95