-
Notifications
You must be signed in to change notification settings - Fork 4
/
test.nix
282 lines (243 loc) · 8.17 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
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
{ pkgs, ... }:
let
#servicescript that creates a file
makeScript = scriptName: pkgs.writeText "serviceScript" ''
#!${pkgs.bash}/bin/bash
while true; do touch /${scriptName}; sleep 1; done
'';
#derviation for aboves script
makeScriptDerivation = script: (pkgs.stdenv.mkDerivation {
name = "testscript";
phases = [ "installPhase" ];
installPhase = ''
mkdir -p $out/bin
cp ${script} $out/bin/yesscript
chmod u+x $out/bin/*
'';
});
#container configuration with that script
makeConfig = scriptName: pkgs.writeText "testScript" ''{
autostart = true;
configuration = {
networking.firewall.allowPing = true;
systemd.services.bar = {
description = "YesItWorks ${scriptName} Daemon";
wantedBy = [ "multi-user.target" ];
after = [ "network.target" ];
serviceConfig = {
ExecStart = "${makeScriptDerivation (makeScript scriptName)}/bin/yesscript";
Restart = "always";
Type="simple";
};
};
};
} '';
containerConfig = makeConfig "yesitworks";
containerConfigRollback = makeConfig "yesitrollsback";
containerConfigUpdate = makeConfig "yesitupdates";
#empty container
containerTestTwo = pkgs.writeText "test2" ''
{
configuration = { };
}'';
#container with fixed ip
containerTestThree = pkgs.writeText "test3" ''
{
network = {
ip = "10.10.10.10";
};
configuration = { };
}'';
containerTest4 = pkgs.writeText "test4" ''
{
configuration = { };
}'';
containerPreBuild = [
(import ./bin/helper/lxc-container.nix {
name="test";
ip="10.101.0.2";
container = import containerConfig;
})
(import ./bin/helper/lxc-container.nix {
name="empty";
ip="10.101.0.2";
container = import containerTestTwo;
})
];
in {
name = "nixcloud-container";
machine = { pkgs, lib, ... }: {
nix.nixPath = [ "nixpkgs=${pkgs.path}" ];
nix.binaryCaches = lib.mkForce [];
nixcloud.container.enable = true;
networking.firewall.enable = false;
virtualisation.memorySize = 2048;
# Needed so that we have all dependencies available for building the
# container config within the VM.
virtualisation.pathsInNixDB = [ pkgs.stdenv ] ++ containerPreBuild;
};
testScript = ''
$machine->waitForUnit('multi-user.target');
$machine->nest('list all containers, should be empty', sub {
$machine->succeed('
if [[ $(nixcloud-container list) ]]; then
echo "Output should be empty."
exit 1
else
exit 0
fi
');
});
$machine->succeed('
#make sure the profile directory exists
mkdir -p /nix/var/nix/profiles
');
$machine->succeed('
#create a new container
nixcloud-container create test ${containerConfig} >&2
');
$machine->nest('list all containers, should not be empty', sub {
$machine->succeed('
if [[ $(nixcloud-container list) ]]; then
exit 0
else
echo "Output should not be empty."
exit 1
fi
');
});
$machine->nest('create a container that already exists', sub {
$machine->fail('nixcloud-container create test ${containerConfig} >&2');
});
$machine->nest('start the container', sub {
$machine->succeed('nixcloud-container start test >&2');
$machine->waitUntilSucceeds('test -f /var/lib/lxc/test/rootfs/yesitworks');
});
$machine->nest('ping the container', sub{
#ping the container
$machine->succeed('ping -c 1 10.101.0.2');
#ping the host
$machine->succeed('lxc-attach -n test -- ping -c 1 10.101.0.1');
});
$machine->nest('test if login into the container works', sub{
#create a file in the container rootFS
$machine->succeed('touch /var/lib/lxc/test/rootfs/logintest');
#login to the container and check if the file exists
$machine->succeed('lxc-attach -n test -- test -f /logintest');
});
$machine->nest('try to start container while it is still running', sub {
$machine->succeed('
if [[ $(nixcloud-container start test) != "*Container is already running.*" ]]; then
exit 0
else
echo "Container should already be running."
exit 1
fi
');
});
$machine->nest('check if container runs with non-root privileges', sub {
$machine->succeed('ps aux | grep "100000.*systemd"');
});
$machine->nest('terminate a machine that does not exist', sub {
$machine->fail('nixcloud-container terminate testbar');
});
$machine->nest('terminate a machine that does exist', sub {
$machine->succeed('nixcloud-container terminate test');
});
$machine->nest('update the existing container', sub {
$machine->succeed(
'nixcloud-container update test ${containerConfigRollback}'
);
$machine->succeed(
'nixcloud-container update test ${containerConfigUpdate}'
);
});
$machine->nest('restart the container', sub {
$machine->succeed('nixcloud-container start test');
$machine->waitUntilSucceeds('test -f /var/lib/lxc/test/rootfs/yesitupdates');
});
$machine->nest('switch to previous profile', sub{
#TODO FIXME currently when switching the profile inside a running container_
# the nixos script returns with exit 2 even if the configuration was
# successfully switched because it does not manage to remount some directorys
# once this bug is fixed this test should be changed back to 'succeed'
$machine->fail('nixcloud-container rollback test >&2');
$machine->waitUntilSucceeds('test -f /var/lib/lxc/test/rootfs/yesitrollsback');
$machine->succeed('nixcloud-container list-generations test >&2');
});
$machine->succeed('
if [[ $(nixcloud-container show-ip test) == "10.101.0.2" ]]; then
exit 0
else
echo "Container should have a valid ip."
exit 1
fi
');
$machine->succeed('nixcloud-container list >&2');
$machine->nest('list all containers, should not be empty', sub {
$machine->succeed('
if [[ $(nixcloud-container list) ]]; then
exit 0
else
echo "Output should not be empty."
exit 1
fi
');
});
$machine->succeed('rm /var/lib/lxc/test/rootfs/yesitrollsback');
#restart the lxc-autostart service this must restart the test container
$machine->nest('testing autostart of the container', sub{
$machine->succeed('systemctl restart lxc-autostart.service');
$machine->waitUntilSucceeds('
nixcloud-container state test | grep "RUNNING"
');
$machine->waitUntilSucceeds('test -f /var/lib/lxc/test/rootfs/yesitrollsback');
});
$machine->nest('terminate a machine that does exist', sub {
$machine->succeed('nixcloud-container terminate test');
});
$machine->nest('delete a machine that does exist', sub {
$machine->succeed('nixcloud-container destroy test');
});
$machine->nest('list all containers, should be empty', sub {
$machine->succeed('
if [[ $(nixcloud-container list) ]]; then
echo "Output should be empty."
exit 1
else
exit 0
fi
');
});
#test if ip is freed
$machine->succeed('nixcloud-container create empty ${containerTestTwo} >&2');
$machine->succeed('
if [[ $(nixcloud-container show-ip empty) == "10.101.0.2" ]]; then
exit 0
else
echo "Container should have the ip 10.101.0.2."
exit 1
fi
');
#test fixed ip
$machine->succeed('nixcloud-container create fixed ${containerTestThree} >&2');
$machine->succeed('
if [[ $(nixcloud-container show-ip fixed) == "\"10.10.10.10\"" ]]; then
exit 0
else
echo "Container should have the ip 10.10.10.10."
exit 1
fi
');
#test fixed ip
$machine->succeed('nixcloud-container create test4 ${containerTest4} >&2');
$machine->succeed('
if [[ $(nixcloud-container show-ip test4) == "10.101.0.3" ]]; then
exit 0
else
echo "Container should have the ip 10.101.0.3."
exit 1
fi
');
'';
}