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
30 BlockBitmapMapping = List[Dict[str, object]]
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 self.vm_a.cmd('block-dirty-bitmap-add',
54 node=self.src_node_name,
55 name=self.src_bmap_name)
57 # Dirty some random megabytes
59 mb_ofs = random.randrange(1024)
60 self.vm_a.hmp_qemu_io(self.src_node_name, f'discard {mb_ofs}M 1M')
62 result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256',
63 node=self.src_node_name,
64 name=self.src_bmap_name)
65 self.bitmap_hash_reference = result['return']['sha256']
67 caps = [{'capability': name, 'state': True}
68 for name in ('dirty-bitmaps', 'events')]
70 for vm in (self.vm_a, self.vm_b):
71 vm.cmd('migrate-set-capabilities', capabilities=caps)
73 def tearDown(self) -> None:
81 def check_bitmap(self, bitmap_name_valid: bool) -> None:
82 result = self.vm_b.qmp('x-debug-block-dirty-bitmap-sha256',
83 node=self.dst_node_name,
84 name=self.dst_bmap_name)
86 self.assert_qmp(result, 'return/sha256',
87 self.bitmap_hash_reference)
89 self.assert_qmp(result, 'error/desc',
90 f"Dirty bitmap '{self.dst_bmap_name}' not found")
92 def migrate(self, bitmap_name_valid: bool = True,
93 migration_success: bool = True) -> None:
94 self.vm_a.cmd('migrate', uri=f'unix:{mig_sock}')
96 with iotests.Timeout(5, 'Timeout waiting for migration to complete'):
97 self.assertEqual(self.vm_a.wait_migration('postmigrate'),
99 self.assertEqual(self.vm_b.wait_migration('running'),
102 if migration_success:
103 self.check_bitmap(bitmap_name_valid)
105 def verify_dest_error(self, msg: Optional[str]) -> None:
107 Check whether the given error message is present in vm_b's log.
108 (vm_b is shut down to do so.)
109 If @msg is None, check that there has not been any error.
113 log = self.vm_b.get_log()
114 assert log is not None # Loaded after shutdown
117 self.assertNotIn('qemu-system-', log)
119 self.assertIn(msg, log)
122 def mapping(node_name: str, node_alias: str,
123 bitmap_name: str, bitmap_alias: str) -> BlockBitmapMapping:
125 'node-name': node_name,
129 'alias': bitmap_alias
133 def set_mapping(self, vm: iotests.VM, mapping: BlockBitmapMapping,
134 error: Optional[str] = None) -> None:
136 Invoke migrate-set-parameters on @vm to set the given @mapping.
137 Check for success if @error is None, or verify the error message
139 On success, verify that "info migrate_parameters" on HMP returns
140 our mapping. (Just to check its formatting code.)
142 result = vm.qmp('migrate-set-parameters',
143 block_bitmap_mapping=mapping)
146 self.assert_qmp(result, 'return', {})
148 result = vm.qmp('human-monitor-command',
149 command_line='info migrate_parameters')
151 m = re.search(r'^block-bitmap-mapping:\r?(\n .*)*\n',
152 result['return'], flags=re.MULTILINE)
153 hmp_mapping = m.group(0).replace('\r', '') if m else None
155 self.assertEqual(hmp_mapping, self.to_hmp_mapping(mapping))
157 self.assert_qmp(result, 'error/desc', error)
160 def to_hmp_mapping(mapping: BlockBitmapMapping) -> str:
161 result = 'block-bitmap-mapping:\n'
164 result += f" '{node['node-name']}' -> '{node['alias']}'\n"
166 assert isinstance(node['bitmaps'], list)
167 for bitmap in node['bitmaps']:
168 result += f" '{bitmap['name']}' -> '{bitmap['alias']}'\n"
173 class TestAliasMigration(TestDirtyBitmapMigration):
174 src_node_name = 'node0'
175 dst_node_name = 'node0'
176 src_bmap_name = 'bmap0'
177 dst_bmap_name = 'bmap0'
179 def test_migration_without_alias(self) -> None:
180 self.migrate(self.src_node_name == self.dst_node_name and
181 self.src_bmap_name == self.dst_bmap_name)
183 # Check for error message on the destination
184 if self.src_node_name != self.dst_node_name:
185 self.verify_dest_error(f"Cannot find "
186 f"device='{self.src_node_name}' nor "
187 f"node-name='{self.src_node_name}'")
189 self.verify_dest_error(None)
191 def test_alias_on_src_migration(self) -> None:
192 mapping = self.mapping(self.src_node_name, self.dst_node_name,
193 self.src_bmap_name, self.dst_bmap_name)
195 self.set_mapping(self.vm_a, mapping)
197 self.verify_dest_error(None)
199 def test_alias_on_dst_migration(self) -> None:
200 mapping = self.mapping(self.dst_node_name, self.src_node_name,
201 self.dst_bmap_name, self.src_bmap_name)
203 self.set_mapping(self.vm_b, mapping)
205 self.verify_dest_error(None)
207 def test_alias_on_both_migration(self) -> None:
208 src_map = self.mapping(self.src_node_name, 'node-alias',
209 self.src_bmap_name, 'bmap-alias')
211 dst_map = self.mapping(self.dst_node_name, 'node-alias',
212 self.dst_bmap_name, 'bmap-alias')
214 self.set_mapping(self.vm_a, src_map)
215 self.set_mapping(self.vm_b, dst_map)
217 self.verify_dest_error(None)
220 class TestNodeAliasMigration(TestAliasMigration):
221 src_node_name = 'node-src'
222 dst_node_name = 'node-dst'
225 class TestBitmapAliasMigration(TestAliasMigration):
226 src_bmap_name = 'bmap-src'
227 dst_bmap_name = 'bmap-dst'
230 class TestFullAliasMigration(TestAliasMigration):
231 src_node_name = 'node-src'
232 dst_node_name = 'node-dst'
233 src_bmap_name = 'bmap-src'
234 dst_bmap_name = 'bmap-dst'
237 class TestLongBitmapNames(TestAliasMigration):
238 # Giving long bitmap names is OK, as long as there is a short alias for
240 src_bmap_name = 'a' * 512
241 dst_bmap_name = 'b' * 512
243 # Skip all tests that do not use the intermediate alias
244 def test_migration_without_alias(self) -> None:
247 def test_alias_on_src_migration(self) -> None:
250 def test_alias_on_dst_migration(self) -> None:
254 class TestBlockBitmapMappingErrors(TestDirtyBitmapMigration):
255 src_node_name = 'node0'
256 dst_node_name = 'node0'
257 src_bmap_name = 'bmap0'
258 dst_bmap_name = 'bmap0'
261 Note that mapping nodes or bitmaps that do not exist is not an error.
264 def test_non_injective_node_mapping(self) -> None:
265 mapping: BlockBitmapMapping = [
267 'node-name': 'node0',
268 'alias': 'common-alias',
271 'alias': 'bmap-alias0'
275 'node-name': 'node1',
276 'alias': 'common-alias',
279 'alias': 'bmap-alias1'
284 self.set_mapping(self.vm_a, mapping,
285 "Invalid mapping given for block-bitmap-mapping: "
286 "The node alias 'common-alias' is used twice")
288 def test_non_injective_bitmap_mapping(self) -> None:
289 mapping: BlockBitmapMapping = [{
290 'node-name': 'node0',
291 'alias': 'node-alias0',
295 'alias': 'common-alias'
299 'alias': 'common-alias'
304 self.set_mapping(self.vm_a, mapping,
305 "Invalid mapping given for block-bitmap-mapping: "
306 "The bitmap alias 'node-alias0'/'common-alias' is "
309 def test_ambiguous_node_mapping(self) -> None:
310 mapping: BlockBitmapMapping = [
312 'node-name': 'node0',
313 'alias': 'node-alias0',
316 'alias': 'bmap-alias0'
320 'node-name': 'node0',
321 'alias': 'node-alias1',
324 'alias': 'bmap-alias0'
329 self.set_mapping(self.vm_a, mapping,
330 "Invalid mapping given for block-bitmap-mapping: "
331 "The node name 'node0' is mapped twice")
333 def test_ambiguous_bitmap_mapping(self) -> None:
334 mapping: BlockBitmapMapping = [{
335 'node-name': 'node0',
336 'alias': 'node-alias0',
340 'alias': 'bmap-alias0'
344 'alias': 'bmap-alias1'
349 self.set_mapping(self.vm_a, mapping,
350 "Invalid mapping given for block-bitmap-mapping: "
351 "The bitmap 'node0'/'bmap0' is mapped twice")
353 def test_migratee_node_is_not_mapped_on_src(self) -> None:
354 self.set_mapping(self.vm_a, [])
355 # Should just ignore all bitmaps on unmapped nodes
357 self.verify_dest_error(None)
359 def test_migratee_node_is_not_mapped_on_dst(self) -> None:
360 self.set_mapping(self.vm_b, [])
362 self.verify_dest_error(f"Unknown node alias '{self.src_node_name}'")
364 def test_migratee_bitmap_is_not_mapped_on_src(self) -> None:
365 mapping: BlockBitmapMapping = [{
366 'node-name': self.src_node_name,
367 'alias': self.dst_node_name,
371 self.set_mapping(self.vm_a, mapping)
372 # Should just ignore all unmapped bitmaps
374 self.verify_dest_error(None)
376 def test_migratee_bitmap_is_not_mapped_on_dst(self) -> None:
377 mapping: BlockBitmapMapping = [{
378 'node-name': self.dst_node_name,
379 'alias': self.src_node_name,
383 self.set_mapping(self.vm_b, mapping)
385 self.verify_dest_error(f"Unknown bitmap alias "
386 f"'{self.src_bmap_name}' "
387 f"on node '{self.dst_node_name}' "
388 f"(alias '{self.src_node_name}')")
390 def test_unused_mapping_on_dst(self) -> None:
391 # Let the source not send any bitmaps
392 self.set_mapping(self.vm_a, [])
394 # Establish some mapping on the destination
395 self.set_mapping(self.vm_b, [])
397 # The fact that there is a mapping on B without any bitmaps
398 # being received should be fine, not fatal
400 self.verify_dest_error(None)
402 def test_non_wellformed_node_alias(self) -> None:
405 mapping: BlockBitmapMapping = [{
406 'node-name': self.src_node_name,
411 self.set_mapping(self.vm_a, mapping,
412 f"Invalid mapping given for block-bitmap-mapping: "
413 f"The node alias '{alias}' is not well-formed")
415 def test_node_alias_too_long(self) -> None:
418 mapping: BlockBitmapMapping = [{
419 'node-name': self.src_node_name,
424 self.set_mapping(self.vm_a, mapping,
425 f"Invalid mapping given for block-bitmap-mapping: "
426 f"The node alias '{alias}' is longer than 255 bytes")
428 def test_bitmap_alias_too_long(self) -> None:
431 mapping = self.mapping(self.src_node_name, self.dst_node_name,
432 self.src_bmap_name, alias)
434 self.set_mapping(self.vm_a, mapping,
435 f"Invalid mapping given for block-bitmap-mapping: "
436 f"The bitmap alias '{alias}' is longer than 255 "
439 def test_bitmap_name_too_long(self) -> None:
442 self.vm_a.cmd('block-dirty-bitmap-add',
443 node=self.src_node_name,
446 self.migrate(False, False)
448 # Check for the error in the source's log
451 log = self.vm_a.get_log()
452 assert log is not None # Loaded after shutdown
454 self.assertIn(f"Cannot migrate bitmap '{name}' on node "
455 f"'{self.src_node_name}': Name is longer than 255 bytes",
458 # Destination VM will terminate w/ error of its own accord
459 # due to the failed migration.
461 rc = self.vm_b.exitcode()
462 assert rc is not None and rc > 0
464 def test_aliased_bitmap_name_too_long(self) -> None:
465 # Longer than the maximum for bitmap names
466 self.dst_bmap_name = 'a' * 1024
468 mapping = self.mapping(self.dst_node_name, self.src_node_name,
469 self.dst_bmap_name, self.src_bmap_name)
471 # We would have to create this bitmap during migration, and
472 # that would fail, because the name is too long. Better to
474 self.set_mapping(self.vm_b, mapping,
475 f"Invalid mapping given for block-bitmap-mapping: "
476 f"The bitmap name '{self.dst_bmap_name}' is longer "
479 def test_node_name_too_long(self) -> None:
480 # Longer than the maximum for node names
481 self.dst_node_name = 'a' * 32
483 mapping = self.mapping(self.dst_node_name, self.src_node_name,
484 self.dst_bmap_name, self.src_bmap_name)
486 # During migration, this would appear simply as a node that
487 # cannot be found. Still better to catch impossible node
488 # names early (similar to test_non_wellformed_node_alias).
489 self.set_mapping(self.vm_b, mapping,
490 f"Invalid mapping given for block-bitmap-mapping: "
491 f"The node name '{self.dst_node_name}' is longer "
495 class TestCrossAliasMigration(TestDirtyBitmapMigration):
497 Swap aliases, both to see that qemu does not get confused, and
498 that we can migrate multiple things at once.
501 node-a.bmap-a -> node-b.bmap-b
502 node-a.bmap-b -> node-b.bmap-a
503 node-b.bmap-a -> node-a.bmap-b
504 node-b.bmap-b -> node-a.bmap-a
507 src_node_name = 'node-a'
508 dst_node_name = 'node-b'
509 src_bmap_name = 'bmap-a'
510 dst_bmap_name = 'bmap-b'
512 def setUp(self) -> None:
513 TestDirtyBitmapMigration.setUp(self)
515 # Now create another block device and let both have two bitmaps each
516 self.vm_a.cmd('blockdev-add',
517 node_name='node-b', driver='null-co')
519 self.vm_b.cmd('blockdev-add',
520 node_name='node-a', driver='null-co')
522 bmaps_to_add = (('node-a', 'bmap-b'),
523 ('node-b', 'bmap-a'),
524 ('node-b', 'bmap-b'))
526 for (node, bmap) in bmaps_to_add:
527 self.vm_a.cmd('block-dirty-bitmap-add',
528 node=node, name=bmap)
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)
592 class TestAliasTransformMigration(TestDirtyBitmapMigration):
594 Tests the 'transform' option which modifies bitmap persistence on
598 src_node_name = 'node-a'
599 dst_node_name = 'node-b'
600 src_bmap_name = 'bmap-a'
601 dst_bmap_name = 'bmap-b'
603 def setUp(self) -> None:
604 TestDirtyBitmapMigration.setUp(self)
606 # Now create another block device and let both have two bitmaps each
607 self.vm_a.cmd('blockdev-add',
608 node_name='node-b', driver='null-co',
611 self.vm_b.cmd('blockdev-add',
612 node_name='node-a', driver='null-co',
615 bmaps_to_add = (('node-a', 'bmap-b'),
616 ('node-b', 'bmap-a'),
617 ('node-b', 'bmap-b'))
619 for (node, bmap) in bmaps_to_add:
620 self.vm_a.cmd('block-dirty-bitmap-add',
621 node=node, name=bmap)
624 def transform_mapping() -> BlockBitmapMapping:
627 'node-name': 'node-a',
645 'node-name': 'node-b',
660 def verify_dest_bitmap_state(self) -> None:
661 bitmaps = self.vm_b.query_bitmaps()
664 bitmaps[node] = sorted(((bmap['name'], bmap['persistent'])
665 for bmap in bitmaps[node]))
667 self.assertEqual(bitmaps,
668 {'node-a': [('bmap-a', True), ('bmap-b', False)],
669 'node-b': [('bmap-a', False), ('bmap-b', False)]})
671 def test_transform_on_src(self) -> None:
672 self.set_mapping(self.vm_a, self.transform_mapping())
675 self.verify_dest_bitmap_state()
676 self.verify_dest_error(None)
678 def test_transform_on_dst(self) -> None:
679 self.set_mapping(self.vm_b, self.transform_mapping())
682 self.verify_dest_bitmap_state()
683 self.verify_dest_error(None)
685 if __name__ == '__main__':
686 iotests.main(supported_protocols=['file'])