Merge pull request #38158 from cachedout/issue_38094

Fix type problem in grains.filter_by
This commit is contained in:
Mike Place 2016-12-09 14:24:40 -07:00 committed by GitHub
commit 8355adc535
2 changed files with 7 additions and 0 deletions

View file

@ -558,6 +558,8 @@ def filter_by(lookup_dict, grain='os_family', merge=None, default='default', bas
# Iterate over the list of grain values to match against patterns in the lookup_dict keys
for each in val if isinstance(val, list) else [val]:
for key in sorted(lookup_dict):
if key not in six.string_types:
key = str(key)
if fnmatch.fnmatchcase(each, key):
ret = lookup_dict[key]
break

View file

@ -157,6 +157,11 @@ class GrainsModuleTestCase(TestCase):
dict1 = {'*OS': 'B', 'C': {'D': {'E': 'F', 'G': 'H'}}}
res = grainsmod.filter_by(dict1)
self.assertEqual(res, 'B')
# Test with non-strings in lookup_dict keys
# Issue #38094
dict1 = {1: 2, 3: {4: 5}, '*OS': 'B'}
res = grainsmod.filter_by(dict1)
self.assertEqual(res, 'B')
# Test with sequence pattern with roles
dict1 = {'Z': 'B', '[BC]': {'D': {'E': 'F', 'G': 'H'}}}
res = grainsmod.filter_by(dict1, grain='roles', default='Z')