nixosTests.angie-api: migrate to runTest

Part Of #386873
This commit is contained in:
Martin Weinelt 2025-03-13 23:24:10 +01:00
parent 769a073a37
commit e045203e0e
No known key found for this signature in database
GPG key ID: 87C1E9888F856759
2 changed files with 144 additions and 146 deletions

View file

@ -166,7 +166,7 @@ in {
amazon-init-shell = runTest ./amazon-init-shell.nix; amazon-init-shell = runTest ./amazon-init-shell.nix;
amazon-ssm-agent = runTest ./amazon-ssm-agent.nix; amazon-ssm-agent = runTest ./amazon-ssm-agent.nix;
amd-sev = runTest ./amd-sev.nix; amd-sev = runTest ./amd-sev.nix;
angie-api = handleTest ./angie-api.nix {}; angie-api = runTest ./angie-api.nix;
anki-sync-server = handleTest ./anki-sync-server.nix {}; anki-sync-server = handleTest ./anki-sync-server.nix {};
anuko-time-tracker = handleTest ./anuko-time-tracker.nix {}; anuko-time-tracker = handleTest ./anuko-time-tracker.nix {};
apcupsd = handleTest ./apcupsd.nix {}; apcupsd = handleTest ./apcupsd.nix {};

View file

@ -1,170 +1,168 @@
import ./make-test-python.nix ( { lib, pkgs, ... }:
{ lib, pkgs, ... }: let
let hosts = ''
hosts = '' 192.168.2.101 example.com
192.168.2.101 example.com 192.168.2.101 api.example.com
192.168.2.101 api.example.com 192.168.2.101 backend.example.com
192.168.2.101 backend.example.com '';
'';
in in
{ {
name = "angie-api"; name = "angie-api";
meta.maintainers = with pkgs.lib.maintainers; [ izorkin ]; meta.maintainers = with pkgs.lib.maintainers; [ izorkin ];
nodes = { nodes = {
server = server =
{ pkgs, ... }: { pkgs, ... }:
{ {
networking = { networking = {
interfaces.eth1 = { interfaces.eth1 = {
ipv4.addresses = [ ipv4.addresses = [
{ {
address = "192.168.2.101"; address = "192.168.2.101";
prefixLength = 24; prefixLength = 24;
} }
]; ];
};
extraHosts = hosts;
firewall.allowedTCPPorts = [ 80 ];
};
services.nginx = {
enable = true;
package = pkgs.angie;
upstreams = {
"backend-http" = {
servers = {
"backend.example.com:8080" = {
fail_timeout = "0";
};
};
extraConfig = ''
zone upstream 256k;
'';
};
"backend-socket" = {
servers = {
"unix:/run/example.sock" = {
fail_timeout = "0";
};
};
extraConfig = ''
zone upstream 256k;
'';
}; };
extraHosts = hosts;
firewall.allowedTCPPorts = [ 80 ];
}; };
services.nginx = { virtualHosts."api.example.com" = {
enable = true; locations."/console/" = {
package = pkgs.angie; extraConfig = ''
api /status/;
upstreams = { allow 192.168.2.201;
"backend-http" = { deny all;
servers = { '';
"backend.example.com:8080" = {
fail_timeout = "0";
};
};
extraConfig = ''
zone upstream 256k;
'';
};
"backend-socket" = {
servers = {
"unix:/run/example.sock" = {
fail_timeout = "0";
};
};
extraConfig = ''
zone upstream 256k;
'';
};
}; };
};
virtualHosts."api.example.com" = { virtualHosts."example.com" = {
locations."/console/" = { locations."/test/" = {
extraConfig = '' root = lib.mkForce (
api /status/; pkgs.runCommandLocal "testdir" { } ''
mkdir -p "$out/test"
cat > "$out/test/index.html" <<EOF
<html><body>Hello World!</body></html>
EOF
''
);
extraConfig = ''
status_zone test_zone;
allow 192.168.2.201; allow 192.168.2.201;
deny all; deny all;
''; '';
};
}; };
locations."/test/locked/" = {
extraConfig = ''
status_zone test_zone;
virtualHosts."example.com" = { deny all;
locations."/test/" = { '';
root = lib.mkForce ( };
pkgs.runCommandLocal "testdir" { } '' locations."/test/error/" = {
mkdir -p "$out/test" extraConfig = ''
cat > "$out/test/index.html" <<EOF status_zone test_zone;
<html><body>Hello World!</body></html>
EOF
''
);
extraConfig = ''
status_zone test_zone;
allow 192.168.2.201; allow all;
deny all; '';
''; };
}; locations."/upstream-http/" = {
locations."/test/locked/" = { proxyPass = "http://backend-http";
extraConfig = '' };
status_zone test_zone; locations."/upstream-socket/" = {
proxyPass = "http://backend-socket";
deny all;
'';
};
locations."/test/error/" = {
extraConfig = ''
status_zone test_zone;
allow all;
'';
};
locations."/upstream-http/" = {
proxyPass = "http://backend-http";
};
locations."/upstream-socket/" = {
proxyPass = "http://backend-socket";
};
}; };
}; };
}; };
};
client = client =
{ pkgs, ... }: { pkgs, ... }:
{ {
environment.systemPackages = [ pkgs.jq ]; environment.systemPackages = [ pkgs.jq ];
networking = { networking = {
interfaces.eth1 = { interfaces.eth1 = {
ipv4.addresses = [ ipv4.addresses = [
{ {
address = "192.168.2.201"; address = "192.168.2.201";
prefixLength = 24; prefixLength = 24;
} }
]; ];
};
extraHosts = hosts;
}; };
extraHosts = hosts;
}; };
}; };
};
testScript = '' testScript = ''
start_all() start_all()
server.wait_for_unit("nginx") server.wait_for_unit("nginx")
server.wait_for_open_port(80) server.wait_for_open_port(80)
# Check Angie version # Check Angie version
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.angie.version' | grep '${pkgs.angie.version}'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.angie.version' | grep '${pkgs.angie.version}'")
# Check access # Check access
client.succeed("curl --verbose --head http://api.example.com/console/ | grep 'HTTP/1.1 200'") client.succeed("curl --verbose --head http://api.example.com/console/ | grep 'HTTP/1.1 200'")
server.succeed("curl --verbose --head http://api.example.com/console/ | grep 'HTTP/1.1 403 Forbidden'") server.succeed("curl --verbose --head http://api.example.com/console/ | grep 'HTTP/1.1 403 Forbidden'")
# Check responses and requests # Check responses and requests
client.succeed("curl --verbose http://example.com/test/") client.succeed("curl --verbose http://example.com/test/")
client.succeed("curl --verbose http://example.com/test/locked/") client.succeed("curl --verbose http://example.com/test/locked/")
client.succeed("curl --verbose http://example.com/test/locked/") client.succeed("curl --verbose http://example.com/test/locked/")
client.succeed("curl --verbose http://example.com/test/error/") client.succeed("curl --verbose http://example.com/test/error/")
client.succeed("curl --verbose http://example.com/test/error/") client.succeed("curl --verbose http://example.com/test/error/")
client.succeed("curl --verbose http://example.com/test/error/") client.succeed("curl --verbose http://example.com/test/error/")
server.succeed("curl --verbose http://example.com/test/") server.succeed("curl --verbose http://example.com/test/")
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.responses.\"200\"' | grep '1'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.responses.\"200\"' | grep '1'")
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.responses.\"403\"' | grep '3'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.responses.\"403\"' | grep '3'")
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.responses.\"404\"' | grep '3'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.responses.\"404\"' | grep '3'")
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.requests.total' | grep '7'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.location_zones.test_zone.requests.total' | grep '7'")
# Check upstreams # Check upstreams
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-http\".peers.\"192.168.2.101:8080\".state' | grep 'up'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-http\".peers.\"192.168.2.101:8080\".state' | grep 'up'")
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-http\".peers.\"192.168.2.101:8080\".health.fails' | grep '0'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-http\".peers.\"192.168.2.101:8080\".health.fails' | grep '0'")
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-socket\".peers.\"unix:/run/example.sock\".state' | grep 'up'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-socket\".peers.\"unix:/run/example.sock\".state' | grep 'up'")
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-socket\".peers.\"unix:/run/example.sock\".health.fails' | grep '0'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-socket\".peers.\"unix:/run/example.sock\".health.fails' | grep '0'")
client.succeed("curl --verbose http://example.com/upstream-http/") client.succeed("curl --verbose http://example.com/upstream-http/")
client.succeed("curl --verbose http://example.com/upstream-socket/") client.succeed("curl --verbose http://example.com/upstream-socket/")
client.succeed("curl --verbose http://example.com/upstream-socket/") client.succeed("curl --verbose http://example.com/upstream-socket/")
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-http\".peers.\"192.168.2.101:8080\".health.fails' | grep '1'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-http\".peers.\"192.168.2.101:8080\".health.fails' | grep '1'")
client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-socket\".peers.\"unix:/run/example.sock\".health.fails' | grep '2'") client.succeed("curl --verbose http://api.example.com/console/ | jq -e '.http.upstreams.\"backend-socket\".peers.\"unix:/run/example.sock\".health.fails' | grep '2'")
server.shutdown() server.shutdown()
client.shutdown() client.shutdown()
''; '';
} }
)