Skip to content
Permalink
Newer
Older
100644 435 lines (373 sloc) 17 KB
1
#!/bin/bash
2
Sep 28, 2018
3
#
4
# Contains common functions usable for midPoint system tests
5
#
6
7
# do not use from outside (ugly signature)
8
function generic_wait_for_log () {
Sep 28, 2018
9
CONTAINER_NAME=$1
10
MESSAGE="$2"
11
WAITING_FOR="$3"
12
FAILURE="$4"
13
ADDITIONAL_CONTAINER_NAME=$5
14
ATTEMPT=0
15
MAX_ATTEMPTS=20
16
DELAY=10
17
18
until [[ $ATTEMPT = $MAX_ATTEMPTS ]]; do
19
ATTEMPT=$((ATTEMPT+1))
20
echo "Waiting $DELAY seconds for $WAITING_FOR (attempt $ATTEMPT) ..."
21
sleep $DELAY
22
docker ps
23
( docker logs $CONTAINER_NAME 2>&1 | grep -F "$MESSAGE" ) && return 0
24
done
25
26
echo "$FAILURE" in $(( $MAX_ATTEMPTS * $DELAY )) seconds in $CONTAINER_NAME
27
echo "========== Container log =========="
28
docker logs $CONTAINER_NAME 2>&1
29
echo "========== End of the container log =========="
30
if [ -n "$ADDITIONAL_CONTAINER_NAME" ]; then
31
echo "========== Container log ($ADDITIONAL_CONTAINER_NAME) =========="
32
docker logs $ADDITIONAL_CONTAINER_NAME 2>&1
33
echo "========== End of the container log ($DATABASE_CONTAINER_NAME) =========="
34
fi
35
return 1
36
}
Sep 28, 2018
37
39
function wait_for_log_message () {
40
generic_wait_for_log $1 "$2" "log message" "log message has not appeared"
41
}
42
43
# Waits until midPoint starts
44
function wait_for_midpoint_start () {
45
generic_wait_for_log $1 "INFO (com.evolveum.midpoint.web.boot.MidPointSpringApplication): Started MidPointSpringApplication in" "midPoint to start" "midPoint did not start" $2
46
}
47
Oct 3, 2018
48
# Waits until Shibboleth IDP starts
49
function wait_for_shibboleth_idp_start_old () {
50
generic_wait_for_log $1 "INFO:oejs.Server:main: Started" "shibboleth idp to start" "shibboleth idp did not start" $2
53
# Waits until Shibboleth IDP starts
54
function wait_for_shibboleth_idp_start () {
55
generic_wait_for_log $1 "[main] INFO org.apache.catalina.startup.Catalina- Server startup in" "shibboleth idp to start" "shibboleth idp did not start" $2
56
}
57
Oct 3, 2018
58
# Waits until Grouper UI starts
59
function wait_for_grouper_ui_start () {
60
generic_wait_for_log $1 "INFO org.apache.catalina.startup.Catalina- Server startup in" "grouper ui to start" "grouper ui did not start" $2
61
}
62
Sep 28, 2018
63
# Checks the health of midPoint server
64
function check_health () {
65
echo Checking health...
66
(set -o pipefail ; curl -k -f https://localhost:8443/midpoint/actuator/health | tr -d '[:space:]' | grep -q "\"status\":\"UP\"")
67
status=$?
68
if [ $status -ne 0 ]; then
69
echo Error: $status
70
docker ps
71
return 1
72
else
73
echo OK
74
return 0
75
fi
76
}
77
78
# Checks the health of Shibboleth IDP server
79
function check_health_shibboleth_idp_old () {
80
echo Checking health of shibboleth idp...
81
status="$(curl -k --write-out %{http_code} --silent --output /dev/null https://localhost:4443/idp/)"
82
if [ $status -ne 200 ]; then
83
echo Error: Http code of response is $status
84
docker ps
85
return 1
86
else
87
echo OK
88
return 0
89
fi
90
}
91
92
function check_health_shibboleth_idp () {
93
echo Checking health of shibboleth idp...
94
status="$(curl -k --write-out %{http_code} --silent --output /dev/null https://localhost/idp/)"
95
if [ $status -ne 200 ]; then
96
echo Error: Http code of response is $status
97
docker ps
98
return 1
99
else
100
echo OK
101
return 0
102
fi
103
}
104
105
# Result is in OUTFILE
106
function get_object () {
107
local TYPE=$1
108
local OID=$2
109
OUTFILE=$(mktemp /tmp/get.XXXXXX)
110
echo out file is $OUTFILE
111
curl -k --user administrator:5ecr3t -H "Content-Type: application/xml" -X GET "https://localhost:8443/midpoint/ws/rest/$TYPE/$OID" >$OUTFILE || (rm $OUTFILE ; return 1)
112
return 0
113
}
114
Sep 28, 2018
115
# Retrieves XML object and checks if the name matches
116
# Object is deleted before return
Sep 28, 2018
117
function get_and_check_object () {
118
local TYPE=$1
119
local OID=$2
120
local NAME=$3
121
local TMPFILE=$(mktemp /tmp/get.XXXXXX)
Sep 28, 2018
122
echo tmp file is $TMPFILE
123
curl -k --user administrator:5ecr3t -H "Content-Type: application/xml" -X GET "https://localhost:8443/midpoint/ws/rest/$TYPE/$OID" >$TMPFILE || (rm $TMPFILE ; return 1)
124
if (grep -q "<name>$NAME</name>" <$TMPFILE); then
125
echo "Object $TYPE/$OID '$NAME' is OK"
126
rm $TMPFILE
127
return 0
128
else
129
echo "Object $TYPE/$OID '$NAME' was not found or not retrieved correctly:"
130
cat $TMPFILE
131
rm $TMPFILE
132
return 1
133
fi
134
}
135
136
# Adds object from a given file
137
function add_object () {
138
local TYPE=$1
139
local FILE=$2
140
TMPFILE=$(mktemp /tmp/addobject.XXXXXX)
Sep 28, 2018
141
echo "Adding to $TYPE from $FILE..."
143
curl -k -sD - --silent --write-out "%{http_code}" --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST "https://localhost:8443/midpoint/ws/rest/$TYPE" -d @$FILE >$TMPFILE
144
local HTTP_CODE=$(sed '$!d' $TMPFILE)
145
146
if [ "$HTTP_CODE" -eq 201 ] || [ "$HTTP_CODE" -eq 202 ]; then
148
OID=$(grep -oP "Location: \K.*" $TMPFILE | awk -F "$TYPE/" '{print $2}') || (echo "Couldn't extract oid from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
150
echo "OID of created object: $OID"
151
rm $TMPFILE
152
return 0
153
else
154
echo "Error code: $HTTP_CODE"
155
if [ "$HTTP_CODE" -ge 500 ]; then
156
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
157
else
158
local ERROR_MESSAGE=$(xmllint --xpath "/*/*[local-name()='error']/text()" $TMPFILE) || (echo "Couldn't extract error message from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
159
echo "Error message: $ERROR_MESSAGE"
161
rm $TMPFILE
162
return 1
163
fi
Sep 28, 2018
164
}
165
166
# parameter $2 (CONTAINER) is just for diagnostics: it is the container whose logs we want to dump on error (might be omitted)
167
function execute_bulk_action () {
168
local FILE=$1
169
local CONTAINER=$2
170
echo "Executing bulk action from $FILE..."
171
TMPFILE=$(mktemp /tmp/execbulkaction.XXXXXX)
173
(curl -k --silent --write-out "%{http_code}" --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST "https://localhost:8443/midpoint/ws/rest/rpc/executeScript" -d @$FILE >$TMPFILE) || (echo "Midpoint logs: " ; ([[ -n "$CONTAINER" ]] && docker logs $CONTAINER ) ; return 1)
174
local HTTP_CODE=$(sed '$!d' $TMPFILE)
175
sed -i '$ d' $TMPFILE
176
177
if [ "$HTTP_CODE" -eq 200 ]; then
178
179
local STATUS=$(xmllint --xpath "/*/*/*[local-name()='status']/text()" $TMPFILE) || (echo "Couldn't extract status from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
180
if [ $STATUS = "success" ]; then
181
local CONSOLE_OUTPUT=$(xmllint --xpath "/*/*/*[local-name()='consoleOutput']/text()" $TMPFILE) || (echo "Couldn't extract console output from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
182
echo "Console output: $CONSOLE_OUTPUT"
183
rm $TMPFILE
184
return 0
185
else
186
echo "Bulk action status is not OK: $STATUS"
187
local CONSOLE_OUTPUT=$(xmllint --xpath "/*/*/*[local-name()='consoleOutput']/text()" $TMPFILE) || (echo "Couldn't extract console output from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
188
echo "Console output: $CONSOLE_OUTPUT"
189
rm $TMPFILE
190
return 1
191
fi
192
193
else
194
echo "Error code: $HTTP_CODE"
195
if [ "$HTTP_CODE" -ge 500 ]; then
196
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
197
else
198
local ERROR_MESSAGE=$(xmllint --xpath "/*/*[local-name()='error']/text()" $TMPFILE) || (echo "Couldn't extract error message from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
199
echo "Error message: $ERROR_MESSAGE"
201
rm $TMPFILE
202
return 1
203
fi
204
}
205
206
function delete_object_by_name () {
207
local TYPE=$1
208
local NAME=$2
209
search_objects_by_name users $NAME
210
local OID=$(xmllint --xpath "/*/*[local-name()='object']/@oid" $SEARCH_RESULT_FILE | awk -F"\"" '{print $2}' ) || (echo "Couldn't extract oid from file:" ; cat $SEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE; return 1)
211
delete_object $TYPE $OID
212
}
213
214
function delete_object () {
215
local TYPE=$1
216
local OID=$2
217
echo "Deleting object with type $TYPE and oid $OID..."
218
TMPFILE=$(mktemp /tmp/delete.XXXXXX)
219
220
curl -k --silent --write-out "%{http_code}" --user administrator:5ecr3t -H "Content-Type: application/xml" -X DELETE "https://localhost:8443/midpoint/ws/rest/$TYPE/$OID" >$TMPFILE
221
local HTTP_CODE=$(sed '$!d' $TMPFILE)
222
sed -i '$ d' $TMPFILE
223
224
if [ "$HTTP_CODE" -eq 204 ]; then
225
echo "Object with type $TYPE and oid $OID was deleted"
226
rm $TMPFILE
227
return 0
228
else
229
echo "Error code: $HTTP_CODE"
230
if [ "$HTTP_CODE" -ge 500 ]; then
231
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
232
else
233
local ERROR_MESSAGE=$(xmllint --xpath "/*/*[local-name()='error']/text()" $TMPFILE) || (echo "Couldn't extract error message from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
234
echo "Error message: $ERROR_MESSAGE"
235
fi
236
rm $TMPFILE
237
return 1
238
fi
239
}
240
Sep 28, 2018
241
# Tries to find an object with a given name
242
# Results of the search are in the $SEARCH_RESULT_FILE
243
# TODO check if the result is valid (i.e. not an error) - return 1 if invalid, otherwise return 0 ("no objects" is considered OK here)
244
function search_objects_by_name () {
Oct 3, 2018
245
local TYPE=$1
246
local NAME="$2"
Sep 28, 2018
247
TMPFILE=$(mktemp /tmp/search.XXXXXX)
248
249
curl -k --write-out %{http_code} --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST "https://localhost:8443/midpoint/ws/rest/$TYPE/search" -d @- << EOF >$TMPFILE || (rm $TMPFILE ; return 1)
Sep 28, 2018
250
<q:query xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3">
251
<q:filter>
252
<q:equal>
253
<q:path>name</q:path>
254
<q:value>$NAME</q:value>
255
</q:equal>
256
</q:filter>
257
</q:query>
258
EOF
Oct 3, 2018
259
local HTTP_CODE=$(sed '$!d' <<<"$(cat $TMPFILE)")
260
sed -i '$ d' $TMPFILE
261
cat $TMPFILE
Oct 3, 2018
263
if [ "$HTTP_CODE" -eq 200 ]; then
264
SEARCH_RESULT_FILE=$TMPFILE
265
return 0
266
else
Oct 3, 2018
267
echo "Error code: $HTTP_CODE"
268
if [ "$HTTP_CODE" -ge 500 ]; then
269
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
270
else
271
local ERROR_MESSAGE=$(xmllint --xpath "/*/*[local-name()='error']/text()" $TMPFILE) || (echo "Couldn't extract error message from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
272
echo "Error message: $ERROR_MESSAGE"
273
fi
274
rm $SEARCH_RESULT_FILE
275
return 1
276
fi
Sep 28, 2018
277
}
278
279
# Searches for object with a given name and verifies it was found
280
function search_and_check_object () {
281
local TYPE=$1
282
local NAME="$2"
283
search_objects_by_name $TYPE "$NAME" || return 1
Sep 28, 2018
284
if (grep -q "<name>$NAME</name>" <$SEARCH_RESULT_FILE); then
285
echo "Object $TYPE/'$NAME' is OK"
286
rm $SEARCH_RESULT_FILE
287
return 0
288
else
289
echo "Object $TYPE/'$NAME' was not found or not retrieved correctly:"
290
cat $SEARCH_RESULT_FILE
291
rm $SEARCH_RESULT_FILE
292
return 1
293
fi
294
}
295
296
# Tests a resource
297
function test_resource () {
298
local OID=$1
299
local TMPFILE=$(mktemp /tmp/test.resource.XXXXXX)
300
301
curl -k --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST "https://localhost:8443/midpoint/ws/rest/resources/$OID/test" >$TMPFILE || (rm $TMPFILE ; return 1)
302
if [[ $(xmllint --xpath "/*/*[local-name()='status']/text()" $TMPFILE) == "success" ]]; then
303
echo "Resource $OID test succeeded"
304
rm $TMPFILE
305
return 0
306
else
307
echo "Resource $OID test failed"
308
cat $TMPFILE
309
rm $TMPFILE
310
return 1
311
fi
312
}
314
function assert_task_success () {
315
local OID=$1
316
get_object tasks $OID
317
TASK_STATUS=$(xmllint --xpath "/*/*[local-name()='resultStatus']/text()" $OUTFILE) || (echo "Couldn't extract task status from task $OID" ; cat $OUTFILE ; rm $OUTFILE ; return 1)
318
if [[ $TASK_STATUS = "success" ]]; then
319
echo "Task $OID status is OK"
320
rm $OUTFILE
321
return 0
322
else
323
echo "Task $OID status is not OK: $TASK_STATUS"
324
cat $OUTFILE
325
rm $OUTFILE
326
return 1
327
fi
328
}
329
330
function wait_for_task_completion () {
331
local OID=$1
332
local ATTEMPT=0
333
local MAX_ATTEMPTS=$2
334
local DELAY=$3
335
336
until [[ $ATTEMPT = $MAX_ATTEMPTS ]]; do
337
ATTEMPT=$((ATTEMPT+1))
338
echo "Waiting $DELAY seconds for task with oid $OID to finish (attempt $ATTEMPT) ..."
339
sleep $DELAY
340
get_object tasks $OID
341
TASK_EXECUTION_STATUS=$(xmllint --xpath "/*/*[local-name()='executionStatus']/text()" $OUTFILE) || (echo "Couldn't extract task status from task $OID" ; cat $OUTFILE ; rm $OUTFILE ; return 1)
342
if [[ $TASK_EXECUTION_STATUS = "suspended" ]] || [[ $TASK_EXECUTION_STATUS = "closed" ]]; then
343
echo "Task $OID is finished"
344
rm $OUTFILE
345
return 0
346
fi
347
done
348
rm $OUTFILE
349
echo Task with $OID did not finish in $(( $MAX_ATTEMPTS * $DELAY )) seconds
350
return 1
351
}
352
353
354
#search LDAP accout by uid
355
function search_ldap_object_by_filter () {
356
local BASE_CONTEXT_FOR_SEARCH=$1
357
local FILTER="$2"
358
local LDAP_CONTAINER=$3
359
TMPFILE=$(mktemp /tmp/ldapsearch.XXXXXX)
360
361
docker exec $LDAP_CONTAINER ldapsearch -h localhost -p 389 -D "cn=Directory Manager" -w password -b "$BASE_CONTEXT_FOR_SEARCH" "($FILTER)" >$TMPFILE || (echo "Couldn't search $FILTER:" ;rm $TMPFILE ; return 1)
362
LDAPSEARCH_RESULT_FILE=$TMPFILE
363
return 0
365
366
function check_ldap_account_by_user_name () {
Oct 3, 2018
367
local NAME="$1"
368
local LDAP_CONTAINER=$2
369
search_ldap_object_by_filter "ou=people,dc=internet2,dc=edu" "uid=$NAME" $LDAP_CONTAINER
370
search_objects_by_name users $NAME
372
local MP_FULL_NAME=$(xmllint --xpath "/*/*/*[local-name()='fullName']/text()" $SEARCH_RESULT_FILE) || (echo "Couldn't extract user fullName from file:" ; cat $SEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE ; rm $LDAPSEARCH_RESULT_FILE ; return 1)
373
local MP_GIVEN_NAME=$(xmllint --xpath "/*/*/*[local-name()='givenName']/text()" $SEARCH_RESULT_FILE) || (echo "Couldn't extract user givenName from file:" ; cat $SEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE ; rm $LDAPSEARCH_RESULT_FILE ; return 1)
374
local MP_FAMILY_NAME=$(xmllint --xpath "/*/*/*[local-name()='familyName']/text()" $SEARCH_RESULT_FILE) || (echo "Couldn't extract user familyName from file:" ; cat $SEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE ; rm $LDAPSEARCH_RESULT_FILE ; return 1)
375
376
local LDAP_CN=$(grep -oP "cn: \K.*" $LDAPSEARCH_RESULT_FILE) || (echo "Couldn't extract user cn from file:" ; cat $LDAPSEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE ; rm $LDAPSEARCH_RESULT_FILE ; return 1)
377
local LDAP_GIVEN_NAME=$(grep -oP "givenName: \K.*" $LDAPSEARCH_RESULT_FILE) || (echo "Couldn't extract user givenName from file:" ; cat $LDAPSEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE ; rm $LDAPSEARCH_RESULT_FILE ; return 1)
378
local LDAP_SN=$(grep -oP "sn: \K.*" $LDAPSEARCH_RESULT_FILE) || (echo "Couldn't extract user sn from file:" ; cat $LDAPSEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE ; rm $LDAPSEARCH_RESULT_FILE ; return 1)
379
380
rm $SEARCH_RESULT_FILE
381
rm $LDAPSEARCH_RESULT_FILE
382
383
if [[ $MP_FULL_NAME = $LDAP_CN ]] && [[ $MP_GIVEN_NAME = $LDAP_GIVEN_NAME ]] && [[ $MP_FAMILY_NAME = $LDAP_SN ]]; then
384
return 0
385
fi
387
echo "User in Midpoint and LDAP Account with uid $NAME are not same"
388
return 1
389
}
390
391
function check_ldap_courses_by_name () {
392
local NAME="$1"
393
local LDAP_CONTAINER=$2
394
search_objects_by_name orgs $NAME
395
396
local MP_ORG_IDENTIFIER=$(xmllint --xpath "/*/*/*[local-name()='identifier']/text()" $SEARCH_RESULT_FILE) || (echo "Couldn't extract user identifier from file:" ; cat $SEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE ; return 1)
397
398
search_ldap_object_by_filter "ou=courses,ou=groups,dc=internet2,dc=edu" "cn=$MP_ORG_IDENTIFIER" $LDAP_CONTAINER
399
400
local LDAP_CN=$(grep -oP "cn: \K.*" $LDAPSEARCH_RESULT_FILE) || (echo "Couldn't extract user cn from file:" ; cat $LDAPSEARCH_RESULT_FILE ; rm $SEARCH_RESULT_FILE ; rm $LDAPSEARCH_RESULT_FILE ; return 1)
401
402
rm $SEARCH_RESULT_FILE
403
rm $LDAPSEARCH_RESULT_FILE
404
405
if [[ $MP_ORG_IDENTIFIER = $LDAP_CN ]]; then
406
return 0
407
fi
408
409
echo "Orgs $NAME in Midpoint and LDAP Group(Course) with cn $MP_ORG_IDENTIFIER are not same"
410
return 1
411
}
412
413
414
function check_of_ldap_membership () {
Oct 3, 2018
415
local NAME_OF_USER="$1"
416
local BASE_CONTEXT_FOR_GROUP="$2"
417
local NAME_OF_GROUP="$3"
418
local LDAP_CONTAINER=$4
419
search_ldap_object_by_filter "ou=people,dc=internet2,dc=edu" "uid=$NAME_OF_USER" $LDAP_CONTAINER
420
421
local LDAP_ACCOUNT_DN=$(grep -oP "dn: \K.*" $LDAPSEARCH_RESULT_FILE) || (echo "Couldn't extract user dn from file:" ; cat $LDAPSEARCH_RESULT_FILE ; rm $LDAPSEARCH_RESULT_FILE ; return 1)
423
search_ldap_object_by_filter "$BASE_CONTEXT_FOR_GROUP" "cn=$NAME_OF_GROUP" $LDAP_CONTAINER
424
425
local LDAP_MEMBERS_DNS=$(grep -oP "uniqueMember: \K.*" $LDAPSEARCH_RESULT_FILE) || (echo "Couldn't extract user uniqueMember from file:" ; cat $LDAPSEARCH_RESULT_FILE ; rm $LDAPSEARCH_RESULT_FILE ; return 1)
426
427
rm $LDAPSEARCH_RESULT_FILE
428
429
if [[ $LDAP_MEMBERS_DNS =~ $LDAP_ACCOUNT_DN ]]; then
430
return 0
431
fi
432
433
echo "LDAP Account with uid $NAME_OF_USER is not member of LDAP Group $NAME_OF_GROUP in base context $BASE_CONTEXT_FOR_GROUP"
434
return 1
435
}
You can’t perform that action at this time.