-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathintegration_test.nix
89 lines (74 loc) · 2.52 KB
/
integration_test.nix
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
{ pkgs
, mdbServer
, mdbWebservice
}:
let
authEnv = {
MDB_HOST = "127.0.0.1";
MDB_DB = "testdb";
MDB_USER = "testuser";
MDB_PASS = "testpass";
};
testFunction = { pkgs, ... }: {
name = "run-mdb-service-with-webservice";
nodes = {
mdb = { pkgs, lib, ... }: {
networking.firewall.allowedTCPPorts = [ 1300 5000 ];
services = {
postgresql = {
enable = true;
package = pkgs.postgresql_10;
enableTCPIP = true;
authentication = "host all all 0.0.0.0/0 md5";
initialScript = pkgs.writeText "postgres-initScript" ''
CREATE ROLE ${authEnv.MDB_USER} WITH LOGIN PASSWORD '${authEnv.MDB_PASS}';
CREATE DATABASE ${authEnv.MDB_DB};
GRANT ALL PRIVILEGES ON DATABASE ${authEnv.MDB_DB} TO ${authEnv.MDB_USER};
'';
};
};
systemd.services.mdb-server = {
wantedBy = [ "multi-user.target" ];
after = [ "network.target" "postgresql.service" ];
serviceConfig.Restart = "always";
script = "exec ${mdbServer}/bin/messagedb-server";
environment = authEnv;
};
systemd.services.mdb-webservice = {
wantedBy = [ "multi-user.target" ];
after = [ "mdb-server.service" ];
serviceConfig.Restart = "always";
script = "exec ${mdbWebservice}/bin/mdb-webserver";
environment = authEnv;
};
};
};
testScript = ''
def send_message(msg):
return mdb.succeed(
f"echo -n {msg} | ${pkgs.nmap}/bin/ncat localhost 1300"
)
def check_count(select, nlines):
output = mdb.succeed(f'su -c "psql -d ${authEnv.MDB_DB} -tAc \\"{select}\\"" postgres')
print(output)
return nlines == len(output.splitlines())
mdb.start()
mdb.wait_for_unit("mdb-webservice.service")
mdb.wait_for_unit("postgresql.service")
print(mdb.succeed("journalctl -u postgresql.service"))
mdb.wait_until_succeeds(
"${pkgs.curl}/bin/curl http://localhost:5000"
)
check_count("SELECT * FROM testcounter;", 0)
send_message("hello")
check_count("SELECT * FROM testcounter;", 1)
assert "hello" in mdb.succeed(
"${pkgs.curl}/bin/curl http://localhost:5000"
)
send_message("foobar")
check_count("SELECT * FROM testcounter;", 2)
check_count("SELECT * FROM testcounter WHERE content = 'foobar';", 1)
'';
};
in
pkgs.nixosTest testFunction