Merge pull request #72904 from WilliButz/nixosTests/port-to-python

nixos/tests: port some tests to python (loki, grafana, pgjwt, initrd-ssh, exporters)
This commit is contained in:
WilliButz 2019-11-07 16:14:17 +01:00 committed by GitHub
commit d7b18bcb37
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
5 changed files with 198 additions and 153 deletions

View File

@ -1,4 +1,4 @@
import ./make-test.nix ({ lib, pkgs, ... }: import ./make-test-python.nix ({ lib, pkgs, ... }:
let let
inherit (lib) mkMerge nameValuePair maintainers; inherit (lib) mkMerge nameValuePair maintainers;
@ -64,28 +64,34 @@ in {
inherit nodes; inherit nodes;
testScript = '' testScript = ''
startAll(); start_all()
subtest "Grafana sqlite", sub { with subtest("Successful API query as admin user with sqlite db"):
$sqlite->waitForUnit("grafana.service"); sqlite.wait_for_unit("grafana.service")
$sqlite->waitForOpenPort(3000); sqlite.wait_for_open_port(3000)
$sqlite->succeed("curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep -q testadmin\@localhost"); sqlite.succeed(
}; "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep -q testadmin\@localhost"
)
sqlite.shutdown()
subtest "Grafana postgresql", sub { with subtest("Successful API query as admin user with postgresql db"):
$postgresql->waitForUnit("grafana.service"); postgresql.wait_for_unit("grafana.service")
$postgresql->waitForUnit("postgresql.service"); postgresql.wait_for_unit("postgresql.service")
$postgresql->waitForOpenPort(3000); postgresql.wait_for_open_port(3000)
$postgresql->waitForOpenPort(5432); postgresql.wait_for_open_port(5432)
$postgresql->succeed("curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep -q testadmin\@localhost"); postgresql.succeed(
}; "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep -q testadmin\@localhost"
)
postgresql.shutdown()
subtest "Grafana mysql", sub { with subtest("Successful API query as admin user with mysql db"):
$mysql->waitForUnit("grafana.service"); mysql.wait_for_unit("grafana.service")
$mysql->waitForUnit("mysql.service"); mysql.wait_for_unit("mysql.service")
$mysql->waitForOpenPort(3000); mysql.wait_for_open_port(3000)
$mysql->waitForOpenPort(3306); mysql.wait_for_open_port(3306)
$mysql->succeed("curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep -q testadmin\@localhost"); mysql.succeed(
}; "curl -sSfN -u testadmin:snakeoilpwd http://127.0.0.1:3000/api/org/users | grep -q testadmin\@localhost"
)
mysql.shutdown()
''; '';
}) })

View File

@ -1,4 +1,4 @@
import ../make-test.nix ({ lib, ... }: import ../make-test-python.nix ({ lib, ... }:
{ {
name = "initrd-network-ssh"; name = "initrd-network-ssh";
@ -35,25 +35,31 @@ import ../make-test.nix ({ lib, ... }:
client = client =
{ config, ... }: { config, ... }:
{ {
environment.etc.knownHosts = { environment.etc = {
text = concatStrings [ knownHosts = {
"server," text = concatStrings [
"${toString (head (splitString " " ( "server,"
toString (elemAt (splitString "\n" config.networking.extraHosts) 2) "${toString (head (splitString " " (
)))} " toString (elemAt (splitString "\n" config.networking.extraHosts) 2)
"${readFile ./dropbear.pub}" )))} "
]; "${readFile ./dropbear.pub}"
];
};
sshKey = {
source = ./openssh.priv; # dont use this anywhere else
mode = "0600";
};
}; };
}; };
}; };
testScript = '' testScript = ''
startAll; start_all()
$client->waitForUnit("network.target"); client.wait_for_unit("network.target")
$client->copyFileFromHost("${./openssh.priv}","/etc/sshKey"); client.wait_until_succeeds("ping -c 1 server")
$client->succeed("chmod 0600 /etc/sshKey"); client.succeed(
$client->waitUntilSucceeds("ping -c 1 server"); "ssh -i /etc/sshKey -o UserKnownHostsFile=/etc/knownHosts server 'touch /fnord'"
$client->succeed("ssh -i /etc/sshKey -o UserKnownHostsFile=/etc/knownHosts server 'touch /fnord'"); )
$client->shutdown; client.shutdown()
''; '';
}) })

View File

@ -1,4 +1,4 @@
import ./make-test.nix ({ lib, pkgs, ... }: import ./make-test-python.nix ({ lib, pkgs, ... }:
{ {
name = "loki"; name = "loki";
@ -26,12 +26,14 @@ import ./make-test.nix ({ lib, pkgs, ... }:
}; };
testScript = '' testScript = ''
$machine->start; machine.start
$machine->waitForUnit("loki.service"); machine.wait_for_unit("loki.service")
$machine->waitForUnit("promtail.service"); machine.wait_for_unit("promtail.service")
$machine->waitForOpenPort(3100); machine.wait_for_open_port(3100)
$machine->waitForOpenPort(9080); machine.wait_for_open_port(9080)
$machine->succeed("echo 'Loki Ingestion Test' > /var/log/testlog"); machine.succeed("echo 'Loki Ingestion Test' > /var/log/testlog")
$machine->waitUntilSucceeds("${pkgs.grafana-loki}/bin/logcli --addr='http://localhost:3100' query --no-labels '{job=\"varlogs\",filename=\"/var/log/testlog\"}' | grep -q 'Loki Ingestion Test'"); machine.wait_until_succeeds(
"${pkgs.grafana-loki}/bin/logcli --addr='http://localhost:3100' query --no-labels '{job=\"varlogs\",filename=\"/var/log/testlog\"}' | grep -q 'Loki Ingestion Test'"
)
''; '';
}) })

View File

@ -1,12 +1,5 @@
import ./make-test.nix ({ pkgs, lib, ...}: import ./make-test-python.nix ({ pkgs, lib, ...}:
let
test = with pkgs; runCommand "patch-test" {
nativeBuildInputs = [ pgjwt ];
}
''
sed -e '12 i CREATE EXTENSION pgcrypto;\nCREATE EXTENSION pgtap;\nSET search_path TO tap,public;' ${pgjwt.src}/test.sql > $out;
'';
in
with pkgs; { with pkgs; {
name = "pgjwt"; name = "pgjwt";
meta = with lib.maintainers; { meta = with lib.maintainers; {
@ -29,9 +22,13 @@ with pkgs; {
pgProve = "${pkgs.perlPackages.TAPParserSourceHandlerpgTAP}"; pgProve = "${pkgs.perlPackages.TAPParserSourceHandlerpgTAP}";
in in
'' ''
startAll; start_all()
$master->waitForUnit("postgresql"); master.wait_for_unit("postgresql")
$master->copyFileFromHost("${test}","/tmp/test.sql"); master.succeed(
$master->succeed("${pkgs.sudo}/bin/sudo -u ${sqlSU} PGOPTIONS=--search_path=tap,public ${pgProve}/bin/pg_prove -d postgres -v -f /tmp/test.sql"); "${pkgs.gnused}/bin/sed -e '12 i CREATE EXTENSION pgcrypto;\\nCREATE EXTENSION pgtap;\\nSET search_path TO tap,public;' ${pgjwt.src}/test.sql > /tmp/test.sql"
)
master.succeed(
"${pkgs.sudo}/bin/sudo -u ${sqlSU} PGOPTIONS=--search_path=tap,public ${pgProve}/bin/pg_prove -d postgres -v -f /tmp/test.sql"
)
''; '';
}) })

View File

@ -4,12 +4,10 @@
}: }:
let let
inherit (import ../lib/testing.nix { inherit system pkgs; }) makeTest; inherit (import ../lib/testing-python.nix { inherit system pkgs; }) makeTest;
inherit (pkgs.lib) concatStringsSep maintainers mapAttrs mkMerge inherit (pkgs.lib) concatStringsSep maintainers mapAttrs mkMerge
removeSuffix replaceChars singleton splitString; removeSuffix replaceChars singleton splitString;
escape' = str: replaceChars [''"'' "$" "\n"] [''\\\"'' "\\$" ""] str;
/* /*
* The attrset `exporterTests` contains one attribute * The attrset `exporterTests` contains one attribute
* for each exporter test. Each of these attributes * for each exporter test. Each of these attributes
@ -33,9 +31,9 @@ let
* services.<metricProvider>.enable = true; * services.<metricProvider>.enable = true;
* }; * };
* exporterTest = '' * exporterTest = ''
* waitForUnit("prometheus-<exporterName>-exporter.service"); * wait_for_unit("prometheus-<exporterName>-exporter.service")
* waitForOpenPort("1234"); * wait_for_open_port("1234")
* succeed("curl -sSf 'localhost:1234/metrics'"); * succeed("curl -sSf 'localhost:1234/metrics'")
* ''; * '';
* }; * };
* *
@ -49,11 +47,11 @@ let
* }; * };
* *
* testScript = '' * testScript = ''
* $<exporterName>->start(); * <exporterName>.start()
* $<exporterName>->waitForUnit("prometheus-<exporterName>-exporter.service"); * <exporterName>.wait_for_unit("prometheus-<exporterName>-exporter.service")
* $<exporterName>->waitForOpenPort("1234"); * <exporterName>.wait_for_open_port("1234")
* $<exporterName>->succeed("curl -sSf 'localhost:1234/metrics'"); * <exporterName>.succeed("curl -sSf 'localhost:1234/metrics'")
* $<exporterName>->shutdown(); * <exporterName>.shutdown()
* ''; * '';
*/ */
@ -72,9 +70,11 @@ let
''; '';
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-bind-exporter.service"); wait_for_unit("prometheus-bind-exporter.service")
waitForOpenPort(9119); wait_for_open_port(9119)
succeed("curl -sSf http://localhost:9119/metrics | grep -q 'bind_query_recursions_total 0'"); succeed(
"curl -sSf http://localhost:9119/metrics | grep -q 'bind_query_recursions_total 0'"
)
''; '';
}; };
@ -89,9 +89,11 @@ let
}); });
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-blackbox-exporter.service"); wait_for_unit("prometheus-blackbox-exporter.service")
waitForOpenPort(9115); wait_for_open_port(9115)
succeed("curl -sSf 'http://localhost:9115/probe?target=localhost&module=icmp_v6' | grep -q 'probe_success 1'"); succeed(
"curl -sSf 'http://localhost:9115/probe?target=localhost&module=icmp_v6' | grep -q 'probe_success 1'"
)
''; '';
}; };
@ -100,7 +102,7 @@ let
enable = true; enable = true;
extraFlags = [ "--web.collectd-push-path /collectd" ]; extraFlags = [ "--web.collectd-push-path /collectd" ];
}; };
exporterTest =let postData = escape' '' exporterTest = let postData = replaceChars [ "\n" ] [ "" ] ''
[{ [{
"values":[23], "values":[23],
"dstypes":["gauge"], "dstypes":["gauge"],
@ -108,13 +110,21 @@ let
"interval":1000, "interval":1000,
"host":"testhost", "host":"testhost",
"plugin":"testplugin", "plugin":"testplugin",
"time":$(date +%s) "time":DATE
}] }]
''; in '' ''; in ''
waitForUnit("prometheus-collectd-exporter.service"); wait_for_unit("prometheus-collectd-exporter.service")
waitForOpenPort(9103); wait_for_open_port(9103)
succeed("curl -sSfH 'Content-Type: application/json' -X POST --data \"${postData}\" localhost:9103/collectd"); succeed(
succeed("curl -sSf localhost:9103/metrics | grep -q 'collectd_testplugin_gauge{instance=\"testhost\"} 23'"); 'echo \'${postData}\'> /tmp/data.json'
)
succeed('sed -ie "s DATE $(date +%s) " /tmp/data.json')
succeed(
"curl -sSfH 'Content-Type: application/json' -X POST --data @/tmp/data.json localhost:9103/collectd"
)
succeed(
"curl -sSf localhost:9103/metrics | grep -q 'collectd_testplugin_gauge{instance=\"testhost\"} 23'"
)
''; '';
}; };
@ -127,9 +137,9 @@ let
services.dnsmasq.enable = true; services.dnsmasq.enable = true;
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-dnsmasq-exporter.service"); wait_for_unit("prometheus-dnsmasq-exporter.service")
waitForOpenPort(9153); wait_for_open_port(9153)
succeed("curl -sSf http://localhost:9153/metrics | grep -q 'dnsmasq_leases 0'"); succeed("curl -sSf http://localhost:9153/metrics | grep -q 'dnsmasq_leases 0'")
''; '';
}; };
@ -144,9 +154,11 @@ let
services.dovecot2.enable = true; services.dovecot2.enable = true;
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-dovecot-exporter.service"); wait_for_unit("prometheus-dovecot-exporter.service")
waitForOpenPort(9166); wait_for_open_port(9166)
succeed("curl -sSf http://localhost:9166/metrics | grep -q 'dovecot_up{scope=\"global\"} 1'"); succeed(
"curl -sSf http://localhost:9166/metrics | grep -q 'dovecot_up{scope=\"global\"} 1'"
)
''; '';
}; };
@ -155,9 +167,11 @@ let
enable = true; enable = true;
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-fritzbox-exporter.service"); wait_for_unit("prometheus-fritzbox-exporter.service")
waitForOpenPort(9133); wait_for_open_port(9133)
succeed("curl -sSf http://localhost:9133/metrics | grep -q 'fritzbox_exporter_collect_errors 0'"); succeed(
"curl -sSf http://localhost:9133/metrics | grep -q 'fritzbox_exporter_collect_errors 0'"
)
''; '';
}; };
@ -180,11 +194,11 @@ let
}; };
}; };
exporterTest = '' exporterTest = ''
waitForUnit("nginx.service"); wait_for_unit("nginx.service")
waitForOpenPort(80); wait_for_open_port(80)
waitForUnit("prometheus-json-exporter.service"); wait_for_unit("prometheus-json-exporter.service")
waitForOpenPort(7979); wait_for_open_port(7979)
succeed("curl -sSf localhost:7979/metrics | grep -q 'json_test_metric 1'"); succeed("curl -sSf localhost:7979/metrics | grep -q 'json_test_metric 1'")
''; '';
}; };
@ -222,10 +236,12 @@ let
users.users.mailexporter.isSystemUser = true; users.users.mailexporter.isSystemUser = true;
}; };
exporterTest = '' exporterTest = ''
waitForUnit("postfix.service") wait_for_unit("postfix.service")
waitForUnit("prometheus-mail-exporter.service") wait_for_unit("prometheus-mail-exporter.service")
waitForOpenPort(9225) wait_for_open_port(9225)
waitUntilSucceeds("curl -sSf http://localhost:9225/metrics | grep -q 'mail_deliver_success{configname=\"testserver\"} 1'") wait_until_succeeds(
"curl -sSf http://localhost:9225/metrics | grep -q 'mail_deliver_success{configname=\"testserver\"} 1'"
)
''; '';
}; };
@ -256,9 +272,9 @@ let
}; };
}; };
exporterTest = '' exporterTest = ''
waitForUnit("nginx.service") wait_for_unit("nginx.service")
waitForUnit("prometheus-nextcloud-exporter.service") wait_for_unit("prometheus-nextcloud-exporter.service")
waitForOpenPort(9205) wait_for_open_port(9205)
succeed("curl -sSf http://localhost:9205/metrics | grep -q 'nextcloud_up 1'") succeed("curl -sSf http://localhost:9205/metrics | grep -q 'nextcloud_up 1'")
''; '';
}; };
@ -275,9 +291,9 @@ let
}; };
}; };
exporterTest = '' exporterTest = ''
waitForUnit("nginx.service") wait_for_unit("nginx.service")
waitForUnit("prometheus-nginx-exporter.service") wait_for_unit("prometheus-nginx-exporter.service")
waitForOpenPort(9113) wait_for_open_port(9113)
succeed("curl -sSf http://localhost:9113/metrics | grep -q 'nginx_up 1'") succeed("curl -sSf http://localhost:9113/metrics | grep -q 'nginx_up 1'")
''; '';
}; };
@ -287,9 +303,11 @@ let
enable = true; enable = true;
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-node-exporter.service"); wait_for_unit("prometheus-node-exporter.service")
waitForOpenPort(9100); wait_for_open_port(9100)
succeed("curl -sSf http://localhost:9100/metrics | grep -q 'node_exporter_build_info{.\\+} 1'"); succeed(
"curl -sSf http://localhost:9100/metrics | grep -q 'node_exporter_build_info{.\\+} 1'"
)
''; '';
}; };
@ -301,9 +319,11 @@ let
services.postfix.enable = true; services.postfix.enable = true;
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-postfix-exporter.service"); wait_for_unit("prometheus-postfix-exporter.service")
waitForOpenPort(9154); wait_for_open_port(9154)
succeed("curl -sSf http://localhost:9154/metrics | grep -q 'postfix_smtpd_connects_total 0'"); succeed(
"curl -sSf http://localhost:9154/metrics | grep -q 'postfix_smtpd_connects_total 0'"
)
''; '';
}; };
@ -316,18 +336,24 @@ let
services.postgresql.enable = true; services.postgresql.enable = true;
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-postgres-exporter.service"); wait_for_unit("prometheus-postgres-exporter.service")
waitForOpenPort(9187); wait_for_open_port(9187)
waitForUnit("postgresql.service"); wait_for_unit("postgresql.service")
succeed("curl -sSf http://localhost:9187/metrics | grep -q 'pg_exporter_last_scrape_error 0'"); succeed(
succeed("curl -sSf http://localhost:9187/metrics | grep -q 'pg_up 1'"); "curl -sSf http://localhost:9187/metrics | grep -q 'pg_exporter_last_scrape_error 0'"
systemctl("stop postgresql.service"); )
succeed("curl -sSf http://localhost:9187/metrics | grep -qv 'pg_exporter_last_scrape_error 0'"); succeed("curl -sSf http://localhost:9187/metrics | grep -q 'pg_up 1'")
succeed("curl -sSf http://localhost:9187/metrics | grep -q 'pg_up 0'"); systemctl("stop postgresql.service")
systemctl("start postgresql.service"); succeed(
waitForUnit("postgresql.service"); "curl -sSf http://localhost:9187/metrics | grep -qv 'pg_exporter_last_scrape_error 0'"
succeed("curl -sSf http://localhost:9187/metrics | grep -q 'pg_exporter_last_scrape_error 0'"); )
succeed("curl -sSf http://localhost:9187/metrics | grep -q 'pg_up 1'"); succeed("curl -sSf http://localhost:9187/metrics | grep -q 'pg_up 0'")
systemctl("start postgresql.service")
wait_for_unit("postgresql.service")
succeed(
"curl -sSf http://localhost:9187/metrics | grep -q 'pg_exporter_last_scrape_error 0'"
)
succeed("curl -sSf http://localhost:9187/metrics | grep -q 'pg_up 1'")
''; '';
}; };
@ -339,11 +365,13 @@ let
services.rspamd.enable = true; services.rspamd.enable = true;
}; };
exporterTest = '' exporterTest = ''
waitForUnit("rspamd.service"); wait_for_unit("rspamd.service")
waitForUnit("prometheus-rspamd-exporter.service"); wait_for_unit("prometheus-rspamd-exporter.service")
waitForOpenPort(11334); wait_for_open_port(11334)
waitForOpenPort(7980); wait_for_open_port(7980)
waitUntilSucceeds("curl -sSf localhost:7980/metrics | grep -q 'rspamd_scanned{host=\"rspamd\"} 0'"); wait_until_succeeds(
"curl -sSf localhost:7980/metrics | grep -q 'rspamd_scanned{host=\"rspamd\"} 0'"
)
''; '';
}; };
@ -356,9 +384,9 @@ let
}; };
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-snmp-exporter.service"); wait_for_unit("prometheus-snmp-exporter.service")
waitForOpenPort(9116); wait_for_open_port(9116)
succeed("curl -sSf localhost:9116/metrics | grep -q 'snmp_request_errors_total 0'"); succeed("curl -sSf localhost:9116/metrics | grep -q 'snmp_request_errors_total 0'")
''; '';
}; };
@ -377,11 +405,11 @@ let
}; };
}; };
exporterTest = '' exporterTest = ''
waitForUnit("nginx.service"); wait_for_unit("nginx.service")
waitForOpenPort(80); wait_for_open_port(80)
waitForUnit("prometheus-surfboard-exporter.service"); wait_for_unit("prometheus-surfboard-exporter.service")
waitForOpenPort(9239); wait_for_open_port(9239)
succeed("curl -sSf localhost:9239/metrics | grep -q 'surfboard_up 1'"); succeed("curl -sSf localhost:9239/metrics | grep -q 'surfboard_up 1'")
''; '';
}; };
@ -396,11 +424,11 @@ let
services.tor.controlPort = 9051; services.tor.controlPort = 9051;
}; };
exporterTest = '' exporterTest = ''
waitForUnit("tor.service"); wait_for_unit("tor.service")
waitForOpenPort(9051); wait_for_open_port(9051)
waitForUnit("prometheus-tor-exporter.service"); wait_for_unit("prometheus-tor-exporter.service")
waitForOpenPort(9130); wait_for_open_port(9130)
succeed("curl -sSf localhost:9130/metrics | grep -q 'tor_version{.\\+} 1'"); succeed("curl -sSf localhost:9130/metrics | grep -q 'tor_version{.\\+} 1'")
''; '';
}; };
@ -426,10 +454,12 @@ let
}; };
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-varnish-exporter.service"); wait_for_unit("prometheus-varnish-exporter.service")
waitForOpenPort(6081); wait_for_open_port(6081)
waitForOpenPort(9131); wait_for_open_port(9131)
succeed("curl -sSf http://localhost:9131/metrics | grep -q 'varnish_up 1'"); succeed(
"curl -sSf http://localhost:9131/metrics | grep -q 'varnish_up 1'"
)
''; '';
}; };
@ -451,9 +481,11 @@ let
systemd.services.prometheus-wireguard-exporter.after = [ "wireguard-wg0.service" ]; systemd.services.prometheus-wireguard-exporter.after = [ "wireguard-wg0.service" ];
}; };
exporterTest = '' exporterTest = ''
waitForUnit("prometheus-wireguard-exporter.service"); wait_for_unit("prometheus-wireguard-exporter.service")
waitForOpenPort(9586); wait_for_open_port(9586)
waitUntilSucceeds("curl -sSf http://localhost:9586/metrics | grep '${snakeoil.peer1.publicKey}'"); wait_until_succeeds(
"curl -sSf http://localhost:9586/metrics | grep '${snakeoil.peer1.publicKey}'"
)
''; '';
}; };
}; };
@ -466,11 +498,13 @@ mapAttrs (exporter: testConfig: (makeTest {
} testConfig.metricProvider or {}]; } testConfig.metricProvider or {}];
testScript = '' testScript = ''
${"$"+exporter}->start(); ${exporter}.start()
${concatStringsSep " " (map (line: '' ${concatStringsSep "\n" (map (line:
${"$"+exporter}->${line}; if (builtins.substring 0 1 line == " " || builtins.substring 0 1 line == ")")
'') (splitString "\n" (removeSuffix "\n" testConfig.exporterTest)))} then line
${"$"+exporter}->shutdown(); else "${exporter}.${line}"
) (splitString "\n" (removeSuffix "\n" testConfig.exporterTest)))}
${exporter}.shutdown()
''; '';
meta = with maintainers; { meta = with maintainers; {