fixup! Add tests documentation to docs page
Some checks failed
CI / build (push) Failing after 45s

This commit is contained in:
Fabian Hauser 2025-03-24 21:50:44 +02:00
parent 362fba1385
commit fc4d569886
6 changed files with 70 additions and 54 deletions

View file

@ -1,9 +1,60 @@
{
linkFarmFromDrvs,
callPackage,
loadSubmodulesFrom,
isFolderWithFile,
getSubDirs,
lib,
testers,
}:
let
tests = map (test: callPackage test { }) (loadSubmodulesFrom ./.);
inherit (lib)
filter
path
mkDefault
readFile
attrNames
concatStringsSep
pipe
;
modulesBaseDir = ../../nixos-modules;
mkTest =
name:
let
getFilePath = file: path.append modulesBaseDir "./${name}/${file}";
in
testers.runNixOSTest {
inherit name;
imports = [
(import (getFilePath "test.nix") {
inherit name;
inherit lib;
})
];
defaults.imports = [ (getFilePath "default.nix") ];
# Calls a `test(...)` python function in the test's python file with the list of nodes and helper functions.
# Helper symbols may be added as function args when needed and can be found in:
# https://github.com/NixOS/nixpkgs/blob/master/nixos/lib/test-driver/src/test_driver/driver.py#L121
testScript = mkDefault (
{ nodes, ... }:
let
script = readFile (getFilePath "test.py");
nodeArgs = pipe nodes [
attrNames
(map (val: "${val}=${val}"))
(concatStringsSep ", ")
];
in
''
${script}
test(${nodeArgs}, subtest=subtest)
''
);
};
in
linkFarmFromDrvs "nixos-modules" tests
pipe modulesBaseDir [
getSubDirs
(filter (isFolderWithFile "test.nix"))
(map mkTest)
(linkFarmFromDrvs "nixos-modules")
]

View file

@ -1,39 +0,0 @@
{
testers,
lib,
...
}:
let
inherit (lib)
mkDefault
readFile
attrNames
concatStringsSep
;
name = "static-page";
in
testers.runNixOSTest {
inherit name;
imports = [
(import ./test.nix {
inherit name;
inherit lib;
})
];
defaults.imports = [ ../../../nixos-modules/${name} ];
# Calls a test function with the list of nodes and helper functions.
# Helper symbols may be added as function args needed and can be found in:
# https://github.com/NixOS/nixpkgs/blob/master/nixos/lib/test-driver/src/test_driver/driver.py#L121
testScript = mkDefault (
{ nodes, ... }:
let
script = readFile ./test.py;
nodeArgs = concatStringsSep ", " (map (val: "${val}=${val}") (attrNames nodes));
in
''
${script}
test(${nodeArgs}, subtest=subtest)
''
);
}

View file

@ -1,30 +0,0 @@
{
...
}:
{
nodes.webserver =
{ pkgs, lib, ... }:
let
inherit (pkgs) curl gnugrep;
inherit (lib) mkForce genAttrs const;
in
{
# Setup simple localhost page with an example.com redirect
qois.static-page = {
enable = true;
pages."localhost".domainAliases = [ "example.com" ];
};
# Disable TLS services
services.nginx.virtualHosts = genAttrs [ "localhost" "example.com" ] (const {
forceSSL = mkForce false;
enableACME = mkForce false;
});
# Test environment
environment.systemPackages = [
curl
gnugrep
];
};
}

View file

@ -1,46 +0,0 @@
def test(subtest, webserver):
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 curl_variable_test(node, variable, expected, url):
value = node.succeed(
f"curl -s --no-location -o /dev/null -w '%{{{variable}}}' '{url}'")
assert value == expected, \
f"expected {variable} to be '{expected}' but got '{value}'"
def expect_http_code(node, code, url):
curl_variable_test(node, "http_code", code, url)
def expect_http_location(node, location, url):
curl_variable_test(node, "redirect_url", location, url)
def expect_http_content(node, expectedContent, url):
content = node.succeed(f"curl --no-location --silent '{url}'")
assert content.strip() == expectedContent.strip(), f'''
expected content:
{expectedContent}
at {url} but got following content:
{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)