2018-01-26 11:07:02 +00:00
|
|
|
///
|
|
|
|
/// Copyright 2018 Oliver Giles
|
|
|
|
///
|
|
|
|
/// This file is part of Laminar
|
|
|
|
///
|
|
|
|
/// Laminar is free software: you can redistribute it and/or modify
|
|
|
|
/// it under the terms of the GNU General Public License as published by
|
|
|
|
/// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
/// (at your option) any later version.
|
|
|
|
///
|
|
|
|
/// Laminar is distributed in the hope that it will be useful,
|
|
|
|
/// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
/// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
/// GNU General Public License for more details.
|
|
|
|
///
|
|
|
|
/// You should have received a copy of the GNU General Public License
|
|
|
|
/// along with Laminar. If not, see <http://www.gnu.org/licenses/>
|
|
|
|
///
|
|
|
|
#include <gtest/gtest.h>
|
|
|
|
#include "run.h"
|
|
|
|
#include "log.h"
|
|
|
|
#include "node.h"
|
|
|
|
#include "conf.h"
|
|
|
|
|
|
|
|
class RunTest : public ::testing::Test {
|
|
|
|
protected:
|
2018-05-12 10:25:19 +00:00
|
|
|
virtual ~RunTest() noexcept override {}
|
|
|
|
|
2018-01-26 11:07:02 +00:00
|
|
|
void SetUp() override {
|
2018-04-20 11:18:10 +00:00
|
|
|
run.node = node;
|
2018-01-26 11:07:02 +00:00
|
|
|
}
|
2018-02-24 16:53:11 +00:00
|
|
|
void wait() {
|
|
|
|
int state = -1;
|
2018-08-03 11:36:24 +00:00
|
|
|
waitpid(run.current_pid.orDefault(0), &state, 0);
|
2018-02-24 16:53:11 +00:00
|
|
|
run.reaped(state);
|
|
|
|
}
|
2018-01-26 11:07:02 +00:00
|
|
|
void runAll() {
|
2018-02-24 16:53:11 +00:00
|
|
|
while(!run.step())
|
|
|
|
wait();
|
2018-01-26 11:07:02 +00:00
|
|
|
}
|
|
|
|
std::string readAllOutput() {
|
|
|
|
std::string res;
|
|
|
|
char tmp[64];
|
2018-07-20 11:15:59 +00:00
|
|
|
for(ssize_t n = read(run.output_fd, tmp, 64); n > 0; n = read(run.output_fd, tmp, 64))
|
2018-01-26 11:07:02 +00:00
|
|
|
res += std::string(tmp, n);
|
|
|
|
// strip the first "[laminar] executing.. line
|
|
|
|
return strchr(res.c_str(), '\n') + 1;
|
|
|
|
}
|
|
|
|
StringMap parseFromString(std::string content) {
|
|
|
|
char tmp[16] = "/tmp/lt.XXXXXX";
|
|
|
|
int fd = mkstemp(tmp);
|
|
|
|
write(fd, content.data(), content.size());
|
|
|
|
close(fd);
|
|
|
|
StringMap map = parseConfFile(tmp);
|
|
|
|
unlink(tmp);
|
|
|
|
return map;
|
|
|
|
}
|
|
|
|
|
|
|
|
class Run run;
|
2018-04-20 11:18:10 +00:00
|
|
|
std::shared_ptr<Node> node = std::shared_ptr<Node>(new Node);
|
2018-01-26 11:07:02 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
TEST_F(RunTest, WorkingDirectory) {
|
|
|
|
run.addScript("/bin/pwd", "/home");
|
|
|
|
runAll();
|
|
|
|
EXPECT_EQ("/home\n", readAllOutput());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RunTest, SuccessStatus) {
|
|
|
|
run.addScript("/bin/true");
|
|
|
|
runAll();
|
|
|
|
EXPECT_EQ(RunState::SUCCESS, run.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RunTest, FailedStatus) {
|
|
|
|
run.addScript("/bin/false");
|
|
|
|
runAll();
|
|
|
|
EXPECT_EQ(RunState::FAILED, run.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RunTest, Environment) {
|
|
|
|
run.name = "foo";
|
|
|
|
run.build = 1234;
|
|
|
|
run.laminarHome = "/tmp";
|
2018-01-27 11:06:06 +00:00
|
|
|
run.addScript("/usr/bin/env");
|
2018-01-26 11:07:02 +00:00
|
|
|
runAll();
|
|
|
|
StringMap map = parseFromString(readAllOutput());
|
|
|
|
EXPECT_EQ("1234", map["RUN"]);
|
|
|
|
EXPECT_EQ("foo", map["JOB"]);
|
|
|
|
EXPECT_EQ("success", map["RESULT"]);
|
|
|
|
EXPECT_EQ("unknown", map["LAST_RESULT"]);
|
|
|
|
EXPECT_EQ("/tmp/run/foo/workspace", map["WORKSPACE"]);
|
|
|
|
EXPECT_EQ("/tmp/archive/foo/1234", map["ARCHIVE"]);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RunTest, ParamsToEnv) {
|
|
|
|
run.params["foo"] = "bar";
|
2018-01-27 11:06:06 +00:00
|
|
|
run.addScript("/usr/bin/env");
|
2018-01-26 11:07:02 +00:00
|
|
|
runAll();
|
|
|
|
StringMap map = parseFromString(readAllOutput());
|
|
|
|
EXPECT_EQ("bar", map["foo"]);
|
|
|
|
}
|
2018-02-24 16:53:11 +00:00
|
|
|
|
|
|
|
TEST_F(RunTest, Abort) {
|
|
|
|
run.addScript("/usr/bin/yes");
|
|
|
|
run.step();
|
|
|
|
usleep(200); // TODO fix
|
|
|
|
run.abort();
|
|
|
|
wait();
|
|
|
|
EXPECT_EQ(RunState::ABORTED, run.result);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST_F(RunTest, AbortAfterFailed) {
|
|
|
|
run.addScript("/bin/false");
|
|
|
|
runAll();
|
|
|
|
run.addScript("/usr/bin/yes");
|
|
|
|
run.step();
|
|
|
|
usleep(200); // TODO fix
|
|
|
|
run.abort();
|
|
|
|
wait();
|
|
|
|
EXPECT_EQ(RunState::FAILED, run.result);
|
|
|
|
}
|