Skip to content
Permalink
Newer
Older
100644 341 lines (290 sloc) 12.7 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 "$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
48
# Waits until Shibboleth IDP starts ... TODO refactor using generic waiting function
49
function wait_for_shibboleth_idp_start () {
50
generic_wait_for_log $1 "INFO:oejs.Server:main: Started" "shibboleth idp to start" "shibboleth idp did not start" $2
Sep 28, 2018
53
# Checks the health of midPoint server
54
function check_health () {
55
echo Checking health...
56
(set -o pipefail ; curl -k -f https://localhost:8443/midpoint/actuator/health | tr -d '[:space:]' | grep -q "\"status\":\"UP\"")
57
status=$?
58
if [ $status -ne 0 ]; then
59
echo Error: $status
60
docker ps
61
return 1
62
else
63
echo OK
64
return 0
65
fi
66
}
67
68
# Checks the health of Shibboleth IDP server
69
function check_health_shibboleth_idp () {
70
echo Checking health of shibboleth idp...
71
status="$(curl -k --write-out %{http_code} --silent --output /dev/null https://localhost:4443/idp/)"
72
if [ $status -ne 200 ]; then
73
echo Error: Http code of response is $status
74
docker ps
75
return 1
76
else
77
echo OK
78
return 0
79
fi
80
}
81
82
83
function get_object () {
84
local TYPE=$1
85
local OID=$2
86
TMPFILE=$(mktemp /tmp/get.XXXXXX)
87
echo tmp file is $TMPFILE
88
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)
89
return 0
90
}
91
Sep 28, 2018
92
# Retrieves XML object and checks if the name matches
93
function get_and_check_object () {
94
TYPE=$1
95
OID=$2
96
NAME=$3
97
TMPFILE=$(mktemp /tmp/get.XXXXXX)
98
echo tmp file is $TMPFILE
99
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)
100
if (grep -q "<name>$NAME</name>" <$TMPFILE); then
101
echo "Object $TYPE/$OID '$NAME' is OK"
102
rm $TMPFILE
103
return 0
104
else
105
echo "Object $TYPE/$OID '$NAME' was not found or not retrieved correctly:"
106
cat $TMPFILE
107
rm $TMPFILE
108
return 1
109
fi
110
}
111
112
# Adds object from a given file
113
# TODO Returns the OID in OID variable
114
# it can be found in the following HTTP reader returned: Location: "https://localhost:8443/midpoint/ws/rest/users/85e62669-d36b-41ce-b4f1-1ffdd9f66262"
115
function add_object () {
116
local TYPE=$1
117
local FILE=$2
Sep 28, 2018
118
echo "Adding to $TYPE from $FILE..."
120
local response=$(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)
121
local http_code=$(sed '$!d' <<<"$response")
123
if [ "$http_code" -eq 201 ] || [ "$http_code" -eq 202 ]; then
Oct 1, 2018
124
125
# get the real Location
126
local location=$(grep -oP "Location: \K.*" <<<"$response")
127
OID=$(sed 's/.*\///' <<<"$location")
129
echo "Oid created object: $OID"
130
return 0
131
else
132
echo "Error code: $http_code"
133
if [ "$http_code" -eq 500 ]; then
134
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
135
else
136
local error_message=$(grep 'message' <<<"$response" | head -1 | awk -F">" '{print $2}' | awk -F"<" '{print $1}')
137
echo "Error message: $error_message"
138
fi
139
return 1
140
fi
Sep 28, 2018
141
}
142
143
function execute_bulk_action () {
144
local FILE=$1
145
echo "Executing bulk action from $FILE..."
146
TMPFILE=$(mktemp /tmp/execbulkaction.XXXXXX)
147
148
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
149
local HTTP_CODE=$(sed '$!d' $TMPFILE)
150
sed -i '$ d' $TMPFILE
151
152
if [ "$HTTP_CODE" -eq 200 ]; then
153
154
local STATUS=$(xmllint --xpath "/*/*/*[local-name()='status']/text()" $TMPFILE) || (echo "Couldn't extract status from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
155
if [ $STATUS = "success" ]; then
156
rm $TMPFILE
157
return 0
158
else
159
echo "Bulk action status is not OK: $STATUS"
160
local CONSOLE_OUTPUT=$(xmllint --xpath "/*/*/*[local-name()='consoleOutput']/text()" $TMPFILE) || (echo "Couldn't extract console output from file:" ; cat $TMPFILE ; rm $TMPFILE; return 1)
161
echo "Console output: $CONSOLE_OUTPUT"
162
rm $TMPFILE
163
return 1
164
fi
165
166
else
167
echo "Error code: $HTTP_CODE"
168
if [ "$http_code" -eq 500 ]; then
169
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
170
fi
171
return 1
172
fi
173
}
174
175
Sep 28, 2018
176
# Tries to find an object with a given name
177
# Results of the search are in the $SEARCH_RESULT_FILE
178
# 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)
179
function search_objects_by_name () {
180
TYPE=$1
181
NAME="$2"
Sep 28, 2018
182
TMPFILE=$(mktemp /tmp/search.XXXXXX)
183
184
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
185
<q:query xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3">
186
<q:filter>
187
<q:equal>
188
<q:path>name</q:path>
189
<q:value>$NAME</q:value>
190
</q:equal>
191
</q:filter>
192
</q:query>
193
EOF
194
SEARCH_RESULT_FILE=$TMPFILE
195
196
http_code=$(sed '$!d' <<<"$(cat $SEARCH_RESULT_FILE)")
197
198
sed -i '$ d' $SEARCH_RESULT_FILE
199
cat $SEARCH_RESULT_FILE
200
if [ "$http_code" -eq 200 ]; then
201
return 0
202
else
203
rm $SEARCH_RESULT_FILE
204
return 1
205
fi
Sep 28, 2018
206
}
207
208
# Searches for object with a given name and verifies it was found
209
function search_and_check_object () {
210
local TYPE=$1
211
local NAME="$2"
212
search_objects_by_name $TYPE "$NAME" || return 1
Sep 28, 2018
213
if (grep -q "<name>$NAME</name>" <$SEARCH_RESULT_FILE); then
214
echo "Object $TYPE/'$NAME' is OK"
215
rm $SEARCH_RESULT_FILE
216
return 0
217
else
218
echo "Object $TYPE/'$NAME' was not found or not retrieved correctly:"
219
cat $SEARCH_RESULT_FILE
220
rm $SEARCH_RESULT_FILE
221
return 1
222
fi
223
}
224
225
# Tests a resource
226
function test_resource () {
227
local OID=$1
228
local TMPFILE=$(mktemp /tmp/test.resource.XXXXXX)
229
230
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)
231
if [[ $(xmllint --xpath "/*/*[local-name()='status']/text()" $TMPFILE) == "success" ]]; then
232
echo "Resource $OID test succeeded"
233
rm $TMPFILE
234
return 0
235
else
236
echo "Resource $OID test failed"
237
cat $TMPFILE
238
rm $TMPFILE
239
return 1
240
fi
241
}
243
function assert_task_success () {
244
local OID=$1
245
get_object tasks $OID
246
TASK_STATUS=$(xmllint --xpath "/*/*[local-name()='resultStatus']/text()" $TMPFILE) || (echo "Couldn't extract task status from task $OID" ; cat $TMPFILE ; rm $TMPFILE ; return 1)
247
if [[ $TASK_STATUS = "success" ]]; then
248
echo "Task $OID status is OK"
249
rm $TMPFILE
250
return 0
251
else
252
echo "Task $OID status is not OK: $TASK_STATUS"
253
cat $TMPFILE
254
rm $TMPFILE
255
return 1
256
fi
257
}
258
259
function wait_for_task_completion () {
260
local OID=$1
261
local ATTEMPT=0
262
local MAX_ATTEMPTS=$2
263
local DELAY=$3
264
265
until [[ $ATTEMPT = $MAX_ATTEMPTS ]]; do
266
ATTEMPT=$((ATTEMPT+1))
267
echo "Waiting $DELAY seconds for task with oid $OID to finish (attempt $ATTEMPT) ..."
268
sleep $DELAY
269
get_object tasks $OID
270
TASK_EXECUTION_STATUS=$(xmllint --xpath "/*/*[local-name()='executionStatus']/text()" $TMPFILE) || (echo "Couldn't extract task status from task $OID" ; cat $TMPFILE ; rm $TMPFILE ; return 1)
271
if [[ $TASK_EXECUTION_STATUS = "suspended" ]] || [[ $TASK_EXECUTION_STATUS = "closed" ]]; then
272
echo "Task $OID is finished"
273
rm $TMPFILE
274
return 0
275
fi
276
done
277
rm $TMPFILE
278
echo Task with $OID did not finish in $(( $MAX_ATTEMPTS * $DELAY )) seconds
279
return 1
280
}
281
282
283
#search LDAP accout by uid
284
function search_ldap_object_by_filter () {
285
local BASE_CONTEXT_FOR_SEARCH=$1
286
local FILTER="$2"
287
local LDAP_CONTAINER=$3
288
TMPFILE=$(mktemp /tmp/ldapsearch.XXXXXX)
289
290
docker exec $LDAP_CONTAINER ldapsearch -h localhost -p 389 -D "cn=Directory Manager" -w password -b "$BASE_CONTEXT_FOR_SEARCH" "($FILTER)" >$TMPFILE || (rm $TMPFILE ; return 1)
291
LDAPSEARCH_RESULT_FILE=$TMPFILE
292
return 0
294
295
function check_ldap_account_by_user_name () {
296
local NAME=$1
297
local LDAP_CONTAINER=$2
298
search_ldap_object_by_filter "ou=people,dc=internet2,dc=edu" "uid=$NAME" $LDAP_CONTAINER
299
search_objects_by_name users $NAME
300
301
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)
302
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)
303
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)
304
305
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)
306
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)
307
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)
308
309
rm $SEARCH_RESULT_FILE
310
rm $LDAPSEARCH_RESULT_FILE
311
312
if [[ $MP_FULL_NAME = $LDAP_CN ]] && [[ $MP_GIVEN_NAME = $LDAP_GIVEN_NAME ]] && [[ $MP_FAMILY_NAME = $LDAP_SN ]]; then
313
return 0
314
fi
315
316
echo "User in Midpoint and LDAP Account with uid $NAME are not same"
317
return 1
318
}
319
320
function check_of_ldap_membership () {
321
local NAME_OF_USER=$1
322
local NAME_OF_GROUP=$2
323
local LDAP_CONTAINER=$3
324
search_ldap_object_by_filter "ou=people,dc=internet2,dc=edu" "uid=$NAME_OF_USER" $LDAP_CONTAINER
325
326
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)
327
328
search_ldap_object_by_filter "ou=groups,dc=internet2,dc=edu" "cn=$NAME_OF_GROUP" $LDAP_CONTAINER
329
330
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)
331
332
rm $LDAPSEARCH_RESULT_FILE
333
334
if [[ $LDAP_MEMBERS_DNS =~ $LDAP_ACCOUNT_DN ]]; then
335
return 0
336
fi
337
338
echo "LDAP Account with uid $NAME_OF_USER is not member of LDAP Group $NAME_OF_GROUP"
339
return 1
340
}
341
You can’t perform that action at this time.