From 808927def5b38d4d39bb297be902f98ca63d41f0 Mon Sep 17 00:00:00 2001 From: Piotr Kwiecinski <2151333+piotrkwiecinski@users.noreply.github.com> Date: Wed, 2 Apr 2025 12:35:10 +0200 Subject: [PATCH] nixosTests.caddy: migrate to runTest Part of #386873 --- nixos/tests/all-tests.nix | 2 +- nixos/tests/caddy.nix | 270 +++++++++++++++++++------------------- 2 files changed, 135 insertions(+), 137 deletions(-) diff --git a/nixos/tests/all-tests.nix b/nixos/tests/all-tests.nix index 5a52c753c16a..e4cff571f75b 100644 --- a/nixos/tests/all-tests.nix +++ b/nixos/tests/all-tests.nix @@ -270,7 +270,7 @@ in buildbot = runTest ./buildbot.nix; buildkite-agents = handleTest ./buildkite-agents.nix { }; c2fmzq = handleTest ./c2fmzq.nix { }; - caddy = handleTest ./caddy.nix { }; + caddy = runTest ./caddy.nix; cadvisor = handleTestOn [ "x86_64-linux" ] ./cadvisor.nix { }; cage = handleTest ./cage.nix { }; cagebreak = handleTest ./cagebreak.nix { }; diff --git a/nixos/tests/caddy.nix b/nixos/tests/caddy.nix index 500c9d7d94d1..b7cf6ff10cc9 100644 --- a/nixos/tests/caddy.nix +++ b/nixos/tests/caddy.nix @@ -1,155 +1,153 @@ -import ./make-test-python.nix ( - { pkgs, ... }: - { - name = "caddy"; - meta = with pkgs.lib.maintainers; { - maintainers = [ - Br1ght0ne - stepbrobd - ]; - }; +{ pkgs, ... }: +{ + name = "caddy"; + meta = with pkgs.lib.maintainers; { + maintainers = [ + Br1ght0ne + stepbrobd + ]; + }; - nodes = { - webserver = - { pkgs, ... }: - { - services.caddy.enable = true; + 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 { - encode gzip - - file_server - root * ${ - pkgs.runCommand "testdir" { } '' - mkdir "$out" - echo hello world > "$out/example.html" - '' - } + http://localhost:8080 { } ''; - 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-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.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" - ''; - }; - specialisation.with-plugins.configuration = { - services.caddy = { - package = pkgs.caddy.withPlugins { - plugins = [ "github.com/caddyserver/replace-response@v0.0.0-20241211194404-3865845790a7" ]; - hash = "sha256-WPmJPnyOrAnuJxvn3ywswqvLGV8SZzzn3gU1Tbtpao4="; - }; - configFile = pkgs.writeText "Caddyfile" '' + }; + specialisation.rfc42.configuration = { + services.caddy.settings = { + apps.http.servers.default = { + listen = [ ":80" ]; + routes = [ { - order replace after encode + handle = [ + { + body = "hello world"; + handler = "static_response"; + status_code = 200; + } + ]; } - - localhost:80 { - respond "hello world" - replace world caddy - } - ''; + ]; }; }; }; - }; + specialisation.explicit-config-file.configuration = { + services.caddy.configFile = pkgs.writeText "Caddyfile" '' + localhost:80 - 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"; - withPluginsConfig = "${nodes.webserver.system.build.toplevel}/specialisation/with-plugins"; - in - '' - url = "http://localhost/example.html" - webserver.wait_for_unit("caddy") - webserver.wait_for_open_port(80) + respond "hello world" + ''; + }; + specialisation.with-plugins.configuration = { + services.caddy = { + package = pkgs.caddy.withPlugins { + plugins = [ "github.com/caddyserver/replace-response@v0.0.0-20241211194404-3865845790a7" ]; + hash = "sha256-WPmJPnyOrAnuJxvn3ywswqvLGV8SZzzn3gU1Tbtpao4="; + }; + configFile = pkgs.writeText "Caddyfile" '' + { + order replace after encode + } + + localhost:80 { + respond "hello world" + replace world caddy + } + ''; + }; + }; + }; + }; + + 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"; + withPluginsConfig = "${nodes.webserver.system.build.toplevel}/specialisation/with-plugins"; + 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("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("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("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("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") + 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") - with subtest("plugins are correctled installed and configurable"): - webserver.succeed( - "${withPluginsConfig}/bin/switch-to-configuration test >&2" - ) - webserver.wait_for_open_port(80) - webserver.succeed("curl http://localhost | grep caddy") - ''; - } -) + with subtest("plugins are correctled installed and configurable"): + webserver.succeed( + "${withPluginsConfig}/bin/switch-to-configuration test >&2" + ) + webserver.wait_for_open_port(80) + webserver.succeed("curl http://localhost | grep caddy") + ''; +}