-
Notifications
You must be signed in to change notification settings - Fork 329
/
Copy pathrun
executable file
·283 lines (234 loc) · 8.14 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
#!/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_LIST="\
test_s2i_usage
test_docker_run_usage
test_application
test_application_user
test_ssl
test_ssl_own_cert
ct_npm_works
test_build_from_dockerfile
test_home_not_in_webroot
"
TEST_CLEAR_ENV="\
clear_env_set
"
# TODO: Make command compatible for Mac users
test_dir="$(readlink -f $(dirname "${BASH_SOURCE[0]}"))"
image_dir=$(readlink -f ${test_dir}/..)
source "${test_dir}/test-lib.sh"
# TODO: This should be part of the image metadata
test_port=8080
test_port_ssl=8443
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}/test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp ${s2i_args} $(ct_build_s2i_npm_variables) $1
}
prepare() {
if ! image_exists ${IMAGE_NAME}; then
echo "ERROR: The image ${IMAGE_NAME} must exist before this script is executed."
return 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}/test-app >/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() {
local run_args=${CONTAINER_ARGS:-}
docker run -d --user=100001 ${run_args} --cidfile=${cid_file} ${IMAGE_NAME}-testapp
}
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 ] && result=0 && break
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
return $result
}
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}")
if ! echo "${out}" | grep -q "${expected}"; then
echo "ERROR[/bin/bash -c "${run_cmd}"] Expected '${expected}', got '${out}'"
return 1
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
}
test_session() {
local check_port=$1 ; shift
local check_protocol=${1:-http} ;
info "Testing PHP session"
response=$(curl -s -k ${check_protocol}://$(container_ip):${check_port}/session_test.php)
if [ "${response}" != "Passed" ]; then
echo "ERROR starting PHP session. Test app returned: '${response}'"
return 1
fi
}
test_connection() {
local check_port=$1 ; shift
local check_protocol=${1:-http}
info "Testing the HTTP connection (${check_protocol}://$(container_ip):${check_port})"
local max_attempts=10
local sleep_time=1
local attempt=1
local result=1
while [ $attempt -le $max_attempts ]; do
response_code=$(curl -s -w %{http_code} -o /dev/null -k ${check_protocol}://$(container_ip):${check_port}/)
status=$?
if [ $status -eq 0 ]; then
if [ $response_code -eq 200 ]; then
result=0
break
fi
fi
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
return $result
}
test_config_writeable() {
local run_cmd="[ -w \${PHP_SYSCONF_PATH}/php.ini ] && [ -w \${PHP_SYSCONF_PATH}/php.d ]"
info "Checking if configuration is writeable"
docker run --rm "${IMAGE_NAME}" /bin/bash -c "${run_cmd}"
}
test_home_not_in_webroot() {
cid_file=$CID_FILE_DIR/$(mktemp -u -p . --suffix .cid)
docker run -d --cidfile=${cid_file} ${IMAGE_NAME}-testapp
# Wait for container to initialize fully
test_connection ${test_port}
info "Testing whether \$HOME points to web server Document root"
homef=$(docker exec -it $(cat ${cid_file}) /bin/bash -c "echo \$HOME")
ct_check_testcase_result $?
if [ "${homef}" = "${homef##/opt/app-root/src}" ]; then
echo "Home folder not set to Document root"
return 0
fi
info "Home folder set to Document root"
return 1
}
test_clear_env_setup() {
local run_cmd="[ -f /etc/php-fpm.d/www.conf ] && grep \"^clear_env = no\" /etc/php-fpm.d/www.conf"
info "Checking if clear_env = no is set in /etc/php-fpm.d/www.conf file."
docker run --rm "${IMAGE_NAME}-testapp" /bin/bash -c "${run_cmd}"
}
clear_env_set() {
PHP_CLEAR_ENV=OFF test_application
}
test_application() {
cid_file=$CID_FILE_DIR/$(mktemp -u -p . --suffix .cid)
# Verify that the HTTP connection can be established to test application container
run_test_application &
# Wait for the container to write it's CID file
wait_for_cid
ct_check_testcase_result $?
test_scl_usage "php --version" "$VERSION"
ct_check_testcase_result $?
test_session ${test_port}
ct_check_testcase_result $?
test_connection ${test_port}
ct_check_testcase_result $?
test_connection ${test_port_ssl} https
ct_check_testcase_result $?
test_config_writeable
ct_check_testcase_result $?
if [ "${OS}" == "rhel9" ] && [ "${PHP_CLEAR_ENV:-ON}" == "OFF" ]; then
test_clear_env_setup
ct_check_testcase_result $?
fi
}
test_application_user() {
# Test application with random uid
CONTAINER_ARGS="--user 12345" test_application
}
test_ssl() {
local cert_dir=/tmp
local cert_base=mycert
ct_gen_self_signed_cert_pem ${cert_dir} ${cert_base}
local private_key=${cert_dir}/${cert_base}-cert-selfsigned.pem
local cert_file=${cert_dir}/${cert_base}-key.pem
}
test_ssl_own_cert() {
local cid_file=$CID_FILE_DIR/$(mktemp -u -p . --suffix .cid)
ct_s2i_build_as_df file://${test_dir}/self-signed-ssl ${IMAGE_NAME} ${IMAGE_NAME}-test-self-signed-ssl ${s2i_args} $(ct_build_s2i_npm_variables)
docker run -d --user=100001 ${run_args} --cidfile=${cid_file} ${IMAGE_NAME}-test-self-signed-ssl
test_connection ${test_port_ssl} https
ct_check_testcase_result $?
echo | openssl s_client -showcerts -servername $(container_ip) -connect $(container_ip):${test_port_ssl} 2>/dev/null | openssl x509 -inform pem -noout -text >./servercert
openssl x509 -in ${test_dir}/self-signed-ssl/httpd-ssl/certs/server-cert-selfsigned.pem -inform pem -noout -text >./configcert
diff ./configcert ./servercert >cert.diff
ct_check_testcase_result $?
}
test_build_from_dockerfile() {
info "Check building using a Dockerfile"
ct_test_app_dockerfile ${test_dir}/examples/from-dockerfile/Dockerfile \
'https://github.com/sclorg/cakephp-ex.git' \
'Welcome to your CakePHP application on OpenShift' \
app-src
ct_check_testcase_result $?
ct_test_app_dockerfile ${test_dir}/examples/from-dockerfile/Dockerfile.s2i \
'https://github.com/sclorg/cakephp-ex.git' \
'Welcome to your CakePHP application on OpenShift' \
app-src
ct_check_testcase_result $?
}
ct_init
# 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
run_s2i_build
ct_check_testcase_result $?
TEST_SET=${TESTS:-$TEST_LIST} ct_run_tests_from_testset "php_tests"
run_s2i_build "-e PHP_CLEAR_ENV=OFF"
ct_check_testcase_result $?
TEST_SET=${TESTS:-$TEST_CLEAR_ENV} ct_run_tests_from_testset "clear_env_set"