summaryrefslogtreecommitdiffstats
path: root/nixos/tests/caddy.nix
blob: d7f2b72ef6def1b343d359be1e3e33b33175e75e (plain)
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
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
{ lib, ... }:

{
  name = "caddy";

  meta.maintainers = with lib.maintainers; [
    stepbrobd
  ];

  nodes = {
    webserver =
      { pkgs, ... }:
      {
        services.caddy.enable = true;
        services.caddy.extraConfig = ''
          http://localhost {
            encode gzip

            file_server
            root * ${
              pkgs.runCommand "testdir" { } ''
                mkdir "$out"
                echo hello world > "$out/example.html"
              ''
            }
          }
        '';
        services.caddy.enableReload = true;

        specialisation.config-reload.configuration = {
          services.caddy.extraConfig = ''
            http://localhost:8080 {
            }
          '';
        };
        specialisation.multiple-configs.configuration = {
          services.caddy.virtualHosts = {
            "http://localhost:8080" = { };
            "http://localhost:8081" = { };
          };
        };
        specialisation.multiple-hostnames.configuration = {
          services.caddy.virtualHosts = {
            "http://localhost:8080 http://localhost:8081" = { };
          };
        };
        specialisation.rfc42.configuration = {
          services.caddy.settings = {
            apps.http.servers.default = {
              listen = [ ":80" ];
              routes = [
                {
                  handle = [
                    {
                      body = "hello world";
                      handler = "static_response";
                      status_code = 200;
                    }
                  ];
                }
              ];
            };
          };
        };
        specialisation.explicit-config-file.configuration = {
          services.caddy.configFile = pkgs.writeText "Caddyfile" ''
            localhost:80

            respond "hello world"
          '';
        };
      };
  };

  testScript =
    { nodes, ... }:
    let
      explicitConfigFile = "${nodes.webserver.system.build.toplevel}/specialisation/explicit-config-file";
      justReloadSystem = "${nodes.webserver.system.build.toplevel}/specialisation/config-reload";
      multipleConfigs = "${nodes.webserver.system.build.toplevel}/specialisation/multiple-configs";
      multipleHostnames = "${nodes.webserver.system.build.toplevel}/specialisation/multiple-hostnames";
      rfc42Config = "${nodes.webserver.system.build.toplevel}/specialisation/rfc42";
    in
    ''
      url = "http://localhost/example.html"
      webserver.wait_for_unit("caddy")
      webserver.wait_for_open_port(80)


      with subtest("config is reloaded on nixos-rebuild switch"):
          webserver.succeed(
              "${justReloadSystem}/bin/switch-to-configuration test >&2"
          )
          webserver.wait_for_open_port(8080)
          webserver.fail("journalctl -u caddy | grep -q -i stopped")
          webserver.succeed("journalctl -u caddy | grep -q -i reloaded")

      with subtest("multiple configs are correctly merged"):
          webserver.succeed(
              "${multipleConfigs}/bin/switch-to-configuration test >&2"
          )
          webserver.wait_for_open_port(8080)
          webserver.wait_for_open_port(8081)

      with subtest("a virtual host with multiple hostnames works"):
          webserver.succeed(
              "${multipleHostnames}/bin/switch-to-configuration test >&2"
          )
          webserver.wait_for_open_port(8080)
          webserver.wait_for_open_port(8081)

      with subtest("rfc42 settings config"):
          webserver.succeed(
              "${rfc42Config}/bin/switch-to-configuration test >&2"
          )
          webserver.wait_for_open_port(80)
          webserver.succeed("curl http://localhost | grep hello")

      with subtest("explicit configFile"):
          webserver.succeed(
              "${explicitConfigFile}/bin/switch-to-configuration test >&2"
          )
          webserver.wait_for_open_port(80)
          webserver.succeed("curl http://localhost | grep hello")
    '';
}