From 15aa7565c260baec4e62b3efca70f14b33f21639 Mon Sep 17 00:00:00 2001
From: Daniele Monti <62102073+Monska85@users.noreply.github.com>
Date: Thu, 15 Apr 2021 14:26:54 +0200
Subject: [PATCH] Improve tests

---
 .github/workflows/docker-publish.yml |  8 ++++----
 Makefile                             |  8 ++++----
 tests/image_verify.sh                | 29 ++++++++++++++++++++++------
 tests/overrides/envfile              |  3 ++-
 tests/overrides/expectations         |  1 +
 5 files changed, 34 insertions(+), 15 deletions(-)

diff --git a/.github/workflows/docker-publish.yml b/.github/workflows/docker-publish.yml
index 477dae4..e813324 100644
--- a/.github/workflows/docker-publish.yml
+++ b/.github/workflows/docker-publish.yml
@@ -29,13 +29,13 @@ jobs:
       - name: Test images
         run: |
           ./tests/image_verify.sh --source tests/expectations --env-file tests/envfile --http-port 80 --user root $IMAGE_NAME:$TAG_1_13
-          ./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --user root $IMAGE_NAME:$TAG_1_13
+          ./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --http-host nginx_default_server_name --user root $IMAGE_NAME:$TAG_1_13
           ./tests/image_verify.sh --source tests/expectations --env-file tests/envfile --http-port 80 --user root $IMAGE_NAME:$TAG_1_17
-          ./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --user root $IMAGE_NAME:$TAG_1_17
+          ./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --http-host nginx_default_server_name --user root $IMAGE_NAME:$TAG_1_17
           ./tests/image_verify.sh --source tests/expectations --env-file tests/envfile --http-port 8080 --user "unknown uid 1001" $IMAGE_NAME:$TAG_1_13_RL
-          ./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --user "unknown uid 1001" $IMAGE_NAME:$TAG_1_13_RL
+          ./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --http-host nginx_default_server_name --user "unknown uid 1001" $IMAGE_NAME:$TAG_1_13_RL
           ./tests/image_verify.sh --source tests/expectations --env-file tests/envfile --http-port 8080 --user "unknown uid 1001" $IMAGE_NAME:$TAG_1_17_RL
-          ./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --user "unknown uid 1001" $IMAGE_NAME:$TAG_1_17_RL
+          ./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --http-host nginx_default_server_name --user "unknown uid 1001" $IMAGE_NAME:$TAG_1_17_RL
 
   deploy:
     needs: test
diff --git a/Makefile b/Makefile
index 92560ae..b3c0ec5 100644
--- a/Makefile
+++ b/Makefile
@@ -6,9 +6,9 @@ build:
 
 test:
 	./tests/image_verify.sh --source tests/expectations --env-file tests/envfile --http-port 80 --user root sparkfabrik/docker-php-drupal-nginx:1.13.6-alpine.d8
-	./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --user root sparkfabrik/docker-php-drupal-nginx:1.13.6-alpine.d8
+	./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --http-host nginx_default_server_name --user root sparkfabrik/docker-php-drupal-nginx:1.13.6-alpine.d8
 	./tests/image_verify.sh --source tests/expectations --env-file tests/envfile --http-port 80 --user root sparkfabrik/docker-php-drupal-nginx:1.17.6-alpine.d8
-	./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --user root sparkfabrik/docker-php-drupal-nginx:1.17.6-alpine.d8
+	./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --http-host nginx_default_server_name --user root sparkfabrik/docker-php-drupal-nginx:1.17.6-alpine.d8
 
 build-rootless:
 	docker build -f Dockerfile-1.13.6-alpine -t sparkfabrik/docker-php-drupal-nginx:1.13.6-alpine.d8-rootless --build-arg user=1001 .
@@ -16,6 +16,6 @@ build-rootless:
 
 test-rootless:
 	./tests/image_verify.sh --source tests/expectations --env-file tests/envfile --http-port 8080 --user "unknown uid 1001" sparkfabrik/docker-php-drupal-nginx:1.13.6-alpine.d8-rootless
-	./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --user "unknown uid 1001" sparkfabrik/docker-php-drupal-nginx:1.13.6-alpine.d8-rootless
+	./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --http-host nginx_default_server_name --user "unknown uid 1001" sparkfabrik/docker-php-drupal-nginx:1.13.6-alpine.d8-rootless
 	./tests/image_verify.sh --source tests/expectations --env-file tests/envfile --http-port 8080 --user "unknown uid 1001" sparkfabrik/docker-php-drupal-nginx:1.17.6-alpine.d8-rootless
-	./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --user "unknown uid 1001" sparkfabrik/docker-php-drupal-nginx:1.17.6-alpine.d8-rootless
+	./tests/image_verify.sh --source tests/overrides/expectations --env-file tests/overrides/envfile --http-port 4321 --http-host nginx_default_server_name --user "unknown uid 1001" sparkfabrik/docker-php-drupal-nginx:1.17.6-alpine.d8-rootless
diff --git a/tests/image_verify.sh b/tests/image_verify.sh
index 53ce6fe..c0f32d8 100755
--- a/tests/image_verify.sh
+++ b/tests/image_verify.sh
@@ -22,6 +22,7 @@ DOCKER_TEST_IMAGE="alpine/httpie:latest"
 DOCKER_TEST_IP=""
 DOCKER_TEST_PORT=80
 DOCKER_TEST_PROTO="http"
+DOCKER_TEST_HOST=""
 
 DOCKER_TEST_OUTPUT=""
 DOCKER_TEST_HEADER_REQ=""
@@ -51,6 +52,7 @@ printf "%-${PAD}s %s\n" "DOCKER_TEST_IMAGE" "${DOCKER_TEST_IMAGE}"
 printf "%-${PAD}s %s\n" "DOCKER_IMAGE" "${DOCKER_IMAGE}"
 printf "%-${PAD}s %s\n" "PORT" "${DOCKER_TEST_PORT}"
 printf "%-${PAD}s %s\n" "PROTO" "${DOCKER_TEST_PROTO}"
+printf "%-${PAD}s %s\n" "HOST" "${DOCKER_TEST_HOST}"
 printf "%-${PAD}s %s\n" "ENV_LIST" "${ENV_LIST}"
 printf "%-${PAD}s %s\n" "ENV_FILE" "${ENV_FILE}"
 printf "%-${PAD}s %s\n" "SOURCE_FILE" "${SOURCE_FILE}"
@@ -99,6 +101,7 @@ Options:
                                       the container image (these variables will override the --env defined ones)
   --http-port N                       Defines the HTTP port, if missing the default 80 port is used
   --http-proto STRING [http|https]    Defines the HTTP protocol to use, if missing the default http is used
+  --http-host STRING                  Defines the HTTP HOST to use, if missing the empty header is used
   --source PATH                       Defines a path for a file which includes the desired expectations
   --user,-u STRING                    Defines the default user for the image
 EOM
@@ -128,6 +131,7 @@ while [ -n "${1}" ]; do
     --env-file) ENV_FILE="${2}"; if [ ! -f "${ENV_FILE}" ]; then exit 3; fi; shift 2 ;;
     --http-port) DOCKER_TEST_PORT="${2}"; shift 2 ;;
     --http-proto) DOCKER_TEST_PROTO="${2}"; shift 2 ;;
+    --http-host) DOCKER_TEST_HOST="${2}"; shift 2 ;;
     --source) SOURCE_FILE="${2}"; if [ ! -f "${SOURCE_FILE}" ]; then exit 2; fi; shift 2 ;;
     --user|-u) TEST_USER="${2}"; shift 2 ;;
     -*|--*=) echo "Error: Unsupported flag $1" >&2; exit 1 ;;
@@ -277,6 +281,15 @@ fi
 #   exit 8
 # fi
 
+stop_container() {
+  if [ -n "${CONTAINER_ID}" ]; then
+    if [ $DEBUG -eq 1 ]; then
+      echo "Docker stop command: docker stop ${CONTAINER_ID} >/dev/null 2>&1"
+    fi
+    docker stop ${CONTAINER_ID} >/dev/null 2>&1
+  fi
+}
+
 echo "Start testing process on image: ${DOCKER_IMAGE} ..."
 
 EXIT_STATUS=0
@@ -288,6 +301,7 @@ fi
 CONTAINER_ID=$(docker run ${DOCKER_ENV} --rm -d -v ${PWD}/tests/html:/var/www/html ${DOCKER_IMAGE})
 if [ $? -ne 0 ]; then
   echo "Failed to start the docker image"
+  stop_container
   exit 9
 fi
 
@@ -301,15 +315,21 @@ fi
 DOCKER_TEST_IP=$(docker inspect -f '{{range.NetworkSettings.Networks}}{{.IPAddress}}{{end}}' ${CONTAINER_ID})
 if [ $? -ne 0 ]; then
   echo "Failed to discover the IP address of the docker image"
+  stop_container
   exit 10
 fi
 
+HTTP_HEADERS=""
+if [ -n "${DOCKER_TEST_HOST}" ]; then
+  HTTP_HEADERS="${HTTP_HEADERS} Host:${DOCKER_TEST_HOST}"
+fi
 if [ $DEBUG -eq 1 ]; then
-  echo "Get the data: docker run --rm ${DOCKER_TEST_IMAGE} -p HhBb GET ${DOCKER_TEST_PROTO}://${DOCKER_TEST_IP}:${DOCKER_TEST_PORT}/test.html"
+  echo "Get the data: docker run --rm ${DOCKER_TEST_IMAGE} -p HhBb GET ${DOCKER_TEST_PROTO}://${DOCKER_TEST_IP}:${DOCKER_TEST_PORT}/test.html ${HTTP_HEADERS}"
 fi
-DOCKER_TEST_OUTPUT=$(docker run --rm ${DOCKER_TEST_IMAGE} -p HhBb GET ${DOCKER_TEST_PROTO}://${DOCKER_TEST_IP}:${DOCKER_TEST_PORT}/test.html)
+DOCKER_TEST_OUTPUT=$(docker run --rm ${DOCKER_TEST_IMAGE} -p HhBb GET ${DOCKER_TEST_PROTO}://${DOCKER_TEST_IP}:${DOCKER_TEST_PORT}/test.html ${HTTP_HEADERS})
 if [ $? -ne 0 ]; then
   echo "Failed to get the data"
+  stop_container
   exit 11
 fi
 
@@ -378,10 +398,7 @@ if [ -n "${TEST_USER}" ]; then
   test_for_user
 fi
 
-if [ $DEBUG -eq 1 ]; then
-  echo "Docker stop command: docker stop ${CONTAINER_ID} >/dev/null 2>&1"
-fi
-docker stop ${CONTAINER_ID} >/dev/null 2>&1
+stop_container
 
 if [ $EXIT_STATUS -eq 0 ]; then
   echo "\e[32mSUCCESS, all tests passed\e[39m"
diff --git a/tests/overrides/envfile b/tests/overrides/envfile
index d4c6638..bedc20c 100644
--- a/tests/overrides/envfile
+++ b/tests/overrides/envfile
@@ -1,3 +1,4 @@
 PHP_HOST=localhost
+NGINX_DEFAULT_SERVER_NAME=nginx_default_server_name
 NGINX_DEFAULT_SERVER_PORT=4321
-NGINX_DEFAULT_ROOT=/var/www/html/folder
\ No newline at end of file
+NGINX_DEFAULT_ROOT=/var/www/html/folder
diff --git a/tests/overrides/expectations b/tests/overrides/expectations
index f9d2fb6..d95a115 100644
--- a/tests/overrides/expectations
+++ b/tests/overrides/expectations
@@ -1,4 +1,5 @@
 HTTP_STATUS="200 OK"
+HEADER_REQ_Host=nginx_default_server_name
 BODY_RES=<h1>It works in another folder</h1>
 HEADER_RES_Server=nginx
 HEADER_RES_Content-Type=text/html