about summary refs log tree commit diff
path: root/nixos/tests/traefik.nix
blob: 989ec390c0603f02778a75f3e2f9b8a05664b492 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
# 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; {
    maintainers = [ joko ];
  };

  nodes = {
    client = { config, pkgs, ... }: {
      environment.systemPackages = [ pkgs.curl ];
    };
    traefik = { config, pkgs, ... }: {
      virtualisation.oci-containers = {
        backend = "docker";
        containers.nginx = {
          extraOptions = [
            "-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")

    client.wait_until_succeeds("curl -sSf -H Host:nginx.traefik.test http://traefik/")

    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/"
        )
  '';
})