kubenix/modules/testing/evalTest.nix

135 lines
2.8 KiB
Nix
Raw Normal View History

2022-04-02 12:40:35 -07:00
{
lib,
config,
testing,
kubenix,
...
}:
with lib; let
2020-04-05 21:25:34 +07:00
modules = [
# testing module
config.module
./test-options.nix
../base.nix
# passthru some options to test
{
config = {
kubenix.project = mkDefault config.name;
2022-04-02 12:40:35 -07:00
_module.args =
{
inherit kubenix;
test = evaled.config;
}
// testing.args;
2020-04-05 21:25:34 +07:00
};
}
];
# 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
2022-04-02 12:40:35 -07:00
(
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
2022-04-02 12:40:35 -07:00
evaled = let
evaled' = kubenix.evalModules {
modules = modulesWithCommonOptions;
};
in
if testing.doThrowError
then evaled'
2020-04-05 21:25:34 +07:00
else if (builtins.tryEval evaled'.config.test.assertions).success
2022-04-02 12:40:35 -07:00
then evaled'
else null;
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;
2022-04-02 12:40:35 -07: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;
})
];
}