xref: /openbmc/qemu/tests/qemu-iotests/234 (revision 9a378495)
1330ca111SKevin Wolf#!/usr/bin/env python
2330ca111SKevin Wolf#
3330ca111SKevin Wolf# Copyright (C) 2018 Red Hat, Inc.
4330ca111SKevin Wolf#
5330ca111SKevin Wolf# This program is free software; you can redistribute it and/or modify
6330ca111SKevin Wolf# it under the terms of the GNU General Public License as published by
7330ca111SKevin Wolf# the Free Software Foundation; either version 2 of the License, or
8330ca111SKevin Wolf# (at your option) any later version.
9330ca111SKevin Wolf#
10330ca111SKevin Wolf# This program is distributed in the hope that it will be useful,
11330ca111SKevin Wolf# but WITHOUT ANY WARRANTY; without even the implied warranty of
12330ca111SKevin Wolf# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
13330ca111SKevin Wolf# GNU General Public License for more details.
14330ca111SKevin Wolf#
15330ca111SKevin Wolf# You should have received a copy of the GNU General Public License
16330ca111SKevin Wolf# along with this program.  If not, see <http://www.gnu.org/licenses/>.
17330ca111SKevin Wolf#
18330ca111SKevin Wolf# Creator/Owner: Kevin Wolf <kwolf@redhat.com>
19330ca111SKevin Wolf#
20330ca111SKevin Wolf# Check that block node activation and inactivation works with a block graph
21330ca111SKevin Wolf# that is built with individually created nodes
22330ca111SKevin Wolf
23330ca111SKevin Wolfimport iotests
24330ca111SKevin Wolfimport os
25330ca111SKevin Wolf
26330ca111SKevin Wolfiotests.verify_image_format(supported_fmts=['qcow2'])
27330ca111SKevin Wolfiotests.verify_platform(['linux'])
28330ca111SKevin Wolf
29*9a378495SMax Reitzdef enable_migration_events(vm, name):
30*9a378495SMax Reitz    iotests.log('Enabling migration QMP events on %s...' % name)
31*9a378495SMax Reitz    iotests.log(vm.qmp('migrate-set-capabilities', capabilities=[
32*9a378495SMax Reitz        {
33*9a378495SMax Reitz            'capability': 'events',
34*9a378495SMax Reitz            'state': True
35*9a378495SMax Reitz        }
36*9a378495SMax Reitz    ]))
37*9a378495SMax Reitz
38*9a378495SMax Reitzdef wait_migration(vm):
39*9a378495SMax Reitz    while True:
40*9a378495SMax Reitz        event = vm.event_wait('MIGRATION')
41*9a378495SMax Reitz        iotests.log(event, filters=[iotests.filter_qmp_event])
42*9a378495SMax Reitz        if event['data']['status'] == 'completed':
43*9a378495SMax Reitz            break
44*9a378495SMax Reitz
45330ca111SKevin Wolfwith iotests.FilePath('img') as img_path, \
46330ca111SKevin Wolf     iotests.FilePath('backing') as backing_path, \
47330ca111SKevin Wolf     iotests.FilePath('mig_fifo_a') as fifo_a, \
48330ca111SKevin Wolf     iotests.FilePath('mig_fifo_b') as fifo_b, \
49330ca111SKevin Wolf     iotests.VM(path_suffix='a') as vm_a, \
50330ca111SKevin Wolf     iotests.VM(path_suffix='b') as vm_b:
51330ca111SKevin Wolf
52330ca111SKevin Wolf    iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, backing_path, '64M')
53330ca111SKevin Wolf    iotests.qemu_img_pipe('create', '-f', iotests.imgfmt, img_path, '64M')
54330ca111SKevin Wolf
55330ca111SKevin Wolf    os.mkfifo(fifo_a)
56330ca111SKevin Wolf    os.mkfifo(fifo_b)
57330ca111SKevin Wolf
58330ca111SKevin Wolf    iotests.log('Launching source VM...')
59330ca111SKevin Wolf    (vm_a.add_blockdev('file,filename=%s,node-name=drive0-file' % (img_path))
60330ca111SKevin Wolf         .add_blockdev('%s,file=drive0-file,node-name=drive0' % (iotests.imgfmt))
61330ca111SKevin Wolf         .add_blockdev('file,filename=%s,node-name=drive0-backing-file' % (backing_path))
62330ca111SKevin Wolf         .add_blockdev('%s,file=drive0-backing-file,node-name=drive0-backing' % (iotests.imgfmt))
63330ca111SKevin Wolf         .launch())
64330ca111SKevin Wolf
65*9a378495SMax Reitz    enable_migration_events(vm_a, 'A')
66*9a378495SMax Reitz
67330ca111SKevin Wolf    iotests.log('Launching destination VM...')
68330ca111SKevin Wolf    (vm_b.add_blockdev('file,filename=%s,node-name=drive0-file' % (img_path))
69330ca111SKevin Wolf         .add_blockdev('%s,file=drive0-file,node-name=drive0' % (iotests.imgfmt))
70330ca111SKevin Wolf         .add_blockdev('file,filename=%s,node-name=drive0-backing-file' % (backing_path))
71330ca111SKevin Wolf         .add_blockdev('%s,file=drive0-backing-file,node-name=drive0-backing' % (iotests.imgfmt))
72330ca111SKevin Wolf         .add_incoming("exec: cat '%s'" % (fifo_a))
73330ca111SKevin Wolf         .launch())
74330ca111SKevin Wolf
75*9a378495SMax Reitz    enable_migration_events(vm_b, 'B')
76*9a378495SMax Reitz
77330ca111SKevin Wolf    # Add a child node that was created after the parent node. The reverse case
78330ca111SKevin Wolf    # is covered by the -blockdev options above.
79330ca111SKevin Wolf    iotests.log(vm_a.qmp('blockdev-snapshot', node='drive0-backing',
80330ca111SKevin Wolf                         overlay='drive0'))
81330ca111SKevin Wolf    iotests.log(vm_b.qmp('blockdev-snapshot', node='drive0-backing',
82330ca111SKevin Wolf                         overlay='drive0'))
83330ca111SKevin Wolf
84330ca111SKevin Wolf    iotests.log('Starting migration to B...')
85330ca111SKevin Wolf    iotests.log(vm_a.qmp('migrate', uri='exec:cat >%s' % (fifo_a)))
86330ca111SKevin Wolf    with iotests.Timeout(3, 'Migration does not complete'):
87*9a378495SMax Reitz        # Wait for the source first (which includes setup=setup)
88*9a378495SMax Reitz        wait_migration(vm_a)
89*9a378495SMax Reitz        # Wait for the destination second (which does not)
90*9a378495SMax Reitz        wait_migration(vm_b)
91330ca111SKevin Wolf
92330ca111SKevin Wolf    iotests.log(vm_a.qmp('query-migrate')['return']['status'])
93330ca111SKevin Wolf    iotests.log(vm_b.qmp('query-migrate')['return']['status'])
94330ca111SKevin Wolf
95330ca111SKevin Wolf    iotests.log(vm_a.qmp('query-status'))
96330ca111SKevin Wolf    iotests.log(vm_b.qmp('query-status'))
97330ca111SKevin Wolf
98330ca111SKevin Wolf    iotests.log('Add a second parent to drive0-file...')
99330ca111SKevin Wolf    iotests.log(vm_b.qmp('blockdev-add', driver='raw', file='drive0-file',
100330ca111SKevin Wolf                         node_name='drive0-raw'))
101330ca111SKevin Wolf
102330ca111SKevin Wolf    iotests.log('Restart A with -incoming and second parent...')
103330ca111SKevin Wolf    vm_a.shutdown()
104330ca111SKevin Wolf    (vm_a.add_blockdev('raw,file=drive0-file,node-name=drive0-raw')
105330ca111SKevin Wolf         .add_incoming("exec: cat '%s'" % (fifo_b))
106330ca111SKevin Wolf         .launch())
107330ca111SKevin Wolf
108*9a378495SMax Reitz    enable_migration_events(vm_a, 'A')
109*9a378495SMax Reitz
110330ca111SKevin Wolf    iotests.log(vm_a.qmp('blockdev-snapshot', node='drive0-backing',
111330ca111SKevin Wolf                         overlay='drive0'))
112330ca111SKevin Wolf
113330ca111SKevin Wolf    iotests.log('Starting migration back to A...')
114330ca111SKevin Wolf    iotests.log(vm_b.qmp('migrate', uri='exec:cat >%s' % (fifo_b)))
115330ca111SKevin Wolf    with iotests.Timeout(3, 'Migration does not complete'):
116*9a378495SMax Reitz        # Wait for the source first (which includes setup=setup)
117*9a378495SMax Reitz        wait_migration(vm_b)
118*9a378495SMax Reitz        # Wait for the destination second (which does not)
119*9a378495SMax Reitz        wait_migration(vm_a)
120330ca111SKevin Wolf
121330ca111SKevin Wolf    iotests.log(vm_a.qmp('query-migrate')['return']['status'])
122330ca111SKevin Wolf    iotests.log(vm_b.qmp('query-migrate')['return']['status'])
123330ca111SKevin Wolf
124330ca111SKevin Wolf    iotests.log(vm_a.qmp('query-status'))
125330ca111SKevin Wolf    iotests.log(vm_b.qmp('query-status'))
126