1#!/usr/bin/env python3
2# group: migration
3#
4# Copyright (c) 2021 Virtuozzo International GmbH
5#
6# This program is free software; you can redistribute it and/or modify
7# it under the terms of the GNU General Public License as published by
8# the Free Software Foundation; either version 2 of the License, or
9# (at your option) any later version.
10#
11# This program is distributed in the hope that it will be useful,
12# but WITHOUT ANY WARRANTY; without even the implied warranty of
13# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
14# GNU General Public License for more details.
15#
16# You should have received a copy of the GNU General Public License
17# along with this program.  If not, see <http://www.gnu.org/licenses/>.
18#
19
20import os
21import iotests
22from iotests import qemu_img_create, qemu_io
23
24
25disk_a = os.path.join(iotests.test_dir, 'disk_a')
26disk_b = os.path.join(iotests.test_dir, 'disk_b')
27size = '1M'
28mig_file = os.path.join(iotests.test_dir, 'mig_file')
29mig_cmd = 'exec: cat > ' + mig_file
30
31
32class TestMigrateDuringBackup(iotests.QMPTestCase):
33    def tearDown(self):
34        self.vm.shutdown()
35        os.remove(disk_a)
36        os.remove(disk_b)
37        os.remove(mig_file)
38
39    def setUp(self):
40        qemu_img_create('-f', iotests.imgfmt, disk_a, size)
41        qemu_img_create('-f', iotests.imgfmt, disk_b, size)
42        qemu_io('-c', f'write 0 {size}', disk_a)
43
44        self.vm = iotests.VM().add_drive(disk_a)
45        self.vm.launch()
46        result = self.vm.qmp('blockdev-add', {
47            'node-name': 'target',
48            'driver': iotests.imgfmt,
49            'file': {
50                'driver': 'file',
51                'filename': disk_b
52            }
53        })
54        self.assert_qmp(result, 'return', {})
55
56    def test_migrate(self):
57        result = self.vm.qmp('blockdev-backup', device='drive0',
58                             target='target', sync='full',
59                             speed=1, x_perf={
60                                 'max-workers': 1,
61                                 'max-chunk': 64 * 1024
62                             })
63        self.assert_qmp(result, 'return', {})
64
65        result = self.vm.qmp('job-pause', id='drive0')
66        self.assert_qmp(result, 'return', {})
67
68        result = self.vm.qmp('migrate-set-capabilities',
69                             capabilities=[{'capability': 'events',
70                                            'state': True}])
71        self.assert_qmp(result, 'return', {})
72        result = self.vm.qmp('migrate', uri=mig_cmd)
73        self.assert_qmp(result, 'return', {})
74
75        e = self.vm.events_wait((('MIGRATION',
76                                  {'data': {'status': 'completed'}}),
77                                 ('MIGRATION',
78                                  {'data': {'status': 'failed'}})))
79
80        # Don't assert that e is 'failed' now: this way we'll miss
81        # possible crash when backup continues :)
82
83        result = self.vm.qmp('block-job-set-speed', device='drive0',
84                             speed=0)
85        self.assert_qmp(result, 'return', {})
86        result = self.vm.qmp('job-resume', id='drive0')
87        self.assert_qmp(result, 'return', {})
88
89        # For future: if something changes so that both migration
90        # and backup pass, let's not miss that moment, as it may
91        # be a bug as well as improvement.
92        self.assert_qmp(e, 'data/status', 'failed')
93
94
95if __name__ == '__main__':
96    iotests.main(supported_fmts=['qcow2'],
97                 supported_protocols=['file'])
98