nixpkgs/nixos/tests/traefik.nix

Ignoring revisions in .git-blame-ignore-revs. Click here to bypass and see the normal blame view.

110 lines
3.1 KiB
Nix
Raw Normal View History

2020-04-06 06:21:35 +00:00
# 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.lib.maintainers; {
2020-04-06 06:21:35 +00:00
maintainers = [ joko ];
};
nodes = {
client =
2020-04-06 06:21:35 +00:00
{ config, pkgs, ... }:
{
2020-04-06 06:21:35 +00:00
environment.systemPackages = [ pkgs.curl ];
};
2020-04-06 06:21:35 +00:00
traefik =
{ config, pkgs, ... }:
{
2022-06-03 09:03:45 +00:00
virtualisation.oci-containers = {
backend = "docker";
containers.nginx = {
2020-04-06 06:21:35 +00:00
extraOptions = [
"-l"
2020-04-06 06:21:35 +00:00
"traefik.enable=true"
"-l"
2020-04-06 06:21:35 +00:00
"traefik.http.routers.nginx.entrypoints=web"
"-l"
2020-04-06 06:21:35 +00:00
"traefik.http.routers.nginx.rule=Host(`nginx.traefik.test`)"
];
image = "nginx-container";
imageStream = pkgs.dockerTools.examples.nginxStream;
};
2020-04-06 06:21:35 +00:00
};
networking.firewall.allowedTCPPorts = [ 80 ];
2020-04-06 06:21:35 +00:00
services.traefik = {
enable = true;
2020-04-06 06:21:35 +00:00
dynamicConfigOptions = {
http.routers.simplehttp = {
2022-06-03 09:03:45 +00:00
rule = "Host(`simplehttp.traefik.test`)";
2020-04-06 06:21:35 +00:00
entryPoints = [ "web" ];
service = "simplehttp";
};
2020-04-06 06:21:35 +00:00
http.services.simplehttp = {
loadBalancer.servers = [
{
url = "http://127.0.0.1:8000";
}
2020-04-06 06:21:35 +00:00
];
};
};
2020-04-06 06:21:35 +00:00
staticConfigOptions = {
global = {
checkNewVersion = false;
sendAnonymousUsage = false;
};
entryPoints.web.address = ":\${HTTP_PORT}";
2020-04-06 06:21:35 +00:00
providers.docker.exposedByDefault = false;
};
environmentFiles = [
(pkgs.writeText "traefik.env" ''
HTTP_PORT=80
'')
];
2020-04-06 06:21:35 +00:00
};
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")
client.wait_until_succeeds("curl -sSf -H Host:nginx.traefik.test http://traefik/")
2020-04-06 06:21:35 +00:00
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/"
)
'';
}
)