1import ./make-test-python.nix ({ pkgs, ... }: {
2 name = "caddy";
3 meta = with pkgs.lib.maintainers; {
4 maintainers = [ xfix Br1ght0ne ];
5 };
6
7 nodes = {
8 webserver = { pkgs, lib, ... }: {
9 services.caddy.enable = true;
10 services.caddy.config = ''
11 http://localhost {
12 encode gzip
13
14 file_server
15 root * ${
16 pkgs.runCommand "testdir" {} ''
17 mkdir "$out"
18 echo hello world > "$out/example.html"
19 ''
20 }
21 }
22 '';
23
24 specialisation.etag.configuration = {
25 services.caddy.config = lib.mkForce ''
26 http://localhost {
27 encode gzip
28
29 file_server
30 root * ${
31 pkgs.runCommand "testdir2" {} ''
32 mkdir "$out"
33 echo changed > "$out/example.html"
34 ''
35 }
36 }
37 '';
38 };
39
40 specialisation.config-reload.configuration = {
41 services.caddy.config = ''
42 http://localhost:8080 {
43 }
44 '';
45 };
46 specialisation.multiple-configs.configuration = {
47 services.caddy.virtualHosts = {
48 "http://localhost:8080" = { };
49 "http://localhost:8081" = { };
50 };
51 };
52 };
53 };
54
55 testScript = { nodes, ... }:
56 let
57 etagSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/etag";
58 justReloadSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/config-reload";
59 multipleConfigs = "${nodes.webserver.config.system.build.toplevel}/specialisation/multiple-configs";
60 in
61 ''
62 url = "http://localhost/example.html"
63 webserver.wait_for_unit("caddy")
64 webserver.wait_for_open_port("80")
65
66
67 def check_etag(url):
68 etag = webserver.succeed(
69 "curl --fail -v '{}' 2>&1 | sed -n -e \"s/^< [Ee][Tt][Aa][Gg]: *//p\"".format(
70 url
71 )
72 )
73 etag = etag.replace("\r\n", " ")
74 http_code = webserver.succeed(
75 "curl --fail --silent --show-error -o /dev/null -w \"%{{http_code}}\" --head -H 'If-None-Match: {}' {}".format(
76 etag, url
77 )
78 )
79 assert int(http_code) == 304, "HTTP code is {}, expected 304".format(http_code)
80 return etag
81
82
83 with subtest("check ETag if serving Nix store paths"):
84 old_etag = check_etag(url)
85 webserver.succeed(
86 "${etagSystem}/bin/switch-to-configuration test >&2"
87 )
88 webserver.sleep(1)
89 new_etag = check_etag(url)
90 assert old_etag != new_etag, "Old ETag {} is the same as {}".format(
91 old_etag, new_etag
92 )
93
94 with subtest("config is reloaded on nixos-rebuild switch"):
95 webserver.succeed(
96 "${justReloadSystem}/bin/switch-to-configuration test >&2"
97 )
98 webserver.wait_for_open_port("8080")
99
100 with subtest("multiple configs are correctly merged"):
101 webserver.succeed(
102 "${multipleConfigs}/bin/switch-to-configuration test >&2"
103 )
104 webserver.wait_for_open_port("8080")
105 webserver.wait_for_open_port("8081")
106 '';
107})