mirror of
https://github.com/valitydev/salt.git
synced 2024-11-07 08:58:59 +00:00
Merge pull request #13443 from makinacorpus/fixlogic
Fix the broken check state logic
This commit is contained in:
commit
c14d799fa2
@ -124,6 +124,7 @@ RED_BOLD = '\033[01;31m'
|
||||
ENDC = '\033[0m'
|
||||
|
||||
log = logging.getLogger(__name__)
|
||||
_empty = object()
|
||||
|
||||
|
||||
def get_function_argspec(func):
|
||||
@ -1434,20 +1435,26 @@ def check_state_result(running):
|
||||
if not running:
|
||||
return False
|
||||
|
||||
ret = True
|
||||
for state_result in running.itervalues():
|
||||
if not isinstance(state_result, dict):
|
||||
# return false when hosts return a list instead of a dict
|
||||
return False
|
||||
|
||||
if 'result' in state_result:
|
||||
if state_result.get('result', False) is False:
|
||||
return False
|
||||
return True
|
||||
|
||||
# Check nested state results
|
||||
return check_state_result(state_result)
|
||||
|
||||
return True
|
||||
ret = False
|
||||
if ret:
|
||||
result = state_result.get('result', _empty)
|
||||
if result is False:
|
||||
ret = False
|
||||
# only override return value if we are not already failed
|
||||
elif (
|
||||
result is _empty
|
||||
and isinstance(state_result, dict)
|
||||
and ret
|
||||
):
|
||||
ret = check_state_result(state_result)
|
||||
# return as soon as we got a failure
|
||||
if not ret:
|
||||
break
|
||||
return ret
|
||||
|
||||
|
||||
def test_mode(**kwargs):
|
||||
|
@ -15,6 +15,7 @@ from salttesting.mock import (
|
||||
ensure_in_syspath('../../')
|
||||
|
||||
# Import Salt libs
|
||||
from salt.utils.odict import OrderedDict
|
||||
from salt import utils
|
||||
from salt.utils import args
|
||||
from salt.exceptions import (SaltInvocationError, SaltSystemExit, CommandNotFoundError)
|
||||
@ -227,10 +228,109 @@ class UtilsTestCase(TestCase):
|
||||
self.assertFalse(utils.check_state_result([]), "Failed to handle an invalid data type.")
|
||||
self.assertFalse(utils.check_state_result({}), "Failed to handle an empty dictionary.")
|
||||
self.assertFalse(utils.check_state_result({'host1': []}), "Failed to handle an invalid host data structure.")
|
||||
|
||||
test_valid_state = {'host1': {'test_state': {'result': 'We have liftoff!'}}}
|
||||
self.assertTrue(utils.check_state_result(test_valid_state))
|
||||
|
||||
test_valid_false_states = {
|
||||
'test1': OrderedDict([
|
||||
('host1',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': False}),
|
||||
])),
|
||||
]),
|
||||
'test2': OrderedDict([
|
||||
('host1',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': True}),
|
||||
])),
|
||||
('host2',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': False}),
|
||||
])),
|
||||
]),
|
||||
'test3': ['a'],
|
||||
'test4': OrderedDict([
|
||||
('asup', OrderedDict([
|
||||
('host1',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': True}),
|
||||
])),
|
||||
('host2',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': False}),
|
||||
]))
|
||||
]))
|
||||
]),
|
||||
'test5': OrderedDict([
|
||||
('asup', OrderedDict([
|
||||
('host1',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': True}),
|
||||
])),
|
||||
('host2', [])
|
||||
]))
|
||||
])
|
||||
}
|
||||
for test, data in test_valid_false_states.items():
|
||||
self.assertFalse(
|
||||
utils.check_state_result(data),
|
||||
msg='{0} failed'.format(test))
|
||||
test_valid_true_states = {
|
||||
'test1': OrderedDict([
|
||||
('host1',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': True}),
|
||||
])),
|
||||
]),
|
||||
'test3': OrderedDict([
|
||||
('host1',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': True}),
|
||||
])),
|
||||
('host2',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': True}),
|
||||
])),
|
||||
]),
|
||||
'test4': OrderedDict([
|
||||
('asup', OrderedDict([
|
||||
('host1',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': True}),
|
||||
])),
|
||||
('host2',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': True}),
|
||||
]))
|
||||
]))
|
||||
]),
|
||||
'test2': OrderedDict([
|
||||
('host1',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': None}),
|
||||
('test_state', {'result': True}),
|
||||
])),
|
||||
('host2',
|
||||
OrderedDict([
|
||||
('test_state0', {'result': True}),
|
||||
('test_state', {'result': 'abc'}),
|
||||
]))
|
||||
])
|
||||
}
|
||||
for test, data in test_valid_true_states.items():
|
||||
self.assertTrue(
|
||||
utils.check_state_result(data),
|
||||
msg='{0} failed'.format(test))
|
||||
test_valid_false_state = {'host1': {'test_state': {'result': False}}}
|
||||
self.assertFalse(utils.check_state_result(test_valid_false_state))
|
||||
|
||||
|
Loading…
Reference in New Issue
Block a user