summary refs log tree commit diff
path: root/nixos/tests/traefik.nix
diff options
context:
space:
mode:
authorIoannis Koutras <ioannis.koutras@gmail.com>2020-04-06 08:21:35 +0200
committerIoannis Koutras <ioannis.koutras@gmail.com>2020-04-12 22:50:36 +0200
commit9360e3723dd9a0c3aa13000bfce2ccc2a974a27e (patch)
tree93a5eb5b45fa01051415b7bb491a010a3dd846ba /nixos/tests/traefik.nix
parent1f61fbf3260dc01afd4d44d309aef682d3558fb3 (diff)
downloadnixpkgs-9360e3723dd9a0c3aa13000bfce2ccc2a974a27e.tar
nixpkgs-9360e3723dd9a0c3aa13000bfce2ccc2a974a27e.tar.gz
nixpkgs-9360e3723dd9a0c3aa13000bfce2ccc2a974a27e.tar.bz2
nixpkgs-9360e3723dd9a0c3aa13000bfce2ccc2a974a27e.tar.lz
nixpkgs-9360e3723dd9a0c3aa13000bfce2ccc2a974a27e.tar.xz
nixpkgs-9360e3723dd9a0c3aa13000bfce2ccc2a974a27e.tar.zst
nixpkgs-9360e3723dd9a0c3aa13000bfce2ccc2a974a27e.zip
nixos/traefik: add test
Diffstat (limited to 'nixos/tests/traefik.nix')
-rw-r--r--nixos/tests/traefik.nix87
1 files changed, 87 insertions, 0 deletions
diff --git a/nixos/tests/traefik.nix b/nixos/tests/traefik.nix
new file mode 100644
index 00000000000..0e21a7cf843
--- /dev/null
+++ b/nixos/tests/traefik.nix
@@ -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/"
+        )
+  '';
+})