diff --git a/CMakeLists.txt b/CMakeLists.txt index 04e7374..b3d73d6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -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++) 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(FILES laminar.service DESTINATION lib/systemd/system) install(FILES laminar.conf DESTINATION etc) diff --git a/src/database.cpp b/src/database.cpp index 528d14c..b26ed4d 100644 --- a/src/database.cpp +++ b/src/database.cpp @@ -41,7 +41,7 @@ Database::Statement::~Statement() { bool Database::Statement::exec() { - return sqlite3_step(stmt) == SQLITE_OK; + return sqlite3_step(stmt) == SQLITE_DONE; } void Database::Statement::bindValue(int i, int e) { diff --git a/src/run.h b/src/run.h index def04e3..4b16e86 100644 --- a/src/run.h +++ b/src/run.h @@ -85,7 +85,7 @@ public: std::string log; pid_t pid; int fd; - int procStatus; + int procStatus = 0; std::unordered_map params; time_t queuedAt; diff --git a/test/test-conf.cpp b/test/test-conf.cpp new file mode 100644 index 0000000..bfde12c --- /dev/null +++ b/test/test-conf.cpp @@ -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 +/// +#include +#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"))); +} diff --git a/test/test-database.cpp b/test/test-database.cpp new file mode 100644 index 0000000..f74709c --- /dev/null +++ b/test/test-database.cpp @@ -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 +/// +#include +#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 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 i){ + n++; + EXPECT_EQ(4, i); + }); + EXPECT_EQ(1, n); +} + +TEST_F(DatabaseTest, Strings) { + std::string res; + db.stmt("select ? || ?").bind("a", "b").fetch([&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 r){ + EXPECT_EQ(i++, r); + }); + EXPECT_EQ(10, i); +} diff --git a/test/test-run.cpp b/test/test-run.cpp new file mode 100644 index 0000000..7da9a31 --- /dev/null +++ b/test/test-run.cpp @@ -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 +/// +#include +#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"]); +}