forked from liferay/liferay-docker
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test_image.sh
executable file
·271 lines (202 loc) · 6.7 KB
/
test_image.sh
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
#!/bin/bash
source ./_common.sh
function check_usage {
if [ ! -n "${LIFERAY_DOCKER_IMAGE_ID}" ]
then
echo "Usage: ${0}"
echo ""
echo "The script reads the following environment variables:"
echo ""
echo " LIFERAY_DOCKER_IMAGE_ID: ID of Docker image"
echo " LIFERAY_DOCKER_NETWORK_NAME: Docker network name of the CI node container"
echo " LIFERAY_DOCKER_TEST_HOTFIX_URL: URL of the test hotfix to install"
echo " LIFERAY_DOCKER_TEST_INSTALLED_PATCHES: Comma separated list of installed patches (e.g. dxp-4-7210,hotfix-1072-7210)"
echo " LIFERAY_DOCKER_TEST_PATCHING_TOOL_URL: URL of the test Patching Tool to install"
echo ""
echo "Example: LIFERAY_DOCKER_IMAGE_ID=liferay/dxp:7.2.10.1-sp1-202001171544 ${0}"
exit 1
fi
check_utils curl docker
}
function clean_up_test_directory {
if [ "${TEST_RESULT}" -eq 0 ]
then
rm -fr "${TEST_DIR}"
fi
}
function log_test_failure {
TEST_RESULT=1
if [ -n "${1}" ]
then
echo "[${1}] FAILED"
else
echo "[${FUNCNAME[1]}] FAILED"
fi
}
function log_test_success {
if [ -n "${1}" ]
then
echo "[${1}] SUCCESS"
else
echo "[${FUNCNAME[1]}] SUCCESS"
fi
}
function main {
check_usage
prepare_mount
start_container
test_health_status
test_docker_image_files
test_docker_image_fix_pack_installed
test_docker_image_hotfix_installed
test_docker_image_patching_tool_updated
test_docker_image_scripts_1
test_docker_image_scripts_2
stop_container
clean_up_test_directory
exit "${TEST_RESULT}"
}
function prepare_mount {
TEST_DIR=temp-test-$(date "$(date)" "+%Y%m%d%H%M")
mkdir -p "${TEST_DIR}"
cp -r templates/test/resources/* "${TEST_DIR}"
mkdir -p "${TEST_DIR}/mnt/liferay/patching"
if [ -n "${LIFERAY_DOCKER_TEST_PATCHING_TOOL_URL}" ]
then
local patching_tool_file_name=${LIFERAY_DOCKER_TEST_PATCHING_TOOL_URL##*/}
download "downloads/patching-tool/${patching_tool_file_name}" "${LIFERAY_DOCKER_TEST_PATCHING_TOOL_URL}"
fi
if [ -n "${LIFERAY_DOCKER_TEST_HOTFIX_URL}" ]
then
local hotfix_file_name=${LIFERAY_DOCKER_TEST_HOTFIX_URL##*/}
download "downloads/hotfix/${hotfix_file_name}" "${LIFERAY_DOCKER_TEST_HOTFIX_URL}"
cp "downloads/hotfix/${hotfix_file_name}" "${TEST_DIR}/mnt/liferay/patching"
fi
if [ -n "${LIFERAY_DOCKER_TEST_PATCHING_TOOL_VERSION}" ]
then
download "downloads/patching-tool/patching-tool-${LIFERAY_DOCKER_TEST_PATCHING_TOOL_VERSION}.zip" "files.liferay.com/private/ee/fix-packs/patching-tool/patching-tool-${LIFERAY_DOCKER_TEST_PATCHING_TOOL_VERSION}.zip"
cp "downloads/patching-tool/patching-tool-${LIFERAY_DOCKER_TEST_PATCHING_TOOL_VERSION}.zip" "${TEST_DIR}/mnt/liferay/patching/"
fi
if [ -e "${TEST_DIR}/mnt/liferay/scripts" ]
then
chmod -R +x "${TEST_DIR}/mnt/liferay/scripts"
fi
}
function start_container {
echo "Starting container from image ${LIFERAY_DOCKER_IMAGE_ID}."
# TODO Temporary fix until IT rebuilds the CI servers
if [ ! -n "${LIFERAY_DOCKER_NETWORK_NAME}" ]
then
LIFERAY_DOCKER_NETWORK_NAME="${DOCKER_NETWORK_NAME}"
fi
CONTAINER_HOSTNAME="localhost"
local network_parameters
local test_dir="${PWD}/${TEST_DIR}"
if [ -n "${LIFERAY_DOCKER_NETWORK_NAME}" ]
then
CONTAINER_HOSTNAME=portal-container
CONTAINER_HTTP_PORT=8080
test_dir="/data/${LIFERAY_DOCKER_NETWORK_NAME}/liferay/liferay-docker/${TEST_DIR}"
network_parameters="--hostname=${CONTAINER_HOSTNAME} --name=${CONTAINER_HOSTNAME} --network=${LIFERAY_DOCKER_NETWORK_NAME}"
fi
CONTAINER_ID=$(docker run -d -p 8080 -v "${test_dir}/mnt:/mnt:rw" ${network_parameters} "${LIFERAY_DOCKER_IMAGE_ID}")
if [ ! -n "${LIFERAY_DOCKER_NETWORK_NAME}" ]
then
CONTAINER_HTTP_PORT=$(docker port "${CONTAINER_ID}" 8080/tcp)
CONTAINER_HTTP_PORT=${CONTAINER_HTTP_PORT##*:}
fi
TEST_RESULT=0
}
function stop_container {
echo "Stopping container."
docker kill "${CONTAINER_ID}" > /dev/null
docker rm "${CONTAINER_ID}" > /dev/null
}
function test_docker_image_files {
test_page "http://${CONTAINER_HOSTNAME}:${CONTAINER_HTTP_PORT}/test_docker_image_files.jsp" "TEST"
}
function test_docker_image_fix_pack_installed {
if [ -n "${LIFERAY_DOCKER_TEST_INSTALLED_PATCHES}" ]
then
local correct_fix_pack=$(echo "${LIFERAY_DOCKER_TEST_INSTALLED_PATCHES}" | tr -d '[:space:]')
local output=$(docker exec -it "${CONTAINER_ID}" /opt/liferay/patching-tool/patching-tool.sh info | grep "Currently installed patches:")
local installed_fix_pack=$(echo "${output##*: }" | tr -d '[:space:]')
if [ "${correct_fix_pack}" == "${installed_fix_pack}" ]
then
log_test_success
else
log_test_failure
echo "The installed patch (${correct_fix_pack}) does not match the patch version retrived from the Patching Tool in the container (${installed_fix_pack})."
fi
else
log_test_success
fi
}
function test_docker_image_hotfix_installed {
if [ -n "${LIFERAY_DOCKER_TEST_HOTFIX_URL}" ]
then
test_page "http://${CONTAINER_HOSTNAME}:${CONTAINER_HTTP_PORT}/" "Hotfix installation on the Docker image was successful."
fi
}
function test_docker_image_patching_tool_updated {
if [ -n "${LIFERAY_DOCKER_TEST_PATCHING_TOOL_VERSION}" ]
then
local output=$(docker logs --details "${CONTAINER_ID}" 2>/dev/null)
if [[ "${output}" =~ .*"Patching Tool updated successfully".* ]]
then
log_test_success
else
log_test_failure
echo "Unable to update the Patching Tool."
fi
fi
}
function test_docker_image_scripts_1 {
test_page "http://${CONTAINER_HOSTNAME}:${CONTAINER_HTTP_PORT}/test_docker_image_scripts_1.jsp" "TEST1"
}
function test_docker_image_scripts_2 {
test_page "http://${CONTAINER_HOSTNAME}:${CONTAINER_HTTP_PORT}/test_docker_image_scripts_2.jsp" "TEST2"
}
function test_health_status {
echo -en "Waiting for health status"
for counter in {1..200}
do
echo -en "."
local health_status=$(docker inspect --format="{{json .State.Health.Status}}" "${CONTAINER_ID}")
local ignore_license=$(docker logs ${CONTAINER_ID} 2> /dev/null | grep -c "Starting Liferay Portal")
local license_status=$(docker logs ${CONTAINER_ID} 2> /dev/null | grep -c "License registered for DXP Development")
if [ "${health_status}" == "\"healthy\"" ] && ([ ${ignore_license} -gt 0 ] || [ ${license_status} -gt 0 ])
then
echo ""
log_test_success
return
fi
sleep 3
done
echo ""
log_test_failure
echo "Container health status is: ${health_status}."
}
function test_page {
local content
content=$(curl --fail -s --show-error -L "${1}")
local exit_code=$?
if [ ${exit_code} -gt 0 ]
then
log_test_failure "${FUNCNAME[1]}"
echo "${content}"
echo ""
echo "curl exit code is: ${exit_code}."
else
if [[ "${content}" =~ .*"${2}".* ]]
then
log_test_success "${FUNCNAME[1]}"
else
log_test_failure "${FUNCNAME[1]}"
echo "${content}"
echo ""
echo "The \"${2}\" string is not present on the page."
fi
fi
}
main "${@}"