feat: refactor testing

This commit is contained in:
Jaka Hudoklin 2019-02-25 17:16:24 +01:00
parent 937ce15748
commit 9bc2406ff2
No known key found for this signature in database
GPG key ID: 6A08896BFD32BD95
7 changed files with 158 additions and 163 deletions

View file

@ -10,6 +10,8 @@
with lib;
let
images = pkgs.callPackage ./images.nix {};
tests = listToAttrs (map (version: let
version' = replaceStrings ["."] ["_"] version;
in nameValuePair "v${version'}" (evalModules {
@ -28,11 +30,13 @@ let
./k8s/deployment.nix
./k8s/crd.nix
./k8s/1.13/crd.nix
./k8s/submodule.nix
./submodules/simple.nix
];
testing.defaults = ({kubenix, ...}: {
imports = [kubenix.k8s];
kubernetes.version = version;
_module.args.images = images;
});
}
];
@ -43,7 +47,4 @@ let
inherit kubenix;
};
}).config) k8sVersions);
in {
inherit tests;
results = mapAttrs (_: test: test.testing.result) tests;
}
in tests

45
tests/images.nix Normal file
View file

@ -0,0 +1,45 @@
{ pkgs, dockerTools, lib, ... }:
with lib;
{
nginx = let
nginxPort = "80";
nginxConf = pkgs.writeText "nginx.conf" ''
user nginx nginx;
daemon off;
error_log /dev/stdout info;
pid /dev/null;
events {}
http {
access_log /dev/stdout;
server {
listen ${nginxPort};
index index.html;
location / {
root ${nginxWebRoot};
}
}
}
'';
nginxWebRoot = pkgs.writeTextDir "index.html" ''
<html><body><h1>Hello from NGINX</h1></body></html>
'';
in dockerTools.buildLayeredImage {
name = "xtruder/nginx";
tag = "latest";
contents = [pkgs.nginx];
extraCommands = ''
mkdir etc
chmod u+w etc
echo "nginx:x:1000:1000::/:" > etc/passwd
echo "nginx:x:1000:nginx" > etc/group
'';
config = {
Cmd = ["nginx" "-c" nginxConf];
ExposedPorts = {
"${nginxPort}/tcp" = {};
};
};
};
}

View file

@ -17,7 +17,7 @@ in {
message = "should have group set";
assertion = cfg.spec.group == "stable.example.com";
}];
check = ''
testScript = ''
$kube->waitUntilSucceeds("kubectl apply -f ${toYAML config.kubernetes.generated}");
$kube->succeed("kubectl get crds | grep -i crontabs");
$kube->succeed("kubectl get crontabs | grep -i crontab");

View file

@ -1,56 +1,17 @@
{ config, lib, pkgs, test, kubenix, ... }:
{ config, lib, pkgs, test, kubenix, images, ... }:
with lib;
let
cfg = config.kubernetes.api.deployments.nginx;
nginxImage = let
nginxPort = "80";
nginxConf = pkgs.writeText "nginx.conf" ''
user nginx nginx;
daemon off;
error_log /dev/stdout info;
pid /dev/null;
events {}
http {
access_log /dev/stdout;
server {
listen ${nginxPort};
index index.html;
location / {
root ${nginxWebRoot};
}
}
}
'';
nginxWebRoot = pkgs.writeTextDir "index.html" ''
<html><body><h1>Hello from NGINX</h1></body></html>
'';
in pkgs.dockerTools.buildLayeredImage {
name = "xtruder/nginx";
tag = "latest";
contents = [pkgs.nginx];
extraCommands = ''
mkdir etc
chmod u+w etc
echo "nginx:x:1000:1000::/:" > etc/passwd
echo "nginx:x:1000:nginx" > etc/group
'';
config = {
Cmd = ["nginx" "-c" nginxConf];
ExposedPorts = {
"${nginxPort}/tcp" = {};
};
};
};
image = images.nginx;
in {
imports = [
kubenix.k8s
];
test = {
name = "k8s-deployment-simple";
name = "k8s-deployment";
description = "Simple k8s testing a simple deployment";
assertions = [{
message = "should have correct apiVersion and kind set";
@ -59,8 +20,8 @@ in {
message = "should have replicas set";
assertion = cfg.spec.replicas == 10;
}];
check = ''
$kube->waitUntilSucceeds("docker load < ${nginxImage}");
testScript = ''
$kube->waitUntilSucceeds("docker load < ${image}");
$kube->waitUntilSucceeds("kubectl apply -f ${toYAML config.kubernetes.generated}");
$kube->succeed("kubectl get deployment | grep -i nginx");
@ -76,7 +37,7 @@ in {
template.metadata.labels.app = "nginx";
template.spec = {
containers.nginx = {
image = "xtruder/nginx:latest";
image = "${image.imageName}:${image.imageTag}";
imagePullPolicy = "Never";
};
};

View file

@ -10,7 +10,7 @@ in {
];
test = {
name = "submodules/simple";
name = "submodules-simple";
description = "Simple k8s submodule test";
assertions = [{
message = "Submodule name is set";