diff options
author | tchernomax <maxime.deroucy@gmail.com> | 2020-02-12 22:40:36 +0100 |
---|---|---|
committer | GitHub <noreply@github.com> | 2020-02-12 22:40:36 +0100 |
commit | 53e043b5febd30f258a233f51b180a543300151b (patch) | |
tree | 4ad3eec9652458e539774bd3e6c1e616fdb3f2d4 /test/units/utils | |
parent | Hotfix for FortiManager Network Connection Plugin (#62534) (diff) | |
download | ansible-53e043b5febd30f258a233f51b180a543300151b.tar.xz ansible-53e043b5febd30f258a233f51b180a543300151b.zip |
combine filter: fine list handling (option b) (#57894)
Diffstat (limited to 'test/units/utils')
-rw-r--r-- | test/units/utils/test_vars.py | 210 |
1 files changed, 200 insertions, 10 deletions
diff --git a/test/units/utils/test_vars.py b/test/units/utils/test_vars.py index 812c167021..c92ce4b6ed 100644 --- a/test/units/utils/test_vars.py +++ b/test/units/utils/test_vars.py @@ -28,12 +28,17 @@ from ansible.utils.vars import combine_vars, merge_hash class TestVariableUtils(unittest.TestCase): + def setUp(self): + pass - test_merge_data = ( + def tearDown(self): + pass + + combine_vars_merge_data = ( dict( a=dict(a=1), b=dict(b=2), - result=dict(a=1, b=2) + result=dict(a=1, b=2), ), dict( a=dict(a=1, c=dict(foo='bar')), @@ -46,7 +51,7 @@ class TestVariableUtils(unittest.TestCase): result=defaultdict(a=1, b=2, c=defaultdict(foo='bar', baz='bam')) ), ) - test_replace_data = ( + combine_vars_replace_data = ( dict( a=dict(a=1), b=dict(b=2), @@ -64,11 +69,7 @@ class TestVariableUtils(unittest.TestCase): ), ) - def test_merge_hash(self): - for test in self.test_merge_data: - self.assertEqual(merge_hash(test['a'], test['b']), test['result']) - - def test_improper_args(self): + def test_combine_vars_improper_args(self): with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'replace'): with self.assertRaises(AnsibleError): combine_vars([1, 2, 3], dict(a=1)) @@ -83,10 +84,199 @@ class TestVariableUtils(unittest.TestCase): def test_combine_vars_replace(self): with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'replace'): - for test in self.test_replace_data: + for test in self.combine_vars_replace_data: self.assertEqual(combine_vars(test['a'], test['b']), test['result']) def test_combine_vars_merge(self): with mock.patch('ansible.constants.DEFAULT_HASH_BEHAVIOUR', 'merge'): - for test in self.test_merge_data: + for test in self.combine_vars_merge_data: self.assertEqual(combine_vars(test['a'], test['b']), test['result']) + + merge_hash_data = { + "low_prio": { + "a": { + "a'": { + "x": "low_value", + "y": "low_value", + "list": ["low_value"] + } + }, + "b": [1, 1, 2, 3] + }, + "high_prio": { + "a": { + "a'": { + "y": "high_value", + "z": "high_value", + "list": ["high_value"] + } + }, + "b": [3, 4, 4, {"5": "value"}] + } + } + + def test_merge_hash_simple(self): + for test in self.combine_vars_merge_data: + self.assertEqual(merge_hash(test['a'], test['b']), test['result']) + + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": { + "a'": { + "x": "low_value", + "y": "high_value", + "z": "high_value", + "list": ["high_value"] + } + }, + "b": high['b'] + } + self.assertEqual(merge_hash(low, high), expected) + + def test_merge_hash_non_recursive_and_list_replace(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = high + self.assertEqual(merge_hash(low, high, False, 'replace'), expected) + + def test_merge_hash_non_recursive_and_list_keep(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": high['a'], + "b": low['b'] + } + self.assertEqual(merge_hash(low, high, False, 'keep'), expected) + + def test_merge_hash_non_recursive_and_list_append(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": high['a'], + "b": low['b'] + high['b'] + } + self.assertEqual(merge_hash(low, high, False, 'append'), expected) + + def test_merge_hash_non_recursive_and_list_prepend(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": high['a'], + "b": high['b'] + low['b'] + } + self.assertEqual(merge_hash(low, high, False, 'prepend'), expected) + + def test_merge_hash_non_recursive_and_list_append_rp(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": high['a'], + "b": [1, 1, 2] + high['b'] + } + self.assertEqual(merge_hash(low, high, False, 'append_rp'), expected) + + def test_merge_hash_non_recursive_and_list_prepend_rp(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": high['a'], + "b": high['b'] + [1, 1, 2] + } + self.assertEqual(merge_hash(low, high, False, 'prepend_rp'), expected) + + def test_merge_hash_recursive_and_list_replace(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": { + "a'": { + "x": "low_value", + "y": "high_value", + "z": "high_value", + "list": ["high_value"] + } + }, + "b": high['b'] + } + self.assertEqual(merge_hash(low, high, True, 'replace'), expected) + + def test_merge_hash_recursive_and_list_keep(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": { + "a'": { + "x": "low_value", + "y": "high_value", + "z": "high_value", + "list": ["low_value"] + } + }, + "b": low['b'] + } + self.assertEqual(merge_hash(low, high, True, 'keep'), expected) + + def test_merge_hash_recursive_and_list_append(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": { + "a'": { + "x": "low_value", + "y": "high_value", + "z": "high_value", + "list": ["low_value", "high_value"] + } + }, + "b": low['b'] + high['b'] + } + self.assertEqual(merge_hash(low, high, True, 'append'), expected) + + def test_merge_hash_recursive_and_list_prepend(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": { + "a'": { + "x": "low_value", + "y": "high_value", + "z": "high_value", + "list": ["high_value", "low_value"] + } + }, + "b": high['b'] + low['b'] + } + self.assertEqual(merge_hash(low, high, True, 'prepend'), expected) + + def test_merge_hash_recursive_and_list_append_rp(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": { + "a'": { + "x": "low_value", + "y": "high_value", + "z": "high_value", + "list": ["low_value", "high_value"] + } + }, + "b": [1, 1, 2] + high['b'] + } + self.assertEqual(merge_hash(low, high, True, 'append_rp'), expected) + + def test_merge_hash_recursive_and_list_prepend_rp(self): + low = self.merge_hash_data['low_prio'] + high = self.merge_hash_data['high_prio'] + expected = { + "a": { + "a'": { + "x": "low_value", + "y": "high_value", + "z": "high_value", + "list": ["high_value", "low_value"] + } + }, + "b": high['b'] + [1, 1, 2] + } + self.assertEqual(merge_hash(low, high, True, 'prepend_rp'), expected) |