2020-04-24 23:36:52 +00:00
|
|
|
import ./make-test-python.nix ({ pkgs, ... }: {
|
|
|
|
name = "caddy";
|
2021-01-15 22:18:51 +00:00
|
|
|
meta = with pkgs.lib.maintainers; {
|
2020-11-19 00:13:47 +00:00
|
|
|
maintainers = [ xfix Br1ght0ne ];
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
|
|
|
nodes = {
|
|
|
|
webserver = { pkgs, lib, ... }: {
|
|
|
|
services.caddy.enable = true;
|
|
|
|
services.caddy.config = ''
|
|
|
|
http://localhost {
|
2020-09-25 04:45:31 +00:00
|
|
|
encode gzip
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
file_server
|
|
|
|
root * ${
|
2020-04-24 23:36:52 +00:00
|
|
|
pkgs.runCommand "testdir" {} ''
|
|
|
|
mkdir "$out"
|
|
|
|
echo hello world > "$out/example.html"
|
|
|
|
''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
|
|
|
|
specialisation.etag.configuration = {
|
|
|
|
services.caddy.config = lib.mkForce ''
|
|
|
|
http://localhost {
|
2020-09-25 04:45:31 +00:00
|
|
|
encode gzip
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2020-09-25 04:45:31 +00:00
|
|
|
file_server
|
|
|
|
root * ${
|
2020-04-24 23:36:52 +00:00
|
|
|
pkgs.runCommand "testdir2" {} ''
|
|
|
|
mkdir "$out"
|
|
|
|
echo changed > "$out/example.html"
|
|
|
|
''
|
|
|
|
}
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
|
|
|
|
|
|
|
specialisation.config-reload.configuration = {
|
|
|
|
services.caddy.config = ''
|
|
|
|
http://localhost:8080 {
|
|
|
|
}
|
|
|
|
'';
|
|
|
|
};
|
2021-08-22 07:53:02 +00:00
|
|
|
specialisation.multiple-configs.configuration = {
|
|
|
|
services.caddy.virtualHosts = {
|
|
|
|
"http://localhost:8080" = { };
|
|
|
|
"http://localhost:8081" = { };
|
|
|
|
};
|
|
|
|
};
|
2020-04-24 23:36:52 +00:00
|
|
|
};
|
|
|
|
|
2021-08-22 07:53:02 +00:00
|
|
|
testScript = { nodes, ... }:
|
|
|
|
let
|
|
|
|
etagSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/etag";
|
|
|
|
justReloadSystem = "${nodes.webserver.config.system.build.toplevel}/specialisation/config-reload";
|
|
|
|
multipleConfigs = "${nodes.webserver.config.system.build.toplevel}/specialisation/multiple-configs";
|
|
|
|
in
|
|
|
|
''
|
|
|
|
url = "http://localhost/example.html"
|
|
|
|
webserver.wait_for_unit("caddy")
|
|
|
|
webserver.wait_for_open_port("80")
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
2021-08-22 07:53:02 +00:00
|
|
|
def check_etag(url):
|
|
|
|
etag = webserver.succeed(
|
|
|
|
"curl --fail -v '{}' 2>&1 | sed -n -e \"s/^< [Ee][Tt][Aa][Gg]: *//p\"".format(
|
|
|
|
url
|
|
|
|
)
|
2020-10-27 00:29:36 +00:00
|
|
|
)
|
2021-08-22 07:53:02 +00:00
|
|
|
etag = etag.replace("\r\n", " ")
|
|
|
|
http_code = webserver.succeed(
|
|
|
|
"curl --fail --silent --show-error -o /dev/null -w \"%{{http_code}}\" --head -H 'If-None-Match: {}' {}".format(
|
|
|
|
etag, url
|
|
|
|
)
|
2020-09-25 04:45:31 +00:00
|
|
|
)
|
2021-08-22 07:53:02 +00:00
|
|
|
assert int(http_code) == 304, "HTTP code is {}, expected 304".format(http_code)
|
|
|
|
return etag
|
2020-04-24 23:36:52 +00:00
|
|
|
|
|
|
|
|
2021-08-22 07:53:02 +00:00
|
|
|
with subtest("check ETag if serving Nix store paths"):
|
|
|
|
old_etag = check_etag(url)
|
|
|
|
webserver.succeed(
|
|
|
|
"${etagSystem}/bin/switch-to-configuration test >&2"
|
|
|
|
)
|
|
|
|
webserver.sleep(1)
|
|
|
|
new_etag = check_etag(url)
|
|
|
|
assert old_etag != new_etag, "Old ETag {} is the same as {}".format(
|
|
|
|
old_etag, new_etag
|
|
|
|
)
|
|
|
|
|
|
|
|
with subtest("config is reloaded on nixos-rebuild switch"):
|
|
|
|
webserver.succeed(
|
|
|
|
"${justReloadSystem}/bin/switch-to-configuration test >&2"
|
|
|
|
)
|
|
|
|
webserver.wait_for_open_port("8080")
|
2020-04-24 23:36:52 +00:00
|
|
|
|
2021-08-22 07:53:02 +00:00
|
|
|
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")
|
|
|
|
'';
|
|
|
|
})
|