2021-10-12 15:18:44 +03:00
|
|
|
#include "machines.hh"
|
2023-10-25 07:43:36 +03:00
|
|
|
#include "file-system.hh"
|
|
|
|
#include "util.hh"
|
2021-10-12 15:18:44 +03:00
|
|
|
|
2024-05-23 06:12:23 +03:00
|
|
|
#include "tests/characterization.hh"
|
|
|
|
|
2024-01-23 22:36:03 +02:00
|
|
|
#include <gtest/gtest.h>
|
2021-10-12 15:18:44 +03:00
|
|
|
#include <gmock/gmock-matchers.h>
|
|
|
|
|
|
|
|
using testing::Contains;
|
|
|
|
using testing::ElementsAre;
|
|
|
|
using testing::Eq;
|
|
|
|
using testing::Field;
|
|
|
|
using testing::SizeIs;
|
|
|
|
|
2024-08-26 19:24:37 +03:00
|
|
|
namespace nix::fs { using namespace std::filesystem; }
|
|
|
|
|
2024-01-23 22:36:03 +02:00
|
|
|
using namespace nix;
|
2021-10-12 15:18:44 +03:00
|
|
|
|
|
|
|
TEST(machines, getMachinesWithEmptyBuilders) {
|
2024-05-23 06:12:23 +03:00
|
|
|
auto actual = Machine::parseConfig({}, "");
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesUriOnly) {
|
2024-05-23 06:12:23 +03:00
|
|
|
auto actual = Machine::parseConfig({"TEST_ARCH-TEST_OS"}, "nix@scratchy.labs.cs.uu.nl");
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
2024-01-23 22:36:03 +02:00
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, Eq(StoreReference::parse("ssh://nix@scratchy.labs.cs.uu.nl"))));
|
2021-10-12 15:18:44 +03:00
|
|
|
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-05-23 06:12:23 +03:00
|
|
|
auto actual = Machine::parseConfig({"TEST_ARCH-TEST_OS"}, "nix@scratchy.labs.cs.uu.nl - - - - - - -");
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
2024-01-23 22:36:03 +02:00
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, Eq(StoreReference::parse("ssh://nix@scratchy.labs.cs.uu.nl"))));
|
2021-10-12 15:18:44 +03:00
|
|
|
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)));
|
|
|
|
}
|
|
|
|
|
2024-01-23 22:36:03 +02:00
|
|
|
MATCHER_P(AuthorityMatches, authority, "") {
|
|
|
|
*result_listener
|
|
|
|
<< "where the authority of "
|
|
|
|
<< arg.render()
|
|
|
|
<< " is "
|
|
|
|
<< authority;
|
|
|
|
auto * generic = std::get_if<StoreReference::Specified>(&arg.variant);
|
|
|
|
if (!generic) return false;
|
|
|
|
return generic->authority == authority;
|
|
|
|
}
|
|
|
|
|
2021-10-12 15:18:44 +03:00
|
|
|
TEST(machines, getMachinesWithNewLineSeparator) {
|
2024-05-23 06:12:23 +03:00
|
|
|
auto actual = Machine::parseConfig({}, "nix@scratchy.labs.cs.uu.nl\nnix@itchy.labs.cs.uu.nl");
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(2));
|
2024-01-23 22:36:03 +02:00
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, AuthorityMatches("nix@scratchy.labs.cs.uu.nl"))));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, AuthorityMatches("nix@itchy.labs.cs.uu.nl"))));
|
2021-10-12 15:18:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithSemicolonSeparator) {
|
2024-05-23 06:12:23 +03:00
|
|
|
auto actual = Machine::parseConfig({}, "nix@scratchy.labs.cs.uu.nl ; nix@itchy.labs.cs.uu.nl");
|
2021-10-12 15:18:44 +03:00
|
|
|
EXPECT_THAT(actual, SizeIs(2));
|
2024-01-23 22:36:03 +02:00
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, AuthorityMatches("nix@scratchy.labs.cs.uu.nl"))));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, AuthorityMatches("nix@itchy.labs.cs.uu.nl"))));
|
2021-10-12 15:18:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithCorrectCompleteSingleBuilder) {
|
2024-05-23 06:12:23 +03:00
|
|
|
auto actual = Machine::parseConfig({},
|
|
|
|
"nix@scratchy.labs.cs.uu.nl i686-linux "
|
|
|
|
"/home/nix/.ssh/id_scratchy_auto 8 3 kvm "
|
|
|
|
"benchmark SSH+HOST+PUBLIC+KEY+BASE64+ENCODED==");
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
2024-01-23 22:36:03 +02:00
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, AuthorityMatches("nix@scratchy.labs.cs.uu.nl")));
|
2021-10-12 15:18:44 +03:00
|
|
|
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-05-23 06:12:23 +03:00
|
|
|
auto actual = Machine::parseConfig({},
|
2021-10-12 15:18:44 +03:00
|
|
|
"nix@scratchy.labs.cs.uu.nl\ti686-linux\t/home/nix/.ssh/"
|
|
|
|
"id_scratchy_auto\t8\t3\tkvm\tbenchmark\tSSH+HOST+PUBLIC+"
|
2024-05-23 06:12:23 +03:00
|
|
|
"KEY+BASE64+ENCODED==");
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
2024-01-23 22:36:03 +02:00
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, AuthorityMatches("nix@scratchy.labs.cs.uu.nl")));
|
2021-10-12 15:18:44 +03:00
|
|
|
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-05-23 06:12:23 +03:00
|
|
|
auto actual = Machine::parseConfig({},
|
|
|
|
"nix@scratchy.labs.cs.uu.nl Arch1,Arch2 - - - "
|
|
|
|
"SupportedFeature1,SupportedFeature2 "
|
|
|
|
"MandatoryFeature1,MandatoryFeature2");
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(1));
|
2024-01-23 22:36:03 +02:00
|
|
|
EXPECT_THAT(actual[0], Field(&Machine::storeUri, AuthorityMatches("nix@scratchy.labs.cs.uu.nl")));
|
2021-10-12 15:18:44 +03:00
|
|
|
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-05-23 06:12:23 +03:00
|
|
|
EXPECT_THROW(
|
|
|
|
Machine::parseConfig({}, "nix@scratchy.labs.cs.uu.nl - - eight"),
|
|
|
|
FormatError);
|
|
|
|
EXPECT_THROW(
|
|
|
|
Machine::parseConfig({}, "nix@scratchy.labs.cs.uu.nl - - -1"),
|
|
|
|
FormatError);
|
|
|
|
EXPECT_THROW(
|
|
|
|
Machine::parseConfig({}, "nix@scratchy.labs.cs.uu.nl - - 8 three"),
|
|
|
|
FormatError);
|
|
|
|
EXPECT_THROW(
|
|
|
|
Machine::parseConfig({}, "nix@scratchy.labs.cs.uu.nl - - 8 -3"),
|
|
|
|
UsageError);
|
|
|
|
EXPECT_THROW(
|
|
|
|
Machine::parseConfig({}, "nix@scratchy.labs.cs.uu.nl - - 8 3 - - BAD_BASE64"),
|
|
|
|
FormatError);
|
2021-10-12 15:18:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithCorrectFileReference) {
|
2024-08-26 19:24:37 +03:00
|
|
|
auto path = fs::weakly_canonical(getUnitTestData() / "machines/valid");
|
|
|
|
ASSERT_TRUE(fs::exists(path));
|
2021-10-12 15:18:44 +03:00
|
|
|
|
2024-08-26 19:24:37 +03:00
|
|
|
auto actual = Machine::parseConfig({}, "@" + path.string());
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(3));
|
2024-01-23 22:36:03 +02:00
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, AuthorityMatches("nix@scratchy.labs.cs.uu.nl"))));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, AuthorityMatches("nix@itchy.labs.cs.uu.nl"))));
|
|
|
|
EXPECT_THAT(actual, Contains(Field(&Machine::storeUri, AuthorityMatches("nix@poochie.labs.cs.uu.nl"))));
|
2021-10-12 15:18:44 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithCorrectFileReferenceToEmptyFile) {
|
2024-08-26 19:24:37 +03:00
|
|
|
fs::path path = "/dev/null";
|
|
|
|
ASSERT_TRUE(fs::exists(path));
|
2021-10-12 15:18:44 +03:00
|
|
|
|
2024-08-26 19:24:37 +03:00
|
|
|
auto actual = Machine::parseConfig({}, "@" + path.string());
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithIncorrectFileReference) {
|
2024-08-26 19:24:37 +03:00
|
|
|
auto path = fs::weakly_canonical("/not/a/file");
|
|
|
|
ASSERT_TRUE(!fs::exists(path));
|
|
|
|
auto actual = Machine::parseConfig({}, "@" + path.string());
|
2021-10-12 15:18:44 +03:00
|
|
|
ASSERT_THAT(actual, SizeIs(0));
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(machines, getMachinesWithCorrectFileReferenceToIncorrectFile) {
|
2024-05-23 06:12:23 +03:00
|
|
|
EXPECT_THROW(
|
2024-08-26 19:24:37 +03:00
|
|
|
Machine::parseConfig({}, "@" + fs::weakly_canonical(getUnitTestData() / "machines" / "bad_format").string()),
|
2024-05-23 06:12:23 +03:00
|
|
|
FormatError);
|
2021-10-12 15:18:44 +03:00
|
|
|
}
|