1
0
mirror of https://github.com/ohwgiles/laminar.git synced 2026-03-02 03:40:21 +00:00

Replace boost/filesystem with kj/filesystem

Lose the boost dependency since recent versions of capnproto's kj
also provide a nice filesystem library. Take the opportunity to
refactor the Run object to become more than POD and to encapsulate
some of the functionality that was done in the Laminar class

Part of #49 refactor
This commit is contained in:
Oliver Giles
2018-09-28 10:36:10 +03:00
parent fe57d63623
commit 08b3f25a22
14 changed files with 332 additions and 308 deletions

45
test/tempdir.h Normal file
View File

@@ -0,0 +1,45 @@
///
/// 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/>
///
#ifndef LAMINAR_TEMPDIR_H_
#define LAMINAR_TEMPDIR_H_
#include <kj/filesystem.h>
#include <stdlib.h>
class TempDir {
public:
TempDir() :
path(mkdtemp()),
fs(kj::newDiskFilesystem()->getRoot().openSubdir(path, kj::WriteMode::CREATE|kj::WriteMode::MODIFY))
{
}
~TempDir() noexcept {
kj::newDiskFilesystem()->getRoot().remove(path);
}
kj::Path path;
kj::Own<const kj::Directory> fs;
private:
static kj::Path mkdtemp() {
char dir[] = "/tmp/laminar-test-XXXXXX";
::mkdtemp(dir);
return kj::Path::parse(&dir[1]);
}
};
#endif // LAMINAR_TEMPDIR_H_

View File

@@ -27,8 +27,12 @@ public:
std::string payload;
};
class LaminarTest : public ::testing::Test {
class LaminarTest : public testing::Test {
protected:
LaminarTest() :
testing::Test(),
laminar("/tmp")
{}
Laminar laminar;
};

View File

@@ -21,23 +21,31 @@
#include "log.h"
#include "node.h"
#include "conf.h"
#include "tempdir.h"
class RunTest : public ::testing::Test {
class RunTest : public testing::Test {
protected:
virtual ~RunTest() noexcept override {}
void SetUp() override {
run.node = node;
RunTest() :
testing::Test(),
node(std::make_shared<Node>()),
tmp(),
run("foo", ParamMap{}, tmp.path.clone())
{
}
~RunTest() noexcept {}
void wait() {
int state = -1;
waitpid(run.current_pid.orDefault(0), &state, 0);
run.reaped(state);
}
void runAll() {
while(!run.step())
wait();
}
std::string readAllOutput() {
std::string res;
char tmp[64];
@@ -56,53 +64,66 @@ protected:
return map;
}
std::shared_ptr<Node> node;
TempDir tmp;
class Run run;
std::shared_ptr<Node> node = std::shared_ptr<Node>(new Node);
void setRunLink(const char* path) {
tmp.fs->symlink(kj::Path{"cfg","jobs",run.name+".run"}, path, kj::WriteMode::CREATE|kj::WriteMode::CREATE_PARENT|kj::WriteMode::EXECUTABLE);
}
};
TEST_F(RunTest, WorkingDirectory) {
run.addScript("/bin/pwd", "/home");
setRunLink("/bin/pwd");
run.configure(1, node, *tmp.fs);
runAll();
EXPECT_EQ("/home\n", readAllOutput());
std::string cwd{tmp.path.append(kj::Path{"run","foo","1"}).toString(true).cStr()};
EXPECT_EQ(cwd + "\n", readAllOutput());
}
TEST_F(RunTest, SuccessStatus) {
run.addScript("/bin/true");
setRunLink("/bin/true");
run.configure(1, node, *tmp.fs);
runAll();
EXPECT_EQ(RunState::SUCCESS, run.result);
}
TEST_F(RunTest, FailedStatus) {
run.addScript("/bin/false");
setRunLink("/bin/false");
run.configure(1, node, *tmp.fs);
runAll();
EXPECT_EQ(RunState::FAILED, run.result);
}
TEST_F(RunTest, Environment) {
run.name = "foo";
run.build = 1234;
run.laminarHome = "/tmp";
run.addScript("/usr/bin/env");
setRunLink("/usr/bin/env");
run.configure(1234, node, *tmp.fs);
runAll();
std::string ws{tmp.path.append(kj::Path{"run","foo","workspace"}).toString(true).cStr()};
std::string archive{tmp.path.append(kj::Path{"archive","foo","1234"}).toString(true).cStr()};
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"]);
EXPECT_EQ(ws, map["WORKSPACE"]);
EXPECT_EQ(archive, map["ARCHIVE"]);
}
TEST_F(RunTest, ParamsToEnv) {
setRunLink("/usr/bin/env");
run.params["foo"] = "bar";
run.addScript("/usr/bin/env");
run.configure(1, node, *tmp.fs);
runAll();
StringMap map = parseFromString(readAllOutput());
EXPECT_EQ("bar", map["foo"]);
}
TEST_F(RunTest, Abort) {
run.addScript("/usr/bin/yes");
setRunLink("/usr/bin/yes");
run.configure(1, node, *tmp.fs);
run.step();
usleep(200); // TODO fix
run.abort(false);
@@ -110,13 +131,3 @@ TEST_F(RunTest, Abort) {
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(false);
wait();
EXPECT_EQ(RunState::FAILED, run.result);
}

View File

@@ -18,28 +18,13 @@
///
#include <gtest/gtest.h>
#include <gmock/gmock.h>
#include <boost/filesystem.hpp>
#include <thread>
#include <sys/socket.h>
#include "server.h"
#include "log.h"
#include "interface.h"
#include "laminar.capnp.h"
namespace fs = boost::filesystem;
class TempDir : public fs::path {
public:
TempDir(const char* tmpl) {
char* t = strdup(tmpl);
mkdtemp(t);
*static_cast<fs::path*>(this) = t;
free(t);
}
~TempDir() {
fs::remove_all(*this);
}
};
#include "tempdir.h"
class MockLaminar : public LaminarInterface {
public:
@@ -57,7 +42,7 @@ public:
}
// MOCK_METHOD does not seem to work with return values whose destructors have noexcept(false)
kj::Own<MappedFile> getArtefact(std::string path) override { return kj::Own<MappedFile>(nullptr, kj::NullDisposer()); }
kj::Maybe<kj::Own<const kj::ReadableFile>> getArtefact(std::string path) override { return nullptr; }
MOCK_METHOD2(queueJob, std::shared_ptr<Run>(std::string name, ParamMap params));
MOCK_METHOD1(registerWaiter, void(LaminarWaiter* waiter));
@@ -72,14 +57,10 @@ public:
class ServerTest : public ::testing::Test {
protected:
ServerTest() :
tempDir("/tmp/laminar-test-XXXXXX")
{
}
void SetUp() override {
EXPECT_CALL(mockLaminar, registerWaiter(testing::_));
EXPECT_CALL(mockLaminar, deregisterWaiter(testing::_));
server = new Server(mockLaminar, "unix:"+fs::path(tempDir/"rpc.sock").string(), "127.0.0.1:8080");
server = new Server(mockLaminar, "unix:"+std::string(tempDir.path.append("rpc.sock").toString(true).cStr()), "127.0.0.1:8080");
}
void TearDown() override {
delete server;