Skip to content

Commit

Permalink
Fixed KeyError in logs when running a state that fails. (#615)
Browse files Browse the repository at this point in the history
Co-authored-by: Megan Wilhite <[email protected]>
  • Loading branch information
vzhestkov and Megan Wilhite authored Jan 18, 2024
1 parent 3e7c5d9 commit f41a8e2
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 2 deletions.
1 change: 1 addition & 0 deletions changelog/64231.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Fixed KeyError in logs when running a state that fails.
2 changes: 1 addition & 1 deletion salt/master.py
Original file line number Diff line number Diff line change
Expand Up @@ -1790,7 +1790,7 @@ def minion_runner(self, clear_load):
def pub_ret(self, load):
"""
Request the return data from a specific jid, only allowed
if the requesting minion also initialted the execution.
if the requesting minion also initiated the execution.
:param dict load: The minion payload
Expand Down
4 changes: 4 additions & 0 deletions salt/minion.py
Original file line number Diff line number Diff line change
Expand Up @@ -2022,6 +2022,8 @@ def _thread_return(cls, minion_instance, opts, data):
ret["jid"] = data["jid"]
ret["fun"] = data["fun"]
ret["fun_args"] = data["arg"]
if "user" in data:
ret["user"] = data["user"]
if "master_id" in data:
ret["master_id"] = data["master_id"]
if "metadata" in data:
Expand Down Expand Up @@ -2141,6 +2143,8 @@ def _thread_multi_return(cls, minion_instance, opts, data):
ret["jid"] = data["jid"]
ret["fun"] = data["fun"]
ret["fun_args"] = data["arg"]
if "user" in data:
ret["user"] = data["user"]
if "metadata" in data:
ret["metadata"] = data["metadata"]
if minion_instance.connected:
Expand Down
3 changes: 2 additions & 1 deletion salt/utils/event.py
Original file line number Diff line number Diff line change
Expand Up @@ -902,7 +902,8 @@ def _fire_ret_load_specific_fun(self, load, fun_index=0):
data["success"] = False
data["return"] = "Error: {}.{}".format(tags[0], tags[-1])
data["fun"] = fun
data["user"] = load["user"]
if "user" in load:
data["user"] = load["user"]
self.fire_event(
data,
tagify([load["jid"], "sub", load["id"], "error", fun], "job"),
Expand Down
38 changes: 38 additions & 0 deletions tests/pytests/integration/states/test_state_test.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
def test_failing_sls(salt_master, salt_minion, salt_cli, caplog):
"""
Test when running state.sls and the state fails.
When the master stores the job and attempts to send
an event a KeyError was previously being logged.
This test ensures we do not log an error when
attempting to send an event about a failing state.
"""
statesls = """
test_state:
test.fail_without_changes:
- name: "bla"
"""
with salt_master.state_tree.base.temp_file("test_failure.sls", statesls):
ret = salt_cli.run("state.sls", "test_failure", minion_tgt=salt_minion.id)
for message in caplog.messages:
assert "Event iteration failed with" not in message


def test_failing_sls_compound(salt_master, salt_minion, salt_cli, caplog):
"""
Test when running state.sls in a compound command and the state fails.
When the master stores the job and attempts to send
an event a KeyError was previously being logged.
This test ensures we do not log an error when
attempting to send an event about a failing state.
"""
statesls = """
test_state:
test.fail_without_changes:
- name: "bla"
"""
with salt_master.state_tree.base.temp_file("test_failure.sls", statesls):
ret = salt_cli.run(
"state.sls,cmd.run", "test_failure,ls", minion_tgt=salt_minion.id
)
for message in caplog.messages:
assert "Event iteration failed with" not in message

0 comments on commit f41a8e2

Please sign in to comment.