-
Notifications
You must be signed in to change notification settings - Fork 16
/
Copy pathtest.sh
executable file
·168 lines (136 loc) · 5.12 KB
/
test.sh
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
#!/usr/bin/env bash
set -euo pipefail
shopt -s nullglob
scriptDir="$(dirname "$(readlink -f "$0")")"
PATH=$scriptDir:$PATH
cleanup() {
set +e
for container in $(extra-container list | grep ^test-); do
extra-container destroy $container
done
set -e
}
trap "cleanup" EXIT
trap "echo \"Error at $(realpath ${BASH_SOURCE[0]}):\$LINENO\"" ERR
testMatches() {
actual="$1"
expected="$2"
if [[ $actual != $expected ]]; then
echo
echo 'Pattern does not match'
echo 'Expected:'
echo "$expected"
echo
echo 'Actual:'
echo "$actual"
echo
return 1
fi
}
# This significantly reduces eval time. Not needed for NixOS ≥ 20.03
baseConfig='config = { documentation.nixos.enable = false; }'
cleanup
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
echo "Test attr arg and container starting "
output=$(extra-container create -A 'a b' --start <<EOF
{
"a b" = { config, pkgs, ... }: {
containers.test-1 = {
$baseConfig;
};
};
}
EOF
)
testMatches "$output" "*Installing*test-1*Starting*test-1*"
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
echo "Test starting and updating"
output=$(extra-container create -s <<EOF
{ config, pkgs, ... }:
{
containers.test-1 = {
$baseConfig // { environment.variables.foo = "a"; };
};
containers.test-2 = {
$baseConfig;
};
}
EOF
)
testMatches "$output" "*Starting*test-2*Updating*test-1*"
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
echo "Test unchanged"
output=$(extra-container create -s <<EOF
{ config, pkgs, ... }:
{
containers.test-1 = {
$baseConfig // { environment.variables.foo = "a"; };
};
}
EOF
)
testMatches "$output" "*test-1 (unchanged, skipped)*"
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
echo "Test updating and restarting"
output=$(extra-container create -u <<EOF
{ config, pkgs, ... }:
{
containers.test-1 = {
$baseConfig // { environment.variables.foo = "b"; };
};
containers.test-2 = {
privateNetwork = true;
$baseConfig;
};
}
EOF
)
testMatches "$output" "*Updating*test-1*Restarting*test-2*"
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
echo "Test list"
output=$(extra-container list | grep ^test- || true)
testMatches "$output" "test-1*test-2"
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
echo "Test destroy"
[[ $(echo /var/lib/*containers/test-*) ]]
cleanup
output=$(extra-container list | grep ^test- || true)
testMatches "$output" ""
[[ ! $(echo /var/lib/*containers/test-*) ]]
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
echo "Test shell run"
read -d '' src <<EOF || true
{ config, pkgs, ... }:
{
containers.test-1 = {
$baseConfig;
};
}
EOF
output=$(extra-container shell -E "$src" --run c uname -a)
testMatches "$output" "*Linux test*"
# Container should be destroyed after running
[[ ! -e /var/lib/*containers/test-1 ]]
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
echo "Test manual build"
storePath=$(extra-container build <<EOF
{ config, pkgs, ... }:
{
containers.test-1 = {
$baseConfig;
};
containers.test-2 = {
$baseConfig;
};
}
EOF
)
testMatches "$storePath" "/nix/store/*"
output=$(extra-container create -s $storePath)
testMatches "$output" "*Starting*test-1*test-2*"
#―――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――――
echo "Test destroy from container definition"
extra-container destroy $storePath
[[ ! -e /var/lib/*containers/test-1 ]]
[[ ! -e /var/lib/*containers/test-2 ]]
# TODO: Add flake tests when flakes have stabilized