Skip to content

Commit b361e25

Browse files
committed
module.run: fix result detection from returned dict
1 parent c620f85 commit b361e25

File tree

3 files changed

+36
-20
lines changed

3 files changed

+36
-20
lines changed

changelog/65842.fixed.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
Fixed result detection of module.run from returned dict

salt/states/module.py

Lines changed: 18 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -452,7 +452,7 @@ def _run(**kwargs):
452452
func_ret = _call_function(
453453
_func, returner=kwargs.get("returner"), func_args=kwargs.get(func)
454454
)
455-
if not _get_result(func_ret, ret["changes"].get("ret", {})):
455+
if not _get_result(func_ret):
456456
if isinstance(func_ret, dict):
457457
failures.append(
458458
"'{}' failed: {}".format(
@@ -658,31 +658,29 @@ def _legacy_run(name, **kwargs):
658658
if kwargs["returner"] in returners:
659659
returners[kwargs["returner"]](ret_ret)
660660
ret["comment"] = f"Module function {name} executed"
661-
ret["result"] = _get_result(mret, ret["changes"])
661+
ret["result"] = _get_result(mret)
662662

663663
return ret
664664

665665

666-
def _get_result(func_ret, changes):
666+
def _get_result(func_ret):
667667
res = True
668-
# if mret is a dict and there is retcode and its non-zero
669-
if isinstance(func_ret, dict) and func_ret.get("retcode", 0) != 0:
670-
res = False
671-
# if its a boolean, return that as the result
672-
elif isinstance(func_ret, bool):
668+
# if mret a boolean, return that as the result
669+
if isinstance(func_ret, bool):
673670
res = func_ret
674-
else:
675-
changes_ret = changes.get("ret", {})
676-
if isinstance(changes_ret, dict):
677-
if isinstance(changes_ret.get("result", {}), bool):
678-
res = changes_ret.get("result", {})
679-
elif changes_ret.get("retcode", 0) != 0:
680-
res = False
681-
# Explore dict in depth to determine if there is a
682-
# 'result' key set to False which sets the global
683-
# state result.
684-
else:
685-
res = _get_dict_result(changes_ret)
671+
# if mret is a dict, check if certain keys exist
672+
elif isinstance(func_ret, dict):
673+
# if return key exists and is boolean, return that as the result
674+
if isinstance(func_ret.get("result", {}), bool):
675+
res = func_ret.get("result", {})
676+
# if retcode exists and it is non-zero, return False
677+
elif func_ret.get("retcode", 0) != 0:
678+
res = False
679+
# Explore dict in depth to determine if there is a
680+
# 'result' key set to False which sets the global
681+
# state result.
682+
else:
683+
res = _get_dict_result(func_ret)
686684

687685
return res
688686

tests/unit/states/test_module.py

Lines changed: 17 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -421,6 +421,23 @@ def test_module_run_missing_arg(self):
421421
self.assertIn("world", ret["comment"])
422422
self.assertIn("hello", ret["comment"])
423423

424+
def test_module_run_ret_result(self):
425+
with patch.dict(module.__salt__, {CMD: _mocked_none_return}), patch.dict(
426+
module.__opts__, {"use_superseded": ["module.run"]}
427+
):
428+
for val, res in [
429+
(True, True),
430+
(False, False),
431+
({"result": True}, True),
432+
({"result": False}, False),
433+
({"retcode": 0}, True),
434+
({"retcode": 1}, False),
435+
({"bla": {"result": True}}, True),
436+
({"bla": {"result": False}}, False),
437+
]:
438+
ret = module.run(**{CMD: [{"ret": val}]})
439+
self.assertEqual(ret["result"], res)
440+
424441
def test_call_function_named_args(self):
425442
"""
426443
Test _call_function routine when params are named. Their position ordering should not matter.

0 commit comments

Comments
 (0)