Merge pull request #72935 from marijanp/port-tests-python

Port tests to Python
This commit is contained in:
Florian Klink 2019-11-08 14:50:04 +01:00 committed by GitHub
commit dd439bc01f
No known key found for this signature in database
GPG key ID: 4AEE18F83AFDEB23
11 changed files with 185 additions and 145 deletions

View file

@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, ... }: { import ./make-test-python.nix ({ pkgs, ... }: {
name = "caddy"; name = "caddy";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ xfix ]; maintainers = [ xfix ];
@ -50,33 +50,38 @@ import ./make-test.nix ({ pkgs, ... }: {
etagSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-1"; etagSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-1";
justReloadSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-2"; justReloadSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-2";
in '' in ''
my $url = 'http://localhost/example.html'; url = "http://localhost/example.html"
$webserver->waitForUnit("caddy"); webserver.wait_for_unit("caddy")
$webserver->waitForOpenPort("80"); webserver.wait_for_open_port("80")
sub checkEtag {
my $etag = $webserver->succeed(
'curl -v '.$url.' 2>&1 | sed -n -e "s/^< [Ee][Tt][Aa][Gg]: *//p"'
);
$etag =~ s/\r?\n$//;
my $httpCode = $webserver->succeed(
'curl -w "%{http_code}" -X HEAD -H \'If-None-Match: '.$etag.'\' '.$url
);
die "HTTP code is not 304" unless $httpCode == 304;
return $etag;
}
subtest "check ETag if serving Nix store paths", sub { def check_etag(url):
my $oldEtag = checkEtag; etag = webserver.succeed(
$webserver->succeed("${etagSystem}/bin/switch-to-configuration test >&2"); "curl -v '{}' 2>&1 | sed -n -e \"s/^< [Ee][Tt][Aa][Gg]: *//p\"".format(url)
$webserver->sleep(1); # race condition )
my $newEtag = checkEtag; etag = etag.replace("\r\n", " ")
die "Old ETag $oldEtag is the same as $newEtag" if $oldEtag eq $newEtag; http_code = webserver.succeed(
}; "curl -w \"%{{http_code}}\" -X HEAD -H 'If-None-Match: {}' {}".format(etag, url)
)
assert int(http_code) == 304, "HTTP code is not 304"
return etag
subtest "config is reloaded on nixos-rebuild switch", sub {
$webserver->succeed("${justReloadSystem}/bin/switch-to-configuration test >&2"); with subtest("check ETag if serving Nix store paths"):
$webserver->waitForOpenPort("8080"); old_etag = check_etag(url)
}; webserver.succeed(
"${etagSystem}/bin/switch-to-configuration test >&2"
)
webserver.sleep(1)
new_etag = check_etag(url)
assert old_etag != new_etag, "Old ETag {} is the same as {}".format(
old_etag, new_etag
)
with subtest("config is reloaded on nixos-rebuild switch"):
webserver.succeed(
"${justReloadSystem}/bin/switch-to-configuration test >&2"
)
webserver.wait_for_open_port("8080")
''; '';
}) })

View file

@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, ... } : { import ./make-test-python.nix ({ pkgs, ... } : {
name = "cadvisor"; name = "cadvisor";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ offline ]; maintainers = [ offline ];
@ -16,20 +16,19 @@ import ./make-test.nix ({ pkgs, ... } : {
}; };
}; };
testScript = testScript = ''
'' start_all()
startAll; machine.wait_for_unit("cadvisor.service")
$machine->waitForUnit("cadvisor.service"); machine.succeed("curl http://localhost:8080/containers/")
$machine->succeed("curl http://localhost:8080/containers/");
$influxdb->waitForUnit("influxdb.service"); influxdb.wait_for_unit("influxdb.service")
# create influxdb database # create influxdb database
$influxdb->succeed(q~ influxdb.succeed(
curl -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE root" 'curl -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE root"'
~); )
$influxdb->waitForUnit("cadvisor.service"); influxdb.wait_for_unit("cadvisor.service")
$influxdb->succeed("curl http://localhost:8080/containers/"); influxdb.succeed("curl http://localhost:8080/containers/")
''; '';
}) })

View file

@ -3,7 +3,7 @@
pkgs ? import ../.. { inherit system config; } pkgs ? import ../.. { inherit system config; }
}: }:
with import ../lib/testing.nix { inherit system pkgs; }; with import ../lib/testing-python.nix { inherit system pkgs; };
let let
mkSpec = { host, service ? null, action }: { mkSpec = { host, service ? null, action }: {
inherit action; inherit action;
@ -123,17 +123,17 @@ in
))); )));
}; };
testScript = '' testScript = ''
$machine->waitForUnit('cfssl.service'); machine.wait_for_unit("cfssl.service")
$machine->waitUntilSucceeds('ls /tmp/decl.example.org-ca.pem'); machine.wait_until_succeeds("ls /tmp/decl.example.org-ca.pem")
$machine->waitUntilSucceeds('ls /tmp/decl.example.org-key.pem'); machine.wait_until_succeeds("ls /tmp/decl.example.org-key.pem")
$machine->waitUntilSucceeds('ls /tmp/decl.example.org-cert.pem'); machine.wait_until_succeeds("ls /tmp/decl.example.org-cert.pem")
$machine->waitUntilSucceeds('ls /tmp/imp.example.org-ca.pem'); machine.wait_until_succeeds("ls /tmp/imp.example.org-ca.pem")
$machine->waitUntilSucceeds('ls /tmp/imp.example.org-key.pem'); machine.wait_until_succeeds("ls /tmp/imp.example.org-key.pem")
$machine->waitUntilSucceeds('ls /tmp/imp.example.org-cert.pem'); machine.wait_until_succeeds("ls /tmp/imp.example.org-cert.pem")
$machine->waitForUnit('nginx.service'); machine.wait_for_unit("nginx.service")
$machine->succeed('[ "1" -lt "$(journalctl -u nginx | grep "Starting Nginx" | wc -l)" ]'); assert 1 < int(machine.succeed('journalctl -u nginx | grep "Starting Nginx" | wc -l'))
$machine->succeed('curl --cacert /tmp/imp.example.org-ca.pem https://imp.example.org'); machine.succeed("curl --cacert /tmp/imp.example.org-ca.pem https://imp.example.org")
$machine->succeed('curl --cacert /tmp/decl.example.org-ca.pem https://decl.example.org'); machine.succeed("curl --cacert /tmp/decl.example.org-ca.pem https://decl.example.org")
''; '';
}; };
@ -143,8 +143,8 @@ in
test = mkSpec { host = "command.example.org"; action = "touch /tmp/command.executed"; }; test = mkSpec { host = "command.example.org"; action = "touch /tmp/command.executed"; };
}; };
testScript = '' testScript = ''
$machine->waitForUnit('cfssl.service'); machine.wait_for_unit("cfssl.service")
$machine->waitUntilSucceeds('stat /tmp/command.executed'); machine.wait_until_succeeds("stat /tmp/command.executed")
''; '';
}; };

View file

@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, ...} : { import ./make-test-python.nix ({ pkgs, ...} : {
name = "cfssl"; name = "cfssl";
machine = { config, lib, pkgs, ... }: machine = { config, lib, pkgs, ... }:
@ -60,8 +60,8 @@ import ./make-test.nix ({ pkgs, ...} : {
}); });
in in
'' ''
$machine->waitForUnit('cfssl.service'); machine.wait_for_unit("cfssl.service")
$machine->waitUntilSucceeds('${cfsslrequest}'); machine.wait_until_succeeds("${cfsslrequest}")
$machine->succeed('ls /tmp/certificate-key.pem'); machine.succeed("ls /tmp/certificate-key.pem")
''; '';
}) })

View file

@ -17,7 +17,7 @@ let
in in
import ./make-test.nix ({ pkgs, ...} : { import ./make-test-python.nix ({ pkgs, ...} : {
name = "cjdns"; name = "cjdns";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ ehmry ]; maintainers = [ ehmry ];
@ -83,36 +83,39 @@ import ./make-test.nix ({ pkgs, ...} : {
testScript = testScript =
'' ''
startAll; import re
$alice->waitForUnit("cjdns.service"); start_all()
$bob->waitForUnit("cjdns.service");
$carol->waitForUnit("cjdns.service");
sub cjdnsIp { alice.wait_for_unit("cjdns.service")
my ($machine) = @_; bob.wait_for_unit("cjdns.service")
my $ip = (split /[ \/]+/, $machine->succeed("ip -o -6 addr show dev tun0"))[3]; carol.wait_for_unit("cjdns.service")
$machine->log("has ip $ip");
return $ip;
}
my $aliceIp6 = cjdnsIp $alice;
my $bobIp6 = cjdnsIp $bob; def cjdns_ip(machine):
my $carolIp6 = cjdnsIp $carol; res = machine.succeed("ip -o -6 addr show dev tun0")
ip = re.split("\s+|/", res)[3]
machine.log("has ip {}".format(ip))
return ip
alice_ip6 = cjdns_ip(alice)
bob_ip6 = cjdns_ip(bob)
carol_ip6 = cjdns_ip(carol)
# ping a few times each to let the routing table establish itself # ping a few times each to let the routing table establish itself
$alice->succeed("ping -c 4 $carolIp6"); alice.succeed("ping -c 4 {}".format(carol_ip6))
$bob->succeed("ping -c 4 $carolIp6"); bob.succeed("ping -c 4 {}".format(carol_ip6))
$carol->succeed("ping -c 4 $aliceIp6"); carol.succeed("ping -c 4 {}".format(alice_ip6))
$carol->succeed("ping -c 4 $bobIp6"); carol.succeed("ping -c 4 {}".format(bob_ip6))
$alice->succeed("ping -c 4 $bobIp6"); alice.succeed("ping -c 4 {}".format(bob_ip6))
$bob->succeed("ping -c 4 $aliceIp6"); bob.succeed("ping -c 4 {}".format(alice_ip6))
$alice->waitForUnit("httpd.service"); alice.wait_for_unit("httpd.service")
$bob->succeed("curl --fail -g http://[$aliceIp6]"); bob.succeed("curl --fail -g http://[{}]".format(alice_ip6))
''; '';
}) })

View file

@ -3,7 +3,7 @@
pkgs ? import ../.. { inherit system config; } pkgs ? import ../.. { inherit system config; }
}: }:
with import ../lib/testing.nix { inherit system pkgs; }; with import ../lib/testing-python.nix { inherit system pkgs; };
with pkgs.lib; with pkgs.lib;
let let
@ -30,6 +30,7 @@ let
''; '';
}; };
in makeTest { in makeTest {
name = "cloud-init";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ lewo ]; maintainers = [ lewo ];
}; };
@ -40,10 +41,12 @@ in makeTest {
services.cloud-init.enable = true; services.cloud-init.enable = true;
}; };
testScript = '' testScript = ''
$machine->start; machine.start()
$machine->waitForUnit("cloud-init.service"); machine.wait_for_unit("cloud-init.service")
$machine->succeed("cat /tmp/cloudinit-write-file | grep -q 'cloudinit'"); machine.succeed("cat /tmp/cloudinit-write-file | grep -q 'cloudinit'")
$machine->waitUntilSucceeds("cat /root/.ssh/authorized_keys | grep -q 'should be a key!'"); machine.wait_until_succeeds(
"cat /root/.ssh/authorized_keys | grep -q 'should be a key!'"
)
''; '';
} }

View file

@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, lib, ...}: import ./make-test-python.nix ({ pkgs, lib, ...}:
with lib; with lib;
@ -35,22 +35,42 @@ with lib;
fi fi
''; '';
in '' in ''
startAll; start_all()
$couchdb1->waitForUnit("couchdb.service"); couchdb1.wait_for_unit("couchdb.service")
$couchdb1->waitUntilSucceeds("${curlJqCheck "GET" "" ".couchdb" "Welcome"}"); couchdb1.wait_until_succeeds(
$couchdb1->waitUntilSucceeds("${curlJqCheck "GET" "_all_dbs" ". | length" "2"}"); "${curlJqCheck "GET" "" ".couchdb" "Welcome"}"
$couchdb1->succeed("${curlJqCheck "PUT" "foo" ".ok" "true"}"); )
$couchdb1->succeed("${curlJqCheck "GET" "_all_dbs" ". | length" "3"}"); couchdb1.wait_until_succeeds(
$couchdb1->succeed("${curlJqCheck "DELETE" "foo" ".ok" "true"}"); "${curlJqCheck "GET" "_all_dbs" ". | length" "2"}"
$couchdb1->succeed("${curlJqCheck "GET" "_all_dbs" ". | length" "2"}"); )
couchdb1.succeed("${curlJqCheck "PUT" "foo" ".ok" "true"}")
couchdb1.succeed(
"${curlJqCheck "GET" "_all_dbs" ". | length" "3"}"
)
couchdb1.succeed(
"${curlJqCheck "DELETE" "foo" ".ok" "true"}"
)
couchdb1.succeed(
"${curlJqCheck "GET" "_all_dbs" ". | length" "2"}"
)
$couchdb2->waitForUnit("couchdb.service"); couchdb2.wait_for_unit("couchdb.service")
$couchdb2->waitUntilSucceeds("${curlJqCheck "GET" "" ".couchdb" "Welcome"}"); couchdb2.wait_until_succeeds(
$couchdb2->waitUntilSucceeds("${curlJqCheck "GET" "_all_dbs" ". | length" "0"}"); "${curlJqCheck "GET" "" ".couchdb" "Welcome"}"
$couchdb2->succeed("${curlJqCheck "PUT" "foo" ".ok" "true"}"); )
$couchdb2->succeed("${curlJqCheck "GET" "_all_dbs" ". | length" "1"}"); couchdb2.wait_until_succeeds(
$couchdb2->succeed("${curlJqCheck "DELETE" "foo" ".ok" "true"}"); "${curlJqCheck "GET" "_all_dbs" ". | length" "0"}"
$couchdb2->succeed("${curlJqCheck "GET" "_all_dbs" ". | length" "0"}"); )
couchdb2.succeed("${curlJqCheck "PUT" "foo" ".ok" "true"}")
couchdb2.succeed(
"${curlJqCheck "GET" "_all_dbs" ". | length" "1"}"
)
couchdb2.succeed(
"${curlJqCheck "DELETE" "foo" ".ok" "true"}"
)
couchdb2.succeed(
"${curlJqCheck "GET" "_all_dbs" ". | length" "0"}"
)
''; '';
}) })

View file

@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, ... }: { import ./make-test-python.nix ({ pkgs, ... }: {
name = "dnscrypt-proxy"; name = "dnscrypt-proxy";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ joachifm ]; maintainers = [ joachifm ];
@ -23,11 +23,13 @@ import ./make-test.nix ({ pkgs, ... }: {
}; };
testScript = '' testScript = ''
$client->waitForUnit("dnsmasq"); client.wait_for_unit("dnsmasq")
# The daemon is socket activated; sending a single ping should activate it. # The daemon is socket activated; sending a single ping should activate it.
$client->fail("systemctl is-active dnscrypt-proxy"); client.fail("systemctl is-active dnscrypt-proxy")
$client->execute("${pkgs.iputils}/bin/ping -c1 example.com"); client.execute(
$client->waitUntilSucceeds("systemctl is-active dnscrypt-proxy"); "${pkgs.iputils}/bin/ping -c1 example.com"
)
client.wait_until_succeeds("systemctl is-active dnscrypt-proxy")
''; '';
}) })

View file

@ -1,6 +1,6 @@
# This test runs docker and checks if simple container starts # This test runs docker and checks if simple container starts
import ./make-test.nix ({ pkgs, ...} : { import ./make-test-python.nix ({ pkgs, ...} : {
name = "docker"; name = "docker";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ nequissimus offline ]; maintainers = [ nequissimus offline ];
@ -31,17 +31,19 @@ import ./make-test.nix ({ pkgs, ...} : {
}; };
testScript = '' testScript = ''
startAll; start_all()
$docker->waitForUnit("sockets.target"); docker.wait_for_unit("sockets.target")
$docker->succeed("tar cv --files-from /dev/null | docker import - scratchimg"); docker.succeed("tar cv --files-from /dev/null | docker import - scratchimg")
$docker->succeed("docker run -d --name=sleeping -v /nix/store:/nix/store -v /run/current-system/sw/bin:/bin scratchimg /bin/sleep 10"); docker.succeed(
$docker->succeed("docker ps | grep sleeping"); "docker run -d --name=sleeping -v /nix/store:/nix/store -v /run/current-system/sw/bin:/bin scratchimg /bin/sleep 10"
$docker->succeed("sudo -u hasprivs docker ps"); )
$docker->fail("sudo -u noprivs docker ps"); docker.succeed("docker ps | grep sleeping")
$docker->succeed("docker stop sleeping"); docker.succeed("sudo -u hasprivs docker ps")
docker.fail("sudo -u noprivs docker ps")
docker.succeed("docker stop sleeping")
# Must match version twice to ensure client and server versions are correct # Must match version twice to ensure client and server versions are correct
$docker->succeed('[ $(docker version | grep ${pkgs.docker-edge.version} | wc -l) = "2" ]'); docker.succeed('[ $(docker version | grep ${pkgs.docker-edge.version} | wc -l) = "2" ]')
''; '';
}) })

View file

@ -1,6 +1,6 @@
# This test runs docker and checks if simple container starts # This test runs docker and checks if simple container starts
import ./make-test.nix ({ pkgs, ...} : { import ./make-test-python.nix ({ pkgs, ...} : {
name = "docker"; name = "docker";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ nequissimus offline ]; maintainers = [ nequissimus offline ];
@ -31,17 +31,19 @@ import ./make-test.nix ({ pkgs, ...} : {
}; };
testScript = '' testScript = ''
startAll; start_all()
$docker->waitForUnit("sockets.target"); docker.wait_for_unit("sockets.target")
$docker->succeed("tar cv --files-from /dev/null | docker import - scratchimg"); docker.succeed("tar cv --files-from /dev/null | docker import - scratchimg")
$docker->succeed("docker run -d --name=sleeping -v /nix/store:/nix/store -v /run/current-system/sw/bin:/bin scratchimg /bin/sleep 10"); docker.succeed(
$docker->succeed("docker ps | grep sleeping"); "docker run -d --name=sleeping -v /nix/store:/nix/store -v /run/current-system/sw/bin:/bin scratchimg /bin/sleep 10"
$docker->succeed("sudo -u hasprivs docker ps"); )
$docker->fail("sudo -u noprivs docker ps"); docker.succeed("docker ps | grep sleeping")
$docker->succeed("docker stop sleeping"); docker.succeed("sudo -u hasprivs docker ps")
docker.fail("sudo -u noprivs docker ps")
docker.succeed("docker stop sleeping")
# Must match version twice to ensure client and server versions are correct # Must match version twice to ensure client and server versions are correct
$docker->succeed('[ $(docker version | grep ${pkgs.docker.version} | wc -l) = "2" ]'); docker.succeed('[ $(docker version | grep ${pkgs.docker.version} | wc -l) = "2" ]')
''; '';
}) })

View file

@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, lib, ...} : { import ./make-test-python.nix ({ pkgs, lib, ...} : {
name = "documize"; name = "documize";
meta = with pkgs.stdenv.lib.maintainers; { meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ ma27 ]; maintainers = [ ma27 ];
@ -29,30 +29,34 @@ import ./make-test.nix ({ pkgs, lib, ...} : {
}; };
testScript = '' testScript = ''
startAll; start_all()
$machine->waitForUnit("documize-server.service"); machine.wait_for_unit("documize-server.service")
$machine->waitForOpenPort(3000); machine.wait_for_open_port(3000)
my $dbhash = $machine->succeed("curl -f localhost:3000 " dbhash = machine.succeed(
. " | grep 'property=\"dbhash' " "curl -f localhost:3000 | grep 'property=\"dbhash' | grep -Po 'content=\"\\K[^\"]*'"
. " | grep -Po 'content=\"\\K[^\"]*'" )
);
chomp($dbhash); dbhash = dbhash.strip()
$machine->succeed("curl -X POST " machine.succeed(
. "--data 'dbname=documize' " (
. "--data 'dbhash=$dbhash' " "curl -X POST"
. "--data 'title=NixOS' " " --data 'dbname=documize'"
. "--data 'message=Docs' " " --data 'dbhash={}'"
. "--data 'firstname=John' " " --data 'title=NixOS'"
. "--data 'lastname=Doe' " " --data 'message=Docs'"
. "--data 'email=john.doe\@nixos.org' " " --data 'firstname=John'"
. "--data 'password=verysafe' " " --data 'lastname=Doe'"
. "-f localhost:3000/api/setup" " --data 'email=john.doe@nixos.org'"
); " --data 'password=verysafe'"
" -f localhost:3000/api/setup"
).format(dbhash)
)
$machine->succeed('test "$(curl -f localhost:3000/api/public/meta | jq ".title" | xargs echo)" = "NixOS"'); machine.succeed(
'test "$(curl -f localhost:3000/api/public/meta | jq ".title" | xargs echo)" = "NixOS"'
)
''; '';
}) })