diff --git a/tests/unit/modules/test_defaults.py b/tests/unit/modules/test_defaults.py index 31427a26a6..d12ad01282 100644 --- a/tests/unit/modules/test_defaults.py +++ b/tests/unit/modules/test_defaults.py @@ -37,3 +37,82 @@ class DefaultsTestCase(TestCase, LoaderModuleMockMixin): MagicMock(return_value={'users': {'root': [0]}})): self.assertEqual(defaults.get('core:users:root'), {'users': {'root': [0]}}) + + def test_merge_with_list_merging(self): + ''' + Test deep merging of dicts with merge_lists enabled. + ''' + + src_dict = { + 'string_key': 'string_val_src', + 'list_key': [ + 'list_val_src', + ], + 'dict_key': { + 'dict_key_src': 'dict_val_src', + } + } + + dest_dict = { + 'string_key': 'string_val_dest', + 'list_key': [ + 'list_val_dest', + ], + 'dict_key': { + 'dict_key_dest': 'dict_val_dest', + } + } + + merged_dict = { + 'string_key': 'string_val_src', + 'list_key': [ + 'list_val_dest', + 'list_val_src' + ], + 'dict_key': { + 'dict_key_dest': 'dict_val_dest', + 'dict_key_src': 'dict_val_src' + } + } + + defaults.merge(dest_dict, src_dict, merge_lists=True) + self.assertEqual(dest_dict, merged_dict) + + def test_merge_without_list_merging(self): + ''' + Test deep merging of dicts with merge_lists disabled. + ''' + + src_dict = { + 'string_key': 'string_val_src', + 'list_key': [ + 'list_val_src', + ], + 'dict_key': { + 'dict_key_src': 'dict_val_src', + } + } + + dest_dict = { + 'string_key': 'string_val_dest', + 'list_key': [ + 'list_val_dest', + ], + 'dict_key': { + 'dict_key_dest': 'dict_val_dest', + } + } + + merged_dict = { + 'string_key': 'string_val_src', + 'list_key': [ + 'list_val_src' + ], + 'dict_key': { + 'dict_key_dest': 'dict_val_dest', + 'dict_key_src': 'dict_val_src' + } + } + + defaults.merge(dest_dict, src_dict, merge_lists=False) + self.assertEqual(dest_dict, merged_dict)