-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathtest.nix
239 lines (230 loc) · 8.69 KB
/
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
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
args@{
self,
system,
nixpkgsFor,
libFor,
nixosLibFor,
ldflags,
...
}:
let
pkgs = nixpkgsFor.${system};
lib = nixosLibFor.${system} { inherit system; };
rootCert = builtins.readFile ./cert/minica.pem;
rootKey = builtins.readFile ./cert/minica-key.pem;
csCert = builtins.readFile ./cert/cs/cert.pem;
csKey = builtins.readFile ./cert/cs/key.pem;
autologin =
{ ... }:
{
services.getty.autologinUser = "root";
};
base =
{ ... }:
{
imports = [ autologin ];
virtualisation.vlans = [ 1 ];
environment.systemPackages = with pkgs; [ wireguard-tools ];
services.logrotate.enable = false; # clogs up the logs
};
in
{
goal = lib.runTest (
let
peerPrivateKey = "kCtV08G5gyM/cGHToObIAtwRq/bqI2Jd3akIsAMXRXM=";
peerPublicKey = "72zpXYpjSWnvyhwZTuRNwtghjxjzhWEVzUNRA82hoUA=";
defaultPrivateKey = "eDq8aX08rF5cLG+NNi14Ae8TIudsMHiWCjsbBTDI1Ec=";
defaultPublicKey = "+atCYz0YmiwBx4AZy5kDGr5WHqHs3RMbIuPfj593sRk=";
etc = self.outputs.packages.${system}.etc;
machine1 = pkgs.writeText "machine1.json" (builtins.toJSON { });
machine2 = pkgs.writeText "machine2.json" (
builtins.toJSON {
Interfaces = [
{
Name = "wiring";
PrivateKey = defaultPrivateKey;
ListenPort = 51820;
Addresses = [ "10.10.0.0/32" ];
Peers = [
{
Name = "peer";
PublicKey = peerPublicKey;
Endpoint = "peer:51820";
AllowedIPs = [ "10.10.0.1/32" ];
PersistentKeepalive = "30s";
}
];
}
];
}
);
machine3 = pkgs.writeText "machine3.json" (
builtins.toJSON {
Interfaces = [
{
Name = "wiring";
PrivateKey = defaultPrivateKey;
ListenPort = 51820;
Addresses = [ "10.10.0.0/32" ];
Peers = [
{
Name = "peer";
PublicKey = peerPublicKey;
Endpoint = "peer:51820";
AllowedIPs = [ "10.10.0.8/32" ];
}
];
}
];
}
);
continuityPort = 51821;
continuityServer = pkgs.writeText "continuityServer.py" ''
import socketserver
import os
class MyTCPHandler(socketserver.BaseRequestHandler):
def handle(self):
counter = 0
while True:
print("waiting...")
self.data = self.request.recv(1024).strip()
print(f"received from {self.client_address[0]}: {self.data}")
i = int(self.data.decode('ascii'))
print('a')
if i != counter + 1:
print("out of order")
exit(1)
counter = i
print('b')
self.request.sendall("ok\n".encode("ascii"))
print("sent.")
if __name__ == "__main__":
host, port = os.getenv("HOST"), int(os.getenv("PORT"))
with socketserver.TCPServer((host, port), MyTCPHandler) as server:
print(f"serving on {host}:{port}...")
server.serve_forever()
'';
continuityClient = pkgs.writeText "continuityClient.py" ''
import os
import signal
import socket
import sys
import time
host, port = os.getenv("HOST"), int(os.getenv("PORT"))
print(f"host {host}, port {port}")
with socket.socket(socket.AF_INET, socket.SOCK_STREAM) as sock:
print("connecting...")
sock.connect((host, port))
print("connected.")
counter = 1
while True:
if counter == 2:
print("setting signal handler for graceful exit...")
signal.signal(signal.SIGTERM, lambda signum, frame: exit(0))
sock.sendall(bytes(str(counter) + "\n", "utf-8"))
received = str(sock.recv(1024), "utf-8")
print(f"received {len(received)}: {received}")
if "ok" not in received:
raise RuntimeError("not ok")
print(f"{counter} ok")
time.sleep(0.3)
counter += 1
'';
in
{
name = "goal";
hostPkgs = pkgs;
nodes.default =
{ pkgs, ... }:
{
imports = [ base ];
environment.systemPackages = [ self.outputs.packages.${system}.etc ];
systemd.services.goal1.script = ''
QRYSTAL_LOGGING_CONFIG=development ${etc}/bin/test-goal -m-path ${machine1}
'';
systemd.services.goal1.serviceConfig.Type = "oneshot";
systemd.services.goal1.path = [ pkgs.iputils ];
systemd.services.goal2.script = ''
QRYSTAL_LOGGING_CONFIG=development ${etc}/bin/test-goal -m-path ${machine2}
'';
systemd.services.goal2.serviceConfig.Type = "oneshot";
systemd.services.goal2.path = [ pkgs.iputils ];
systemd.services.goal3.script = ''
QRYSTAL_LOGGING_CONFIG=development ${etc}/bin/test-goal -m-path ${machine3}
'';
systemd.services.goal3.serviceConfig.Type = "oneshot";
systemd.services.goal3.path = [ pkgs.iputils ];
systemd.services."continuityClient" = {
environment.HOST = "peer";
environment.PORT = builtins.toString continuityPort;
script = "${pkgs.python3}/bin/python3 ${continuityClient}";
};
};
nodes.peer =
{ pkgs, ... }:
{
imports = [ base ];
#TODO: wireguard config
networking.wireguard.interfaces = {
wiring = {
ips = [ "10.10.0.1/32" ];
privateKey = peerPrivateKey;
listenPort = 51820;
peers = [
{
publicKey = defaultPublicKey;
allowedIPs = [ "10.10.0.0/32" ];
endpoint = "default:51820";
persistentKeepalive = 30;
}
];
};
};
networking.firewall.allowedTCPPorts = [ continuityPort ];
systemd.services."continuityServer" = {
environment.HOST = "0.0.0.0";
environment.PORT = builtins.toString continuityPort;
script = "${pkgs.python3}/bin/python3 ${continuityServer}";
};
};
testScript =
{ ... }:
''
peer.start()
default.start()
peer.wait_until_succeeds("wg show wiring")
# sanity check
default.systemctl("--wait start goal1.service")
assert "Result=success" in default.execute("systemctl show goal1.service --property=Result")[1]
# run goal1
default.systemctl("--wait start goal2.service")
assert "Result=success" in default.execute("systemctl show goal2.service --property=Result")[1]
print(default.succeed("ip link show"))
print("default addr", default.succeed("ip addr"))
print("peer addr", peer.succeed("ip addr"))
print("peer: ", peer.succeed("wg show wiring"))
print("default: ", default.succeed("wg show wiring"))
print("default: ", default.wait_until_succeeds("ping -c 2 10.10.0.0"))
print("default: ", default.wait_until_succeeds("ping -c 2 10.10.0.1"))
peer.wait_until_succeeds("ping -c 2 10.10.0.0")
peer.wait_until_succeeds("ping -c 2 10.10.0.1")
peer.systemctl("start continuityServer.service")
default.systemctl("start continuityClient.service")
default.systemctl("--wait start goal3.service")
assert "Result=success" in default.execute("systemctl show goal3.service --property=Result")[1]
print(default.succeed("ip link show"))
print("default: ", default.succeed("wg show wiring"))
peer.fail("ping -c 4 10.10.0.0")
peer.succeed("ping -c 4 10.10.0.1")
default.succeed("ping -c 4 10.10.0.0")
default.fail("ping -c 4 10.10.0.1")
default.fail("ping -c 4 10.10.0.8")
print(peer.systemctl("status continuityServer.service")[1])
print(default.systemctl("stop continuityClient.service")[1])
print(default.systemctl("status continuityClient.service")[1])
assert "Result=success" in peer.execute("systemctl show continuityServer.service --property=Result")[1]
assert "Result=success" in default.execute("systemctl show continuityClient.service --property=Result")[1]
'';
}
);
}