Use another linked container for testing instead of using host ports

This commit is contained in:
Daniel Carrillo 2019-12-24 17:59:04 +01:00
parent 8b86bde182
commit 5faf9f447e
1 changed files with 20 additions and 10 deletions

View File

@ -9,6 +9,7 @@ fi
trap _catch_err ERR trap _catch_err ERR
trap _cleanup EXIT trap _cleanup EXIT
ALPINE_VERSION="alpine:3.11"
LOCAL_DIR="$(cd "$(dirname "$0")" ; pwd -P)" LOCAL_DIR="$(cd "$(dirname "$0")" ; pwd -P)"
. "$LOCAL_DIR"/../conf.env . "$LOCAL_DIR"/../conf.env
@ -23,16 +24,17 @@ _cleanup()
{ {
echo "Cleaning up..." echo "Cleaning up..."
docker rm -f "${NGINX_VERSION}"_test > /dev/null 2>&1 docker rm -f "${NGINX_VERSION}"_test > /dev/null 2>&1
docker rm -f "${NGINX_VERSION}"_requester > /dev/null 2>&1
rm -rf "$TMP_DIR" rm -rf "$TMP_DIR"
} }
_setup_crypto_stuff() _setup_crypto_stuff()
{ {
openssl dhparam -out "$TMP_DIR"/dhparams.pem 512 echo "Generating SSL files..."
openssl dhparam -out "$TMP_DIR"/dhparams.pem 512 > /dev/null 2>&1
openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \ openssl req -new -newkey rsa:4096 -days 365 -nodes -x509 \
-subj "/C=ES/ST=Madrid/L=Madrid/O=dcarrillo/CN=localhost" \ -subj "/C=ES/ST=Madrid/L=Madrid/O=dcarrillo/CN=localhost" \
-keyout "$TMP_DIR"/cert.key -out "$TMP_DIR"/cert.pem -keyout "$TMP_DIR"/cert.key -out "$TMP_DIR"/cert.pem > /dev/null 2>&1
} }
_check_status_code() _check_status_code()
@ -50,14 +52,15 @@ _check_if_is_ip()
if echo "$1" | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" > /dev/null; then if echo "$1" | grep -E "^[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}$" > /dev/null; then
echo "Test succeeded" echo "Test succeeded"
else else
printf "Test failed, response %s is not an IP" "$RESPONSE" printf "Response: %s\nTest failed, response is not an IP\n" "$RESPONSE"
exit 1 exit 1
fi fi
} }
_setup_crypto_stuff _setup_crypto_stuff
docker run --name "${NGINX_VERSION}"_test --rm -p 65521:80 -p 65523:443 \ echo "Running container to be tested..."
docker run --name "${NGINX_VERSION}"_test --rm \
-v "$LOCAL_DIR"/nginx.conf:/usr/local/nginx/conf/nginx.conf:ro \ -v "$LOCAL_DIR"/nginx.conf:/usr/local/nginx/conf/nginx.conf:ro \
-v "$LOCAL_DIR"/GeoLite2-Country.mmdb:/tmp/GeoLite2-Country.mmdb:ro \ -v "$LOCAL_DIR"/GeoLite2-Country.mmdb:/tmp/GeoLite2-Country.mmdb:ro \
-v "$TMP_DIR"/cert.pem:/tmp/cert.pem:ro \ -v "$TMP_DIR"/cert.pem:/tmp/cert.pem:ro \
@ -65,14 +68,21 @@ docker run --name "${NGINX_VERSION}"_test --rm -p 65521:80 -p 65523:443 \
-v "$TMP_DIR"/dhparams.pem:/tmp/dhparams.pem:ro \ -v "$TMP_DIR"/dhparams.pem:/tmp/dhparams.pem:ro \
-d "${DOCKER_IMAGE}":"${NGINX_VERSION}" > /dev/null -d "${DOCKER_IMAGE}":"${NGINX_VERSION}" > /dev/null
for request in http://localhost:65521/nginx_status https://localhost:65523/nginx_status; do echo "Preparing requester container..."
printf "\nRequesting %s\n" $request docker run --name "${NGINX_VERSION}"_requester --rm --link "${NGINX_VERSION}"_test \
STATUS_CODE=$(curl -s -k -m 5 -o /dev/null -w "%{http_code}" $request) -ti -d $ALPINE_VERSION sh > /dev/null
exec_docker="docker exec -ti ${NGINX_VERSION}_requester"
$exec_docker apk add curl > /dev/null
for request in http://${NGINX_VERSION}_test/nginx_status https://${NGINX_VERSION}_test/nginx_status; do
printf "\nRequesting %s\n" "$request"
STATUS_CODE=$($exec_docker curl -s -k -m 5 -o /dev/null -w "%{http_code}" "$request")
_check_status_code "$STATUS_CODE" _check_status_code "$STATUS_CODE"
done done
printf "\nRequesting http://localhost:65521/ip\n" request="http://${NGINX_VERSION}_test/ip"
RESPONSE=$(curl -s -m 5 http://localhost:65521/ip) printf "\nRequesting %s\n" "$request"
RESPONSE=$($exec_docker curl -s -m 5 "$request" | tr -d '\r')
_check_if_is_ip "$RESPONSE" _check_if_is_ip "$RESPONSE"
echo "All tests succeeded !" echo "All tests succeeded !"