Skip to content

Commit

Permalink
nixos/traefik: add test
Browse files Browse the repository at this point in the history
  • Loading branch information
jokogr committed Apr 12, 2020
1 parent 1f61fbf commit 9360e37
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 1 deletion.
2 changes: 2 additions & 0 deletions nixos/tests/all-tests.nix
Original file line number Diff line number Diff line change
Expand Up @@ -303,6 +303,8 @@ in
timezone = handleTest ./timezone.nix {};
tinydns = handleTest ./tinydns.nix {};
tor = handleTest ./tor.nix {};
# traefik test relies on docker-containers
traefik = handleTestOn ["x86_64-linux"] ./traefik.nix {};
transmission = handleTest ./transmission.nix {};
trac = handleTest ./trac.nix {};
trilium-server = handleTestOn ["x86_64-linux"] ./trilium-server.nix {};
Expand Down
87 changes: 87 additions & 0 deletions nixos/tests/traefik.nix
Original file line number Diff line number Diff line change
@@ -0,0 +1,87 @@
# Test Traefik as a reverse proxy of a local web service
# and a Docker container.
import ./make-test-python.nix ({ pkgs, ... }: {
name = "traefik";
meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ joko ];
};

nodes = {
client = { config, pkgs, ... }: {
environment.systemPackages = [ pkgs.curl ];
};
traefik = { config, pkgs, ... }: {
docker-containers.nginx = {
extraDockerOptions = [
"-l" "traefik.enable=true"
"-l" "traefik.http.routers.nginx.entrypoints=web"
"-l" "traefik.http.routers.nginx.rule=Host(`nginx.traefik.test`)"
];
image = "nginx-container";
imageFile = pkgs.dockerTools.examples.nginx;
};

networking.firewall.allowedTCPPorts = [ 80 ];

services.traefik = {
enable = true;

dynamicConfigOptions = {
http.routers.simplehttp = {
rule = "Host(`simplehttp.traefik.test`)";
entryPoints = [ "web" ];
service = "simplehttp";
};

http.services.simplehttp = {
loadBalancer.servers = [{
url = "http://127.0.0.1:8000";
}];
};
};

staticConfigOptions = {
global = {
checkNewVersion = false;
sendAnonymousUsage = false;
};

entryPoints.web.address = ":80";

providers.docker.exposedByDefault = false;
};
};

systemd.services.simplehttp = {
script = "${pkgs.python3}/bin/python -m http.server 8000";
serviceConfig.Type = "simple";
wantedBy = [ "multi-user.target" ];
};

users.users.traefik.extraGroups = [ "docker" ];
};
};

testScript = ''
start_all()
traefik.wait_for_unit("docker-nginx.service")
traefik.wait_until_succeeds("docker ps | grep nginx-container")
traefik.wait_for_unit("simplehttp.service")
traefik.wait_for_unit("traefik.service")
traefik.wait_for_open_port(80)
traefik.wait_for_unit("multi-user.target")
client.wait_for_unit("multi-user.target")
with subtest("Check that a container can be reached via Traefik"):
assert "Hello from NGINX" in client.succeed(
"curl -sSf -H Host:nginx.traefik.test http://traefik/"
)
with subtest("Check that dynamic configuration works"):
assert "Directory listing for " in client.succeed(
"curl -sSf -H Host:simplehttp.traefik.test http://traefik/"
)
'';
})
4 changes: 3 additions & 1 deletion pkgs/servers/traefik/default.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
{ stdenv, buildGoModule, fetchFromGitHub, go-bindata }:
{ stdenv, buildGoModule, fetchFromGitHub, go-bindata, nixosTests }:

buildGoModule rec {
pname = "traefik";
Expand All @@ -16,6 +16,8 @@ buildGoModule rec {

nativeBuildInputs = [ go-bindata ];

passthru.tests = { inherit (nixosTests) traefik; };

preBuild = ''
go generate
Expand Down

0 comments on commit 9360e37

Please sign in to comment.