feat(examples/nginx-deployment): minor refactor, fixes and e2e test

This commit is contained in:
Jaka Hudoklin 2019-03-07 23:24:28 +01:00
parent eac2d78667
commit fc9abaa519
No known key found for this signature in database
GPG key ID: 6A08896BFD32BD95
4 changed files with 54 additions and 14 deletions

View file

@ -5,10 +5,10 @@ image
## Usage ## Usage
### Building and applying kubernetes yaml file ### Building and applying kubernetes configuration
``` ```
nix-instantiate --eval --strict --json -A listObject | kubectl apply -f - nix-instantiate --eval --strict --json -A result | kubectl apply -f -
``` ```
### Building and pushing docker images ### Building and pushing docker images
@ -16,3 +16,13 @@ nix-instantiate --eval --strict --json -A listObject | kubectl apply -f -
``` ```
nix run -f ./. pushDockerImages -c copy-docker-images nix run -f ./. pushDockerImages -c copy-docker-images
``` ```
### Running tests
Test will spawn vm with kubernetes and run test script, which checks if everyting
works as expected.
```
nix build -f ./. test
cat result | jq '.'
```

View file

@ -1,26 +1,36 @@
{ kubenix ? import ../.. {} }: { kubenix ? import ../.. {}, registry ? "docker.io/gatehub" }:
with kubenix.lib; with kubenix.lib;
let rec {
registry = "docker.io/gatehub";
in rec {
# evaluated configuration # evaluated configuration
config = (kubenix.evalModules { config = (kubenix.evalModules {
modules = [ modules = [
./module.nix ./module.nix
{ docker.registry.url = registry; } { docker.registry.url = registry; }
kubenix.modules.testing
{
testing.tests = [ ./test.nix ];
testing.defaults = ({ lib, ... }: with lib; {
docker.registry.url = mkForce "";
kubernetes.version = config.kubernetes.version;
});
}
]; ];
}).config; }).config;
# list of kubernetes objects # e2e test
objects = config.kubernetes.objects; test = config.testing.result;
# nixos test script for running the test
test-script = config.testing.testsByName.nginx-deployment.test;
# hashed kubernetes List object # hashed kubernetes List object
listObject = k8s.mkHashedList { items = config.kubernetes.objects; }; result = k8s.mkHashedList { items = config.kubernetes.objects; };
# YAML file you can deploy to kubernetes # YAML file you can deploy to kubernetes
yaml = toYAML listObject; yaml = toYAML k8s-result;
# Exported docker images # Exported docker images
images = config.docker.export; images = config.docker.export;

View file

@ -1,9 +1,9 @@
{ config, lib, pkgs, test, kubenix, ... }: { config, lib, pkgs, kubenix, ... }:
with lib; with lib;
let let
nginx = pkgs.callPackage ./image.nix { }; nginx = pkgs.callPackage ./image.nix { };
in { in {
imports = [ kubenix.module ]; imports = [ kubenix.module ];
@ -23,8 +23,8 @@ in {
volumeMounts."/etc/nginx".name = "config"; volumeMounts."/etc/nginx".name = "config";
volumeMounts."/var/lib/html".name = "static"; volumeMounts."/var/lib/html".name = "static";
}; };
volumes.config.persistentVolumeClaim.claimName = "config"; volumes.config.configMap.name = "nginx-config";
volumes.static.persistentVolumeClaim.claimName = "static"; volumes.static.configMap.name = "nginx-static";
}; };
}; };
}; };

View file

@ -0,0 +1,20 @@
{ config, lib, pkgs, kubenix, ... }:
with lib;
{
imports = [ kubenix.modules.test ./module.nix ];
test = {
name = "nginx-deployment";
description = "Test testing nginx deployment";
testScript = ''
$kube->waitUntilSucceeds("docker load < ${config.docker.images.nginx.image}");
$kube->waitUntilSucceeds("kubectl apply -f ${toYAML config.kubernetes.generated}");
$kube->succeed("kubectl get deployment | grep -i nginx");
$kube->waitUntilSucceeds("kubectl get deployment -o go-template nginx --template={{.status.readyReplicas}} | grep 10");
$kube->waitUntilSucceeds("${pkgs.curl}/bin/curl http://nginx.default.svc.cluster.local | grep -i hello");
'';
};
}