forked from nginxinc/nginx-s3-gateway
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest.sh
executable file
·220 lines (182 loc) · 6.63 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
#!/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 nounset # abort on unbound variable
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"
}
if [ $# -eq 0 ]; then
nginx_type="oss"
p "No argument specified - defaulting to NGINX OSS. Valid arguments: oss, plus"
elif [ "$1" = "plus" ]; then
nginx_type="plus"
p "Testing with NGINX Plus"
else
nginx_type="oss"
p "Testing with NGINX OSS"
fi
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() {
${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"
# See if Minio is already running, if it isn't then we don't need to build it
if [ -z "$(docker ps -q -f name=${test_compose_project}_minio_1)" ]; then
p "Building Docker Compose environment"
AWS_SIGS_VERSION=$1 ALLOW_DIRECTORY_LIST=$2 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"
AWS_SIGS_VERSION=$1 ALLOW_DIRECTORY_LIST=$2 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
$wait_for_it_cmd -h ${nginx_server_host} -p ${nginx_server_port}
fi
p "Starting HTTP API tests (v$1 signatures)"
bash "${test_dir}/integration/test_api.sh" "$test_server" "$test_dir" "$1" "$2"
# 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
p "Building NGINX S3 gateway Docker image"
if [ "${nginx_type}" = "plus" ]; then
if docker info 2> /dev/null | grep --quiet 'Build with BuildKit'; then
p "Building using BuildKit"
export DOCKER_BUILDKIT=1
docker build -f Dockerfile.buildkit.${nginx_type} -t nginx-s3-gateway \
--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 .
else
docker build -f Dockerfile.${nginx_type} -t nginx-s3-gateway .
fi
else
docker build -f Dockerfile.${nginx_type} -t nginx-s3-gateway .
fi
### UNIT TESTS
p "Running unit tests in Docker image"
${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
# Test API with AWS Signature V2 and allow directory listing off
integration_test 2 0
compose stop nginx-s3-gateway # Restart with new config
# Test API with AWS Signature V2 and allow directory listing on
integration_test 2 1
compose stop nginx-s3-gateway # Restart with new config
# Test API with AWS Signature V4 and allow directory listing off
integration_test 4 0
compose stop nginx-s3-gateway # Restart with new config
# Test API with AWS Signature V4 and allow directory listing on
integration_test 4 1
p "All tests complete"