4 # Copyright (C) 2020 Red Hat, Inc.
6 # Tests for dirty bitmaps migration with node aliases
8 # This program is free software; you can redistribute it and/or modify
9 # it under the terms of the GNU General Public License as published by
10 # the Free Software Foundation; either version 2 of the License, or
11 # (at your option) any later version.
13 # This program is distributed in the hope that it will be useful,
14 # but WITHOUT ANY WARRANTY; without even the implied warranty of
15 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 # GNU General Public License for more details.
18 # You should have received a copy of the GNU General Public License
19 # along with this program. If not, see <http://www.gnu.org/licenses/>.
25 from typing import Dict, List, Optional, Union
29 BlockBitmapMapping = List[Dict[str, Union[str, List[Dict[str, str]]]]]
31 assert iotests.sock_dir is not None
32 mig_sock = os.path.join(iotests.sock_dir, 'mig_sock')
35 class TestDirtyBitmapMigration(iotests.QMPTestCase):
36 src_node_name: str = ''
37 dst_node_name: str = ''
38 src_bmap_name: str = ''
39 dst_bmap_name: str = ''
41 def setUp(self) -> None:
42 self.vm_a = iotests.VM(path_suffix='-a')
43 self.vm_a.add_blockdev(f'node-name={self.src_node_name},'
47 self.vm_b = iotests.VM(path_suffix='-b')
48 self.vm_b.add_blockdev(f'node-name={self.dst_node_name},'
50 self.vm_b.add_incoming(f'unix:{mig_sock}')
53 result = self.vm_a.qmp('block-dirty-bitmap-add',
54 node=self.src_node_name,
55 name=self.src_bmap_name)
56 self.assert_qmp(result, 'return', {})
58 # Dirty some random megabytes
60 mb_ofs = random.randrange(1024)
61 self.vm_a.hmp_qemu_io(self.src_node_name, f'discard {mb_ofs}M 1M')
63 result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256',
64 node=self.src_node_name,
65 name=self.src_bmap_name)
66 self.bitmap_hash_reference = result['return']['sha256']
68 caps = [{'capability': name, 'state': True}
69 for name in ('dirty-bitmaps', 'events')]
71 for vm in (self.vm_a, self.vm_b):
72 result = vm.qmp('migrate-set-capabilities', capabilities=caps)
73 self.assert_qmp(result, 'return', {})
75 def tearDown(self) -> None:
83 def check_bitmap(self, bitmap_name_valid: bool) -> None:
84 result = self.vm_b.qmp('x-debug-block-dirty-bitmap-sha256',
85 node=self.dst_node_name,
86 name=self.dst_bmap_name)
88 self.assert_qmp(result, 'return/sha256',
89 self.bitmap_hash_reference)
91 self.assert_qmp(result, 'error/desc',
92 f"Dirty bitmap '{self.dst_bmap_name}' not found")
94 def migrate(self, bitmap_name_valid: bool = True,
95 migration_success: bool = True) -> None:
96 result = self.vm_a.qmp('migrate', uri=f'unix:{mig_sock}')
97 self.assert_qmp(result, 'return', {})
99 with iotests.Timeout(5, 'Timeout waiting for migration to complete'):
100 self.assertEqual(self.vm_a.wait_migration('postmigrate'),
102 self.assertEqual(self.vm_b.wait_migration('running'),
105 if migration_success:
106 self.check_bitmap(bitmap_name_valid)
108 def verify_dest_error(self, msg: Optional[str]) -> None:
110 Check whether the given error message is present in vm_b's log.
111 (vm_b is shut down to do so.)
112 If @msg is None, check that there has not been any error.
116 self.assertNotIn('qemu-system-', self.vm_b.get_log())
118 self.assertIn(msg, self.vm_b.get_log())
121 def mapping(node_name: str, node_alias: str,
122 bitmap_name: str, bitmap_alias: str) -> BlockBitmapMapping:
124 'node-name': node_name,
128 'alias': bitmap_alias
132 def set_mapping(self, vm: iotests.VM, mapping: BlockBitmapMapping,
133 error: Optional[str] = None) -> None:
135 Invoke migrate-set-parameters on @vm to set the given @mapping.
136 Check for success if @error is None, or verify the error message
138 On success, verify that "info migrate_parameters" on HMP returns
139 our mapping. (Just to check its formatting code.)
141 result = vm.qmp('migrate-set-parameters',
142 block_bitmap_mapping=mapping)
145 self.assert_qmp(result, 'return', {})
147 result = vm.qmp('human-monitor-command',
148 command_line='info migrate_parameters')
150 m = re.search(r'^block-bitmap-mapping:\r?(\n .*)*\n',
151 result['return'], flags=re.MULTILINE)
152 hmp_mapping = m.group(0).replace('\r', '') if m else None
154 self.assertEqual(hmp_mapping, self.to_hmp_mapping(mapping))
156 self.assert_qmp(result, 'error/desc', error)
159 def to_hmp_mapping(mapping: BlockBitmapMapping) -> str:
160 result = 'block-bitmap-mapping:\n'
163 result += f" '{node['node-name']}' -> '{node['alias']}'\n"
165 assert isinstance(node['bitmaps'], list)
166 for bitmap in node['bitmaps']:
167 result += f" '{bitmap['name']}' -> '{bitmap['alias']}'\n"
172 class TestAliasMigration(TestDirtyBitmapMigration):
173 src_node_name = 'node0'
174 dst_node_name = 'node0'
175 src_bmap_name = 'bmap0'
176 dst_bmap_name = 'bmap0'
178 def test_migration_without_alias(self) -> None:
179 self.migrate(self.src_node_name == self.dst_node_name and
180 self.src_bmap_name == self.dst_bmap_name)
182 # Check for error message on the destination
183 if self.src_node_name != self.dst_node_name:
184 self.verify_dest_error(f"Cannot find "
185 f"device={self.src_node_name} nor "
186 f"node_name={self.src_node_name}")
188 self.verify_dest_error(None)
190 def test_alias_on_src_migration(self) -> None:
191 mapping = self.mapping(self.src_node_name, self.dst_node_name,
192 self.src_bmap_name, self.dst_bmap_name)
194 self.set_mapping(self.vm_a, mapping)
196 self.verify_dest_error(None)
198 def test_alias_on_dst_migration(self) -> None:
199 mapping = self.mapping(self.dst_node_name, self.src_node_name,
200 self.dst_bmap_name, self.src_bmap_name)
202 self.set_mapping(self.vm_b, mapping)
204 self.verify_dest_error(None)
206 def test_alias_on_both_migration(self) -> None:
207 src_map = self.mapping(self.src_node_name, 'node-alias',
208 self.src_bmap_name, 'bmap-alias')
210 dst_map = self.mapping(self.dst_node_name, 'node-alias',
211 self.dst_bmap_name, 'bmap-alias')
213 self.set_mapping(self.vm_a, src_map)
214 self.set_mapping(self.vm_b, dst_map)
216 self.verify_dest_error(None)
219 class TestNodeAliasMigration(TestAliasMigration):
220 src_node_name = 'node-src'
221 dst_node_name = 'node-dst'
224 class TestBitmapAliasMigration(TestAliasMigration):
225 src_bmap_name = 'bmap-src'
226 dst_bmap_name = 'bmap-dst'
229 class TestFullAliasMigration(TestAliasMigration):
230 src_node_name = 'node-src'
231 dst_node_name = 'node-dst'
232 src_bmap_name = 'bmap-src'
233 dst_bmap_name = 'bmap-dst'
236 class TestLongBitmapNames(TestAliasMigration):
237 # Giving long bitmap names is OK, as long as there is a short alias for
239 src_bmap_name = 'a' * 512
240 dst_bmap_name = 'b' * 512
242 # Skip all tests that do not use the intermediate alias
243 def test_migration_without_alias(self) -> None:
246 def test_alias_on_src_migration(self) -> None:
249 def test_alias_on_dst_migration(self) -> None:
253 class TestBlockBitmapMappingErrors(TestDirtyBitmapMigration):
254 src_node_name = 'node0'
255 dst_node_name = 'node0'
256 src_bmap_name = 'bmap0'
257 dst_bmap_name = 'bmap0'
260 Note that mapping nodes or bitmaps that do not exist is not an error.
263 def test_non_injective_node_mapping(self) -> None:
264 mapping: BlockBitmapMapping = [
266 'node-name': 'node0',
267 'alias': 'common-alias',
270 'alias': 'bmap-alias0'
274 'node-name': 'node1',
275 'alias': 'common-alias',
278 'alias': 'bmap-alias1'
283 self.set_mapping(self.vm_a, mapping,
284 "Invalid mapping given for block-bitmap-mapping: "
285 "The node alias 'common-alias' is used twice")
287 def test_non_injective_bitmap_mapping(self) -> None:
288 mapping: BlockBitmapMapping = [{
289 'node-name': 'node0',
290 'alias': 'node-alias0',
294 'alias': 'common-alias'
298 'alias': 'common-alias'
303 self.set_mapping(self.vm_a, mapping,
304 "Invalid mapping given for block-bitmap-mapping: "
305 "The bitmap alias 'node-alias0'/'common-alias' is "
308 def test_ambiguous_node_mapping(self) -> None:
309 mapping: BlockBitmapMapping = [
311 'node-name': 'node0',
312 'alias': 'node-alias0',
315 'alias': 'bmap-alias0'
319 'node-name': 'node0',
320 'alias': 'node-alias1',
323 'alias': 'bmap-alias0'
328 self.set_mapping(self.vm_a, mapping,
329 "Invalid mapping given for block-bitmap-mapping: "
330 "The node name 'node0' is mapped twice")
332 def test_ambiguous_bitmap_mapping(self) -> None:
333 mapping: BlockBitmapMapping = [{
334 'node-name': 'node0',
335 'alias': 'node-alias0',
339 'alias': 'bmap-alias0'
343 'alias': 'bmap-alias1'
348 self.set_mapping(self.vm_a, mapping,
349 "Invalid mapping given for block-bitmap-mapping: "
350 "The bitmap 'node0'/'bmap0' is mapped twice")
352 def test_migratee_node_is_not_mapped_on_src(self) -> None:
353 self.set_mapping(self.vm_a, [])
354 # Should just ignore all bitmaps on unmapped nodes
356 self.verify_dest_error(None)
358 def test_migratee_node_is_not_mapped_on_dst(self) -> None:
359 self.set_mapping(self.vm_b, [])
361 self.verify_dest_error(f"Unknown node alias '{self.src_node_name}'")
363 def test_migratee_bitmap_is_not_mapped_on_src(self) -> None:
364 mapping: BlockBitmapMapping = [{
365 'node-name': self.src_node_name,
366 'alias': self.dst_node_name,
370 self.set_mapping(self.vm_a, mapping)
371 # Should just ignore all unmapped bitmaps
373 self.verify_dest_error(None)
375 def test_migratee_bitmap_is_not_mapped_on_dst(self) -> None:
376 mapping: BlockBitmapMapping = [{
377 'node-name': self.dst_node_name,
378 'alias': self.src_node_name,
382 self.set_mapping(self.vm_b, mapping)
384 self.verify_dest_error(f"Unknown bitmap alias "
385 f"'{self.src_bmap_name}' "
386 f"on node '{self.dst_node_name}' "
387 f"(alias '{self.src_node_name}')")
389 def test_unused_mapping_on_dst(self) -> None:
390 # Let the source not send any bitmaps
391 self.set_mapping(self.vm_a, [])
393 # Establish some mapping on the destination
394 self.set_mapping(self.vm_b, [])
396 # The fact that there is a mapping on B without any bitmaps
397 # being received should be fine, not fatal
399 self.verify_dest_error(None)
401 def test_non_wellformed_node_alias(self) -> None:
404 mapping: BlockBitmapMapping = [{
405 'node-name': self.src_node_name,
410 self.set_mapping(self.vm_a, mapping,
411 f"Invalid mapping given for block-bitmap-mapping: "
412 f"The node alias '{alias}' is not well-formed")
414 def test_node_alias_too_long(self) -> None:
417 mapping: BlockBitmapMapping = [{
418 'node-name': self.src_node_name,
423 self.set_mapping(self.vm_a, mapping,
424 f"Invalid mapping given for block-bitmap-mapping: "
425 f"The node alias '{alias}' is longer than 255 bytes")
427 def test_bitmap_alias_too_long(self) -> None:
430 mapping = self.mapping(self.src_node_name, self.dst_node_name,
431 self.src_bmap_name, alias)
433 self.set_mapping(self.vm_a, mapping,
434 f"Invalid mapping given for block-bitmap-mapping: "
435 f"The bitmap alias '{alias}' is longer than 255 "
438 def test_bitmap_name_too_long(self) -> None:
441 result = self.vm_a.qmp('block-dirty-bitmap-add',
442 node=self.src_node_name,
444 self.assert_qmp(result, 'return', {})
446 self.migrate(False, False)
448 # Check for the error in the source's log
450 self.assertIn(f"Cannot migrate bitmap '{name}' on node "
451 f"'{self.src_node_name}': Name is longer than 255 bytes",
454 # Expect abnormal shutdown of the destination VM because of
455 # the failed migration
458 except qemu.machine.AbnormalShutdown:
461 def test_aliased_bitmap_name_too_long(self) -> None:
462 # Longer than the maximum for bitmap names
463 self.dst_bmap_name = 'a' * 1024
465 mapping = self.mapping(self.dst_node_name, self.src_node_name,
466 self.dst_bmap_name, self.src_bmap_name)
468 # We would have to create this bitmap during migration, and
469 # that would fail, because the name is too long. Better to
471 self.set_mapping(self.vm_b, mapping,
472 f"Invalid mapping given for block-bitmap-mapping: "
473 f"The bitmap name '{self.dst_bmap_name}' is longer "
476 def test_node_name_too_long(self) -> None:
477 # Longer than the maximum for node names
478 self.dst_node_name = 'a' * 32
480 mapping = self.mapping(self.dst_node_name, self.src_node_name,
481 self.dst_bmap_name, self.src_bmap_name)
483 # During migration, this would appear simply as a node that
484 # cannot be found. Still better to catch impossible node
485 # names early (similar to test_non_wellformed_node_alias).
486 self.set_mapping(self.vm_b, mapping,
487 f"Invalid mapping given for block-bitmap-mapping: "
488 f"The node name '{self.dst_node_name}' is longer "
492 class TestCrossAliasMigration(TestDirtyBitmapMigration):
494 Swap aliases, both to see that qemu does not get confused, and
495 that we can migrate multiple things at once.
498 node-a.bmap-a -> node-b.bmap-b
499 node-a.bmap-b -> node-b.bmap-a
500 node-b.bmap-a -> node-a.bmap-b
501 node-b.bmap-b -> node-a.bmap-a
504 src_node_name = 'node-a'
505 dst_node_name = 'node-b'
506 src_bmap_name = 'bmap-a'
507 dst_bmap_name = 'bmap-b'
509 def setUp(self) -> None:
510 TestDirtyBitmapMigration.setUp(self)
512 # Now create another block device and let both have two bitmaps each
513 result = self.vm_a.qmp('blockdev-add',
514 node_name='node-b', driver='null-co')
515 self.assert_qmp(result, 'return', {})
517 result = self.vm_b.qmp('blockdev-add',
518 node_name='node-a', driver='null-co')
519 self.assert_qmp(result, 'return', {})
521 bmaps_to_add = (('node-a', 'bmap-b'),
522 ('node-b', 'bmap-a'),
523 ('node-b', 'bmap-b'))
525 for (node, bmap) in bmaps_to_add:
526 result = self.vm_a.qmp('block-dirty-bitmap-add',
527 node=node, name=bmap)
528 self.assert_qmp(result, 'return', {})
531 def cross_mapping() -> BlockBitmapMapping:
534 'node-name': 'node-a',
548 'node-name': 'node-b',
563 def verify_dest_has_all_bitmaps(self) -> None:
564 bitmaps = self.vm_b.query_bitmaps()
566 # Extract and sort bitmap names
568 bitmaps[node] = sorted((bmap['name'] for bmap in bitmaps[node]))
570 self.assertEqual(bitmaps,
571 {'node-a': ['bmap-a', 'bmap-b'],
572 'node-b': ['bmap-a', 'bmap-b']})
574 def test_alias_on_src(self) -> None:
575 self.set_mapping(self.vm_a, self.cross_mapping())
577 # Checks that node-a.bmap-a was migrated to node-b.bmap-b, and
580 self.verify_dest_has_all_bitmaps()
581 self.verify_dest_error(None)
583 def test_alias_on_dst(self) -> None:
584 self.set_mapping(self.vm_b, self.cross_mapping())
586 # Checks that node-a.bmap-a was migrated to node-b.bmap-b, and
589 self.verify_dest_has_all_bitmaps()
590 self.verify_dest_error(None)
593 if __name__ == '__main__':
594 iotests.main(supported_protocols=['file'])