Skip to content

Commit

Permalink
fixing runAsUser when building command (#292)
Browse files Browse the repository at this point in the history
* fixing runAsUser when building command

* Added a unit test for runAsUser not provided
  • Loading branch information
xlcheng1 authored Jul 14, 2022
1 parent b78192f commit 0487d63
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 6 deletions.
26 changes: 20 additions & 6 deletions source/jobs/JobEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -260,13 +260,27 @@ int JobEngine::exec_cmd(const string &operation, PlainJobDocument::JobAction act
{
argSize = action.input.args->size();
}
std::unique_ptr<const char *[]> argv(new const char *[argSize + 3]);
argv[0] = operation.c_str();
argv[1] = action.runAsUser->c_str();
argv[argSize + 2] = nullptr;
for (size_t i = 0; i < argSize; i++)
std::unique_ptr<const char *[]> argv;
if (action.runAsUser.has_value() && !action.runAsUser->empty())
{
argv[i + 2] = action.input.args->at(i).c_str();
argv.reset(new const char *[argSize + 3]);
argv[0] = operation.c_str();
argv[1] = action.runAsUser->c_str();
argv[argSize + 2] = nullptr;
for (size_t i = 0; i < argSize; i++)
{
argv[i + 2] = action.input.args->at(i).c_str();
}
}
else
{
argv.reset(new const char *[argSize + 2]);
argv[0] = operation.c_str();
argv[argSize + 1] = nullptr;
for (size_t i = 0; i < argSize; i++)
{
argv[i + 1] = action.input.args->at(i).c_str();
}
}

int execResult;
Expand Down
15 changes: 15 additions & 0 deletions test/jobs/TestJobEngine.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -102,6 +102,21 @@ TEST_F(TestJobEngine, ExecuteStepsHappy)
ASSERT_STREQ(jobEngine.getStdOut().c_str(), std::string(testStdout + "\n").c_str());
}

TEST_F(TestJobEngine, ExecuteWithoutRunAsUser)
{
vector<PlainJobDocument::JobAction> steps;
vector<std::string> args;
args.push_back("-c");
args.push_back("echo " + testStdout);
steps.push_back(createJobAction("testAction", "runHandler", "sh", args, "/bin", false));
PlainJobDocument jobDocument = createTestJobDocument(steps, true);
JobEngine jobEngine;

int executionStatus = jobEngine.exec_steps(jobDocument, testHandlerDirectoryPath);
ASSERT_EQ(executionStatus, 0);
ASSERT_STREQ(jobEngine.getStdOut().c_str(), std::string(testStdout + "\n").c_str());
}

TEST_F(TestJobEngine, ExecuteSucceedThenFail)
{
vector<PlainJobDocument::JobAction> steps;
Expand Down

0 comments on commit 0487d63

Please sign in to comment.