Skip to content
This repository has been archived by the owner on Apr 12, 2021. It is now read-only.

Commit

Permalink
Merge pull request #72935 from marijanp/port-tests-python
Browse files Browse the repository at this point in the history
Port tests to Python
  • Loading branch information
flokli committed Nov 8, 2019
2 parents a00572a + 2c3785a commit dd439bc
Show file tree
Hide file tree
Showing 11 changed files with 192 additions and 152 deletions.
57 changes: 31 additions & 26 deletions nixos/tests/caddy.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, ... }: {
import ./make-test-python.nix ({ pkgs, ... }: {
name = "caddy";
meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ xfix ];
Expand Down Expand Up @@ -50,33 +50,38 @@ import ./make-test.nix ({ pkgs, ... }: {
etagSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-1";
justReloadSystem = "${nodes.webserver.config.system.build.toplevel}/fine-tune/child-2";
in ''
my $url = 'http://localhost/example.html';
$webserver->waitForUnit("caddy");
$webserver->waitForOpenPort("80");
url = "http://localhost/example.html"
webserver.wait_for_unit("caddy")
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 {
my $oldEtag = checkEtag;
$webserver->succeed("${etagSystem}/bin/switch-to-configuration test >&2");
$webserver->sleep(1); # race condition
my $newEtag = checkEtag;
die "Old ETag $oldEtag is the same as $newEtag" if $oldEtag eq $newEtag;
};
def check_etag(url):
etag = webserver.succeed(
"curl -v '{}' 2>&1 | sed -n -e \"s/^< [Ee][Tt][Aa][Gg]: *//p\"".format(url)
)
etag = etag.replace("\r\n", " ")
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");
$webserver->waitForOpenPort("8080");
};
with subtest("check ETag if serving Nix store paths"):
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")
'';
})
23 changes: 11 additions & 12 deletions nixos/tests/cadvisor.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, ... } : {
import ./make-test-python.nix ({ pkgs, ... } : {
name = "cadvisor";
meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ offline ];
Expand All @@ -16,20 +16,19 @@ import ./make-test.nix ({ pkgs, ... } : {
};
};

testScript =
''
startAll;
$machine->waitForUnit("cadvisor.service");
$machine->succeed("curl http://localhost:8080/containers/");
testScript = ''
start_all()
machine.wait_for_unit("cadvisor.service")
machine.succeed("curl http://localhost:8080/containers/")
$influxdb->waitForUnit("influxdb.service");
influxdb.wait_for_unit("influxdb.service")
# create influxdb database
$influxdb->succeed(q~
curl -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE root"
~);
influxdb.succeed(
'curl -XPOST http://localhost:8086/query --data-urlencode "q=CREATE DATABASE root"'
)
$influxdb->waitForUnit("cadvisor.service");
$influxdb->succeed("curl http://localhost:8080/containers/");
influxdb.wait_for_unit("cadvisor.service")
influxdb.succeed("curl http://localhost:8080/containers/")
'';
})
28 changes: 14 additions & 14 deletions nixos/tests/certmgr.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
pkgs ? import ../.. { inherit system config; }
}:

with import ../lib/testing.nix { inherit system pkgs; };
with import ../lib/testing-python.nix { inherit system pkgs; };
let
mkSpec = { host, service ? null, action }: {
inherit action;
Expand Down Expand Up @@ -123,17 +123,17 @@ in
)));
};
testScript = ''
$machine->waitForUnit('cfssl.service');
$machine->waitUntilSucceeds('ls /tmp/decl.example.org-ca.pem');
$machine->waitUntilSucceeds('ls /tmp/decl.example.org-key.pem');
$machine->waitUntilSucceeds('ls /tmp/decl.example.org-cert.pem');
$machine->waitUntilSucceeds('ls /tmp/imp.example.org-ca.pem');
$machine->waitUntilSucceeds('ls /tmp/imp.example.org-key.pem');
$machine->waitUntilSucceeds('ls /tmp/imp.example.org-cert.pem');
$machine->waitForUnit('nginx.service');
$machine->succeed('[ "1" -lt "$(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/decl.example.org-ca.pem https://decl.example.org');
machine.wait_for_unit("cfssl.service")
machine.wait_until_succeeds("ls /tmp/decl.example.org-ca.pem")
machine.wait_until_succeeds("ls /tmp/decl.example.org-key.pem")
machine.wait_until_succeeds("ls /tmp/decl.example.org-cert.pem")
machine.wait_until_succeeds("ls /tmp/imp.example.org-ca.pem")
machine.wait_until_succeeds("ls /tmp/imp.example.org-key.pem")
machine.wait_until_succeeds("ls /tmp/imp.example.org-cert.pem")
machine.wait_for_unit("nginx.service")
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/decl.example.org-ca.pem https://decl.example.org")
'';
};

Expand All @@ -143,8 +143,8 @@ in
test = mkSpec { host = "command.example.org"; action = "touch /tmp/command.executed"; };
};
testScript = ''
$machine->waitForUnit('cfssl.service');
$machine->waitUntilSucceeds('stat /tmp/command.executed');
machine.wait_for_unit("cfssl.service")
machine.wait_until_succeeds("stat /tmp/command.executed")
'';
};

Expand Down
8 changes: 4 additions & 4 deletions nixos/tests/cfssl.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, ...} : {
import ./make-test-python.nix ({ pkgs, ...} : {
name = "cfssl";

machine = { config, lib, pkgs, ... }:
Expand Down Expand Up @@ -60,8 +60,8 @@ import ./make-test.nix ({ pkgs, ...} : {
});
in
''
$machine->waitForUnit('cfssl.service');
$machine->waitUntilSucceeds('${cfsslrequest}');
$machine->succeed('ls /tmp/certificate-key.pem');
machine.wait_for_unit("cfssl.service")
machine.wait_until_succeeds("${cfsslrequest}")
machine.succeed("ls /tmp/certificate-key.pem")
'';
})
47 changes: 25 additions & 22 deletions nixos/tests/cjdns.nix
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,7 @@ let

in

import ./make-test.nix ({ pkgs, ...} : {
import ./make-test-python.nix ({ pkgs, ...} : {
name = "cjdns";
meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ ehmry ];
Expand Down Expand Up @@ -83,36 +83,39 @@ import ./make-test.nix ({ pkgs, ...} : {

testScript =
''
startAll;
import re
$alice->waitForUnit("cjdns.service");
$bob->waitForUnit("cjdns.service");
$carol->waitForUnit("cjdns.service");
start_all()
sub cjdnsIp {
my ($machine) = @_;
my $ip = (split /[ \/]+/, $machine->succeed("ip -o -6 addr show dev tun0"))[3];
$machine->log("has ip $ip");
return $ip;
}
alice.wait_for_unit("cjdns.service")
bob.wait_for_unit("cjdns.service")
carol.wait_for_unit("cjdns.service")
my $aliceIp6 = cjdnsIp $alice;
my $bobIp6 = cjdnsIp $bob;
my $carolIp6 = cjdnsIp $carol;
def cjdns_ip(machine):
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
$alice->succeed("ping -c 4 $carolIp6");
$bob->succeed("ping -c 4 $carolIp6");
alice.succeed("ping -c 4 {}".format(carol_ip6))
bob.succeed("ping -c 4 {}".format(carol_ip6))
$carol->succeed("ping -c 4 $aliceIp6");
$carol->succeed("ping -c 4 $bobIp6");
carol.succeed("ping -c 4 {}".format(alice_ip6))
carol.succeed("ping -c 4 {}".format(bob_ip6))
$alice->succeed("ping -c 4 $bobIp6");
$bob->succeed("ping -c 4 $aliceIp6");
alice.succeed("ping -c 4 {}".format(bob_ip6))
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))
'';
})
13 changes: 8 additions & 5 deletions nixos/tests/cloud-init.nix
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@
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;

let
Expand All @@ -30,6 +30,7 @@ let
'';
};
in makeTest {
name = "cloud-init";
meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ lewo ];
};
Expand All @@ -40,10 +41,12 @@ in makeTest {
services.cloud-init.enable = true;
};
testScript = ''
$machine->start;
$machine->waitForUnit("cloud-init.service");
$machine->succeed("cat /tmp/cloudinit-write-file | grep -q 'cloudinit'");
machine.start()
machine.wait_for_unit("cloud-init.service")
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!'"
)
'';
}
56 changes: 38 additions & 18 deletions nixos/tests/couchdb.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, lib, ...}:
import ./make-test-python.nix ({ pkgs, lib, ...}:

with lib;

Expand Down Expand Up @@ -35,22 +35,42 @@ with lib;
fi
'';
in ''
startAll;
$couchdb1->waitForUnit("couchdb.service");
$couchdb1->waitUntilSucceeds("${curlJqCheck "GET" "" ".couchdb" "Welcome"}");
$couchdb1->waitUntilSucceeds("${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->waitUntilSucceeds("${curlJqCheck "GET" "" ".couchdb" "Welcome"}");
$couchdb2->waitUntilSucceeds("${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"}");
start_all()
couchdb1.wait_for_unit("couchdb.service")
couchdb1.wait_until_succeeds(
"${curlJqCheck "GET" "" ".couchdb" "Welcome"}"
)
couchdb1.wait_until_succeeds(
"${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.wait_for_unit("couchdb.service")
couchdb2.wait_until_succeeds(
"${curlJqCheck "GET" "" ".couchdb" "Welcome"}"
)
couchdb2.wait_until_succeeds(
"${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"}"
)
'';
})
12 changes: 7 additions & 5 deletions nixos/tests/dnscrypt-proxy.nix
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
import ./make-test.nix ({ pkgs, ... }: {
import ./make-test-python.nix ({ pkgs, ... }: {
name = "dnscrypt-proxy";
meta = with pkgs.stdenv.lib.maintainers; {
maintainers = [ joachifm ];
Expand All @@ -23,11 +23,13 @@ import ./make-test.nix ({ pkgs, ... }: {
};

testScript = ''
$client->waitForUnit("dnsmasq");
client.wait_for_unit("dnsmasq")
# The daemon is socket activated; sending a single ping should activate it.
$client->fail("systemctl is-active dnscrypt-proxy");
$client->execute("${pkgs.iputils}/bin/ping -c1 example.com");
$client->waitUntilSucceeds("systemctl is-active dnscrypt-proxy");
client.fail("systemctl is-active dnscrypt-proxy")
client.execute(
"${pkgs.iputils}/bin/ping -c1 example.com"
)
client.wait_until_succeeds("systemctl is-active dnscrypt-proxy")
'';
})

0 comments on commit dd439bc

Please sign in to comment.