-
Notifications
You must be signed in to change notification settings - Fork 156
/
run
executable file
·204 lines (171 loc) · 5.38 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
#!/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.
#
if [ -z $IMAGE_NAME ]; then
echo "\$IMAGE_NAME not specified"
exit 1
fi
declare -a WEB_SERVERS=(db puma rack)
#declare -a WEB_SERVERS=(db)
# TODO: Make command compatible for Mac users
test_dir="$(readlink -f $(dirname "${BASH_SOURCE[0]}"))"
image_dir=$(readlink -f ${test_dir}/..)
TEST_LIST="\
test_docker_run_usage
test_application
test_connection
test_scl_usage
test_npm_functionality
"
source "${test_dir}/test-lib.sh"
# Read exposed port from image meta data
test_port="$(docker inspect --format='{{range $key, $value := .Config.ExposedPorts }}{{$key}}{{end}}' ${IMAGE_NAME} | sed 's/\/.*//')"
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}-test-app ${IMAGE_NAME} ${IMAGE_NAME}-testapp ${s2i_args} $(ct_build_s2i_npm_variables)
}
run_test_application() {
docker run --user=100001 --rm --cidfile=${cid_file} ${IMAGE_NAME}-testapp
}
ct_check_testcase_result() {
local result="$1"
if [[ "$result" != "0" ]]; then
TESTCASE_RESULT=1
fi
return $result
}
test_s2i_usage() {
info "Testing 's2i usage'"
ct_s2i_usage ${IMAGE_NAME} ${s2i_args} &>/dev/null
ct_check_testcase_result "$?"
}
test_docker_run_usage() {
info "Testing 'docker run' usage"
docker run --rm ${IMAGE_NAME} &>/dev/null
ct_check_testcase_result "$?"
}
test_connection() {
info "Testing the HTTP connection (http://$(container_ip):${test_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 http://$(container_ip):${test_port}/)
status=$?
if [ $status -eq 0 ]; then
if [ $response_code -eq 200 ]; then
echo "SUCCESS, container returned 200"
result=0
fi
break
fi
attempt=$(( $attempt + 1 ))
sleep $sleep_time
done
return $result
}
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
return 0
}
function test_scl_usage() {
scl_usage "ruby --version" "ruby ${VERSION}."
ct_check_testcase_result "$?"
}
function test_npm_functionality() {
info "Testing npm availibility"
ct_npm_works
ct_check_testcase_result "$?"
}
function test_application() {
# 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
ct_wait_for_cid "${cid_file}"
ct_check_testcase_result $?
}
function test_from_dockerfile() {
dockerfile="Dockerfile${1:-}"
TESTCASE_RESULT=0
info "Check building using a $dockerfile"
# Ruby 3.3 introduced too many incompatibilities to be able
# to use the same Gemfile for RHEL 7 and also newer RHELs.
# we can use the same Gemfile for RHEL 7 and newer
# as long as Ruby MAJOR.MINOR <= 3.1. Newer Ruby needs dependencies
# that are not compatible with RHEL 7.
# Latest stable
rails_example_repo_branch="3.3"
if { echo "$VERSION"; echo "3.1"; } | sort --version-sort --check=quiet; then
# Ruby 3.1 and prior.
rails_example_repo_branch="master"
fi
rails_example_repo_url="https://github.com/sclorg/rails-ex.git@${rails_example_repo_branch}"
ct_test_app_dockerfile $test_dir/examples/from-dockerfile/$dockerfile "$rails_example_repo_url" 'Welcome to your Rails application on OpenShift' app-src
ct_check_testcase_result $?
}
function test_from_dockerfile_s2i() {
test_from_dockerfile ".s2i"
}
app_cleanup() {
for server in ${WEB_SERVERS[*]}; do
if [ ! -z "${server}" ]; then
rm -rf ${test_dir}/${server}-test-app/.git
fi
done
if [[ "${server}" == "db" ]]; then
rm -rf ${test_dir}/db-test-app
fi
}
pushd ${test_dir}
if [ -d db-test-app ]; then
rm -rf db-test-app
fi
git clone https://github.com/openshift/ruby-hello-world.git db-test-app
popd
ct_init
for server in ${WEB_SERVERS[@]}; do
cid_file=$CID_FILE_DIR/$(mktemp -u -p . --suffix=.cid)
# Since we built the candidate image locally, we don't want S2I attempt to pull
# it from Docker hub
s2i_args="--pull-policy=never"
run_s2i_build "${server}"
ct_check_testcase_result $?
TEST_SET=${TESTS:-$TEST_LIST} ct_run_tests_from_testset "$server"
done
TEST_LIST="test_from_dockerfile test_from_dockerfile_s2i"
TEST_SET=${TESTS:-$TEST_LIST} ct_run_tests_from_testset "from_dockerfile"
app_cleanup