Skip to content

Commit ab74071

Browse files
use next available port for testing
1 parent bb9534e commit ab74071

File tree

5 files changed

+31
-10
lines changed

5 files changed

+31
-10
lines changed

scripts.sh

Lines changed: 19 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,10 @@ clean_module() {
4747
}
4848

4949
start_nginx() {
50-
printf "${BLUE}Starting NGINX container (${IMAGE_NAME})...${NC}\n"
51-
docker run --rm --name "${IMAGE_NAME}" -d -p 8000:80 ${FULL_IMAGE_NAME} >/dev/null
50+
local port=$(get_port)
51+
52+
printf "${BLUE}Starting NGINX container (${IMAGE_NAME}) on port ${port}...${NC}\n"
53+
docker run --rm --name "${IMAGE_NAME}" -d -p ${PORT}:80 ${FULL_IMAGE_NAME} >/dev/null
5254
}
5355

5456
stop_nginx() {
@@ -82,11 +84,13 @@ build_test_runner() {
8284
local dockerArgs=${1:-}
8385
local configHash=$(get_hash $(find test -type f -not -name 'test.sh' -not -name '*.yml' -not -name 'Dockerfile*'))
8486
local sourceHash=$(get_hash test/test.sh)
85-
86-
printf "${BLUE}Building test runner...${NC}\n"
87+
local port=$(get_port)
88+
89+
printf "${BLUE}Building test runner using port ${port}...${NC}\n"
8790
docker compose -f ./test/docker-compose-test.yml build ${dockerArgs} \
8891
--build-arg CONFIG_HASH=${configHash}\
89-
--build-arg SOURCE_HASH=${sourceHash}
92+
--build-arg SOURCE_HASH=${sourceHash} \
93+
--build-arg PORT=${port}
9094
}
9195

9296
rebuild_test_runner() {
@@ -105,7 +109,7 @@ test() {
105109
docker logs ${CONTAINER_NAME_PREFIX}
106110
printf "${NC}\n"
107111
else
108-
docker start -a ${CONTAINER_NAME_PREFIX}-runner
112+
test_now
109113
fi
110114

111115
docker compose -f ./test/docker-compose-test.yml down
@@ -119,6 +123,15 @@ get_hash() {
119123
sha1sum $@ | sed -E 's|\s+|:|' | tr '\n' ' ' | sha1sum | head -c 40
120124
}
121125

126+
get_port() {
127+
for p in $(seq 8000 8100); do
128+
if ! ss -ln | grep -q ":${p} "; then
129+
echo ${p}
130+
break
131+
fi
132+
done
133+
}
134+
122135
if [ $# -eq 0 ]; then
123136
all
124137
else

test/Dockerfile-test-nginx

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,12 @@
11
ARG BASE_IMAGE
22
ARG CONFIG_HASH
3+
ARG PORT
34

45
FROM ${BASE_IMAGE} as NGINX
56
ARG CONFIG_HASH
7+
ARG PORT
68
RUN echo "Config Hash: ${CONFIG_HASH}"
79
COPY /docker-entrypoint.d/* /docker-entrypoint.d/
810
COPY /etc/nginx/conf.d/test.conf /etc/nginx/conf.d/test.conf
911
COPY /etc/nginx/rsa_key_2048-pub.pem /etc/nginx/rsa-key.conf
12+
RUN sed -i "s|%{PORT}|${PORT}|" /etc/nginx/conf.d/test.conf

test/Dockerfile-test-runner

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,13 @@
11
ARG SOURCE_HASH
2+
ARG PORT
23

34
FROM alpine:3.7 AS test-base
45
RUN apk add curl bash
56

67
FROM test-base AS test
78
ARG SOURCE_HASH
9+
ARG PORT
10+
ENV PORT=${PORT}
811
RUN echo "Source Hash: ${SOURCE_HASH}"
912
COPY test.sh .
10-
CMD ["./test.sh"]
13+
CMD ./test.sh ${PORT}

test/etc/nginx/conf.d/test.conf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,7 @@ error_log /var/log/nginx/debug.log debug;
22
access_log /var/log/nginx/access.log;
33

44
server {
5-
listen 8000;
5+
listen %{PORT};
66
server_name localhost;
77

88
auth_jwt_key "00112233445566778899AABBCCDDEEFF00112233445566778899AABBCCDDEEFF";

test/test.sh

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
#!/bin/bash
1+
#!/bin/bash -u
22

33
# set a test # here to execute only that test and output additional info
4+
PORT=${1:-8000}
45
DEBUG=
56

67
RED='\e[31m'
@@ -45,7 +46,7 @@ run_test () {
4546
esac
4647
done
4748

48-
curlCommand="curl -s -v http://nginx:8000${path} -H 'Cache-Control: no-cache' ${extraCurlOpts} 2>&1"
49+
curlCommand="curl -s -v http://nginx:${PORT}${path} -H 'Cache-Control: no-cache' ${extraCurlOpts} 2>&1"
4950
response=$(eval "${curlCommand}")
5051
exitCode=$?
5152

@@ -224,4 +225,5 @@ if [ "${DEBUG}" != '' ]; then
224225
printf "\n${RED}Some tests will be skipped since DEBUG is set.${NC}\n"
225226
fi
226227

228+
printf "\n${GRAY}Starting tests using port ${PORT}...${NC}\n"
227229
main

0 commit comments

Comments
 (0)