1# Test Traefik as a reverse proxy of a local web service
2# and a Docker container.
3import ./make-test-python.nix ({ pkgs, ... }: {
4 name = "traefik";
5 meta = with pkgs.lib.maintainers; {
6 maintainers = [ joko ];
7 };
8
9 nodes = {
10 client = { config, pkgs, ... }: {
11 environment.systemPackages = [ pkgs.curl ];
12 };
13 traefik = { config, pkgs, ... }: {
14 virtualisation.oci-containers.containers.nginx = {
15 extraOptions = [
16 "-l" "traefik.enable=true"
17 "-l" "traefik.http.routers.nginx.entrypoints=web"
18 "-l" "traefik.http.routers.nginx.rule=Host(`nginx.traefik.test`)"
19 ];
20 image = "nginx-container";
21 imageFile = pkgs.dockerTools.examples.nginx;
22 };
23
24 networking.firewall.allowedTCPPorts = [ 80 ];
25
26 services.traefik = {
27 enable = true;
28
29 dynamicConfigOptions = {
30 http.routers.simplehttp = {
31 rule = "Host(`simplehttp.traefik.test`)";
32 entryPoints = [ "web" ];
33 service = "simplehttp";
34 };
35
36 http.services.simplehttp = {
37 loadBalancer.servers = [{
38 url = "http://127.0.0.1:8000";
39 }];
40 };
41 };
42
43 staticConfigOptions = {
44 global = {
45 checkNewVersion = false;
46 sendAnonymousUsage = false;
47 };
48
49 entryPoints.web.address = ":80";
50
51 providers.docker.exposedByDefault = false;
52 };
53 };
54
55 systemd.services.simplehttp = {
56 script = "${pkgs.python3}/bin/python -m http.server 8000";
57 serviceConfig.Type = "simple";
58 wantedBy = [ "multi-user.target" ];
59 };
60
61 users.users.traefik.extraGroups = [ "docker" ];
62 };
63 };
64
65 testScript = ''
66 start_all()
67
68 traefik.wait_for_unit("docker-nginx.service")
69 traefik.wait_until_succeeds("docker ps | grep nginx-container")
70 traefik.wait_for_unit("simplehttp.service")
71 traefik.wait_for_unit("traefik.service")
72 traefik.wait_for_open_port(80)
73 traefik.wait_for_unit("multi-user.target")
74
75 client.wait_for_unit("multi-user.target")
76
77 client.wait_until_succeeds("curl -sSf -H Host:nginx.traefik.test http://traefik/")
78
79 with subtest("Check that a container can be reached via Traefik"):
80 assert "Hello from NGINX" in client.succeed(
81 "curl -sSf -H Host:nginx.traefik.test http://traefik/"
82 )
83
84 with subtest("Check that dynamic configuration works"):
85 assert "Directory listing for " in client.succeed(
86 "curl -sSf -H Host:simplehttp.traefik.test http://traefik/"
87 )
88 '';
89})