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
29 # Import qemu after iotests.py has amended sys.path
30 # pylint: disable=wrong-import-order
31 from qemu.machine import machine
33 BlockBitmapMapping = List[Dict[str, object]]
35 mig_sock = os.path.join(iotests.sock_dir, 'mig_sock')
38 class TestDirtyBitmapMigration(iotests.QMPTestCase):
39 src_node_name: str = ''
40 dst_node_name: str = ''
41 src_bmap_name: str = ''
42 dst_bmap_name: str = ''
44 def setUp(self) -> None:
45 self.vm_a = iotests.VM(path_suffix='-a')
46 self.vm_a.add_blockdev(f'node-name={self.src_node_name},'
50 self.vm_b = iotests.VM(path_suffix='-b')
51 self.vm_b.add_blockdev(f'node-name={self.dst_node_name},'
53 self.vm_b.add_incoming(f'unix:{mig_sock}')
56 result = self.vm_a.qmp('block-dirty-bitmap-add',
57 node=self.src_node_name,
58 name=self.src_bmap_name)
59 self.assert_qmp(result, 'return', {})
61 # Dirty some random megabytes
63 mb_ofs = random.randrange(1024)
64 self.vm_a.hmp_qemu_io(self.src_node_name, f'discard {mb_ofs}M 1M')
66 result = self.vm_a.qmp('x-debug-block-dirty-bitmap-sha256',
67 node=self.src_node_name,
68 name=self.src_bmap_name)
69 self.bitmap_hash_reference = result['return']['sha256']
71 caps = [{'capability': name, 'state': True}
72 for name in ('dirty-bitmaps', 'events')]
74 for vm in (self.vm_a, self.vm_b):
75 result = vm.qmp('migrate-set-capabilities', capabilities=caps)
76 self.assert_qmp(result, 'return', {})
78 def tearDown(self) -> None:
86 def check_bitmap(self, bitmap_name_valid: bool) -> None:
87 result = self.vm_b.qmp('x-debug-block-dirty-bitmap-sha256',
88 node=self.dst_node_name,
89 name=self.dst_bmap_name)
91 self.assert_qmp(result, 'return/sha256',
92 self.bitmap_hash_reference)
94 self.assert_qmp(result, 'error/desc',
95 f"Dirty bitmap '{self.dst_bmap_name}' not found")
97 def migrate(self, bitmap_name_valid: bool = True,
98 migration_success: bool = True) -> None:
99 result = self.vm_a.qmp('migrate', uri=f'unix:{mig_sock}')
100 self.assert_qmp(result, 'return', {})
102 with iotests.Timeout(5, 'Timeout waiting for migration to complete'):
103 self.assertEqual(self.vm_a.wait_migration('postmigrate'),
105 self.assertEqual(self.vm_b.wait_migration('running'),
108 if migration_success:
109 self.check_bitmap(bitmap_name_valid)
111 def verify_dest_error(self, msg: Optional[str]) -> None:
113 Check whether the given error message is present in vm_b's log.
114 (vm_b is shut down to do so.)
115 If @msg is None, check that there has not been any error.
119 log = self.vm_b.get_log()
120 assert log is not None # Loaded after shutdown
123 self.assertNotIn('qemu-system-', log)
125 self.assertIn(msg, log)
128 def mapping(node_name: str, node_alias: str,
129 bitmap_name: str, bitmap_alias: str) -> BlockBitmapMapping:
131 'node-name': node_name,
135 'alias': bitmap_alias
139 def set_mapping(self, vm: iotests.VM, mapping: BlockBitmapMapping,
140 error: Optional[str] = None) -> None:
142 Invoke migrate-set-parameters on @vm to set the given @mapping.
143 Check for success if @error is None, or verify the error message
145 On success, verify that "info migrate_parameters" on HMP returns
146 our mapping. (Just to check its formatting code.)
148 result = vm.qmp('migrate-set-parameters',
149 block_bitmap_mapping=mapping)
152 self.assert_qmp(result, 'return', {})
154 result = vm.qmp('human-monitor-command',
155 command_line='info migrate_parameters')
157 m = re.search(r'^block-bitmap-mapping:\r?(\n .*)*\n',
158 result['return'], flags=re.MULTILINE)
159 hmp_mapping = m.group(0).replace('\r', '') if m else None
161 self.assertEqual(hmp_mapping, self.to_hmp_mapping(mapping))
163 self.assert_qmp(result, 'error/desc', error)
166 def to_hmp_mapping(mapping: BlockBitmapMapping) -> str:
167 result = 'block-bitmap-mapping:\n'
170 result += f" '{node['node-name']}' -> '{node['alias']}'\n"
172 assert isinstance(node['bitmaps'], list)
173 for bitmap in node['bitmaps']:
174 result += f" '{bitmap['name']}' -> '{bitmap['alias']}'\n"
179 class TestAliasMigration(TestDirtyBitmapMigration):
180 src_node_name = 'node0'
181 dst_node_name = 'node0'
182 src_bmap_name = 'bmap0'
183 dst_bmap_name = 'bmap0'
185 def test_migration_without_alias(self) -> None:
186 self.migrate(self.src_node_name == self.dst_node_name and
187 self.src_bmap_name == self.dst_bmap_name)
189 # Check for error message on the destination
190 if self.src_node_name != self.dst_node_name:
191 self.verify_dest_error(f"Cannot find "
192 f"device='{self.src_node_name}' nor "
193 f"node-name='{self.src_node_name}'")
195 self.verify_dest_error(None)
197 def test_alias_on_src_migration(self) -> None:
198 mapping = self.mapping(self.src_node_name, self.dst_node_name,
199 self.src_bmap_name, self.dst_bmap_name)
201 self.set_mapping(self.vm_a, mapping)
203 self.verify_dest_error(None)
205 def test_alias_on_dst_migration(self) -> None:
206 mapping = self.mapping(self.dst_node_name, self.src_node_name,
207 self.dst_bmap_name, self.src_bmap_name)
209 self.set_mapping(self.vm_b, mapping)
211 self.verify_dest_error(None)
213 def test_alias_on_both_migration(self) -> None:
214 src_map = self.mapping(self.src_node_name, 'node-alias',
215 self.src_bmap_name, 'bmap-alias')
217 dst_map = self.mapping(self.dst_node_name, 'node-alias',
218 self.dst_bmap_name, 'bmap-alias')
220 self.set_mapping(self.vm_a, src_map)
221 self.set_mapping(self.vm_b, dst_map)
223 self.verify_dest_error(None)
226 class TestNodeAliasMigration(TestAliasMigration):
227 src_node_name = 'node-src'
228 dst_node_name = 'node-dst'
231 class TestBitmapAliasMigration(TestAliasMigration):
232 src_bmap_name = 'bmap-src'
233 dst_bmap_name = 'bmap-dst'
236 class TestFullAliasMigration(TestAliasMigration):
237 src_node_name = 'node-src'
238 dst_node_name = 'node-dst'
239 src_bmap_name = 'bmap-src'
240 dst_bmap_name = 'bmap-dst'
243 class TestLongBitmapNames(TestAliasMigration):
244 # Giving long bitmap names is OK, as long as there is a short alias for
246 src_bmap_name = 'a' * 512
247 dst_bmap_name = 'b' * 512
249 # Skip all tests that do not use the intermediate alias
250 def test_migration_without_alias(self) -> None:
253 def test_alias_on_src_migration(self) -> None:
256 def test_alias_on_dst_migration(self) -> None:
260 class TestBlockBitmapMappingErrors(TestDirtyBitmapMigration):
261 src_node_name = 'node0'
262 dst_node_name = 'node0'
263 src_bmap_name = 'bmap0'
264 dst_bmap_name = 'bmap0'
267 Note that mapping nodes or bitmaps that do not exist is not an error.
270 def test_non_injective_node_mapping(self) -> None:
271 mapping: BlockBitmapMapping = [
273 'node-name': 'node0',
274 'alias': 'common-alias',
277 'alias': 'bmap-alias0'
281 'node-name': 'node1',
282 'alias': 'common-alias',
285 'alias': 'bmap-alias1'
290 self.set_mapping(self.vm_a, mapping,
291 "Invalid mapping given for block-bitmap-mapping: "
292 "The node alias 'common-alias' is used twice")
294 def test_non_injective_bitmap_mapping(self) -> None:
295 mapping: BlockBitmapMapping = [{
296 'node-name': 'node0',
297 'alias': 'node-alias0',
301 'alias': 'common-alias'
305 'alias': 'common-alias'
310 self.set_mapping(self.vm_a, mapping,
311 "Invalid mapping given for block-bitmap-mapping: "
312 "The bitmap alias 'node-alias0'/'common-alias' is "
315 def test_ambiguous_node_mapping(self) -> None:
316 mapping: BlockBitmapMapping = [
318 'node-name': 'node0',
319 'alias': 'node-alias0',
322 'alias': 'bmap-alias0'
326 'node-name': 'node0',
327 'alias': 'node-alias1',
330 'alias': 'bmap-alias0'
335 self.set_mapping(self.vm_a, mapping,
336 "Invalid mapping given for block-bitmap-mapping: "
337 "The node name 'node0' is mapped twice")
339 def test_ambiguous_bitmap_mapping(self) -> None:
340 mapping: BlockBitmapMapping = [{
341 'node-name': 'node0',
342 'alias': 'node-alias0',
346 'alias': 'bmap-alias0'
350 'alias': 'bmap-alias1'
355 self.set_mapping(self.vm_a, mapping,
356 "Invalid mapping given for block-bitmap-mapping: "
357 "The bitmap 'node0'/'bmap0' is mapped twice")
359 def test_migratee_node_is_not_mapped_on_src(self) -> None:
360 self.set_mapping(self.vm_a, [])
361 # Should just ignore all bitmaps on unmapped nodes
363 self.verify_dest_error(None)
365 def test_migratee_node_is_not_mapped_on_dst(self) -> None:
366 self.set_mapping(self.vm_b, [])
368 self.verify_dest_error(f"Unknown node alias '{self.src_node_name}'")
370 def test_migratee_bitmap_is_not_mapped_on_src(self) -> None:
371 mapping: BlockBitmapMapping = [{
372 'node-name': self.src_node_name,
373 'alias': self.dst_node_name,
377 self.set_mapping(self.vm_a, mapping)
378 # Should just ignore all unmapped bitmaps
380 self.verify_dest_error(None)
382 def test_migratee_bitmap_is_not_mapped_on_dst(self) -> None:
383 mapping: BlockBitmapMapping = [{
384 'node-name': self.dst_node_name,
385 'alias': self.src_node_name,
389 self.set_mapping(self.vm_b, mapping)
391 self.verify_dest_error(f"Unknown bitmap alias "
392 f"'{self.src_bmap_name}' "
393 f"on node '{self.dst_node_name}' "
394 f"(alias '{self.src_node_name}')")
396 def test_unused_mapping_on_dst(self) -> None:
397 # Let the source not send any bitmaps
398 self.set_mapping(self.vm_a, [])
400 # Establish some mapping on the destination
401 self.set_mapping(self.vm_b, [])
403 # The fact that there is a mapping on B without any bitmaps
404 # being received should be fine, not fatal
406 self.verify_dest_error(None)
408 def test_non_wellformed_node_alias(self) -> None:
411 mapping: BlockBitmapMapping = [{
412 'node-name': self.src_node_name,
417 self.set_mapping(self.vm_a, mapping,
418 f"Invalid mapping given for block-bitmap-mapping: "
419 f"The node alias '{alias}' is not well-formed")
421 def test_node_alias_too_long(self) -> None:
424 mapping: BlockBitmapMapping = [{
425 'node-name': self.src_node_name,
430 self.set_mapping(self.vm_a, mapping,
431 f"Invalid mapping given for block-bitmap-mapping: "
432 f"The node alias '{alias}' is longer than 255 bytes")
434 def test_bitmap_alias_too_long(self) -> None:
437 mapping = self.mapping(self.src_node_name, self.dst_node_name,
438 self.src_bmap_name, alias)
440 self.set_mapping(self.vm_a, mapping,
441 f"Invalid mapping given for block-bitmap-mapping: "
442 f"The bitmap alias '{alias}' is longer than 255 "
445 def test_bitmap_name_too_long(self) -> None:
448 result = self.vm_a.qmp('block-dirty-bitmap-add',
449 node=self.src_node_name,
451 self.assert_qmp(result, 'return', {})
453 self.migrate(False, False)
455 # Check for the error in the source's log
458 log = self.vm_a.get_log()
459 assert log is not None # Loaded after shutdown
461 self.assertIn(f"Cannot migrate bitmap '{name}' on node "
462 f"'{self.src_node_name}': Name is longer than 255 bytes",
465 # Expect abnormal shutdown of the destination VM because of
466 # the failed migration
469 except machine.AbnormalShutdown:
472 def test_aliased_bitmap_name_too_long(self) -> None:
473 # Longer than the maximum for bitmap names
474 self.dst_bmap_name = 'a' * 1024
476 mapping = self.mapping(self.dst_node_name, self.src_node_name,
477 self.dst_bmap_name, self.src_bmap_name)
479 # We would have to create this bitmap during migration, and
480 # that would fail, because the name is too long. Better to
482 self.set_mapping(self.vm_b, mapping,
483 f"Invalid mapping given for block-bitmap-mapping: "
484 f"The bitmap name '{self.dst_bmap_name}' is longer "
487 def test_node_name_too_long(self) -> None:
488 # Longer than the maximum for node names
489 self.dst_node_name = 'a' * 32
491 mapping = self.mapping(self.dst_node_name, self.src_node_name,
492 self.dst_bmap_name, self.src_bmap_name)
494 # During migration, this would appear simply as a node that
495 # cannot be found. Still better to catch impossible node
496 # names early (similar to test_non_wellformed_node_alias).
497 self.set_mapping(self.vm_b, mapping,
498 f"Invalid mapping given for block-bitmap-mapping: "
499 f"The node name '{self.dst_node_name}' is longer "
503 class TestCrossAliasMigration(TestDirtyBitmapMigration):
505 Swap aliases, both to see that qemu does not get confused, and
506 that we can migrate multiple things at once.
509 node-a.bmap-a -> node-b.bmap-b
510 node-a.bmap-b -> node-b.bmap-a
511 node-b.bmap-a -> node-a.bmap-b
512 node-b.bmap-b -> node-a.bmap-a
515 src_node_name = 'node-a'
516 dst_node_name = 'node-b'
517 src_bmap_name = 'bmap-a'
518 dst_bmap_name = 'bmap-b'
520 def setUp(self) -> None:
521 TestDirtyBitmapMigration.setUp(self)
523 # Now create another block device and let both have two bitmaps each
524 result = self.vm_a.qmp('blockdev-add',
525 node_name='node-b', driver='null-co')
526 self.assert_qmp(result, 'return', {})
528 result = self.vm_b.qmp('blockdev-add',
529 node_name='node-a', driver='null-co')
530 self.assert_qmp(result, 'return', {})
532 bmaps_to_add = (('node-a', 'bmap-b'),
533 ('node-b', 'bmap-a'),
534 ('node-b', 'bmap-b'))
536 for (node, bmap) in bmaps_to_add:
537 result = self.vm_a.qmp('block-dirty-bitmap-add',
538 node=node, name=bmap)
539 self.assert_qmp(result, 'return', {})
542 def cross_mapping() -> BlockBitmapMapping:
545 'node-name': 'node-a',
559 'node-name': 'node-b',
574 def verify_dest_has_all_bitmaps(self) -> None:
575 bitmaps = self.vm_b.query_bitmaps()
577 # Extract and sort bitmap names
579 bitmaps[node] = sorted((bmap['name'] for bmap in bitmaps[node]))
581 self.assertEqual(bitmaps,
582 {'node-a': ['bmap-a', 'bmap-b'],
583 'node-b': ['bmap-a', 'bmap-b']})
585 def test_alias_on_src(self) -> None:
586 self.set_mapping(self.vm_a, self.cross_mapping())
588 # Checks that node-a.bmap-a was migrated to node-b.bmap-b, and
591 self.verify_dest_has_all_bitmaps()
592 self.verify_dest_error(None)
594 def test_alias_on_dst(self) -> None:
595 self.set_mapping(self.vm_b, self.cross_mapping())
597 # Checks that node-a.bmap-a was migrated to node-b.bmap-b, and
600 self.verify_dest_has_all_bitmaps()
601 self.verify_dest_error(None)
603 class TestAliasTransformMigration(TestDirtyBitmapMigration):
605 Tests the 'transform' option which modifies bitmap persistence on
609 src_node_name = 'node-a'
610 dst_node_name = 'node-b'
611 src_bmap_name = 'bmap-a'
612 dst_bmap_name = 'bmap-b'
614 def setUp(self) -> None:
615 TestDirtyBitmapMigration.setUp(self)
617 # Now create another block device and let both have two bitmaps each
618 result = self.vm_a.qmp('blockdev-add',
619 node_name='node-b', driver='null-co',
621 self.assert_qmp(result, 'return', {})
623 result = self.vm_b.qmp('blockdev-add',
624 node_name='node-a', driver='null-co',
626 self.assert_qmp(result, 'return', {})
628 bmaps_to_add = (('node-a', 'bmap-b'),
629 ('node-b', 'bmap-a'),
630 ('node-b', 'bmap-b'))
632 for (node, bmap) in bmaps_to_add:
633 result = self.vm_a.qmp('block-dirty-bitmap-add',
634 node=node, name=bmap)
635 self.assert_qmp(result, 'return', {})
638 def transform_mapping() -> BlockBitmapMapping:
641 'node-name': 'node-a',
659 'node-name': 'node-b',
674 def verify_dest_bitmap_state(self) -> None:
675 bitmaps = self.vm_b.query_bitmaps()
678 bitmaps[node] = sorted(((bmap['name'], bmap['persistent'])
679 for bmap in bitmaps[node]))
681 self.assertEqual(bitmaps,
682 {'node-a': [('bmap-a', True), ('bmap-b', False)],
683 'node-b': [('bmap-a', False), ('bmap-b', False)]})
685 def test_transform_on_src(self) -> None:
686 self.set_mapping(self.vm_a, self.transform_mapping())
689 self.verify_dest_bitmap_state()
690 self.verify_dest_error(None)
692 def test_transform_on_dst(self) -> None:
693 self.set_mapping(self.vm_b, self.transform_mapping())
696 self.verify_dest_bitmap_state()
697 self.verify_dest_error(None)
699 if __name__ == '__main__':
700 iotests.main(supported_protocols=['file'])