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        self.vm.cmd('blockdev-add', {
47            'node-name': 'target',
48            'driver': iotests.imgfmt,
49            'file': {
50                'driver': 'file',
51                'filename': disk_b
52            }
53        })
54
55    def test_migrate(self):
56        self.vm.cmd('blockdev-backup', device='drive0',
57                    target='target', sync='full',
58                    speed=1, x_perf={
59                        'max-workers': 1,
60                        'max-chunk': 64 * 1024
61                    })
62
63        self.vm.cmd('job-pause', id='drive0')
64
65        self.vm.cmd('migrate-set-capabilities',
66                    capabilities=[{'capability': 'events',
67                                   'state': True}])
68        self.vm.cmd('migrate', uri=mig_cmd)
69
70        e = self.vm.events_wait((('MIGRATION',
71                                  {'data': {'status': 'completed'}}),
72                                 ('MIGRATION',
73                                  {'data': {'status': 'failed'}})))
74
75        # Don't assert that e is 'failed' now: this way we'll miss
76        # possible crash when backup continues :)
77
78        self.vm.cmd('block-job-set-speed', device='drive0',
79                    speed=0)
80        self.vm.cmd('job-resume', id='drive0')
81
82        # For future: if something changes so that both migration
83        # and backup pass, let's not miss that moment, as it may
84        # be a bug as well as improvement.
85        self.assert_qmp(e, 'data/status', 'failed')
86
87
88if __name__ == '__main__':
89    iotests.main(supported_fmts=['qcow2'],
90                 supported_protocols=['file'])
91