-
Notifications
You must be signed in to change notification settings - Fork 0
/
integration-test.sh
executable file
·295 lines (248 loc) · 13.7 KB
/
integration-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
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
283
284
285
286
287
288
289
290
291
292
293
294
295
#!/bin/bash
set -eEuo pipefail
trap '__error_handing__ $? $LINENO' ERR
# This file is responsible to test the k8s-ces-control feature in integration with the whole cluster.
# To run this script a local cluster is needed.
# It is also required to install the following dogus: ldap, postfix
KUBECTL_BIN_PATH="${KUBECTL_BIN:-kubectl}"
GRPCURL_BIN_PATH="${GRPCURL_BIN:-grpcurl}"
GRPCURL_BIN_PATH_WITH_AUTH=
JQ_BIN_PATH="${JQ_BIN:-jq}"
PORT_FORWARD_PID=
function __error_handling__() {
killPortForward
local last_status_code=$1;
local error_line_number=$2;
echo 1>&2 "Error - exited with status $last_status_code at line $error_line_number";
perl -slne 'if($.+5 >= $ln && $.-4 <= $ln){ $_="$. $_"; s/$ln/">" x length($ln)/eg; s/^\D+.*?$/\e[1;31m$&\e[0m/g; print}' -- -ln=$error_line_number $0
}
startPortForward() {
echo "Starting Port-Forward on ${PORT_FORWARD_PID}..."
GRPCURL_PORT="$(python3 -c 'import socket; s=socket.socket(); s.bind(("", 0)); print(s.getsockname()[1]); s.close()')"
"${KUBECTL_BIN_PATH}" port-forward service/k8s-ces-control "${GRPCURL_PORT}":50051 >/dev/null 2>&1 &
PORT_FORWARD_PID=$!
sleep 2s
echo "Started Port-Forward on ${GRPCURL_PORT}"
}
killPortForward() {
echo "Stopping Port-Forward..."
kill -kill "${PORT_FORWARD_PID}" || true
}
INTEGRATION_TEST_RESULT_FOLDER=target/bash-integration-test
INTEGRATION_TEST_RESULT_FILE="${INTEGRATION_TEST_RESULT_FOLDER}"/results.xml
# Creates the xml test file containing the results for the tests.
function createIntegrationTestFile() {
rm -rf "${INTEGRATION_TEST_RESULT_FOLDER}"
mkdir -p "${INTEGRATION_TEST_RESULT_FOLDER}"
touch "${INTEGRATION_TEST_RESULT_FILE}"
cat <<EOT >"${INTEGRATION_TEST_RESULT_FILE}"
<?xml version="1.0" encoding="UTF-8"?>
<testsuite name="github.com/cloudogu/k8s-ces-control/integration">
EOT
}
# Adds a successful test case to the result xml.
#
# Parameters:
# 1 - Name of the test case.
# 2 - Message of the test case.
function addSuccessTestCase() {
local name="$1"
local message="$2"
cat <<EOT >>"${INTEGRATION_TEST_RESULT_FILE}"
<testcase name="${name}">
<system-out>${message}</system-out>
</testcase>
EOT
}
# Adds a failing test case to the result xml.
#
# Parameters:
# 1 - Name of the test case.
# 2 - Message of the test case.
function addFailingTestCase() {
local name="$1"
local message="$2"
cat <<EOT >>"${INTEGRATION_TEST_RESULT_FILE}"
<testcase name="${name}">
<failure message="bash integration test failed">
${message}
</failure>
</testcase>
EOT
}
# Finishes the xml syntax for the xml test file.
function finishIntegrationTestFile() {
cat <<EOT >>"${INTEGRATION_TEST_RESULT_FILE}"
</testsuite>
EOT
}
runTests() {
echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
echo "Test-Suite: Dogu-Administration: Test GetAllDogus, Start, Stop, Restart"
testDoguAdministration_GetAllDogus
testDoguAdministration_StartStopDogus
testDoguAdministration_RestartDogus
echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
echo "Test-Suite: Dogu-Health: Test GetAll, GetByNames, GetByName"
testDoguHealth_GetAll
testDoguHealth_GetByNames
testDoguHealth_GetByName
echo "- - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - - -"
}
testDoguAdministration_GetAllDogus() {
local doguListJson
doguListJson=$(${GRPCURL_BIN_PATH} -plaintext localhost:"${GRPCURL_PORT}" doguAdministration.DoguAdministration.GetDoguList)
local getDoguList
getDoguList=$(echo ${doguListJson} | ${JQ_BIN_PATH} '.dogus | map(select(.name)) | .[].name')
if [[ "${getDoguList}" == *"\"ldap\""* ]]; then
echo "Test: Check if Ldap returned: Success!"
addSuccessTestCase "Dogu-Administration-GetAll-Ldap" "List of returned Dogus contained the 'ldap' dogu."
else
echo "Test: Check if Ldap returned: Failed!"
addFailingTestCase "Dogu-Administration-GetAll-Ldap" "Expected to get Dogu 'ldap' but got only:
${getDoguList}"
fi
if [[ "${getDoguList}" == *"\"postfix\""* ]]; then
echo "Test: Check if Postfix returned: Success!"
addSuccessTestCase "Dogu-Administration-GetAll-Postfix" "List of returned Dogus contained the 'postfix' dogu."
else
echo "Test: Check if Postfix returned: Failed!"
addFailingTestCase "Dogu-Administration-GetAll-Postfix" "Expected to get Dogu 'postfix' but got only:
${getDoguList}"
fi
}
testDoguAdministration_StartStopDogus() {
${GRPCURL_BIN_PATH} -plaintext -d '{"doguName": "postfix"}' localhost:"${GRPCURL_PORT}" doguAdministration.DoguAdministration.StopDogu >/dev/null 2>&1
# Wait for dogu to be terminated
sleep 5s
local replicas=""
replicas="$(${KUBECTL_BIN_PATH} get deployment/postfix -o json | ${JQ_BIN_PATH} '.spec.replicas')"
if [[ "${replicas}" == 0 ]]; then
echo "Test: Postfix stopped? Success!"
addSuccessTestCase "Dogu-Administration-StopDogu-Postfix" "k8s-ces-control successfully stopped the Postfix dogu."
else
echo "Test: Postfix stopped? Failed!"
addFailingTestCase "Dogu-Administration-StopDogu-Postfix" "Expected the replicas of postfix to be 0 but got: ${replicas}"
fi
${GRPCURL_BIN_PATH} -plaintext -d '{"doguName": "postfix"}' localhost:"${GRPCURL_PORT}" doguAdministration.DoguAdministration.StartDogu >/dev/null 2>&1
# Wait for dogu to be started
sleep 5s
local replicas=""
replicas="$(${KUBECTL_BIN_PATH} get deployment/postfix -o json | ${JQ_BIN_PATH} '.spec.replicas')"
if [[ "${replicas}" == 1 ]]; then
echo "Test: Postfix started? Success!"
addSuccessTestCase "Dogu-Administration-StartDogu-Postfix" "k8s-ces-control successfully started the Postfix dogu."
else
echo "Test: Postfix started? Failed!"
addFailingTestCase "Dogu-Administration-StartDogu-Postfix" "Expected the replicas of postfix to be 1 but got: ${replicas}"
fi
}
testDoguAdministration_RestartDogus() {
local postfixBeforePodName=""
postfixBeforePodName="$(${KUBECTL_BIN_PATH} get pods -o name | grep postfix)"
${GRPCURL_BIN_PATH} -plaintext -d '{"doguName": "postfix"}' localhost:"${GRPCURL_PORT}" doguAdministration.DoguAdministration.RestartDogu >/dev/null 2>&1
# Wait for dogu to be restarted
sleep 5s
local postfixAfterPodName=""
postfixAfterPodName="$(${KUBECTL_BIN_PATH} get pods -o name | grep postfix)"
if [[ "${postfixAfterPodName}" != "${postfixBeforePodName}" ]]; then
echo "Test: Postfix restarted? Success!"
addSuccessTestCase "Dogu-Administration-RestartDogu-Postfix" "k8s-ces-control successfully restarted Postfix."
else
echo "Test: Postfix restarted? Failed!"
addFailingTestCase "Dogu-Administration-RestartDogu-Postfix" "Expected that k8s-ces-control restarted pod, but pod name did not change after restart request. However, it should do so as the restart consist of killing the old pod.
Name before killing the pod: ${postfixBeforePodName}
Name after killing the pod: ${postfixAfterPodName}"
fi
}
testDoguHealth_GetAll() {
local allDogusHealthJson
allDogusHealthJson=$(${GRPCURL_BIN_PATH} -plaintext localhost:"${GRPCURL_PORT}" health.DoguHealth.GetAll)
if [[ $(echo ${allDogusHealthJson} | ${JQ_BIN_PATH} -r '.results.ldap.fullName') == 'ldap' && $(echo ${allDogusHealthJson} | ${JQ_BIN_PATH} -r '.results.ldap.healthy') == 'true' ]]; then
echo "Test: [Dogu-Health-GetAll] Check if Ldap is healthy: Success!"
addSuccessTestCase "Dogu-Health-GetAll-Ldap" "List of returned Dogus contained a healthy 'ldap' dogu."
else
echo "Test: [Dogu-Health-GetAll] Check if Ldap is healthy: Failed!"
addFailingTestCase "Dogu-Health-GetAll-Ldap" "Expected to get Dogu 'ldap' is healthy but got only: ${allDogusHealthJson}"
fi
if [[ $(echo ${allDogusHealthJson} | ${JQ_BIN_PATH} -r '.results."nginx-static".fullName') == 'nginx-static' && $(echo ${allDogusHealthJson} | ${JQ_BIN_PATH} -r '.results."nginx-static".healthy') == 'true' ]]; then
echo "Test: [Dogu-Health-GetAll] Check if NginxStatic is healthy: Success!"
addSuccessTestCase "Dogu-Health-GetAll-NginxStatic" "List of returned Dogus contained a healthy 'nginx-static' dogu."
else
echo "Test: [Dogu-Health-GetAll] Check if NginxStatic is healthy: Failed!"
addFailingTestCase "Dogu-Health-GetAll-NginxStatic" "Expected to get Dogu 'nginx-static' is healthy but got only: ${allDogusHealthJson}"
fi
if [[ $(echo ${allDogusHealthJson} | ${JQ_BIN_PATH} -r '.results."nginx-ingress".fullName') == 'nginx-ingress' && $(echo ${allDogusHealthJson} | ${JQ_BIN_PATH} -r '.results."nginx-ingress".healthy') == 'true' ]]; then
echo "Test: [Dogu-Health-GetAll] Check if NginxIngress is healthy: Success!"
addSuccessTestCase "Dogu-Health-GetAll-NginxIngress" "List of returned Dogus contained a healthy 'nginx-ingress' dogu."
else
echo "Test: [Dogu-Health-GetAll] Check if NginxIngress is healthy: Failed!"
addFailingTestCase "Dogu-Health-GetAll-NginxIngress" "Expected to get Dogu 'nginx-ingress' is healthy but got only: ${allDogusHealthJson}"
fi
}
testDoguHealth_GetByNames() {
local dogusHealthJson
dogusHealthJson=$(${GRPCURL_BIN_PATH} -plaintext -d '{"dogus": ["nginx-static", "ldap"]}' localhost:"${GRPCURL_PORT}" health.DoguHealth.GetByNames)
if [[ $(echo ${dogusHealthJson} | ${JQ_BIN_PATH} -r '.results.ldap.fullName') == 'ldap' && $(echo ${dogusHealthJson} | ${JQ_BIN_PATH} -r '.results.ldap.healthy') == 'true' ]]; then
echo "Test: [Dogu-Health-GetByNames] Check if Ldap is healthy: Success!"
addSuccessTestCase "Dogu-Health-GetByNames-Ldap" "List of returned Dogus contained a healthy 'ldap' dogu."
else
echo "Test: [Dogu-Health-GetByNames] Check if Ldap is healthy: Failed!"
addFailingTestCase "Dogu-Health-GetByNames-Ldap" "Expected to get Dogu 'ldap' is healthy but got only: ${dogusHealthJson}"
fi
if [[ $(echo ${dogusHealthJson} | ${JQ_BIN_PATH} -r '.results."nginx-static".fullName') == 'nginx-static' && $(echo ${dogusHealthJson} | ${JQ_BIN_PATH} -r '.results."nginx-static".healthy') == 'true' ]]; then
echo "Test: [Dogu-Health-GetByNames] Check if NginxStatic is healthy: Success!"
addSuccessTestCase "Dogu-Health-GetByNames-NginxStatic" "List of returned Dogus contained a healthy 'nginx-static' dogu."
else
echo "Test: [Dogu-Health-GetByNames] Check if NginxStatic is healthy: Failed!"
addFailingTestCase "Dogu-Health-GetByNames-NginxStatic" "Expected to get Dogu 'nginx-static' is healthy but got only: ${dogusHealthJson}"
fi
if [[ $(echo ${dogusHealthJson} | ${JQ_BIN_PATH} -r '.results | length') == '2' ]]; then
echo "Test: [Dogu-Health-GetByNames] Check result-length: Success!"
addSuccessTestCase "Dogu-Health-GetByNames-ResultLength" "List of returned Dogus has 2 items."
else
echo "Test: [Dogu-Health-GetByNames] Check result-length: Failed!"
addFailingTestCase "Dogu-Health-GetByNames-ResultLength" "Expected to get 2 items in result but got only: ${dogusHealthJson}"
fi
}
testDoguHealth_GetByName() {
local doguHealthJson
doguHealthJson=$(${GRPCURL_BIN_PATH} -plaintext -d '{"dogu_name": "nginx-static"}' localhost:"${GRPCURL_PORT}" health.DoguHealth.GetByName)
if [[ $(echo ${doguHealthJson} | ${JQ_BIN_PATH} -r '.fullName') == 'nginx-static' && $(echo ${doguHealthJson} | ${JQ_BIN_PATH} -r '.healthy') == 'true' ]]; then
echo "Test: [Dogu-Health-GetByName] Check if NginxStatic is healthy: Success!"
addSuccessTestCase "Dogu-Health-GetByName-NginxStatic" "List of returned Dogus contained a healthy 'nginx-static' dogu."
else
echo "Test: [Dogu-Health-GetByName] Check if NginxStatic is healthy: Failed!"
addFailingTestCase "Dogu-Health-GetByName-NginxStatic" "Expected to get Dogu 'nginx-static' is healthy but got only: ${doguHealthJson}"
fi
${GRPCURL_BIN_PATH} -plaintext -d '{"doguName": "nginx-static"}' localhost:"${GRPCURL_PORT}" doguAdministration.DoguAdministration.StopDogu >/dev/null 2>&1
# Wait for dogu to be terminated
sleep 5s
doguHealthJson=$(${GRPCURL_BIN_PATH} -plaintext -d '{"dogu_name": "nginx-static"}' localhost:"${GRPCURL_PORT}" health.DoguHealth.GetByName)
if [[ $(echo ${doguHealthJson} | ${JQ_BIN_PATH} -r '.fullName') == 'nginx-static' && $(echo ${doguHealthJson} | ${JQ_BIN_PATH} -r 'has("healthy")') == 'false' ]]; then
echo "Test: [Dogu-Health-GetByName] Check if NginxStatic is not healthy: Success!"
addSuccessTestCase "Dogu-Health-GetByName-NginxStatic" "List of returned Dogus contained a not-healthy 'nginx-static' dogu."
else
echo "Test: [Dogu-Health-GetByName] Check if NginxStatic is not healthy: Failed!"
addFailingTestCase "Dogu-Health-GetByName-NginxStatic" "Expected to get Dogu 'nginx-static' is not-healthy but got only: ${doguHealthJson}"
fi
${GRPCURL_BIN_PATH} -plaintext -d '{"doguName": "nginx-static"}' localhost:"${GRPCURL_PORT}" doguAdministration.DoguAdministration.StartDogu >/dev/null 2>&1
# Wait for dogu to be started
sleep 20s
doguHealthJson=$(${GRPCURL_BIN_PATH} -plaintext -d '{"dogu_name": "nginx-static"}' localhost:"${GRPCURL_PORT}" health.DoguHealth.GetByName)
if [[ $(echo ${doguHealthJson} | ${JQ_BIN_PATH} -r '.fullName') == 'nginx-static' && $(echo ${doguHealthJson} | ${JQ_BIN_PATH} -r '.healthy') == 'true' ]]; then
echo "Test: [Dogu-Health-GetByName] Check if NginxStatic is healthy: Success!"
addSuccessTestCase "Dogu-Health-GetByName-NginxStatic" "List of returned Dogus contained a healthy 'nginx-static' dogu."
else
echo "Test: [Dogu-Health-GetByName] Check if NginxStatic is healthy: Failed!"
addFailingTestCase "Dogu-Health-GetByName-NginxStatic" "Expected to get Dogu 'nginx-static' is healthy but got only: ${doguHealthJson}"
fi
}
echo "Using KUBECTL=${KUBECTL_BIN_PATH}"
echo "Using GRPCURL=${GRPCURL_BIN_PATH}"
echo "Using JQ=${JQ_BIN_PATH}"
createIntegrationTestFile
startPortForward
runTests
killPortForward
finishIntegrationTestFile