xref: /openbmc/qemu/tests/qemu-iotests/234 (revision 750541c4)
1#!/usr/bin/env python
2#
3# Copyright (C) 2018 Red Hat, Inc.
4#
5# This program is free software; you can redistribute it and/or modify
6# it under the terms of the GNU General Public License as published by
7# the Free Software Foundation; either version 2 of the License, or
8# (at your option) any later version.
9#
10# This program is distributed in the hope that it will be useful,
11# but WITHOUT ANY WARRANTY; without even the implied warranty of
12# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13# GNU General Public License for more details.
14#
15# You should have received a copy of the GNU General Public License
16# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17#
18# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
19#
20# Check that block node activation and inactivation works with a block graph
21# that is built with individually created nodes
22
23import iotests
24import os
25
26iotests.verify_image_format(supported_fmts=['qcow2'])
27iotests.verify_platform(['linux'])
28
29def enable_migration_events(vm, name):
30    iotests.log('Enabling migration QMP events on %s...' % name)
31    iotests.log(vm.qmp('migrate-set-capabilities', capabilities=[
32        {
33            'capability': 'events',
34            'state': True
35        }
36    ]))
37
38def wait_migration(vm):
39    while True:
40        event = vm.event_wait('MIGRATION')
41        iotests.log(event, filters=[iotests.filter_qmp_event])
42        if event['data']['status'] == 'completed':
43            break
44
45with iotests.FilePath('img') as img_path, \
46     iotests.FilePath('backing') as backing_path, \
47     iotests.FilePath('mig_fifo_a') as fifo_a, \
48     iotests.FilePath('mig_fifo_b') as fifo_b, \
49     iotests.VM(path_suffix='a') as vm_a, \
50     iotests.VM(path_suffix='b') as vm_b:
51
52    iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, backing_path, '64M')
53    iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, img_path, '64M')
54
55    os.mkfifo(fifo_a)
56    os.mkfifo(fifo_b)
57
58    iotests.log('Launching source VM...')
59    (vm_a.add_blockdev('file,filename=%s,node-name=drive0-file' % (img_path))
60         .add_blockdev('%s,file=drive0-file,node-name=drive0' % (iotests.imgfmt))
61         .add_blockdev('file,filename=%s,node-name=drive0-backing-file' % (backing_path))
62         .add_blockdev('%s,file=drive0-backing-file,node-name=drive0-backing' % (iotests.imgfmt))
63         .launch())
64
65    enable_migration_events(vm_a, 'A')
66
67    iotests.log('Launching destination VM...')
68    (vm_b.add_blockdev('file,filename=%s,node-name=drive0-file' % (img_path))
69         .add_blockdev('%s,file=drive0-file,node-name=drive0' % (iotests.imgfmt))
70         .add_blockdev('file,filename=%s,node-name=drive0-backing-file' % (backing_path))
71         .add_blockdev('%s,file=drive0-backing-file,node-name=drive0-backing' % (iotests.imgfmt))
72         .add_incoming("exec: cat '%s'" % (fifo_a))
73         .launch())
74
75    enable_migration_events(vm_b, 'B')
76
77    # Add a child node that was created after the parent node. The reverse case
78    # is covered by the -blockdev options above.
79    iotests.log(vm_a.qmp('blockdev-snapshot', node='drive0-backing',
80                         overlay='drive0'))
81    iotests.log(vm_b.qmp('blockdev-snapshot', node='drive0-backing',
82                         overlay='drive0'))
83
84    iotests.log('Starting migration to B...')
85    iotests.log(vm_a.qmp('migrate', uri='exec:cat >%s' % (fifo_a)))
86    with iotests.Timeout(3, 'Migration does not complete'):
87        # Wait for the source first (which includes setup=setup)
88        wait_migration(vm_a)
89        # Wait for the destination second (which does not)
90        wait_migration(vm_b)
91
92    iotests.log(vm_a.qmp('query-migrate')['return']['status'])
93    iotests.log(vm_b.qmp('query-migrate')['return']['status'])
94
95    iotests.log(vm_a.qmp('query-status'))
96    iotests.log(vm_b.qmp('query-status'))
97
98    iotests.log('Add a second parent to drive0-file...')
99    iotests.log(vm_b.qmp('blockdev-add', driver='raw', file='drive0-file',
100                         node_name='drive0-raw'))
101
102    iotests.log('Restart A with -incoming and second parent...')
103    vm_a.shutdown()
104    (vm_a.add_blockdev('raw,file=drive0-file,node-name=drive0-raw')
105         .add_incoming("exec: cat '%s'" % (fifo_b))
106         .launch())
107
108    enable_migration_events(vm_a, 'A')
109
110    iotests.log(vm_a.qmp('blockdev-snapshot', node='drive0-backing',
111                         overlay='drive0'))
112
113    iotests.log('Starting migration back to A...')
114    iotests.log(vm_b.qmp('migrate', uri='exec:cat >%s' % (fifo_b)))
115    with iotests.Timeout(3, 'Migration does not complete'):
116        # Wait for the source first (which includes setup=setup)
117        wait_migration(vm_b)
118        # Wait for the destination second (which does not)
119        wait_migration(vm_a)
120
121    iotests.log(vm_a.qmp('query-migrate')['return']['status'])
122    iotests.log(vm_b.qmp('query-migrate')['return']['status'])
123
124    iotests.log(vm_a.qmp('query-status'))
125    iotests.log(vm_b.qmp('query-status'))
126