mirror of
https://github.com/valitydev/salt.git
synced 2024-11-08 01:18:58 +00:00
Merge pull request #29192 from bastiaanb/fix/issue-29191-subdict_match-regression
fix issue 29191: only try partial matches when a wildcard has been sp…
This commit is contained in:
commit
7bd15621c8
@ -1370,7 +1370,8 @@ def subdict_match(data,
|
|||||||
return fnmatch.fnmatch(str(target).lower(), pattern.lower())
|
return fnmatch.fnmatch(str(target).lower(), pattern.lower())
|
||||||
|
|
||||||
def _dict_match(target, pattern, regex_match=False, exact_match=False):
|
def _dict_match(target, pattern, regex_match=False, exact_match=False):
|
||||||
if pattern.startswith('*:'):
|
wildcard = pattern.startswith('*:')
|
||||||
|
if wildcard:
|
||||||
pattern = pattern[2:]
|
pattern = pattern[2:]
|
||||||
|
|
||||||
if pattern == '*':
|
if pattern == '*':
|
||||||
@ -1384,25 +1385,26 @@ def subdict_match(data,
|
|||||||
regex_match=regex_match,
|
regex_match=regex_match,
|
||||||
exact_match=exact_match):
|
exact_match=exact_match):
|
||||||
return True
|
return True
|
||||||
for key in target.keys():
|
if wildcard:
|
||||||
if _match(key,
|
for key in target.keys():
|
||||||
pattern,
|
if _match(key,
|
||||||
regex_match=regex_match,
|
pattern,
|
||||||
exact_match=exact_match):
|
regex_match=regex_match,
|
||||||
return True
|
exact_match=exact_match):
|
||||||
if isinstance(target[key], dict):
|
|
||||||
if _dict_match(target[key],
|
|
||||||
pattern,
|
|
||||||
regex_match=regex_match,
|
|
||||||
exact_match=exact_match):
|
|
||||||
return True
|
return True
|
||||||
elif isinstance(target[key], list):
|
if isinstance(target[key], dict):
|
||||||
for item in target[key]:
|
if _dict_match(target[key],
|
||||||
if _match(item,
|
pattern,
|
||||||
pattern,
|
regex_match=regex_match,
|
||||||
regex_match=regex_match,
|
exact_match=exact_match):
|
||||||
exact_match=exact_match):
|
|
||||||
return True
|
return True
|
||||||
|
elif isinstance(target[key], list):
|
||||||
|
for item in target[key]:
|
||||||
|
if _match(item,
|
||||||
|
pattern,
|
||||||
|
regex_match=regex_match,
|
||||||
|
exact_match=exact_match):
|
||||||
|
return True
|
||||||
return False
|
return False
|
||||||
|
|
||||||
for idx in range(1, expr.count(delimiter) + 1):
|
for idx in range(1, expr.count(delimiter) + 1):
|
||||||
|
@ -216,6 +216,7 @@ class UtilsTestCase(TestCase):
|
|||||||
test_two_level_dict_and_list = {
|
test_two_level_dict_and_list = {
|
||||||
'abc': ['def', 'ghi', {'lorem': {'ipsum': [{'dolor': 'sit'}]}}],
|
'abc': ['def', 'ghi', {'lorem': {'ipsum': [{'dolor': 'sit'}]}}],
|
||||||
}
|
}
|
||||||
|
test_three_level_dict = {'a': {'b': {'c': 'v'}}}
|
||||||
|
|
||||||
self.assertTrue(
|
self.assertTrue(
|
||||||
utils.subdict_match(
|
utils.subdict_match(
|
||||||
@ -264,6 +265,24 @@ class UtilsTestCase(TestCase):
|
|||||||
test_two_level_dict_and_list, 'abc:lorem:ipsum:dolor:sit'
|
test_two_level_dict_and_list, 'abc:lorem:ipsum:dolor:sit'
|
||||||
)
|
)
|
||||||
)
|
)
|
||||||
|
# Test four level dict match for reference
|
||||||
|
self.assertTrue(
|
||||||
|
utils.subdict_match(
|
||||||
|
test_three_level_dict, 'a:b:c:v'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
self.assertFalse(
|
||||||
|
# Test regression in 2015.8 where 'a:c:v' would match 'a:b:c:v'
|
||||||
|
utils.subdict_match(
|
||||||
|
test_three_level_dict, 'a:c:v'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
# Test wildcard match
|
||||||
|
self.assertTrue(
|
||||||
|
utils.subdict_match(
|
||||||
|
test_three_level_dict, 'a:*:c:v'
|
||||||
|
)
|
||||||
|
)
|
||||||
|
|
||||||
def test_traverse_dict(self):
|
def test_traverse_dict(self):
|
||||||
test_two_level_dict = {'foo': {'bar': 'baz'}}
|
test_two_level_dict = {'foo': {'bar': 'baz'}}
|
||||||
|
Loading…
Reference in New Issue
Block a user