feat: refactor tests

This commit is contained in:
Jaka Hudoklin 2019-02-17 19:42:01 +01:00
parent 0d68a401f0
commit b4c4f17cc9
No known key found for this signature in database
GPG key ID: 916062A1C4748647
10 changed files with 3 additions and 2 deletions

42
tests/default.nix Normal file
View file

@ -0,0 +1,42 @@
{ pkgs ? import <nixpkgs> {}
, kubenix ? import ../. {inherit pkgs;}
, lib ? kubenix.lib
, k8sVersions ? ["1.7" "1.8" "1.9" "1.10" "1.11" "1.12" "1.13"]
# whether any testing error should throw an error
, throwError ? true }:
with lib;
listToAttrs (map (version: let
version' = replaceStrings ["."] ["_"] version;
in nameValuePair "v${version'}" (evalModules {
modules = [
kubenix.testing
{
imports = [kubenix.k8s kubenix.submodules];
kubernetes.version = version;
testing.throwError = throwError;
testing.tests = [
./k8s/simple.nix
./k8s/deployment.nix
./k8s/crd.nix
./k8s/1.13/crd.nix
./submodules/simple.nix
];
testing.defaults = ({kubenix, ...}: {
imports = [kubenix.k8s];
kubernetes.version = version;
});
}
];
args = {
inherit pkgs;
};
specialArgs = {
inherit kubenix;
};
}).config.testing.result) k8sVersions)

40
tests/k8s/1.13/crd.nix Normal file
View file

@ -0,0 +1,40 @@
{ config, lib, kubenix, ... }:
with lib;
let
cfg = config.kubernetes.api.customresourcedefinitions.crontabs;
in {
imports = [
kubenix.k8s
];
test = {
name = "k8s/1.13/crd";
description = "Simple test tesing CRD for k8s 1.13";
enable = builtins.compareVersions config.kubernetes.version "1.13" >= 0;
assertions = [{
message = "should have versions set";
assertion = (head cfg.spec.versions).name == "v1";
}];
};
kubernetes.api.customresourcedefinitions.crontabs = {
metadata.name = "crontabs.stable.example.com";
spec = {
group = "stable.example.com";
versions = [{
name = "v1";
served = true;
storage = true;
}];
scope = "Namespaced";
names = {
plural = "crontabs";
singular = "crontab";
kind = "CronTab";
shortNames = ["ct"];
};
};
};
}

36
tests/k8s/crd.nix Normal file
View file

@ -0,0 +1,36 @@
{ config, lib, kubenix, ... }:
with lib;
let
cfg = config.kubernetes.api.customresourcedefinitions.crontabs;
in {
imports = [
kubenix.k8s
];
test = {
name = "k8s/crd";
description = "Simple test tesing CRD";
enable = builtins.compareVersions config.kubernetes.version "1.8" >= 0;
assertions = [{
message = "should have group set";
assertion = cfg.spec.group == "stable.example.com";
}];
};
kubernetes.api.customresourcedefinitions.crontabs = {
metadata.name = "crontabs.stable.example.com";
spec = {
group = "stable.example.com";
version = "v1";
scope = "Namespaced";
names = {
plural = "crontabs";
singular = "crontab";
kind = "CronTab";
shortNames = ["ct"];
};
};
};
}

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

@ -0,0 +1,34 @@
{ config, test, kubenix, ... }:
let
cfg = config.kubernetes.api.deployments.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.deployments.nginx = {
spec = {
replicas = 10;
selector.matchLabels.app = "nginx";
template.metadata.labels.app = "nginx";
template.spec = {
containers.nginx = {
image = "nginx";
};
};
};
};
}

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

@ -0,0 +1,23 @@
{ config, test, kubenix, ... }:
let
cfg = config.kubernetes.api.pods.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.pods.nginx = {};
}

View file

@ -0,0 +1,41 @@
{ config, lib, kubenix, ... }:
with lib;
{
imports = [
kubenix.submodules
];
test = {
name = "submodules/simple";
description = "Simple k8s submodule test";
assertions = [{
message = "Submodule name is set";
assertion = config.submodules.instances.empty.name == "empty";
} {
message = "Submodule version is set";
assertion = config.submodules.instances.empty.version == null;
} {
message = "Submodule config has submodule definition";
assertion = config.submodules.instances.empty.config.submodule.name == "empty";
} {
message = "Should have argument set";
assertion = config.submodules.instances.empty.config.args.value == "test";
}];
};
submodules.imports = [{
module = {
config.submodule.name = "empty";
options.args.value = mkOption {
description = "Submodule argument";
type = types.str;
};
};
}];
submodules.instances.empty = {
config.args.value = "test";
};
}