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

add basic tests for conf, database and run

This commit is contained in:
Oliver Giles
2018-01-26 13:07:02 +02:00
parent ae961b97cb
commit 5ff3bbe2bb
6 changed files with 247 additions and 2 deletions

63
test/test-conf.cpp Normal file
View 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
View 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
View 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"]);
}