Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[3006.x] Fixes to state check_cmd #63970

Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/63948.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Hhandle 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.
garethgreenaway marked this conversation as resolved.
Show resolved Hide resolved
17 changes: 12 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,17 @@ 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" in state_func_sig.parameters:
ret.update(self.states[state_check_cmd](low))
else:
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"