forked from nginxinc/nginx-s3-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
test.sh
executable file
·340 lines (283 loc) · 10.6 KB
/
test.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
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
#!/usr/bin/env bash
#
# Copyright 2020 F5 Networks
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
set -o errexit # abort on nonzero exit status
set -o pipefail # don't hide errors within pipes
nginx_server_proto="http"
nginx_server_host="localhost"
nginx_server_port="8989"
minio_server="http://localhost:9090"
test_server="${nginx_server_proto}://${nginx_server_host}:${nginx_server_port}"
test_fail_exit_code=2
no_dep_exit_code=3
script_dir="$( cd "$( dirname "${BASH_SOURCE[0]}" )" >/dev/null 2>&1 && pwd )"
test_dir="${script_dir}/test"
test_compose_config="${test_dir}/docker-compose.yaml"
test_compose_project="ngt"
p() {
printf "\033[34;1m▶\033[0m "
echo "$1"
}
e() {
>&2 echo "$1"
}
usage() { e "Usage: $0 [--latest-njs <default:false>] [--unprivileged <default:false>] [--type <default:oss|plus>" 1>&2; exit 1; }
for arg in "$@"; do
shift
case "$arg" in
'--help') set -- "$@" '-h' ;;
'--latest-njs') set -- "$@" '-j' ;;
'--unprivileged') set -- "$@" '-u' ;;
'--type') set -- "$@" '-t' ;;
*) set -- "$@" "$arg" ;;
esac
done
while getopts "hjut:" arg; do
case "${arg}" in
j)
njs_latest="1"
;;
u)
unprivileged="1"
;;
t)
nginx_type="${OPTARG}"
;;
*)
usage
;;
esac
done
shift $((OPTIND-1))
startup_message=""
if [ -z "${nginx_type}" ]; then
nginx_type="oss"
startup_message="Starting NGINX ${nginx_type} (default)"
elif ! { [ ${nginx_type} == "oss" ] || [ ${nginx_type} == "plus" ]; }; then
e "Invalid NGINX type: ${nginx_type} - must be either 'oss' or 'plus'"
usage
else
startup_message="Starting NGINX ${nginx_type}"
fi
if [ -z "${njs_latest}" ]; then
njs_latest="0"
startup_message="${startup_message} with the release NJS module (default)"
elif [ ${njs_latest} -eq 1 ]; then
startup_message="${startup_message} with the latest NJS module"
else
startup_message="${startup_message} with the release NJS module"
fi
if [ -z "${unprivileged}" ]; then
unprivileged="0"
startup_message="${startup_message} in privileged mode (default)"
elif [ ${unprivileged} -eq 1 ]; then
startup_message="${startup_message} in unprivileged mode"
else
startup_message="${startup_message} in privileged mode"
fi
e "${startup_message}"
set -o nounset # abort on unbound variable
docker_cmd="$(command -v docker)"
if ! [ -x "${docker_cmd}" ]; then
e "required dependency not found: docker not found in the path or not executable"
exit ${no_dep_exit_code}
fi
docker_compose_cmd="$(command -v docker-compose)"
if ! [ -x "${docker_compose_cmd}" ]; then
e "required dependency not found: docker-compose not found in the path or not executable"
exit ${no_dep_exit_code}
fi
curl_cmd="$(command -v curl)"
if ! [ -x "${curl_cmd}" ]; then
e "required dependency not found: curl not found in the path or not executable"
exit ${no_dep_exit_code}
fi
wait_for_it_cmd="$(command -v wait-for-it || true)"
if [ -x "${wait_for_it_cmd}" ]; then
wait_for_it_installed=1
else
e "wait-for-it command not available, consider installing to prevent race conditions"
wait_for_it_installed=0
fi
if [ "${nginx_type}" = "plus" ]; then
if [ ! -f "./plus/etc/ssl/nginx/nginx-repo.crt" ]; then
e "NGINX Plus certificate file not found: $(pwd)/plus/etc/ssl/nginx/nginx-repo.crt"
exit ${no_dep_exit_code}
fi
if [ ! -f "./plus/etc/ssl/nginx/nginx-repo.key" ]; then
e "NGINX Plus key file not found: $(pwd)/plus/etc/ssl/nginx/nginx-repo.key"
exit ${no_dep_exit_code}
fi
fi
compose() {
# Hint to docker-compose the internal port to map for the container
if [ ${unprivileged} -eq 1 ]; then
export NGINX_INTERNAL_PORT=8080
else
export NGINX_INTERNAL_PORT=80
fi
"${docker_compose_cmd}" -f "${test_compose_config}" -p "${test_compose_project}" "$@"
}
integration_test() {
printf "\033[34;1m▶\033[0m"
printf "\e[1m Integration test suite for v%s signatures\e[22m\n" "$1"
printf "\033[34;1m▶\033[0m"
printf "\e[1m Integration test suite with ALLOW_DIRECTORY_LIST=%s\e[22m\n" "$2"
printf "\033[34;1m▶\033[0m"
printf "\e[1m Integration test suite with PROVIDE_INDEX_PAGE=%s\e[22m\n" "$3"
printf "\033[34;1m▶\033[0m"
printf "\e[1m Integration test suite with APPEND_SLASH_FOR_POSSIBLE_DIRECTORY=%s\e[22m\n" "$4"
# See if Minio is already running, if it isn't then we don't need to build it
# COMPOSE_COMPATIBILITY=true Supports older style compose filenames with _ vs -
if [ -z "$(docker ps -q -f name=${test_compose_project}_minio-_1)" ]; then
p "Building Docker Compose environment"
COMPOSE_COMPATIBILITY=true AWS_SIGS_VERSION=$1 ALLOW_DIRECTORY_LIST=$2 PROVIDE_INDEX_PAGE=$3 APPEND_SLASH_FOR_POSSIBLE_DIRECTORY=$4 compose up --no-start
p "Adding test data to container"
echo "Copying contents of ${test_dir}/data to Docker container ${test_compose_project}_minio_1:/"
"${docker_cmd}" cp "${test_dir}/data" "${test_compose_project}"_minio_1:/
echo "Docker diff output:"
"${docker_cmd}" diff "${test_compose_project}"_minio_1
fi
p "Starting Docker Compose Environment"
COMPOSE_COMPATIBILITY=true AWS_SIGS_VERSION=$1 ALLOW_DIRECTORY_LIST=$2 PROVIDE_INDEX_PAGE=$3 APPEND_SLASH_FOR_POSSIBLE_DIRECTORY=$4 compose up -d
if [ "${wait_for_it_installed}" ]; then
# Hit minio's health check end point to see if it has started up
for (( i=1; i<=3; i++ ))
do
echo "Querying minio server to see if it is ready"
minio_is_up="$(${curl_cmd} -s -o /dev/null -w '%{http_code}' "${minio_server}"/minio/health/cluster)"
if [ "${minio_is_up}" = "200" ]; then
break
else
sleep 2
fi
done
if [ -x "${wait_for_it_cmd}" ]; then
"${wait_for_it_cmd}" -h "${nginx_server_host}" -p "${nginx_server_port}"
fi
fi
p "Starting HTTP API tests (v$1 signatures)"
echo " test/integration/test_api.sh \"$test_server\" \"$test_dir\" $1 $2 $3 $4"
bash "${test_dir}/integration/test_api.sh" "${test_server}" "${test_dir}" "$1" "$2" "$3" "$4";
# We check to see if NGINX is in fact using the correct version of AWS
# signatures as it was configured to do.
sig_versions_found_count=$(compose logs nginx-s3-gateway | grep -c "AWS Signatures Version: v$1\|AWS v$1 Auth")
if [ "${sig_versions_found_count}" -lt 3 ]; then
e "NGINX was not detected as using the correct signatures version - examine logs"
compose logs nginx-s3-gateway
exit "$test_fail_exit_code"
fi
}
finish() {
result=$?
if [ $result -ne 0 ]; then
e "Error running tests - outputting container logs"
compose logs
fi
p "Cleaning up Docker compose environment"
compose stop
compose rm -f
exit ${result}
}
trap finish EXIT ERR SIGTERM SIGINT
### BUILD
p "Building NGINX S3 gateway Docker image"
if [ "${nginx_type}" = "plus" ]; then
if docker buildx > /dev/null 2>&1; then
p "Building using BuildKit"
export DOCKER_BUILDKIT=1
docker build -f Dockerfile.buildkit.${nginx_type} \
--secret id=nginx-crt,src=plus/etc/ssl/nginx/nginx-repo.crt \
--secret id=nginx-key,src=plus/etc/ssl/nginx/nginx-repo.key \
--no-cache --squash \
--tag nginx-s3-gateway --tag nginx-s3-gateway:${nginx_type} .
else
docker build -f Dockerfile.${nginx_type} \
--tag nginx-s3-gateway --tag nginx-s3-gateway:${nginx_type} .
fi
else
docker build -f Dockerfile.${nginx_type} \
--tag nginx-s3-gateway --tag nginx-s3-gateway:${nginx_type} .
fi
if [ ${njs_latest} -eq 1 ]; then
p "Layering in latest NJS build"
docker build -f Dockerfile.latest-njs \
--tag nginx-s3-gateway --tag nginx-s3-gateway:latest-njs-${nginx_type} .
fi
if [ ${unprivileged} -eq 1 ]; then
p "Layering in unprivileged build"
docker build -f Dockerfile.unprivileged \
--tag nginx-s3-gateway --tag nginx-s3-gateway:unprivileged-${nginx_type} .
fi
### UNIT TESTS
p "Running unit tests with an access key ID and a secret key in Docker image"
#MSYS_NO_PATHCONV=1 added to resolve automatic path conversion
# https://github.com/docker/for-win/issues/6754#issuecomment-629702199
MSYS_NO_PATHCONV=1 "${docker_cmd}" run \
--rm \
-v "$(pwd)/test/unit:/var/tmp" \
--workdir /var/tmp \
-e "S3_DEBUG=true" \
-e "S3_STYLE=virtual" \
-e "S3_ACCESS_KEY_ID=unit_test" \
-e "S3_SECRET_KEY=unit_test" \
-e "S3_SESSION_TOKEN=unit_test" \
-e "S3_BUCKET_NAME=unit_test" \
-e "S3_SERVER=unit_test" \
-e "S3_SERVER_PROTO=https" \
-e "S3_SERVER_PORT=443" \
-e "S3_REGION=test-1" \
-e "AWS_SIGS_VERSION=4" \
--entrypoint /usr/bin/njs \
nginx-s3-gateway -t module -p '/etc/nginx' /var/tmp/s3gateway_test.js
p "Running unit tests with a session token in Docker image"
#MSYS_NO_PATHCONV=1 added to resolve automatic path conversion
# https://github.com/docker/for-win/issues/6754#issuecomment-629702199
MSYS_NO_PATHCONV=1 "${docker_cmd}" run \
--rm \
-v "$(pwd)/test/unit:/var/tmp" \
--workdir /var/tmp \
-e "S3_DEBUG=true" \
-e "S3_STYLE=virtual" \
-e "S3_ACCESS_KEY_ID=unit_test" \
-e "S3_SECRET_KEY=unit_test" \
-e "S3_BUCKET_NAME=unit_test" \
-e "S3_SERVER=unit_test" \
-e "S3_SERVER_PROTO=https" \
-e "S3_SERVER_PORT=443" \
-e "S3_REGION=test-1" \
-e "AWS_SIGS_VERSION=4" \
--entrypoint /usr/bin/njs \
nginx-s3-gateway -t module -p '/etc/nginx' /var/tmp/s3gateway_test.js
### INTEGRATION TESTS
p "Testing API with AWS Signature V2 and allow directory listing off"
integration_test 2 0 0 0
compose stop nginx-s3-gateway # Restart with new config
p "Testing API with AWS Signature V2 and allow directory listing on"
integration_test 2 1 0 0
compose stop nginx-s3-gateway # Restart with new config
p "Testing API with AWS Signature V2 and static site on"
integration_test 2 0 1 0
compose stop nginx-s3-gateway # Restart with new config
p "Test API with AWS Signature V4 and allow directory listing off"
integration_test 4 0 0 0
compose stop nginx-s3-gateway # Restart with new config
p "Test API with AWS Signature V4 and allow directory listing on and appending /"
integration_test 4 1 0 1
compose stop nginx-s3-gateway # Restart with new config
p "Test API with AWS Signature V4 and static site on appending /"
integration_test 4 0 1 1
p "All integration tests complete"