Pass cmd.run arguments to onlyif/unless cmd

This commit is contained in:
Christian McHugh 2020-06-28 08:42:13 +01:00 committed by Daniel Wozniak
parent 256a6fe294
commit 452f1906c5
3 changed files with 68 additions and 3 deletions

1
changelog/57760.fixed Normal file
View file

@ -0,0 +1 @@
Pass cmd.run state arguments to unless and onlyif when they exist

View file

@ -877,7 +877,23 @@ class State:
ret = {"result": False, "comment": []}
cmd_opts = {}
if "shell" in self.opts["grains"]:
# Set arguments from cmd.run state as appropriate
POSSIBLE_CMD_ARGS = (
"cwd",
"root",
"runas",
"env",
"prepend_path",
"umask",
"timeout",
"success_retcodes",
)
for run_cmd_arg in POSSIBLE_CMD_ARGS:
cmd_opts[run_cmd_arg] = low_data.get(run_cmd_arg)
if "shell" in low_data:
cmd_opts["shell"] = low_data["shell"]
elif "shell" in self.opts["grains"]:
cmd_opts["shell"] = self.opts["grains"].get("shell")
if "onlyif" in low_data:

View file

@ -146,7 +146,8 @@ class StateCompilerTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
"""
low_data = {
"onlyif": "somecommand",
"runas" "doesntexist" "name": "echo something",
"runas": "doesntexist",
"name": "echo something",
"state": "cmd",
"__id__": "this is just a test",
"fun": "run",
@ -178,7 +179,8 @@ class StateCompilerTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
"""
low_data = {
"unless": "somecommand",
"runas" "doesntexist" "name": "echo something",
"runas": "doesntexist",
"name": "echo something",
"state": "cmd",
"__id__": "this is just a test",
"fun": "run",
@ -389,6 +391,52 @@ class StateCompilerTestCase(TestCase, AdaptedConfigurationTestCaseMixin):
return_result = state_obj._run_check_onlyif(low_data, {})
self.assertEqual(expected_result, return_result)
def test_verify_onlyif_cmd_args(self):
"""
Verify cmd.run state arguments are properly passed to cmd.retcode in onlyif
"""
low_data = {
"onlyif": "somecommand",
"cwd": "acwd",
"root": "aroot",
"env": [{"akey": "avalue"}],
"prepend_path": "apath",
"umask": "0700",
"success_retcodes": 1,
"timeout": 5,
"runas": "doesntexist",
"name": "echo something",
"shell": "/bin/dash",
"state": "cmd",
"__id__": "this is just a test",
"fun": "run",
"__env__": "base",
"__sls__": "sometest",
"order": 10000,
}
with patch("salt.state.State._gather_pillar") as state_patch:
minion_opts = self.get_temp_config("minion")
state_obj = salt.state.State(minion_opts)
mock = MagicMock()
with patch.dict(state_obj.functions, {"cmd.retcode": mock}):
# The mock handles the exception, but the runas dict is being passed as it would actually be
return_result = state_obj._run_check(low_data)
mock.assert_called_once_with(
"somecommand",
ignore_retcode=True,
python_shell=True,
cwd="acwd",
root="aroot",
runas="doesntexist",
env=[{"akey": "avalue"}],
prepend_path="apath",
umask="0700",
timeout=5,
success_retcodes=1,
shell="/bin/dash",
)
@with_tempfile()
def test_verify_unless_parse_slots(self, name):
with salt.utils.files.fopen(name, "w") as fp: