-
Notifications
You must be signed in to change notification settings - Fork 197
/
run
executable file
·430 lines (342 loc) · 12.4 KB
/
run
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
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
#!/bin/bash
#
# The 'run' performs a simple test that verifies that S2I image.
# The main focus here is to excersise the S2I scripts.
#
# IMAGE_NAME specifies a name of the candidate image used for testing.
# The image has to be available before this script is executed.
#
test -n "${IMAGE_NAME-}" || false 'make sure $IMAGE_NAME is defined'
test -n "${VERSION-}" || false 'make sure $VERSION is defined'
test -n "${OS-}" || false 'make sure $OS is defined'
# TODO: Make command compatible for Mac users
test_dir="$(readlink -f $(dirname "${BASH_SOURCE[0]}"))"
TEST_LIST_APP="\
test_s2i_usage
test_docker_run_usage
test_application
test_log_output
test_log_volume_output
test_application_user
"
TEST_LIST_HOOK_APP="test_pre_init_script"
TEST_LIST_PERL_APP="test_perl_directive"
TEST_LIST_MISC=run_dockerfiles_test
source "$test_dir/test-lib.sh"
# TODO: This should be part of the image metadata
test_port=8080
info() {
echo -e "\n\e[1m[INFO] $@...\e[0m\n"
}
image_exists() {
docker inspect $1 &>/dev/null
}
container_exists() {
image_exists $(cat $cid_file)
}
container_ip() {
docker inspect --format="{{ .NetworkSettings.IPAddress }}" $(cat $cid_file)
}
run_s2i_build() {
ct_s2i_build_as_df file://${test_dir}/${1} ${IMAGE_NAME} ${IMAGE_NAME}-${1} ${s2i_args}
}
prepare() {
if ! image_exists ${IMAGE_NAME}; then
echo "ERROR: The image ${IMAGE_NAME} must exist before this script is executed."
exit 1
fi
if ! [ -d "${test_dir}/${1}" ] ; then
echo "ERROR: The testing app ${test_dir}/${1} does not exist."
exit 1
fi
# TODO: S2I build require the application is a valid 'GIT' repository, we
# should remove this restriction in the future when a file:// is used.
info "Build the test application image"
pushd ${test_dir}/${1} >/dev/null
git init
git config user.email "build@localhost" && git config user.name "builder"
git add -A && git commit -m "Sample commit"
popd >/dev/null
}
run_test_application() {
run_args=${CONTAINER_ARGS:-}
info "Running docker run --user=10001 ${run_args} -d --cidfile=${cid_file} ${IMAGE_NAME}-${1}"
docker run --user=10001 ${run_args} -d --cidfile=${cid_file} ${IMAGE_NAME}-${1}
}
cleanup_test_app() {
info "Cleaning up the test application"
if [ -n "$cid_file" ] && [ -f "$cid_file" ]; then
if container_exists; then
docker stop $(cat $cid_file)
docker rm $(cat $cid_file)
fi
rm $cid_file
fi
}
check_result() {
local result="$1"
if [[ "$result" != "0" ]]; then
cleanup_test_app
TESTCASE_RESULT=1
fi
return $result
}
wait_for_cid() {
local max_attempts=10
local sleep_time=1
local attempt=1
local result=1
info "Waiting for application container to start"
while [ $attempt -le $max_attempts ]; do
[ -f $cid_file ] && [ -s $cid_file ] && break
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
}
test_s2i_usage() {
info "Testing 's2i usage'"
ct_s2i_usage ${IMAGE_NAME} ${s2i_args} &>/dev/null
}
test_docker_run_usage() {
info "Testing 'docker run' usage"
docker run --rm ${IMAGE_NAME} &>/dev/null
}
test_scl_usage() {
local run_cmd="$1"
local expected="$2"
info "Testing the image SCL enable"
out=$(docker run --rm ${IMAGE_NAME} /bin/bash -c "${run_cmd} 2>&1")
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
test_command "$run_cmd" "$expected"
return $?
}
test_command() {
local run_cmd="$1"
local expected="$2"
local message="$3"
echo "Test command"
if [ $message ]; then
info ${3}
fi
out=$(docker exec "$(cat ${cid_file})" /bin/bash -c "${run_cmd}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
out=$(docker exec "$(cat ${cid_file})" /bin/sh -ic "${run_cmd}" 2>&1)
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[exec /bin/sh -ic "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
fi
return 0
}
test_logs() {
local expected="$1"
local message="$2"
local logfile="$3"
if [ "$message" ]; then
info ${message}
fi
if [ "x$logfile" = "xstdouterr" ]; then
out=$(docker logs $(cat ${cid_file}) 2>&1)
elif [ "x$logfile" = "xstdout" ]; then
out=$(docker logs $(cat ${cid_file}) 2>/dev/null)
elif [ "x$logfile" = "xstderr" ]; then
out=$(docker logs $(cat ${cid_file}) 2>&1 1>/dev/null)
elif [ -n "$logfile" ]; then
info "Extracting $logfile via docker exec <cont> cat ${logfile}..."
out=$(docker exec $(cat ${cid_file}) cat ${logfile})
else
out=$(docker logs $(cat ${cid_file}) 2>&1)
fi
if ! echo "${out}" | egrep -q "${expected}"; then
echo "ERROR[docker logs $logfile] Expected '${expected}', got '${out}'"
return 1
fi
return 0
}
test_for_output() {
local url="$1"
local expect="$2"
local host="${3-localhost}"
local max_attempts=5
local sleep_time=1
local attempt=1
local result=1
info "Testing URL $url with HTTP hostname ${host} produces output $expect"
while [ $attempt -le $max_attempts ]; do
response=$(curl -is -H "Host: ${host}" ${url})
status=$?
echo "${response}"
if [ $status -eq 0 ]; then
if echo "${response}" | grep -q "${expect}"; then
info "Success!"
result=0
break
else
echo "Response: ${response}"
fi
fi
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
return $result
}
test_connection() {
cat $cid_file
info "Testing the HTTP connection (http://$(container_ip):${test_port})"
if test_for_output "http://$(container_ip):${test_port}/" "NGINX is working" &&
test_for_output "http://$(container_ip):${test_port}/" "NGINX2 is working" localhost2 &&
test_for_output "http://$(container_ip):${test_port}/aliased/index2.html" "NGINX2 is working" &&
test_for_output "http://$(container_ip):${test_port}/nginx-cfg/default.conf" "404"; then
return 0
fi
return 1
}
test_connection_s2i() {
cat $cid_file
info "Testing the HTTP connection (http://$(container_ip):${test_port})"
if test_for_output "http://$(container_ip):${test_port}/" "NGINX is working" &&
test_for_output "http://$(container_ip):${test_port}/" "NGINX2 is working" localhost2 &&
test_for_output "http://$(container_ip):${test_port}/nginx-cfg/default.conf" "404"; then
return 0
fi
return 1
}
test_application() {
# Verify that the HTTP connection can be established to test application container
run_test_application "test-app" &
# Wait for the container to write it's CID file
wait_for_cid
test_scl_usage "nginx -v" "nginx version: nginx/${VERSION%%-*}."
check_result $? || return 1
test_connection
check_result $? || return 1
cleanup_test_app
return 0
}
test_pre_init_script() {
# Verify that the HTTP connection can be established to test application container
run_test_application "start-hook-test-app" &
# Wait for the container to write it's CID file
wait_for_cid
test_command "cat /opt/app-root/etc/nginx.d/default.conf | grep 'resolver' | sed -e 's/resolver\(.*\);/\1/' | grep 'DNS_SERVER'" ""
check_result $? || return 1
test_connection_s2i
check_result $? || return 1
cleanup_test_app
return 0
}
test_log_output() {
info "Testing log output for $IMAGE_NAME"
# Verify that the HTTP connection can be established to test application container
run_test_application "test-app" &
# Wait for the container to write it's CID file
wait_for_cid
info "Testing that GET / was logged as 200"
test_for_output "http://$(container_ip):${test_port}/" "NGINX is working"
check_result $? || return 1
test_logs '"GET / HTTP/1.1" 200' 'Test that GET / is logged as 200' stdout
check_result $? || return 1
info "Testing that GET /nothing-at-all was logged as 404"
test_for_output "http://$(container_ip):${test_port}/nothing-at-all" "404"
check_result $? || return 1
test_logs '"GET /nothing-at-all HTTP/1.1" 404' 'Test that bad GET is logged as 404' stdout
check_result $? || return 1
test_logs 'open.*failed.*No such file or directory' 'Test that bad GET is logged to error.log' stdouterr
check_result $? || return 1
cleanup_test_app
return 0
}
test_log_volume_output() {
info "Testing log volume output for $IMAGE_NAME"
# Verify that the HTTP connection can be established to test application container
CONTAINER_ARGS="-e NGINX_LOG_TO_VOLUME=y" run_test_application "test-app" &
# Wait for the container to write it's CID file
wait_for_cid
info "Testing that GET / was logged as 200"
test_for_output "http://$(container_ip):${test_port}/" "NGINX is working"
check_result $? || return 1
test_logs '"GET / HTTP/1.1" 200' 'Test that GET / is logged as 200' /var/log/nginx/access.log
check_result $? || return 1
info "Testing that GET /nothing-at-all was logged as 404"
test_for_output "http://$(container_ip):${test_port}/nothing-at-all" "404"
check_result $? || return 1
test_logs '"GET /nothing-at-all HTTP/1.1" 404' 'Test that bad GET is logged as 404' /var/log/nginx/access.log
check_result $? || return 1
test_logs 'open.*failed.*No such file or directory' 'Test that bad GET is logged to error.log' /var/log/nginx/error.log
check_result $? || return 1
info "Log volume output tested successfully."
cleanup_test_app
return 0
}
test_perl_directive() {
run_test_application "perl-test-app" &
# Wait for the container to write it's CID file
wait_for_cid
local perl_version=$(docker exec $(cat ${cid_file}) /bin/bash -c "perl -e 'print \"$^V\"'" 2>&1)
info "Testing perl_set directive sets header with perl version"
test_for_output "http://$(container_ip):${test_port}/" "X-Perl-Version: ${perl_version}"
check_result $? || return 1
info "Testing perl directive runs perl location handler"
test_for_output "http://$(container_ip):${test_port}/perl" "Perl location handler is working"
check_result $? || return 1
cleanup_test_app
return 0
}
test_application_user() {
# Test application with random uid
CONTAINER_ARGS="--user=12345" test_application
}
function build_image() {
local test_name="$1"
cid_file="$CID_FILE_DIR/$test_name"
# Since we built the candidate image locally, we don't want S2I attempt to pull
# it from Docker hub
s2i_args="--pull-policy=never"
prepare "${test_name}"
run_s2i_build "${test_name}"
check_result $?
}
function run_dockerfiles_test() {
# This is a hack for moby engine.
# From unknown reason, moby does not like when using glob in a directory
# behind a symlink. In this case, 1.22-micro is a symlink to 1.22 example
# app and when using this example app, moby engine does not match *.html
# files despite being there (confirmed that replacing glob with file names
# works). Here we also expect that symlinks are only the micro variants,
# so therefore we remove -micro suffix in case when the example app
# directory is a symlink.
exammple_to_use="$VERSION"
if [ -L ${test_dir}/examples/$VERSION ] ; then
exammple_to_use="${VERSION%-micro}"
echo "WARNING: using '${exammple_to_use}' example app instead of '${VERSION}' to make the test work in moby"
fi
local dockerfile_tmp=$(mktemp)
sed -e "s/^ENV NGINX_VERSION.*$/ENV NGINX_VERSION=$exammple_to_use/" -e "s/\$NGINX_VERSION/$exammple_to_use/g" "${test_dir}/examples/Dockerfile" >"$dockerfile_tmp"
ct_test_app_dockerfile "$dockerfile_tmp" 'https://github.com/sclorg/nginx-container.git' "NGINX is working" nginx-container
check_result $? || return 1
sed -e "s/^ENV NGINX_VERSION.*$/ENV NGINX_VERSION=$exammple_to_use/" -e "s/\$NGINX_VERSION/$exammple_to_use/g" "${test_dir}/examples/Dockerfile.s2i" >"$dockerfile_tmp"
ct_test_app_dockerfile "$dockerfile_tmp" 'https://github.com/sclorg/nginx-container.git' "NGINX is working" nginx-container
check_result $? || return 1
rm "$dockerfile_tmp"
}
ct_init
cid_file="$CID_FILE_DIR/$(mktemp -u -p . --suffix .cid)"
build_image "test-app"
# Run the chosen tests
TEST_SET=${TEST_LIST_APP} ct_run_tests_from_testset "test-app"
build_image "start-hook-test-app"
# Run the chosen tests
TEST_SET=${TEST_LIST_HOOK_APP} ct_run_tests_from_testset "start-hook-test-app"
# Run the chosen tests (not for micro variant which lacks perl)
if ! [[ "${IMAGE_NAME}" =~ .*micro.* ]] ; then
build_image "perl-test-app"
TEST_LIST=${TEST_LIST_PERL_APP} ct_run_tests_from_testset "perl-test-app"
fi
# Execute the rest of the tests
TEST_SET=${TEST_LIST_MISC} ct_run_tests_from_testset "miscellaneous-tests"
# vim: set tabstop=2:shiftwidth=2:expandtab: