Skip to content

Commit

Permalink
Merge pull request #63970 from garethgreenaway/63948_file_state_check…
Browse files Browse the repository at this point in the history
…_cmd_fixes

[3006.x] Fixes to state check_cmd
  • Loading branch information
garethgreenaway committed Mar 28, 2023
2 parents 470b6e6 + ed42b21 commit 7e54d71
Show file tree
Hide file tree
Showing 4 changed files with 57 additions and 5 deletions.
1 change: 1 addition & 0 deletions changelog/63948.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Handle the scenario when the check_cmd requisite is used with a state function when the state has a local check_cmd function but that function isn't used by that function.
15 changes: 10 additions & 5 deletions salt/state.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@
import datetime
import fnmatch
import importlib
import inspect
import logging
import os
import random
Expand Down Expand Up @@ -2374,11 +2375,15 @@ def call(self, low, chunks=None, running=None, retries=1):
*cdata["args"], **cdata["kwargs"]
)
self.states.inject_globals = {}
if (
"check_cmd" in low
and "{0[state]}.mod_run_check_cmd".format(low) not in self.states
):
ret.update(self._run_check_cmd(low))
if "check_cmd" in low:
state_check_cmd = "{0[state]}.mod_run_check_cmd".format(low)
state_func = "{0[state]}.{0[fun]}".format(low)
state_func_sig = inspect.signature(self.states[state_func])
if state_check_cmd not in self.states:
ret.update(self._run_check_cmd(low))
else:
if "check_cmd" not in state_func_sig.parameters:
ret.update(self._run_check_cmd(low))
except Exception as exc: # pylint: disable=broad-except
log.debug(
"An exception occurred in this state: %s",
Expand Down
22 changes: 22 additions & 0 deletions tests/pytests/functional/states/file/test_append.py
Original file line number Diff line number Diff line change
Expand Up @@ -138,3 +138,25 @@ def test_issue_1896_file_append_source(file, tmp_path, state_tree):
testfile_contents = testfile.read_text()

assert testfile_contents == FIRST_IF_CONTENTS + SECOND_IF_CONTENTS


def test_file_append_check_cmd(modules, state_tree, tmp_path):
"""
Test that check_cmd works for file.append
and those states do not run.
"""
sls_contents = f"""
append_in_file:
file.append:
- name: /tmp/test
- text: "appended text"
- check_cmd:
- "djasjahj"
"""
with pytest.helpers.temp_file(
"file-append-check-cmd.sls", sls_contents, state_tree
):
ret = modules.state.sls("file-append-check-cmd")
for state_run in ret:
assert state_run.result is False
assert state_run.comment == "check_cmd determined the state failed"
24 changes: 24 additions & 0 deletions tests/pytests/functional/states/file/test_replace.py
Original file line number Diff line number Diff line change
Expand Up @@ -376,3 +376,27 @@ def test_file_replace_prerequired_issues_55775(modules, state_tree, tmp_path):
assert state_run.result is True

assert managed_file.exists()


def test_file_replace_check_cmd(modules, state_tree, tmp_path):
"""
Test that check_cmd works for file.replace
and those states do not run.
"""
sls_contents = f"""
replace_in_file:
file.replace:
- name: /tmp/test
- pattern: hi
- repl: "replacement text"
- append_if_not_found: True
- check_cmd:
- "djasjahj"
"""
with pytest.helpers.temp_file(
"file-replace-check-cmd.sls", sls_contents, state_tree
):
ret = modules.state.sls("file-replace-check-cmd")
for state_run in ret:
assert state_run.result is False
assert state_run.comment == "check_cmd determined the state failed"

0 comments on commit 7e54d71

Please sign in to comment.