feat: initial testing support

This commit is contained in:
Jaka Hudoklin 2019-02-12 16:22:18 +01:00
parent e286c9b0e8
commit 9f8ca8447e
No known key found for this signature in database
GPG key ID: 6A08896BFD32BD95
6 changed files with 362 additions and 127 deletions

28
test/default.nix Normal file
View file

@ -0,0 +1,28 @@
{ pkgs ? import <nixpkgs> {}
, kubenix ? import ../. {inherit pkgs;}
, lib ? kubenix.lib
# whether any testing error should throw an error
, throwError ? true }:
with lib;
(evalModules {
modules = [
./modules/testing.nix
{
testing.throwError = throwError;
testing.tests = [
./k8s/simple.nix
./k8s/deployment.nix
];
}
];
args = {
inherit pkgs;
};
specialArgs = {
inherit kubenix;
};
}).config.testing.result

34
test/k8s/deployment.nix Normal file
View file

@ -0,0 +1,34 @@
{ config, test, kubenix, ... }:
let
cfg = config.kubernetes.api.Deployment.nginx;
in {
imports = [
kubenix.k8s
];
test = {
name = "k8s/deployment/simple";
description = "Simple k8s testing a simple deployment";
assertions = [{
message = "should have correct apiVersion and kind set";
assertion = cfg.apiVersion == "apps/v1" && cfg.kind == "Deployment";
} {
message = "should have replicas set";
assertion = cfg.spec.replicas == 10;
}];
};
kubernetes.api.Deployment.nginx = {
spec = {
replicas = 10;
selector.matchLabels.app = "nginx";
template.metadata.labels.app = "nginx";
template.spec = {
containers.nginx = {
image = "nginx";
};
};
};
};
}

23
test/k8s/simple.nix Normal file
View file

@ -0,0 +1,23 @@
{ config, test, kubenix, ... }:
let
cfg = config.kubernetes.api.Pod.nginx;
in {
imports = [
kubenix.k8s
];
test = {
name = "k8s/simple";
description = "Simple k8s testing wheter name, apiVersion and kind are preset";
assertions = [{
message = "should have apiVersion and kind set";
assertion = cfg.apiVersion == "v1" && cfg.kind == "Pod";
} {
message = "should have name set";
assertion = cfg.metadata.name == "nginx";
}];
};
kubernetes.api.Pod.nginx = {};
}

41
test/modules/test.nix Normal file
View file

@ -0,0 +1,41 @@
{ lib, ... }:
with lib;
{
options.test = {
name = mkOption {
description = "Test name";
type = types.str;
};
description = mkOption {
description = "Test description";
type = types.str;
};
assertions = mkOption {
type = types.listOf (types.submodule {
options = {
assertion = mkOption {
description = "assertion value";
type = types.bool;
default = false;
};
message = mkOption {
description = "assertion message";
type = types.str;
};
};
});
default = [];
example = [ { assertion = false; message = "you can't enable this for that reason"; } ];
description = ''
This option allows modules to express conditions that must
hold for the evaluation of the system configuration to
succeed, along with associated error messages for the user.
'';
};
};
}

101
test/modules/testing.nix Normal file
View file

@ -0,0 +1,101 @@
{ config, pkgs, lib, kubenix, ... }:
with lib;
let
cfg = config.testing;
in {
options = {
testing.throwError = mkOption {
description = "Whether to throw error";
type = types.bool;
default = true;
};
testing.tests = mkOption {
description = "Attribute set of test cases";
default = [];
type = types.listOf (types.coercedTo types.path (module: {inherit module;}) (types.submodule ({config, ...}: let
modules = [config.module ./test.nix {
config._module.args.test = config;
}];
test = (kubenix.evalKubernetesModules {
check = false;
inherit modules;
}).config.test;
evaled = builtins.trace "testing ${test.name}" (kubenix.evalKubernetesModules {
inherit modules;
});
in {
options = {
module = mkOption {
description = "Module defining submodule";
type = types.unspecified;
};
name = mkOption {
description = "test name";
type = types.str;
internal = true;
};
description = mkOption {
description = "test description";
type = types.str;
internal = true;
};
evaled = mkOption {
description = "Wheter test was evaled";
type = types.bool;
default =
if cfg.throwError
then if evaled.config.test.assertions != [] then true else true
else (builtins.tryEval evaled.config.test.assertions).success;
internal = true;
};
success = mkOption {
description = "Whether test was success";
type = types.bool;
internal = true;
default = false;
};
assertions = mkOption {
description = "Test result";
type = types.unspecified;
internal = true;
default = [];
};
};
config = {
inherit (test) name description;
assertions = mkIf config.evaled evaled.config.test.assertions;
success = mkIf config.evaled (all (el: el.assertion) config.assertions);
};
})));
};
testing.success = mkOption {
description = "Whether testing was a success";
type = types.bool;
default = all (test: test.success) cfg.tests;
};
testing.result = mkOption {
description = "Testing result";
type = types.package;
default = pkgs.writeText "testing-report.json" (builtins.toJSON {
success = cfg.success;
tests = map (test: {
inherit (test) name description evaled success;
assertions = moduleToAttrs test.assertions;
}) cfg.tests;
});
};
};
}