Skip to content
Permalink
Newer
Older
100644 248 lines (219 sloc) 8.03 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 ] || [ "$http_code" -eq 202 ]; then
Oct 1, 2018
141
142
# get the real Location
Oct 1, 2018
143
location=$(grep -oP "Location: \K.*" <<<"$response")
144
oid=$(sed 's/.*\///' <<<"$location")
145
146
echo "Oid created object: $oid"
147
return 0
148
else
149
echo "Error code: $http_code"
150
if [ "$http_code" -eq 500 ]; then
151
echo "Error message: Internal server error. Unexpected error occurred, if necessary please contact system administrator."
152
else
153
error_message=$(grep 'message' <<<"$response" | head -1 | awk -F">" '{print $2}' | awk -F"<" '{print $1}')
154
echo "Error message: $error_message"
155
fi
156
return 1
157
fi
158
#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
159
#TODO check the returned XML
160
}
161
162
# Tries to find an object with a given name
163
# Results of the search are in the $SEARCH_RESULT_FILE
164
# 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)
165
function search_objects_by_name () {
166
TYPE=$1
167
NAME="$2"
Sep 28, 2018
168
TMPFILE=$(mktemp /tmp/search.XXXXXX)
169
170
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
171
<q:query xmlns:q="http://prism.evolveum.com/xml/ns/public/query-3">
172
<q:filter>
173
<q:equal>
174
<q:path>name</q:path>
175
<q:value>$NAME</q:value>
176
</q:equal>
177
</q:filter>
178
</q:query>
179
EOF
180
SEARCH_RESULT_FILE=$TMPFILE
181
182
http_code=$(sed '$!d' <<<"$(cat $SEARCH_RESULT_FILE)")
183
184
sed -i '$ d' $SEARCH_RESULT_FILE
185
cat $SEARCH_RESULT_FILE
186
if [ "$http_code" -eq 200 ]; then
187
return 0
188
else
189
return 1
190
fi
Sep 28, 2018
191
}
192
193
# Searches for object with a given name and verifies it was found
194
function search_and_check_object () {
195
local TYPE=$1
196
local NAME="$2"
197
search_objects_by_name $TYPE "$NAME" || return 1
Sep 28, 2018
198
if (grep -q "<name>$NAME</name>" <$SEARCH_RESULT_FILE); then
199
echo "Object $TYPE/'$NAME' is OK"
200
rm $SEARCH_RESULT_FILE
201
return 0
202
else
203
echo "Object $TYPE/'$NAME' was not found or not retrieved correctly:"
204
cat $SEARCH_RESULT_FILE
205
rm $SEARCH_RESULT_FILE
206
return 1
207
fi
208
}
209
210
# Tests a resource
211
function test_resource () {
212
local OID=$1
213
local TMPFILE=$(mktemp /tmp/test.resource.XXXXXX)
214
215
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)
216
if [[ $(xmllint --xpath "/*/*[local-name()='status']/text()" $TMPFILE) == "success" ]]; then
217
echo "Resource $OID test succeeded"
218
rm $TMPFILE
219
return 0
220
else
221
echo "Resource $OID test failed"
222
cat $TMPFILE
223
rm $TMPFILE
224
return 1
225
fi
226
}
228
function assert_task_success () {
229
local OID=$1
230
get_object tasks $OID
231
TASK_STATUS=$(xmllint --xpath "/*/*[local-name()='resultStatus']/text()" $TMPFILE) || (echo "Couldn't extract task status from task $OID" ; cat $TMPFILE ; rm $TMPFILE ; return 1)
232
if [[ $TASK_STATUS = "success" ]]; then
233
echo "Task $OID status is OK"
234
rm $TMPFILE
235
return 0
236
else
237
echo "Task $OID status is not OK: $TASK_STATUS"
238
cat $TMPFILE
239
rm $TMPFILE
240
return 1
241
fi
242
}
243
244
function wait_for_task_completion () {
245
local OID=$1
246
sleep 60 # TODO
247
return 0
You can’t perform that action at this time.