2024-11-12 23:07:17 +00:00
|
|
|
#include "lix/libutil/file-system.hh"
|
|
|
|
#include "lix/libstore/machines.hh"
|
|
|
|
#include "lix/libstore/globals.hh"
|
|
|
|
#include "lix/libutil/strings.hh"
|
2024-04-08 19:43:35 +00:00
|
|
|
#include "tests/test-data.hh"
|
2021-10-12 12:18:44 +00:00
|
|
|
|
|
|
|
#include <gmock/gmock-matchers.h>
|
|
|
|
|
|
|
|
using testing::Contains;
|
|
|
|
using testing::ElementsAre;
|
|
|
|
using testing::EndsWith;
|
|
|
|
using testing::Eq;
|
|
|
|
using testing::Field;
|
|
|
|
using testing::SizeIs;
|
|
|
|
|
|
|
|
using nix::absPath;
|
|
|
|
using nix::FormatError;
|
2024-03-04 07:40:38 +00:00
|
|
|
using nix::UsageError;
|
2021-10-12 12:18:44 +00:00
|
|
|
using nix::getMachines;
|
|
|
|
using nix::Machine;
|
|
|
|
using nix::Machines;
|
|
|
|
using nix::pathExists;
|
|
|
|
using nix::settings;
|
|
|
|
|
|
|
|
class Environment : public ::testing::Environment {
|
|
|
|
public:
|
2024-09-02 18:09:35 +00:00
|
|
|
void SetUp() override { settings.thisSystem.override("TEST_ARCH-TEST_OS"); }
|
2021-10-12 12:18:44 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
testing::Environment* const foo_env =
|
|
|
|
testing::AddGlobalTestEnvironment(new Environment);
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithEmptyBuilders) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("");
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesUriOnly) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl");
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, Eq("ssh://nix@scratchy.labs.cs.uu.nl")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::systemTypes, ElementsAre("TEST_ARCH-TEST_OS")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::sshKey, SizeIs(0)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::maxJobs, Eq(1)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::speedFactor, Eq(1)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::supportedFeatures, SizeIs(0)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::mandatoryFeatures, SizeIs(0)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::sshPublicHostKey, SizeIs(0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesDefaults) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl - - - - - - -");
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, Eq("ssh://nix@scratchy.labs.cs.uu.nl")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::systemTypes, ElementsAre("TEST_ARCH-TEST_OS")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::sshKey, SizeIs(0)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::maxJobs, Eq(1)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::speedFactor, Eq(1)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::supportedFeatures, SizeIs(0)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::mandatoryFeatures, SizeIs(0)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::sshPublicHostKey, SizeIs(0)));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithNewLineSeparator) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl\nnix@itchy.labs.cs.uu.nl");
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(2));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, EndsWith("nix@scratchy.labs.cs.uu.nl"))));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, EndsWith("nix@itchy.labs.cs.uu.nl"))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithSemicolonSeparator) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl ; nix@itchy.labs.cs.uu.nl");
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
EXPECT_THAT(actual, SizeIs(2));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, EndsWith("nix@scratchy.labs.cs.uu.nl"))));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, EndsWith("nix@itchy.labs.cs.uu.nl"))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithCorrectCompleteSingleBuilder) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl i686-linux "
|
2021-10-12 12:18:44 +00:00
|
|
|
"/home/nix/.ssh/id_scratchy_auto 8 3 kvm "
|
2024-09-02 18:09:35 +00:00
|
|
|
"benchmark SSH+HOST+PUBLIC+KEY+BASE64+ENCODED==");
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, EndsWith("nix@scratchy.labs.cs.uu.nl")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::systemTypes, ElementsAre("i686-linux")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::sshKey, Eq("/home/nix/.ssh/id_scratchy_auto")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::maxJobs, Eq(8)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::speedFactor, Eq(3)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::supportedFeatures, ElementsAre("kvm")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::mandatoryFeatures, ElementsAre("benchmark")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::sshPublicHostKey, Eq("SSH+HOST+PUBLIC+KEY+BASE64+ENCODED==")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines,
|
|
|
|
getMachinesWithCorrectCompleteSingleBuilderWithTabColumnDelimiter) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override(
|
2021-10-12 12:18:44 +00:00
|
|
|
"nix@scratchy.labs.cs.uu.nl\ti686-linux\t/home/nix/.ssh/"
|
|
|
|
"id_scratchy_auto\t8\t3\tkvm\tbenchmark\tSSH+HOST+PUBLIC+"
|
2024-09-02 18:09:35 +00:00
|
|
|
"KEY+BASE64+ENCODED==");
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, EndsWith("nix@scratchy.labs.cs.uu.nl")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::systemTypes, ElementsAre("i686-linux")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::sshKey, Eq("/home/nix/.ssh/id_scratchy_auto")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::maxJobs, Eq(8)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::speedFactor, Eq(3)));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::supportedFeatures, ElementsAre("kvm")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::mandatoryFeatures, ElementsAre("benchmark")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::sshPublicHostKey, Eq("SSH+HOST+PUBLIC+KEY+BASE64+ENCODED==")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithMultiOptions) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl Arch1,Arch2 - - - "
|
2021-10-12 12:18:44 +00:00
|
|
|
"SupportedFeature1,SupportedFeature2 "
|
2024-09-02 18:09:35 +00:00
|
|
|
"MandatoryFeature1,MandatoryFeature2");
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, EndsWith("nix@scratchy.labs.cs.uu.nl")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::systemTypes, ElementsAre("Arch1", "Arch2")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::supportedFeatures, ElementsAre("SupportedFeature1", "SupportedFeature2")));
|
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::mandatoryFeatures, ElementsAre("MandatoryFeature1", "MandatoryFeature2")));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithIncorrectFormat) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl - - eight");
|
2021-10-12 12:18:44 +00:00
|
|
|
EXPECT_THROW(getMachines(), FormatError);
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl - - -1");
|
2021-10-12 12:18:44 +00:00
|
|
|
EXPECT_THROW(getMachines(), FormatError);
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl - - 8 three");
|
2021-10-12 12:18:44 +00:00
|
|
|
EXPECT_THROW(getMachines(), FormatError);
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl - - 8 -3");
|
2024-03-04 07:40:38 +00:00
|
|
|
EXPECT_THROW(getMachines(), UsageError);
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override("nix@scratchy.labs.cs.uu.nl - - 8 3 - - BAD_BASE64");
|
2021-10-12 12:18:44 +00:00
|
|
|
EXPECT_THROW(getMachines(), FormatError);
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithCorrectFileReference) {
|
2024-04-08 19:43:35 +00:00
|
|
|
auto path = nix::getUnitTestDataPath("machines.valid");
|
2021-10-12 12:18:44 +00:00
|
|
|
ASSERT_TRUE(pathExists(path));
|
|
|
|
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override(std::string("@") + path);
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(3));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, EndsWith("nix@scratchy.labs.cs.uu.nl"))));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, EndsWith("nix@itchy.labs.cs.uu.nl"))));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, EndsWith("nix@poochie.labs.cs.uu.nl"))));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithCorrectFileReferenceToEmptyFile) {
|
|
|
|
auto path = "/dev/null";
|
|
|
|
ASSERT_TRUE(pathExists(path));
|
|
|
|
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override(std::string("@") + path);
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithIncorrectFileReference) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override(std::string("@") + absPath("/not/a/file"));
|
2021-10-12 12:18:44 +00:00
|
|
|
Machines actual = getMachines();
|
|
|
|
ASSERT_THAT(actual, SizeIs(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithCorrectFileReferenceToIncorrectFile) {
|
2024-09-02 18:09:35 +00:00
|
|
|
settings.builders.override(std::string("@") + nix::getUnitTestDataPath("machines.bad_format"));
|
2021-10-12 12:18:44 +00:00
|
|
|
EXPECT_THROW(getMachines(), FormatError);
|
|
|
|
}
|