role: Fix role's hash_params (#55263)

* role: Fix role's hash_params

Fix based upon work done by alikins.

Fixes: #20596

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>

* review comments

Signed-off-by: Abhijeet Kasurde <akasurde@redhat.com>
This commit is contained in:
Abhijeet Kasurde 2019-07-08 18:56:00 +05:30 committed by Brian Coca
parent 894d19b108
commit 79fdc2190a
3 changed files with 50 additions and 2 deletions

View file

@ -0,0 +1,2 @@
bugfixes:
- Fixed role's hash_params behavior to not union (https://github.com/ansible/ansible/issues/20596).

View file

@ -68,7 +68,7 @@ def hash_params(params):
new_params = set() new_params = set()
for k, v in params.items(): for k, v in params.items():
# Hash each entry individually # Hash each entry individually
new_params.update((k, hash_params(v))) new_params.add((k, hash_params(v)))
new_params = frozenset(new_params) new_params = frozenset(new_params)
elif isinstance(params, (Set, Sequence)): elif isinstance(params, (Set, Sequence)):
@ -79,7 +79,7 @@ def hash_params(params):
new_params = set() new_params = set()
for v in params: for v in params:
# Hash each entry individually # Hash each entry individually
new_params.update(hash_params(v)) new_params.add(hash_params(v))
new_params = frozenset(new_params) new_params = frozenset(new_params)
else: else:
# This is just a guess. # This is just a guess.

View file

@ -118,6 +118,52 @@ class TestHashParams(unittest.TestCase):
self.assertRaises(TypeError, hash_params, params) self.assertRaises(TypeError, hash_params, params)
def test_param_dict_dupe_values(self):
params1 = {'foo': False}
params2 = {'bar': False}
res1 = hash_params(params1)
res2 = hash_params(params2)
hash1 = hash(res1)
hash2 = hash(res2)
self.assertNotEqual(res1, res2)
self.assertNotEqual(hash1, hash2)
def test_param_dupe(self):
params1 = {
# 'from_files': {},
'tags': [],
u'testvalue': False,
u'testvalue2': True,
# 'when': []
}
params2 = {
# 'from_files': {},
'tags': [],
u'testvalue': True,
u'testvalue2': False,
# 'when': []
}
res1 = hash_params(params1)
res2 = hash_params(params2)
self.assertNotEqual(hash(res1), hash(res2))
self.assertNotEqual(res1, res2)
foo = {}
foo[res1] = 'params1'
foo[res2] = 'params2'
self.assertEqual(len(foo), 2)
del foo[res2]
self.assertEqual(len(foo), 1)
for key in foo:
self.assertTrue(key in foo)
self.assertIn(key, foo)
class TestRole(unittest.TestCase): class TestRole(unittest.TestCase):