2 # 1. nginx generates config file with shared http context definitions above
3 # generated virtual hosts config.
4 # 2. whether the ETag header is properly generated whenever we're serving
5 # files in Nix store paths
6 # 3. nginx doesn't restart on configuration changes (only reloads)
7 import ./make-test-python.nix ({ pkgs, ... }: {
9 meta = with pkgs.lib.maintainers; {
10 maintainers = [ mbbx6spp danbst ];
14 webserver = { pkgs, lib, ... }: {
15 services.nginx.enable = true;
16 services.nginx.commonHttpConfig = ''
17 log_format ceeformat '@cee: {"status":"$status",'
18 '"request_time":$request_time,'
19 '"upstream_response_time":$upstream_response_time,'
20 '"pipe":"$pipe","bytes_sent":$bytes_sent,'
21 '"connection":"$connection",'
22 '"remote_addr":"$remote_addr",'
24 '"timestamp":"$time_iso8601",'
25 '"request":"$request",'
26 '"http_referer":"$http_referer",'
27 '"upstream_addr":"$upstream_addr"}';
29 services.nginx.virtualHosts."0.my.test" = {
31 access_log syslog:server=unix:/dev/log,facility=user,tag=mytag,severity=info ceeformat;
32 location /favicon.ico { allow all; access_log off; log_not_found off; }
36 services.nginx.virtualHosts.localhost = {
37 root = pkgs.runCommand "testdir" {} ''
39 echo hello world > "$out/index.html"
43 services.nginx.enableReload = true;
45 specialisation.etagSystem.configuration = {
46 services.nginx.virtualHosts.localhost = {
47 root = lib.mkForce (pkgs.runCommand "testdir2" {} ''
49 echo content changed > "$out/index.html"
54 specialisation.justReloadSystem.configuration = {
55 services.nginx.virtualHosts."1.my.test".listen = [ { addr = "127.0.0.1"; port = 8080; }];
58 specialisation.reloadRestartSystem.configuration = {
59 services.nginx.package = pkgs.nginxMainline;
62 specialisation.reloadWithErrorsSystem.configuration = {
63 services.nginx.package = pkgs.nginxMainline;
64 services.nginx.virtualHosts."!@$$(#*%".locations."~@#*$*!)".proxyPass = ";;;";
69 testScript = { nodes, ... }: let
70 etagSystem = "${nodes.webserver.system.build.toplevel}/specialisation/etagSystem";
71 justReloadSystem = "${nodes.webserver.system.build.toplevel}/specialisation/justReloadSystem";
72 reloadRestartSystem = "${nodes.webserver.system.build.toplevel}/specialisation/reloadRestartSystem";
73 reloadWithErrorsSystem = "${nodes.webserver.system.build.toplevel}/specialisation/reloadWithErrorsSystem";
75 url = "http://localhost/index.html"
79 etag = webserver.succeed(
80 f'curl -v {url} 2>&1 | sed -n -e "s/^< etag: *//ip"'
82 http_code = webserver.succeed(
83 f"curl -w '%{{http_code}}' --head --fail -H 'If-None-Match: {etag}' {url}"
85 assert http_code.split("\n")[-1] == "304"
90 def wait_for_nginx_on_port(port):
91 webserver.wait_for_unit("nginx")
92 webserver.wait_for_open_port(port)
95 # nginx can be ready before multi-user.target, in which case switching to
96 # a different configuration might not realize it needs to restart nginx.
97 webserver.wait_for_unit("multi-user.target")
99 wait_for_nginx_on_port(80)
101 with subtest("check ETag if serving Nix store paths"):
102 old_etag = check_etag()
104 "${etagSystem}/bin/switch-to-configuration test >&2"
106 wait_for_nginx_on_port(80)
107 new_etag = check_etag()
108 assert old_etag != new_etag
110 with subtest("config is reloaded on nixos-rebuild switch"):
112 "${justReloadSystem}/bin/switch-to-configuration test >&2"
114 wait_for_nginx_on_port(8080)
115 webserver.fail("journalctl -u nginx | grep -q -i stopped")
116 webserver.succeed("journalctl -u nginx | grep -q -i reloaded")
118 with subtest("restart when nginx package changes"):
120 "${reloadRestartSystem}/bin/switch-to-configuration test >&2"
122 wait_for_nginx_on_port(80)
123 webserver.succeed("journalctl -u nginx | grep -q -i stopped")
125 with subtest("nixos-rebuild --switch should fail when there are configuration errors"):
127 "${reloadWithErrorsSystem}/bin/switch-to-configuration test >&2"
129 webserver.succeed("[[ $(systemctl is-failed nginx-config-reload) == failed ]]")
130 webserver.succeed("[[ $(systemctl is-failed nginx) == active ]]")
131 # just to make sure operation is idempotent. During development I had a situation
132 # when first time it shows error, but stops showing it on subsequent rebuilds
134 "${reloadWithErrorsSystem}/bin/switch-to-configuration test >&2"