Merge remote-tracking branch 'remotes/huth-gitlab/tags/pull-request-2018-10-17' into...
[qemu/ar7.git] / tests / qemu-iotests / 169
blobf243db9955a8a99d378916afd70a809b08dca74d
1 #!/usr/bin/env python
3 # Tests for dirty bitmaps migration.
5 # Copyright (c) 2016-2017 Virtuozzo International GmbH. All rights reserved.
7 # This program is free software; you can redistribute it and/or modify
8 # it under the terms of the GNU General Public License as published by
9 # the Free Software Foundation; either version 2 of the License, or
10 # (at your option) any later version.
12 # This program is distributed in the hope that it will be useful,
13 # but WITHOUT ANY WARRANTY; without even the implied warranty of
14 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 # GNU General Public License for more details.
17 # You should have received a copy of the GNU General Public License
18 # along with this program. If not, see <http://www.gnu.org/licenses/>.
21 import os
22 import iotests
23 import time
24 import itertools
25 import operator
26 import new
27 from iotests import qemu_img
30 disk_a = os.path.join(iotests.test_dir, 'disk_a')
31 disk_b = os.path.join(iotests.test_dir, 'disk_b')
32 size = '1M'
33 mig_file = os.path.join(iotests.test_dir, 'mig_file')
34 mig_cmd = 'exec: cat > ' + mig_file
35 incoming_cmd = 'exec: cat ' + mig_file
38 class TestDirtyBitmapMigration(iotests.QMPTestCase):
39 def tearDown(self):
40 self.vm_a.shutdown()
41 self.vm_b.shutdown()
42 os.remove(disk_a)
43 os.remove(disk_b)
44 os.remove(mig_file)
46 def setUp(self):
47 qemu_img('create', '-f', iotests.imgfmt, disk_a, size)
48 qemu_img('create', '-f', iotests.imgfmt, disk_b, size)
50 self.vm_a = iotests.VM(path_suffix='a').add_drive(disk_a)
51 self.vm_a.launch()
53 self.vm_b = iotests.VM(path_suffix='b')
55 def add_bitmap(self, vm, granularity, persistent):
56 params = {'node': 'drive0',
57 'name': 'bitmap0',
58 'granularity': granularity}
59 if persistent:
60 params['persistent'] = True
61 params['autoload'] = True
63 result = vm.qmp('block-dirty-bitmap-add', **params)
64 self.assert_qmp(result, 'return', {});
66 def get_bitmap_hash(self, vm):
67 result = vm.qmp('x-debug-block-dirty-bitmap-sha256',
68 node='drive0', name='bitmap0')
69 return result['return']['sha256']
71 def check_bitmap(self, vm, sha256):
72 result = vm.qmp('x-debug-block-dirty-bitmap-sha256',
73 node='drive0', name='bitmap0')
74 if sha256:
75 self.assert_qmp(result, 'return/sha256', sha256);
76 else:
77 self.assert_qmp(result, 'error/desc',
78 "Dirty bitmap 'bitmap0' not found");
80 def do_test_migration(self, persistent, migrate_bitmaps, online,
81 shared_storage):
82 granularity = 512
84 # regions = ((start, count), ...)
85 regions = ((0, 0x10000),
86 (0xf0000, 0x10000),
87 (0xa0201, 0x1000))
89 should_migrate = migrate_bitmaps or persistent and shared_storage
90 mig_caps = [{'capability': 'events', 'state': True}]
91 if migrate_bitmaps:
92 mig_caps.append({'capability': 'dirty-bitmaps', 'state': True})
94 result = self.vm_a.qmp('migrate-set-capabilities',
95 capabilities=mig_caps)
96 self.assert_qmp(result, 'return', {})
98 self.vm_b.add_incoming(incoming_cmd if online else "defer")
99 self.vm_b.add_drive(disk_a if shared_storage else disk_b)
101 if online:
102 os.mkfifo(mig_file)
103 self.vm_b.launch()
104 result = self.vm_b.qmp('migrate-set-capabilities',
105 capabilities=mig_caps)
106 self.assert_qmp(result, 'return', {})
108 self.add_bitmap(self.vm_a, granularity, persistent)
109 for r in regions:
110 self.vm_a.hmp_qemu_io('drive0', 'write %d %d' % r)
111 sha256 = self.get_bitmap_hash(self.vm_a)
113 result = self.vm_a.qmp('migrate', uri=mig_cmd)
114 while True:
115 event = self.vm_a.event_wait('MIGRATION')
116 if event['data']['status'] == 'completed':
117 break
119 if not online:
120 self.vm_a.shutdown()
121 self.vm_b.launch()
122 result = self.vm_b.qmp('migrate-set-capabilities',
123 capabilities=mig_caps)
124 self.assert_qmp(result, 'return', {})
125 result = self.vm_b.qmp('migrate-incoming', uri=incoming_cmd)
126 self.assert_qmp(result, 'return', {})
128 while True:
129 event = self.vm_b.event_wait('MIGRATION')
130 if event['data']['status'] == 'completed':
131 break
133 self.check_bitmap(self.vm_b, sha256 if should_migrate else False)
135 if should_migrate:
136 self.vm_b.shutdown()
137 # recreate vm_b, as we don't want -incoming option (this will lead
138 # to "cat" process left alive after test finish)
139 self.vm_b = iotests.VM(path_suffix='b')
140 self.vm_b.add_drive(disk_a if shared_storage else disk_b)
141 self.vm_b.launch()
142 self.check_bitmap(self.vm_b, sha256 if persistent else False)
145 def inject_test_case(klass, name, method, *args, **kwargs):
146 mc = operator.methodcaller(method, *args, **kwargs)
147 setattr(klass, 'test_' + name, new.instancemethod(mc, None, klass))
149 for cmb in list(itertools.product((True, False), repeat=4)):
150 name = ('_' if cmb[0] else '_not_') + 'persistent_'
151 name += ('_' if cmb[1] else '_not_') + 'migbitmap_'
152 name += '_online' if cmb[2] else '_offline'
153 name += '_shared' if cmb[3] else '_nonshared'
155 inject_test_case(TestDirtyBitmapMigration, name, 'do_test_migration',
156 *list(cmb))
159 if __name__ == '__main__':
160 iotests.main(supported_fmts=['qcow2'])