Implement nixos-modules/static-page test
All checks were successful
CI / build (push) Successful in 4m9s

This commit is contained in:
Fabian Hauser 2025-03-24 14:17:53 +02:00
parent c3962b9738
commit c9844276b8
8 changed files with 124 additions and 4 deletions

View file

@ -0,0 +1,9 @@
{
linkFarmFromDrvs,
callPackage,
loadSubmodulesFrom,
}:
let
tests = map (test: callPackage test { }) (loadSubmodulesFrom ./.);
in
linkFarmFromDrvs "nixos-modules" tests

View file

@ -0,0 +1,45 @@
{
testers,
curl,
lib,
gnugrep,
...
}:
testers.runNixOSTest {
name = "static-page";
nodes.webserver =
{ ... }:
{
# Service under test
imports = [ ../../../nixos-modules/static-page ];
qois.static-page = {
enable = true;
pages = lib.mkForce {
"localhost" = {
domainAliases = [ "example.com" ];
};
};
};
# Test environment
environment.systemPackages = [
curl
gnugrep
];
# Disable TLS services
services.nginx.virtualHosts =
let
tlsOff = {
forceSSL = lib.mkForce false;
enableACME = lib.mkForce false;
};
in
{
"localhost" = tlsOff;
"example.com" = tlsOff;
};
};
testScript = lib.readFile ./test.py;
}

View file

@ -0,0 +1,46 @@
webserver.wait_for_unit("nginx")
webserver.wait_for_open_port(80)
# Preparations
webserverRoot = "/var/lib/nginx-localhost/root"
indexContent = "It works!"
webserver.succeed(f"mkdir {webserverRoot}")
webserver.succeed(f"echo '{indexContent}' > {webserverRoot}/index.html")
webserver.succeed(f"chown -R nginx-localhost\: {webserverRoot}")
# Helpers
def expect_http_code(node, code, url):
http_code = node.succeed(
f"curl -s --no-location -o /dev/null -w '%{{http_code}}' '{url}'")
assert http_code == code, \
f"expected {code} but got following response:\n{http_code}"
def expect_http_location(node, location, url):
redirect_url = node.succeed(
f"curl -s --no-location -o /dev/null -w '%{{redirect_url}}' '{url}'")
assert redirect_url == location, \
f"expected redirect to {location} but got:\n{redirect_url}"
def expect_http_content(node, expectedContent, url):
content = node.succeed(f"curl --no-location --silent '{url}'")
assert content.strip() == expectedContent.strip(), \
f"expected:\n{expectedContent}\n at {
url} but got following content:\n'{content}'"
# Tests
with subtest("website is successfully served on localhost"):
expect_http_code(webserver, "200", "http://localhost/index.html")
expect_http_content(webserver, indexContent, "http://localhost/index.html")
with subtest("example.com is a hosts alias and redirects to localhost"):
webserver.succeed("grep example.com /etc/hosts")
url = "http://example.com/index.html"
expect_http_code(webserver, "301", url)
expect_http_location(
webserver, "http://localhost/index.html", url)