mirror of
https://github.com/TECHNOFAB11/kubenix.git
synced 2025-12-12 08:00:06 +01:00
feat: initial testing support
This commit is contained in:
parent
e286c9b0e8
commit
9f8ca8447e
6 changed files with 362 additions and 127 deletions
14
release.nix
14
release.nix
|
|
@ -1,6 +1,10 @@
|
|||
{pkgs ? import <nixpkgs> {}}:
|
||||
|
||||
let
|
||||
kubenix = import ./. { inherit pkgs; };
|
||||
|
||||
lib = kubenix.lib;
|
||||
|
||||
generateK8S = path: import ./k8s/generator.nix {
|
||||
inherit pkgs;
|
||||
inherit (pkgs) lib;
|
||||
|
|
@ -12,8 +16,6 @@ let
|
|||
inherit (pkgs) lib;
|
||||
inherit spec;
|
||||
};
|
||||
|
||||
kubenix = import ./. { inherit pkgs; };
|
||||
in {
|
||||
generate.k8s = pkgs.linkFarm "k8s-generated.nix" [{
|
||||
name = "v1.7.nix";
|
||||
|
|
@ -34,7 +36,12 @@ in {
|
|||
path = generateIstio ./istio/istio-schema.json;
|
||||
}];
|
||||
|
||||
test = kubenix.buildResources ({lib, config, kubenix, ...}: with lib; {
|
||||
test = import ./test {
|
||||
inherit pkgs lib kubenix;
|
||||
};
|
||||
|
||||
test-old = kubenix.buildResources ({
|
||||
module = {lib, config, kubenix, ...}: with lib; {
|
||||
imports = [
|
||||
kubenix.k8s
|
||||
kubenix.submodules
|
||||
|
|
@ -181,5 +188,6 @@ in {
|
|||
|
||||
#kubernetes.customResources = config.modules.nginx1.kubernetes.customResources;
|
||||
};
|
||||
};
|
||||
});
|
||||
}
|
||||
|
|
|
|||
28
test/default.nix
Normal file
28
test/default.nix
Normal 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
34
test/k8s/deployment.nix
Normal 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
23
test/k8s/simple.nix
Normal 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
41
test/modules/test.nix
Normal 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
101
test/modules/testing.nix
Normal 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;
|
||||
});
|
||||
};
|
||||
};
|
||||
}
|
||||
Loading…
Add table
Add a link
Reference in a new issue