3 # Copyright (C) 2020 Red Hat, Inc.
5 # Tests for dirty bitmaps migration with node aliases
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/>.
24 from typing import Dict, List, Optional, Union
28 BlockBitmapMapping = List[Dict[str, Union[str, List[Dict[str, str]]]]]
30 assert iotests.sock_dir is not None
31 mig_sock = os.path.join(iotests.sock_dir, 'mig_sock')
34 class TestDirtyBitmapMigration(iotests.QMPTestCase):
35 src_node_name: str = ''
36 dst_node_name: str = ''
37 src_bmap_name: str = ''
38 dst_bmap_name: str = ''
40 def setUp(self) -> None:
41 self.vm_a = iotests.VM(path_suffix='-a')
42 self.vm_a.add_blockdev(f'node-name={self.src_node_name},'
46 self.vm_b = iotests.VM(path_suffix='-b')
47 self.vm_b.add_blockdev(f'node-name={self.dst_node_name},'
49 self.vm_b.add_incoming(f'unix:{mig_sock}')
52 result = self.vm_a.qmp('block-dirty-bitmap-add',
53 node=self.src_node_name,
54 name=self.src_bmap_name)
55 self.assert_qmp(result, 'return', {})
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 result = vm.qmp('migrate-set-capabilities', capabilities=caps)
72 self.assert_qmp(result, 'return', {})
74 def tearDown(self) -> None:
82 def check_bitmap(self, bitmap_name_valid: bool) -> None:
83 result = self.vm_b.qmp('x-debug-block-dirty-bitmap-sha256',
84 node=self.dst_node_name,
85 name=self.dst_bmap_name)
87 self.assert_qmp(result, 'return/sha256',
88 self.bitmap_hash_reference)
90 self.assert_qmp(result, 'error/desc',
91 f"Dirty bitmap '{self.dst_bmap_name}' not found")
93 def migrate(self, bitmap_name_valid: bool = True,
94 migration_success: bool = True) -> None:
95 result = self.vm_a.qmp('migrate', uri=f'unix:{mig_sock}')
96 self.assert_qmp(result, 'return', {})
98 with iotests.Timeout(5, 'Timeout waiting for migration to complete'):
99 self.assertEqual(self.vm_a.wait_migration('postmigrate'),
101 self.assertEqual(self.vm_b.wait_migration('running'),
104 if migration_success:
105 self.check_bitmap(bitmap_name_valid)
107 def verify_dest_error(self, msg: Optional[str]) -> None:
109 Check whether the given error message is present in vm_b's log.
110 (vm_b is shut down to do so.)
111 If @msg is None, check that there has not been any error.
115 self.assertNotIn('qemu-system-', self.vm_b.get_log())
117 self.assertIn(msg, self.vm_b.get_log())
120 def mapping(node_name: str, node_alias: str,
121 bitmap_name: str, bitmap_alias: str) -> BlockBitmapMapping:
123 'node-name': node_name,
127 'alias': bitmap_alias
131 def set_mapping(self, vm: iotests.VM, mapping: BlockBitmapMapping,
132 error: Optional[str] = None) -> None:
134 Invoke migrate-set-parameters on @vm to set the given @mapping.
135 Check for success if @error is None, or verify the error message
137 On success, verify that "info migrate_parameters" on HMP returns
138 our mapping. (Just to check its formatting code.)
140 result = vm.qmp('migrate-set-parameters',
141 block_bitmap_mapping=mapping)
144 self.assert_qmp(result, 'return', {})
146 result = vm.qmp('human-monitor-command',
147 command_line='info migrate_parameters')
149 m = re.search(r'^block-bitmap-mapping:\r?(\n .*)*\n',
150 result['return'], flags=re.MULTILINE)
151 hmp_mapping = m.group(0).replace('\r', '') if m else None
153 self.assertEqual(hmp_mapping, self.to_hmp_mapping(mapping))
155 self.assert_qmp(result, 'error/desc', error)
158 def to_hmp_mapping(mapping: BlockBitmapMapping) -> str:
159 result = 'block-bitmap-mapping:\n'
162 result += f" '{node['node-name']}' -> '{node['alias']}'\n"
164 assert isinstance(node['bitmaps'], list)
165 for bitmap in node['bitmaps']:
166 result += f" '{bitmap['name']}' -> '{bitmap['alias']}'\n"
171 class TestAliasMigration(TestDirtyBitmapMigration):
172 src_node_name = 'node0'
173 dst_node_name = 'node0'
174 src_bmap_name = 'bmap0'
175 dst_bmap_name = 'bmap0'
177 def test_migration_without_alias(self) -> None:
178 self.migrate(self.src_node_name == self.dst_node_name and
179 self.src_bmap_name == self.dst_bmap_name)
181 # Check for error message on the destination
182 if self.src_node_name != self.dst_node_name:
183 self.verify_dest_error(f"Cannot find "
184 f"device={self.src_node_name} nor "
185 f"node_name={self.src_node_name}")
187 self.verify_dest_error(None)
189 def test_alias_on_src_migration(self) -> None:
190 mapping = self.mapping(self.src_node_name, self.dst_node_name,
191 self.src_bmap_name, self.dst_bmap_name)
193 self.set_mapping(self.vm_a, mapping)
195 self.verify_dest_error(None)
197 def test_alias_on_dst_migration(self) -> None:
198 mapping = self.mapping(self.dst_node_name, self.src_node_name,
199 self.dst_bmap_name, self.src_bmap_name)
201 self.set_mapping(self.vm_b, mapping)
203 self.verify_dest_error(None)
205 def test_alias_on_both_migration(self) -> None:
206 src_map = self.mapping(self.src_node_name, 'node-alias',
207 self.src_bmap_name, 'bmap-alias')
209 dst_map = self.mapping(self.dst_node_name, 'node-alias',
210 self.dst_bmap_name, 'bmap-alias')
212 self.set_mapping(self.vm_a, src_map)
213 self.set_mapping(self.vm_b, dst_map)
215 self.verify_dest_error(None)
218 class TestNodeAliasMigration(TestAliasMigration):
219 src_node_name = 'node-src'
220 dst_node_name = 'node-dst'
223 class TestBitmapAliasMigration(TestAliasMigration):
224 src_bmap_name = 'bmap-src'
225 dst_bmap_name = 'bmap-dst'
228 class TestFullAliasMigration(TestAliasMigration):
229 src_node_name = 'node-src'
230 dst_node_name = 'node-dst'
231 src_bmap_name = 'bmap-src'
232 dst_bmap_name = 'bmap-dst'
235 class TestLongBitmapNames(TestAliasMigration):
236 # Giving long bitmap names is OK, as long as there is a short alias for
238 src_bmap_name = 'a' * 512
239 dst_bmap_name = 'b' * 512
241 # Skip all tests that do not use the intermediate alias
242 def test_migration_without_alias(self) -> None:
245 def test_alias_on_src_migration(self) -> None:
248 def test_alias_on_dst_migration(self) -> None:
252 class TestBlockBitmapMappingErrors(TestDirtyBitmapMigration):
253 src_node_name = 'node0'
254 dst_node_name = 'node0'
255 src_bmap_name = 'bmap0'
256 dst_bmap_name = 'bmap0'
259 Note that mapping nodes or bitmaps that do not exist is not an error.
262 def test_non_injective_node_mapping(self) -> None:
263 mapping: BlockBitmapMapping = [
265 'node-name': 'node0',
266 'alias': 'common-alias',
269 'alias': 'bmap-alias0'
273 'node-name': 'node1',
274 'alias': 'common-alias',
277 'alias': 'bmap-alias1'
282 self.set_mapping(self.vm_a, mapping,
283 "Invalid mapping given for block-bitmap-mapping: "
284 "The node alias 'common-alias' is used twice")
286 def test_non_injective_bitmap_mapping(self) -> None:
287 mapping: BlockBitmapMapping = [{
288 'node-name': 'node0',
289 'alias': 'node-alias0',
293 'alias': 'common-alias'
297 'alias': 'common-alias'
302 self.set_mapping(self.vm_a, mapping,
303 "Invalid mapping given for block-bitmap-mapping: "
304 "The bitmap alias 'node-alias0'/'common-alias' is "
307 def test_ambiguous_node_mapping(self) -> None:
308 mapping: BlockBitmapMapping = [
310 'node-name': 'node0',
311 'alias': 'node-alias0',
314 'alias': 'bmap-alias0'
318 'node-name': 'node0',
319 'alias': 'node-alias1',
322 'alias': 'bmap-alias0'
327 self.set_mapping(self.vm_a, mapping,
328 "Invalid mapping given for block-bitmap-mapping: "
329 "The node name 'node0' is mapped twice")
331 def test_ambiguous_bitmap_mapping(self) -> None:
332 mapping: BlockBitmapMapping = [{
333 'node-name': 'node0',
334 'alias': 'node-alias0',
338 'alias': 'bmap-alias0'
342 'alias': 'bmap-alias1'
347 self.set_mapping(self.vm_a, mapping,
348 "Invalid mapping given for block-bitmap-mapping: "
349 "The bitmap 'node0'/'bmap0' is mapped twice")
351 def test_migratee_node_is_not_mapped_on_src(self) -> None:
352 self.set_mapping(self.vm_a, [])
353 # Should just ignore all bitmaps on unmapped nodes
355 self.verify_dest_error(None)
357 def test_migratee_node_is_not_mapped_on_dst(self) -> None:
358 self.set_mapping(self.vm_b, [])
360 self.verify_dest_error(f"Unknown node alias '{self.src_node_name}'")
362 def test_migratee_bitmap_is_not_mapped_on_src(self) -> None:
363 mapping: BlockBitmapMapping = [{
364 'node-name': self.src_node_name,
365 'alias': self.dst_node_name,
369 self.set_mapping(self.vm_a, mapping)
370 # Should just ignore all unmapped bitmaps
372 self.verify_dest_error(None)
374 def test_migratee_bitmap_is_not_mapped_on_dst(self) -> None:
375 mapping: BlockBitmapMapping = [{
376 'node-name': self.dst_node_name,
377 'alias': self.src_node_name,
381 self.set_mapping(self.vm_b, mapping)
383 self.verify_dest_error(f"Unknown bitmap alias "
384 f"'{self.src_bmap_name}' "
385 f"on node '{self.dst_node_name}' "
386 f"(alias '{self.src_node_name}')")
388 def test_unused_mapping_on_dst(self) -> None:
389 # Let the source not send any bitmaps
390 self.set_mapping(self.vm_a, [])
392 # Establish some mapping on the destination
393 self.set_mapping(self.vm_b, [])
395 # The fact that there is a mapping on B without any bitmaps
396 # being received should be fine, not fatal
398 self.verify_dest_error(None)
400 def test_non_wellformed_node_alias(self) -> None:
403 mapping: BlockBitmapMapping = [{
404 'node-name': self.src_node_name,
409 self.set_mapping(self.vm_a, mapping,
410 f"Invalid mapping given for block-bitmap-mapping: "
411 f"The node alias '{alias}' is not well-formed")
413 def test_node_alias_too_long(self) -> None:
416 mapping: BlockBitmapMapping = [{
417 'node-name': self.src_node_name,
422 self.set_mapping(self.vm_a, mapping,
423 f"Invalid mapping given for block-bitmap-mapping: "
424 f"The node alias '{alias}' is longer than 255 bytes")
426 def test_bitmap_alias_too_long(self) -> None:
429 mapping = self.mapping(self.src_node_name, self.dst_node_name,
430 self.src_bmap_name, alias)
432 self.set_mapping(self.vm_a, mapping,
433 f"Invalid mapping given for block-bitmap-mapping: "
434 f"The bitmap alias '{alias}' is longer than 255 "
437 def test_bitmap_name_too_long(self) -> None:
440 result = self.vm_a.qmp('block-dirty-bitmap-add',
441 node=self.src_node_name,
443 self.assert_qmp(result, 'return', {})
445 self.migrate(False, False)
447 # Check for the error in the source's log
449 self.assertIn(f"Cannot migrate bitmap '{name}' on node "
450 f"'{self.src_node_name}': Name is longer than 255 bytes",
453 # Expect abnormal shutdown of the destination VM because of
454 # the failed migration
457 except qemu.machine.AbnormalShutdown:
460 def test_aliased_bitmap_name_too_long(self) -> None:
461 # Longer than the maximum for bitmap names
462 self.dst_bmap_name = 'a' * 1024
464 mapping = self.mapping(self.dst_node_name, self.src_node_name,
465 self.dst_bmap_name, self.src_bmap_name)
467 # We would have to create this bitmap during migration, and
468 # that would fail, because the name is too long. Better to
470 self.set_mapping(self.vm_b, mapping,
471 f"Invalid mapping given for block-bitmap-mapping: "
472 f"The bitmap name '{self.dst_bmap_name}' is longer "
475 def test_node_name_too_long(self) -> None:
476 # Longer than the maximum for node names
477 self.dst_node_name = 'a' * 32
479 mapping = self.mapping(self.dst_node_name, self.src_node_name,
480 self.dst_bmap_name, self.src_bmap_name)
482 # During migration, this would appear simply as a node that
483 # cannot be found. Still better to catch impossible node
484 # names early (similar to test_non_wellformed_node_alias).
485 self.set_mapping(self.vm_b, mapping,
486 f"Invalid mapping given for block-bitmap-mapping: "
487 f"The node name '{self.dst_node_name}' is longer "
491 class TestCrossAliasMigration(TestDirtyBitmapMigration):
493 Swap aliases, both to see that qemu does not get confused, and
494 that we can migrate multiple things at once.
497 node-a.bmap-a -> node-b.bmap-b
498 node-a.bmap-b -> node-b.bmap-a
499 node-b.bmap-a -> node-a.bmap-b
500 node-b.bmap-b -> node-a.bmap-a
503 src_node_name = 'node-a'
504 dst_node_name = 'node-b'
505 src_bmap_name = 'bmap-a'
506 dst_bmap_name = 'bmap-b'
508 def setUp(self) -> None:
509 TestDirtyBitmapMigration.setUp(self)
511 # Now create another block device and let both have two bitmaps each
512 result = self.vm_a.qmp('blockdev-add',
513 node_name='node-b', driver='null-co')
514 self.assert_qmp(result, 'return', {})
516 result = self.vm_b.qmp('blockdev-add',
517 node_name='node-a', driver='null-co')
518 self.assert_qmp(result, 'return', {})
520 bmaps_to_add = (('node-a', 'bmap-b'),
521 ('node-b', 'bmap-a'),
522 ('node-b', 'bmap-b'))
524 for (node, bmap) in bmaps_to_add:
525 result = self.vm_a.qmp('block-dirty-bitmap-add',
526 node=node, name=bmap)
527 self.assert_qmp(result, 'return', {})
530 def cross_mapping() -> BlockBitmapMapping:
533 'node-name': 'node-a',
547 'node-name': 'node-b',
562 def verify_dest_has_all_bitmaps(self) -> None:
563 bitmaps = self.vm_b.query_bitmaps()
565 # Extract and sort bitmap names
567 bitmaps[node] = sorted((bmap['name'] for bmap in bitmaps[node]))
569 self.assertEqual(bitmaps,
570 {'node-a': ['bmap-a', 'bmap-b'],
571 'node-b': ['bmap-a', 'bmap-b']})
573 def test_alias_on_src(self) -> None:
574 self.set_mapping(self.vm_a, self.cross_mapping())
576 # Checks that node-a.bmap-a was migrated to node-b.bmap-b, and
579 self.verify_dest_has_all_bitmaps()
580 self.verify_dest_error(None)
582 def test_alias_on_dst(self) -> None:
583 self.set_mapping(self.vm_b, self.cross_mapping())
585 # Checks that node-a.bmap-a was migrated to node-b.bmap-b, and
588 self.verify_dest_has_all_bitmaps()
589 self.verify_dest_error(None)
592 if __name__ == '__main__':
593 iotests.main(supported_protocols=['file'])