mirror of
https://github.com/ohwgiles/laminar.git
synced 2024-10-27 20:34:20 +00:00
add basic tests for conf, database and run
This commit is contained in:
parent
ae961b97cb
commit
5ff3bbe2bb
@ -88,6 +88,15 @@ target_link_libraries(laminard capnp-rpc capnp kj-async kj pthread boost_filesys
|
|||||||
add_executable(laminarc src/client.cpp laminar.capnp.c++)
|
add_executable(laminarc src/client.cpp laminar.capnp.c++)
|
||||||
target_link_libraries(laminarc capnp-rpc capnp kj-async kj pthread)
|
target_link_libraries(laminarc capnp-rpc capnp kj-async kj pthread)
|
||||||
|
|
||||||
|
## Tests
|
||||||
|
set(BUILD_TESTS FALSE CACHE BOOL "Build tests")
|
||||||
|
if(BUILD_TESTS)
|
||||||
|
find_package(GTest REQUIRED)
|
||||||
|
include_directories(${GTEST_INCLUDE_DIRS} src)
|
||||||
|
add_executable(laminar-tests src/conf.cpp src/database.cpp src/run.cpp test/test-conf.cpp test/test-database.cpp test/test-run.cpp)
|
||||||
|
target_link_libraries(laminar-tests ${GTEST_BOTH_LIBRARIES} kj sqlite3 boost_filesystem boost_system)
|
||||||
|
endif()
|
||||||
|
|
||||||
install(TARGETS laminard laminarc RUNTIME DESTINATION usr/bin)
|
install(TARGETS laminard laminarc RUNTIME DESTINATION usr/bin)
|
||||||
install(FILES laminar.service DESTINATION lib/systemd/system)
|
install(FILES laminar.service DESTINATION lib/systemd/system)
|
||||||
install(FILES laminar.conf DESTINATION etc)
|
install(FILES laminar.conf DESTINATION etc)
|
||||||
|
@ -41,7 +41,7 @@ Database::Statement::~Statement() {
|
|||||||
|
|
||||||
|
|
||||||
bool Database::Statement::exec() {
|
bool Database::Statement::exec() {
|
||||||
return sqlite3_step(stmt) == SQLITE_OK;
|
return sqlite3_step(stmt) == SQLITE_DONE;
|
||||||
}
|
}
|
||||||
|
|
||||||
void Database::Statement::bindValue(int i, int e) {
|
void Database::Statement::bindValue(int i, int e) {
|
||||||
|
@ -85,7 +85,7 @@ public:
|
|||||||
std::string log;
|
std::string log;
|
||||||
pid_t pid;
|
pid_t pid;
|
||||||
int fd;
|
int fd;
|
||||||
int procStatus;
|
int procStatus = 0;
|
||||||
std::unordered_map<std::string, std::string> params;
|
std::unordered_map<std::string, std::string> params;
|
||||||
|
|
||||||
time_t queuedAt;
|
time_t queuedAt;
|
||||||
|
63
test/test-conf.cpp
Normal file
63
test/test-conf.cpp
Normal file
@ -0,0 +1,63 @@
|
|||||||
|
///
|
||||||
|
/// 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 "conf.h"
|
||||||
|
|
||||||
|
class ConfTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
void SetUp() override {
|
||||||
|
fd = mkstemp(tmpFile);
|
||||||
|
}
|
||||||
|
void TearDown() override {
|
||||||
|
close(fd);
|
||||||
|
unlink(tmpFile);
|
||||||
|
}
|
||||||
|
void parseConf(std::string conf) {
|
||||||
|
lseek(fd, SEEK_SET, 0);
|
||||||
|
write(fd, conf.data(), conf.size());
|
||||||
|
cfg = parseConfFile(tmpFile);
|
||||||
|
}
|
||||||
|
StringMap cfg;
|
||||||
|
int fd;
|
||||||
|
char tmpFile[32] = "/tmp/lt.XXXXXX";
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(ConfTest, Empty) {
|
||||||
|
EXPECT_TRUE(cfg.empty());
|
||||||
|
parseConf("");
|
||||||
|
EXPECT_TRUE(cfg.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ConfTest, Comments) {
|
||||||
|
parseConf("#");
|
||||||
|
EXPECT_TRUE(cfg.empty());
|
||||||
|
parseConf("#foo=bar");
|
||||||
|
EXPECT_TRUE(cfg.empty());
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ConfTest, Parse) {
|
||||||
|
parseConf("foo=bar\nbar=3");
|
||||||
|
ASSERT_EQ(2, cfg.size());
|
||||||
|
EXPECT_EQ("bar", cfg.get("foo", std::string("fallback")));
|
||||||
|
EXPECT_EQ(3, cfg.get("bar", 0));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(ConfTest, Fallback) {
|
||||||
|
EXPECT_EQ("foo", cfg.get("test", std::string("foo")));
|
||||||
|
}
|
75
test/test-database.cpp
Normal file
75
test/test-database.cpp
Normal file
@ -0,0 +1,75 @@
|
|||||||
|
///
|
||||||
|
/// 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 "database.h"
|
||||||
|
|
||||||
|
class DatabaseTest : public ::testing::Test {
|
||||||
|
protected:
|
||||||
|
DatabaseTest() :
|
||||||
|
::testing::Test(),
|
||||||
|
db(":memory:")
|
||||||
|
{}
|
||||||
|
Database db;
|
||||||
|
};
|
||||||
|
|
||||||
|
TEST_F(DatabaseTest, Exec) {
|
||||||
|
EXPECT_FALSE(db.exec("garbage non-sql"));
|
||||||
|
EXPECT_TRUE(db.exec("create temporary table test(id int)"));
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DatabaseTest, Fetch) {
|
||||||
|
int n = 0;
|
||||||
|
db.stmt("select 2, 'cat', 4294967299").fetch<int, std::string, uint64_t>([&](int i, std::string s, uint64_t ui){
|
||||||
|
n++;
|
||||||
|
EXPECT_EQ(2, i);
|
||||||
|
EXPECT_EQ("cat", s);
|
||||||
|
EXPECT_EQ(4294967299, ui);
|
||||||
|
});
|
||||||
|
EXPECT_EQ(1, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DatabaseTest, Bind) {
|
||||||
|
int n = 0;
|
||||||
|
db.stmt("select ? * 2").bind(2).fetch<int>([&](int i){
|
||||||
|
n++;
|
||||||
|
EXPECT_EQ(4, i);
|
||||||
|
});
|
||||||
|
EXPECT_EQ(1, n);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DatabaseTest, Strings) {
|
||||||
|
std::string res;
|
||||||
|
db.stmt("select ? || ?").bind("a", "b").fetch<std::string>([&res](std::string s){
|
||||||
|
EXPECT_TRUE(res.empty());
|
||||||
|
res = s;
|
||||||
|
});
|
||||||
|
EXPECT_EQ("ab", res);
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_F(DatabaseTest, MultiRow) {
|
||||||
|
ASSERT_TRUE(db.exec("create table test(id int)"));
|
||||||
|
int i = 0;
|
||||||
|
while(i < 10)
|
||||||
|
EXPECT_TRUE(db.stmt("insert into test values(?)").bind(i++).exec());
|
||||||
|
i = 0;
|
||||||
|
db.stmt("select * from test").fetch<int>([&](int r){
|
||||||
|
EXPECT_EQ(i++, r);
|
||||||
|
});
|
||||||
|
EXPECT_EQ(10, i);
|
||||||
|
}
|
98
test/test-run.cpp
Normal file
98
test/test-run.cpp
Normal file
@ -0,0 +1,98 @@
|
|||||||
|
///
|
||||||
|
/// 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:
|
||||||
|
void SetUp() override {
|
||||||
|
run.node = &node;
|
||||||
|
}
|
||||||
|
void runAll() {
|
||||||
|
while(!run.step()) {
|
||||||
|
int state = -1;
|
||||||
|
waitpid(run.pid, &state, 0);
|
||||||
|
run.reaped(state);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
std::string readAllOutput() {
|
||||||
|
std::string res;
|
||||||
|
char tmp[64];
|
||||||
|
for(ssize_t n = read(run.fd, tmp, 64); n > 0; n = read(run.fd, tmp, 64))
|
||||||
|
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;
|
||||||
|
Node node;
|
||||||
|
};
|
||||||
|
|
||||||
|
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";
|
||||||
|
run.addScript("/bin/env");
|
||||||
|
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";
|
||||||
|
run.addScript("/bin/env");
|
||||||
|
runAll();
|
||||||
|
StringMap map = parseFromString(readAllOutput());
|
||||||
|
EXPECT_EQ("bar", map["foo"]);
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user