1#!/usr/bin/env python3
2# group: quick migration
3#
4# Copyright (C) 2018 Red Hat, Inc.
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# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
20#
21# Check that block node activation and inactivation works with a block graph
22# that is built with individually created nodes
23
24import iotests
25import os
26
27iotests.script_initialize(supported_fmts=['qcow2'],
28                          supported_platforms=['linux'])
29
30with iotests.FilePath('img') as img_path, \
31     iotests.FilePath('backing') as backing_path, \
32     iotests.FilePath('mig_fifo_a') as fifo_a, \
33     iotests.FilePath('mig_fifo_b') as fifo_b, \
34     iotests.VM(path_suffix='a') as vm_a, \
35     iotests.VM(path_suffix='b') as vm_b:
36
37    iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, backing_path, '64M')
38    iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, img_path, '64M')
39
40    os.mkfifo(fifo_a)
41    os.mkfifo(fifo_b)
42
43    iotests.log('Launching source VM...')
44    (vm_a.add_blockdev('file,filename=%s,node-name=drive0-file' % (img_path))
45         .add_blockdev('%s,file=drive0-file,node-name=drive0' % (iotests.imgfmt))
46         .add_blockdev('file,filename=%s,node-name=drive0-backing-file' % (backing_path))
47         .add_blockdev('%s,file=drive0-backing-file,node-name=drive0-backing' % (iotests.imgfmt))
48         .launch())
49
50    vm_a.enable_migration_events('A')
51
52    iotests.log('Launching destination VM...')
53    (vm_b.add_blockdev('file,filename=%s,node-name=drive0-file' % (img_path))
54         .add_blockdev('%s,file=drive0-file,node-name=drive0' % (iotests.imgfmt))
55         .add_blockdev('file,filename=%s,node-name=drive0-backing-file' % (backing_path))
56         .add_blockdev('%s,file=drive0-backing-file,node-name=drive0-backing' % (iotests.imgfmt))
57         .add_incoming("exec: cat '%s'" % (fifo_a))
58         .launch())
59
60    vm_b.enable_migration_events('B')
61
62    # Add a child node that was created after the parent node. The reverse case
63    # is covered by the -blockdev options above.
64    iotests.log(vm_a.qmp('blockdev-snapshot', node='drive0-backing',
65                         overlay='drive0'))
66    iotests.log(vm_b.qmp('blockdev-snapshot', node='drive0-backing',
67                         overlay='drive0'))
68
69    iotests.log('Starting migration to B...')
70    iotests.log(vm_a.qmp('migrate', uri='exec:cat >%s' % (fifo_a)))
71    with iotests.Timeout(3, 'Migration does not complete'):
72        # Wait for the source first (which includes setup=setup)
73        vm_a.wait_migration('postmigrate')
74        # Wait for the destination second (which does not)
75        vm_b.wait_migration('running')
76
77    iotests.log(vm_a.qmp('query-migrate')['return']['status'])
78    iotests.log(vm_b.qmp('query-migrate')['return']['status'])
79
80    iotests.log(vm_a.qmp('query-status'))
81    iotests.log(vm_b.qmp('query-status'))
82
83    iotests.log('Add a second parent to drive0-file...')
84    iotests.log(vm_b.qmp('blockdev-add', driver='raw', file='drive0-file',
85                         node_name='drive0-raw'))
86
87    iotests.log('Restart A with -incoming and second parent...')
88    vm_a.shutdown()
89    (vm_a.add_blockdev('raw,file=drive0-file,node-name=drive0-raw')
90         .add_incoming("exec: cat '%s'" % (fifo_b))
91         .launch())
92
93    vm_a.enable_migration_events('A')
94
95    iotests.log(vm_a.qmp('blockdev-snapshot', node='drive0-backing',
96                         overlay='drive0'))
97
98    iotests.log('Starting migration back to A...')
99    iotests.log(vm_b.qmp('migrate', uri='exec:cat >%s' % (fifo_b)))
100    with iotests.Timeout(3, 'Migration does not complete'):
101        # Wait for the source first (which includes setup=setup)
102        vm_b.wait_migration('postmigrate')
103        # Wait for the destination second (which does not)
104        vm_a.wait_migration('running')
105
106    iotests.log(vm_a.qmp('query-migrate')['return']['status'])
107    iotests.log(vm_b.qmp('query-migrate')['return']['status'])
108
109    iotests.log(vm_a.qmp('query-status'))
110    iotests.log(vm_b.qmp('query-status'))
111