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

(maint) Support input_method on tasks #635

Merged
merged 1 commit into from
Aug 29, 2017
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
12 changes: 6 additions & 6 deletions lib/src/modules/task.cc
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,7 @@ static const std::string TASK_RUN_ACTION_INPUT_SCHEMA { R"(
"input": {
"type": "object"
},
"inputFormat": {
"input_method": {
"type": "string"
}
},
Expand Down Expand Up @@ -402,25 +402,25 @@ void Task::callNonBlockingAction(
ActionResponse Task::callAction(const ActionRequest& request)
{
auto task_execution_params = request.params();
auto task_input_format = task_execution_params.includes("inputFormat") ?
task_execution_params.get<std::string>("inputFormat") : std::string{""};
auto task_input_method = task_execution_params.includes("input_method") ?
task_execution_params.get<std::string>("input_method") : std::string{""};
std::map<std::string, std::string> task_environment;
std::string task_input;

bool has_input = false;
if (task_input_format.empty() || task_input_format == "stdin:json") {
if (task_input_method.empty() || task_input_method == "stdin") {
task_input = task_execution_params.get<lth_jc::JsonContainer>("input").toString();
has_input = true;
}

if (task_input_format.empty() || task_input_format == "environment") {
if (task_input_method.empty() || task_input_method == "environment") {
addParametersToEnvironment(task_execution_params.get<lth_jc::JsonContainer>("input"), task_environment);
has_input = true;
}

if (!has_input) {
throw Module::ProcessingError {
lth_loc::format("unsupported task input format: {1}", task_input_format) };
lth_loc::format("unsupported task input method: {1}", task_input_method) };
}

auto task_command = getTaskCommand(
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
#!/bin/sh
echo $PT_message
cat -
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
@echo %PT_message%
@more
56 changes: 56 additions & 0 deletions lib/tests/unit/modules/task_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,36 @@ TEST_CASE("Modules::Task::executeAction", "[modules][output]") {
REQUIRE(output == "{\"message\":\"hello\"}");
}

SECTION("passes input only on stdin when input_method is stdin") {
Modules::Task e_m { PXP_AGENT_BIN_PATH, TASK_CACHE_DIR, MASTER_URIS, CA, CRT, KEY, SPOOL_DIR };
auto echo_txt =
#ifndef _WIN32
(DATA_FORMAT % "\"0632\""
% "\"task\""
% "\"run\""
% "{\"input\":{\"message\":\"hello\"}, \"input_method\": \"stdin\", \"files\" : [{\"sha256\": \"823c013467ce03b12dbe005757a6c842894373e8bcfb0cf879329afb5abcd543\", \"filename\": \"multi\"}]}").str();
#else
(DATA_FORMAT % "\"0632\""
% "\"task\""
% "\"run\""
% "{\"input\":{\"message\":\"hello\"}, \"input_method\": \"stdin\", \"files\" : [{\"sha256\": \"88a07e5b672aa44a91aa7d63e22c91510af5d4707e12f75e0d5de2dfdbde1dec\", \"filename\": \"multi.bat\"}]}").str();
#endif
PCPClient::ParsedChunks echo_content {
lth_jc::JsonContainer(ENVELOPE_TXT),
lth_jc::JsonContainer(echo_txt),
{},
0 };
ActionRequest request { RequestType::Blocking, echo_content };

auto output = e_m.executeAction(request).action_metadata.get<std::string>({ "results", "output" });
boost::trim(output);
#ifdef _WIN32
REQUIRE(output == "ECHO is on.\r\n{\"message\":\"hello\"}");
#else
REQUIRE(output == "{\"message\":\"hello\"}");
#endif
}

SECTION("passes input as env variables") {
Modules::Task e_m { PXP_AGENT_BIN_PATH, TASK_CACHE_DIR, MASTER_URIS, CA, CRT, KEY, SPOOL_DIR };
auto echo_txt =
Expand All @@ -216,6 +246,32 @@ TEST_CASE("Modules::Task::executeAction", "[modules][output]") {
REQUIRE(output == "hello");
}

SECTION("passes input only as env variables when input_method is environment") {
Modules::Task e_m { PXP_AGENT_BIN_PATH, TASK_CACHE_DIR, MASTER_URIS, CA, CRT, KEY, SPOOL_DIR };
auto echo_txt =
#ifndef _WIN32
(DATA_FORMAT % "\"0632\""
% "\"task\""
% "\"run\""
% "{\"input\":{\"message\":\"hello\"}, \"input_method\": \"environment\", \"files\" : [{\"sha256\": \"823c013467ce03b12dbe005757a6c842894373e8bcfb0cf879329afb5abcd543\", \"filename\": \"multi\"}]}").str();
#else
(DATA_FORMAT % "\"0632\""
% "\"task\""
% "\"run\""
% "{\"input\":{\"message\":\"hello\"}, \"input_method\": \"environment\", \"files\" : [{\"sha256\": \"88a07e5b672aa44a91aa7d63e22c91510af5d4707e12f75e0d5de2dfdbde1dec\", \"filename\": \"multi.bat\"}]}").str();
#endif
PCPClient::ParsedChunks echo_content {
lth_jc::JsonContainer(ENVELOPE_TXT),
lth_jc::JsonContainer(echo_txt),
{},
0 };
ActionRequest request { RequestType::Blocking, echo_content };

auto output = e_m.executeAction(request).action_metadata.get<std::string>({ "results", "output" });
boost::trim(output);
REQUIRE(output == "hello");
}

SECTION("succeeds on non-zero exit") {
Modules::Task e_m { PXP_AGENT_BIN_PATH, TASK_CACHE_DIR, MASTER_URIS, CA, CRT, KEY, SPOOL_DIR };
auto echo_txt =
Expand Down
2 changes: 1 addition & 1 deletion locales/pxp-agent.pot
Original file line number Diff line number Diff line change
Expand Up @@ -692,7 +692,7 @@ msgid ""
msgstr ""

#: lib/src/modules/task.cc
msgid "unsupported task input format: {1}"
msgid "unsupported task input method: {1}"
msgstr ""

#. debug
Expand Down