Skip to content
Permalink
Newer
Older
100644 249 lines (220 sloc) 8.04 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
CONTAINER_NAME=$1
51
ATTEMPT=0
52
MAX_ATTEMPTS=20
53
DELAY=10
54
55
until [[ $ATTEMPT = $MAX_ATTEMPTS ]]; do
56
ATTEMPT=$((ATTEMPT+1))
57
echo "Waiting $DELAY seconds for Shibboleth IDP to start (attempt $ATTEMPT) ..."
58
sleep $DELAY
59
docker ps
60
( docker logs $CONTAINER_NAME 2>&1 | grep "INFO:oejs.Server:main: Started" ) && return 0
61
done
62
63
echo Shibboleth IDP did not start in $(( $MAX_ATTEMPTS * $DELAY )) seconds in $CONTAINER_NAME
64
echo "========== Container log =========="
65
docker logs $CONTAINER_NAME 2>&1
66
echo "========== End of the container log =========="
67
return 1
68
}
69
Sep 28, 2018
70
# Checks the health of midPoint server
71
function check_health () {
72
echo Checking health...
73
(set -o pipefail ; curl -k -f https://localhost:8443/midpoint/actuator/health | tr -d '[:space:]' | grep -q "\"status\":\"UP\"")
74
status=$?
75
if [ $status -ne 0 ]; then
76
echo Error: $status
77
docker ps
78
return 1
79
else
80
echo OK
81
return 0
82
fi
83
}
84
85
# Checks the health of Shibboleth IDP server
86
function check_health_shibboleth_idp () {
87
echo Checking health of shibboleth idp...
88
status="$(curl -k --write-out %{http_code} --silent --output /dev/null https://localhost:4443/idp/)"
89
if [ $status -ne 200 ]; then
90
echo Error: Http code of response is $status
91
docker ps
92
return 1
93
else
94
echo OK
95
return 0
96
fi
97
}
98
99
100
function get_object () {
101
local TYPE=$1
102
local OID=$2
103
TMPFILE=$(mktemp /tmp/get.XXXXXX)
104
echo tmp file is $TMPFILE
105
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)
106
return 0
107
}
108
Sep 28, 2018
109
# Retrieves XML object and checks if the name matches
110
function get_and_check_object () {
111
TYPE=$1
112
OID=$2
113
NAME=$3
114
TMPFILE=$(mktemp /tmp/get.XXXXXX)
115
echo tmp file is $TMPFILE
116
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)
117
if (grep -q "<name>$NAME</name>" <$TMPFILE); then
118
echo "Object $TYPE/$OID '$NAME' is OK"
119
rm $TMPFILE
120
return 0
121
else
122
echo "Object $TYPE/$OID '$NAME' was not found or not retrieved correctly:"
123
cat $TMPFILE
124
rm $TMPFILE
125
return 1
126
fi
127
}
128
129
# Adds object from a given file
130
# TODO Returns the OID in OID variable
131
# it can be found in the following HTTP reader returned: Location: "https://localhost:8443/midpoint/ws/rest/users/85e62669-d36b-41ce-b4f1-1ffdd9f66262"
132
function add_object () {
133
local TYPE=$1
134
local FILE=$2
Sep 28, 2018
135
echo "Adding to $TYPE from $FILE..."
136
137
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)
138
http_code=$(sed '$!d' <<<"$response")
139
140
if [ "$http_code" -eq 201 ]; then
141
headers=$(sed -n '1,/^\r$/p' <<<"$response")
142
143
# get the real Location
144
location=$(grep -oP 'Location: \K.*' <<<"$headers")
145
oid=$(sed 's/.*\///' <<<"$location")
146
147
echo "Oid created object: $oid"
148
return 0
149
else
150
echo "Error code: $http_code"
151
if [ "$http_code" -eq 500 ]; then
152
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
153
else
154
error_message=$(grep 'message' <<<"$response" | head -1 | awk -F">" '{print $2}' | awk -F"<" '{print $1}')
155
echo "Error message: $error_message"
156
fi
157
return 1
158
fi
159
#curl -k --user administrator:5ecr3t -H "Content-Type: application/xml" -X POST "https://localhost:8443/midpoint/ws/rest/$TYPE" -d @$FILE || return 1
Sep 28, 2018
160
#TODO check the returned XML
161
}
162
163
# Tries to find an object with a given name
164
# Results of the search are in the $SEARCH_RESULT_FILE
165
# 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)
166
function search_objects_by_name () {
167
TYPE=$1
168
NAME="$2"
Sep 28, 2018
169
TMPFILE=$(mktemp /tmp/search.XXXXXX)
170
171
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
172
<q:query xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3">
173
<q:filter>
174
<q:equal>
175
<q:path>name</q:path>
176
<q:value>$NAME</q:value>
177
</q:equal>
178
</q:filter>
179
</q:query>
180
EOF
181
SEARCH_RESULT_FILE=$TMPFILE
182
183
http_code=$(sed '$!d' <<<"$(cat $SEARCH_RESULT_FILE)")
184
185
sed -i '$ d' $SEARCH_RESULT_FILE
186
cat $SEARCH_RESULT_FILE
187
if [ "$http_code" -eq 200 ]; then
188
return 0
189
else
190
return 1
191
fi
Sep 28, 2018
192
}
193
194
# Searches for object with a given name and verifies it was found
195
function search_and_check_object () {
196
local TYPE=$1
197
local NAME="$2"
198
search_objects_by_name $TYPE "$NAME" || return 1
Sep 28, 2018
199
if (grep -q "<name>$NAME</name>" <$SEARCH_RESULT_FILE); then
200
echo "Object $TYPE/'$NAME' is OK"
201
rm $SEARCH_RESULT_FILE
202
return 0
203
else
204
echo "Object $TYPE/'$NAME' was not found or not retrieved correctly:"
205
cat $SEARCH_RESULT_FILE
206
rm $SEARCH_RESULT_FILE
207
return 1
208
fi
209
}
210
211
# Tests a resource
212
function test_resource () {
213
local OID=$1
214
local TMPFILE=$(mktemp /tmp/test.resource.XXXXXX)
215
216
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)
217
if [[ $(xmllint --xpath "/*/*[local-name()='status']/text()" $TMPFILE) == "success" ]]; then
218
echo "Resource $OID test succeeded"
219
rm $TMPFILE
220
return 0
221
else
222
echo "Resource $OID test failed"
223
cat $TMPFILE
224
rm $TMPFILE
225
return 1
226
fi
227
}
229
function assert_task_success () {
230
local OID=$1
231
get_object tasks $OID
232
TASK_STATUS=$(xmllint --xpath "/*/*[local-name()='resultStatus']/text()" $TMPFILE) || (echo "Couldn't extract task status from task $OID" ; cat $TMPFILE ; rm $TMPFILE ; return 1)
233
if [[ $TASK_STATUS = "success" ]]; then
234
echo "Task $OID status is OK"
235
rm $TMPFILE
236
return 0
237
else
238
echo "Task $OID status is not OK: $TASK_STATUS"
239
cat $TMPFILE
240
rm $TMPFILE
241
return 1
242
fi
243
}
244
245
function wait_for_task_completion () {
246
local OID=$1
247
sleep 60 # TODO
248
return 0
You can’t perform that action at this time.