kubenix/modules/testing/evalTest.nix

129 lines
2.7 KiB
Nix
Raw Normal View History

2020-04-05 21:25:34 +07:00
{ lib, config, testing, kubenix, ... }:
with lib;
let
modules = [
# testing module
config.module
./test-options.nix
../base.nix
# passthru some options to test
{
config = {
kubenix.project = mkDefault config.name;
_module.args = {
inherit kubenix;
test = evaled.config;
2020-04-05 21:25:34 +07:00
} // testing.args;
};
}
];
# eval without checking
evaled' = kubenix.evalModules {
check = false;
inherit modules;
};
# test configuration
testConfig = evaled'.config.test;
# test features
testFeatures = evaled'.config._m.features;
# common options that can be applied on this test
commonOpts =
2021-05-13 17:27:08 -04:00
filter
(d:
(intersectLists d.features testFeatures) == d.features ||
(length d.features) == 0
)
testing.common;
2020-04-05 21:25:34 +07:00
# add common options modules to all modules
modulesWithCommonOptions = modules ++ (map (d: d.options) commonOpts);
2020-04-05 21:25:34 +07:00
# evaled test
2021-05-13 17:27:08 -04:00
evaled =
let
evaled' = kubenix.evalModules {
modules = modulesWithCommonOptions;
2021-05-13 17:27:08 -04:00
};
in
2020-04-05 21:25:34 +07:00
if testing.throwError then evaled'
else if (builtins.tryEval evaled'.config.test.assertions).success
then evaled' else null;
2021-05-13 17:27:08 -04:00
in
{
2020-04-05 21:25:34 +07:00
options = {
module = mkOption {
description = "Module defining kubenix test";
type = types.unspecified;
};
evaled = mkOption {
description = "Test evaulation result";
type = types.nullOr types.attrs;
internal = true;
};
success = mkOption {
description = "Whether test assertions were successfull";
type = types.bool;
internal = true;
default = false;
};
2021-05-06 16:07:24 -04:00
# transparently forwarded from the test's `test` attribute for ease of access
name = mkOption {
description = "test name";
type = types.str;
internal = true;
};
description = mkOption {
description = "test description";
type = types.str;
internal = true;
};
enable = mkOption {
description = "Whether to enable test";
type = types.bool;
internal = true;
};
2020-04-05 21:25:34 +07:00
assertions = mkOption {
description = "Test result";
type = types.unspecified;
internal = true;
2021-05-13 17:27:08 -04:00
default = [ ];
2020-04-05 21:25:34 +07:00
};
script = mkOption {
description = "Test script to use for e2e test";
type = types.nullOr (types.either types.lines types.path);
internal = true;
};
};
config = mkMerge [
{
inherit evaled;
2021-05-06 16:07:24 -04:00
inherit (testConfig) name description enable;
2020-04-05 21:25:34 +07:00
}
# if test is evaled check assertions
(mkIf (config.evaled != null) {
2021-05-06 16:07:24 -04:00
inherit (evaled.config.test) assertions script;
2020-04-05 21:25:34 +07:00
# if all assertions are true, test is successfull
success = all (el: el.assertion) config.assertions;
})
];
}