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

Fixing slack engine when calling runners #63005

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

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions changelog/52400.fixed
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Swapping out args and kwargs for arg and kwarg respectively in the Slack engine when the command passed is a runner.
2 changes: 1 addition & 1 deletion salt/engines/slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -948,7 +948,7 @@ def run_command_async(self, msg):
log.debug("Command %s will run via runner_functions", cmd)
# pylint is tripping
# pylint: disable=missing-whitespace-after-comma
job_id_dict = runner.asynchronous(cmd, {"args": args, "kwargs": kwargs})
job_id_dict = runner.asynchronous(cmd, {"arg": args, "kwarg": kwargs})
job_id = job_id_dict["jid"]

# Default to trying to run as a client module.
Expand Down
151 changes: 151 additions & 0 deletions tests/pytests/unit/engines/test_slack.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,44 @@
]


class MockRunnerClient:
"""
Mock RunnerClient class
"""

def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

def asynchronous(self, *args, **kwargs):
"""
Mock asynchronous method
"""
return True


class MockLocalClient:
"""
Mock RunnerClient class
"""

def __init__(self, *args, **kwargs):
self.args = args
self.kwargs = kwargs

def __enter__(self, *args, **kwargs):
return self

def __exit__(self, *args, **kwargs):
pass

def cmd_async(self, *args, **kwargs):
"""
Mock cmd_async method
"""
return True


class MockSlackBoltSocketMode:
def __init__(self, *args, **kwargs):
self.args = args
Expand Down Expand Up @@ -360,3 +398,116 @@ def test_run_commands_from_slack_async(slack_client):
app_client_files_upload.asser_has_calls(upload_calls)
app_client_chat_postMessage.asser_has_calls(chat_postMessage_calls)
mock_event_send.asser_has_calls(event_send_calls)


def test_run_command_async(slack_client):
"""
Test slack engine: test_run_command_async
"""

msg = {
"message_data": {
"client_msg_id": "6c71d7f9-a44d-402f-8f9f-d1bb5b650853",
"type": "message",
"text": '!test.ping target="minion"',
"user": "U02QY11UJ",
"ts": "1667427929.764169",
"blocks": [
{
"type": "rich_text",
"block_id": "AjL",
"elements": [
{
"type": "rich_text_section",
"elements": [
{"type": "text", "text": '!test.ping target="minion"'}
],
}
],
}
],
"team": "T02QY11UG",
"channel": "C02QY11UQ",
"event_ts": "1667427929.764169",
"channel_type": "channel",
},
"channel": "C02QY11UQ",
"user": "U02QY11UJ",
"user_name": "garethgreenaway",
"cmdline": ["test.ping"],
"target": {"target": "minion", "tgt_type": "glob"},
}

local_client_mock = MagicMock(autospec=True, return_value=MockLocalClient())
patch_local_client = patch("salt.client.LocalClient", local_client_mock)

local_client_cmd_async_mock = MagicMock(
autospec=True, return_value={"jid": "20221027001127600438"}
)
patch_local_client_cmd_async = patch.object(
MockLocalClient, "cmd_async", local_client_cmd_async_mock
)

expected_calls = [call("minion", "test.ping", arg=[], kwarg={}, tgt_type="glob")]
with patch_local_client, patch_local_client_cmd_async as local_client_cmd_async:
ret = slack_client.run_command_async(msg)
local_client_cmd_async.assert_has_calls(expected_calls)

msg = {
"message_data": {
"client_msg_id": "35f4783f-8913-4687-8f04-21182bcacd5a",
"type": "message",
"text": "!test.arg arg1 arg2 arg3 key1=value1 key2=value2",
"user": "U02QY11UJ",
"ts": "1667429460.576889",
"blocks": [
{
"type": "rich_text",
"block_id": "EAzTy",
"elements": [
{
"type": "rich_text_section",
"elements": [
{
"type": "text",
"text": "!test.arg arg1 arg2 arg3 key1=value1 key2=value2",
}
],
}
],
}
],
"team": "T02QY11UG",
"channel": "C02QY11UQ",
"event_ts": "1667429460.576889",
"channel_type": "channel",
},
"channel": "C02QY11UQ",
"user": "U02QY11UJ",
"user_name": "garethgreenaway",
"cmdline": ["test.arg", "arg1", "arg2", "arg3", "key1=value1", "key2=value2"],
"target": {"target": "*", "tgt_type": "glob"},
}

runner_client_mock = MagicMock(autospec=True, return_value=MockRunnerClient())
patch_runner_client = patch("salt.runner.RunnerClient", runner_client_mock)

runner_client_asynchronous_mock = MagicMock(
autospec=True, return_value={"jid": "20221027001127600438"}
)
patch_runner_client_asynchronous = patch.object(
MockRunnerClient, "asynchronous", runner_client_asynchronous_mock
)

expected_calls = [
call(
"test.arg",
{
"arg": ["arg1", "arg2", "arg3"],
"kwarg": {"key1": "value1", "key2": "value2"},
},
)
]
with patch_runner_client, patch_runner_client_asynchronous as runner_client_asynchronous:
ret = slack_client.run_command_async(msg)
runner_client_asynchronous.assert_has_calls(expected_calls)