From d5c7fa8cfbefb8db538abcbe6f6da82bc884f2a2 Mon Sep 17 00:00:00 2001
From: Manuel Alejandro de Brito Fontes <aledbf@gmail.com>
Date: Tue, 9 Jul 2019 09:24:52 -0400
Subject: [PATCH] Fix scripts to be able to run tests in docker

---
 .gitignore                                |  2 ++
 Makefile                                  | 23 ++++---------
 build/run-in-docker.sh                    | 39 +++++++++++++++--------
 build/run-ingress-controller.sh           |  3 +-
 images/custom-error-pages/Makefile        |  1 -
 internal/ingress/controller/nginx_test.go |  4 +--
 test/e2e-image/Makefile                   | 11 +++----
 test/e2e/run.sh                           |  3 ++
 8 files changed, 45 insertions(+), 41 deletions(-)

diff --git a/.gitignore b/.gitignore
index 6b187ddf7d..be6015075e 100644
--- a/.gitignore
+++ b/.gitignore
@@ -47,3 +47,5 @@ gh-pages
 /bin/
 
 test/e2e-image/wait-for-nginx\.sh
+
+.cache
diff --git a/Makefile b/Makefile
index 05b8b12148..04b954b187 100644
--- a/Makefile
+++ b/Makefile
@@ -44,7 +44,7 @@ PKG = k8s.io/ingress-nginx
 
 ARCH ?= $(shell go env GOARCH)
 GOARCH = ${ARCH}
-DUMB_ARCH = ${ARCH}
+
 
 GOBUILD_FLAGS := -v
 
@@ -56,13 +56,9 @@ BUSTED_ARGS =-v --pattern=_test
 
 GOOS = linux
 
-IMGNAME = nginx-ingress-controller
-IMAGE = $(REGISTRY)/$(IMGNAME)
-
-MULTI_ARCH_IMG = ${IMAGE}-${ARCH}
+MULTI_ARCH_IMAGE = $(REGISTRY)/nginx-ingress-controller-${ARCH}
 
 export ARCH
-export DUMB_ARCH
 export TAG
 export PKG
 export GOARCH
@@ -116,7 +112,6 @@ container: clean-container .container-$(ARCH)
 	cp -RP ./* $(TEMP_DIR)
 	$(SED_I) "s|BASEIMAGE|$(BASEIMAGE)|g" $(DOCKERFILE)
 	$(SED_I) "s|QEMUARCH|$(QEMUARCH)|g" $(DOCKERFILE)
-	$(SED_I) "s|DUMB_ARCH|$(DUMB_ARCH)|g" $(DOCKERFILE)
 
 ifeq ($(ARCH),amd64)
 	# When building "normally" for amd64, remove the whole line, it has no part in the amd64 image
@@ -127,16 +122,13 @@ else
 	$(SED_I) "s/CROSS_BUILD_//g" $(DOCKERFILE)
 endif
 
-	@$(DOCKER) build --no-cache --pull -t $(MULTI_ARCH_IMG):$(TAG) $(TEMP_DIR)/rootfs
+	echo "Building docker image..."
+	$(DOCKER) build --no-cache --pull -t $(MULTI_ARCH_IMAGE):$(TAG) $(TEMP_DIR)/rootfs
 
-ifeq ($(ARCH), amd64)
-	# This is for maintaining backward compatibility
-	@$(DOCKER) tag $(MULTI_ARCH_IMG):$(TAG) $(IMAGE):$(TAG)
-endif
 
 .PHONY: clean-container
 clean-container:
-	@$(DOCKER) rmi -f $(MULTI_ARCH_IMG):$(TAG) || true
+	@$(DOCKER) rmi -f $(MULTI_ARCH_IMAGE):$(TAG) || true
 
 .PHONY: register-qemu
 register-qemu:
@@ -148,10 +140,7 @@ push: .push-$(ARCH)
 
 .PHONY: .push-$(ARCH)
 .push-$(ARCH):
-	$(DOCKER) push $(MULTI_ARCH_IMG):$(TAG)
-ifeq ($(ARCH), amd64)
-	$(DOCKER) push $(IMAGE):$(TAG)
-endif
+	$(DOCKER) push $(MULTI_ARCH_IMAGE):$(TAG)
 
 .PHONY: build
 build:
diff --git a/build/run-in-docker.sh b/build/run-in-docker.sh
index 0e321d471e..01867e59bf 100755
--- a/build/run-in-docker.sh
+++ b/build/run-in-docker.sh
@@ -22,10 +22,20 @@ set -o errexit
 set -o nounset
 set -o pipefail
 
+# temporal directory for the fake SSL certificate
+SSL_VOLUME=$(mktemp -d)
+
+function cleanup {
+  rm -rf "${SSL_VOLUME}"
+}
+trap cleanup EXIT
+
 E2E_IMAGE=quay.io/kubernetes-ingress-controller/e2e:v06262019-ecce3fd7b
 
 DOCKER_OPTS=${DOCKER_OPTS:-}
 
+KUBE_ROOT=$(cd $(dirname "${BASH_SOURCE}")/.. && pwd -P)
+
 FLAGS=$@
 
 PKG=k8s.io/ingress-nginx
@@ -34,20 +44,21 @@ ARCH=$(go env GOARCH)
 MINIKUBE_PATH=${HOME}/.minikube
 MINIKUBE_VOLUME="-v ${MINIKUBE_PATH}:${MINIKUBE_PATH}"
 if [ ! -d "${MINIKUBE_PATH}" ]; then
-    echo "Minikube directory not found! Volume will be excluded from docker build."
-    MINIKUBE_VOLUME=""
+  echo "Minikube directory not found! Volume will be excluded from docker build."
+  MINIKUBE_VOLUME=""
 fi
 
-docker run                                       \
-  --tty                                          \
-  --rm                                           \
-  ${DOCKER_OPTS}                                 \
-  -v "${HOME}/.kube:${HOME}/.kube"               \
-  -v "${PWD}:/go/src/${PKG}"                     \
-  -v "${PWD}/.gocache:${HOME}/.cache/go-build"   \
-  -v "${PWD}/bin/${ARCH}:/go/bin/linux_${ARCH}"  \
-  -v "/var/run/docker.sock:/var/run/docker.sock" \
-  ${MINIKUBE_VOLUME}                             \
-  -w "/go/src/${PKG}"                            \
-  -u $(id -u ${USER}):$(id -g ${USER})           \
+docker run                                            \
+  --tty                                               \
+  --rm                                                \
+  ${DOCKER_OPTS}                                      \
+  -e GOCACHE="/go/src/${PKG}/.cache"                  \
+  -v "${HOME}/.kube:${HOME}/.kube"                    \
+  -v "${KUBE_ROOT}:/go/src/${PKG}"                    \
+  -v "${KUBE_ROOT}/bin/${ARCH}:/go/bin/linux_${ARCH}" \
+  -v "/var/run/docker.sock:/var/run/docker.sock"      \
+  -v "${SSL_VOLUME}:/etc/ingress-controller/ssl/"     \
+  ${MINIKUBE_VOLUME}                                  \
+  -w "/go/src/${PKG}"                                 \
+  -u $(id -u ${USER}):$(id -g ${USER})                \
   ${E2E_IMAGE} /bin/bash -c "${FLAGS}"
diff --git a/build/run-ingress-controller.sh b/build/run-ingress-controller.sh
index 5d9b9686e6..7b97a77c11 100755
--- a/build/run-ingress-controller.sh
+++ b/build/run-ingress-controller.sh
@@ -62,6 +62,7 @@ POD_NAMESPACE="invalid-namespace"
 POD_NAME="invalid-namespace"
 
 export TAG=local
+export IMAGE
 
 if [[ "${ARCH}" != "amd64" ]]; then
   echo -e "${BGREEN}Register ${RED}/usr/bin/qemu-ARCH-static${BGREEN} as the handler for binaries in multiple platforms${NC}"
@@ -102,7 +103,7 @@ docker run \
   -v "${SSL_VOLUME}:/etc/ingress-controller/ssl/" \
   -v "${HOME}/.kube:${HOME}/.kube:ro" \
   ${MINIKUBE_VOLUME} \
-  "${IMAGE}-${ARCH}:local" /nginx-ingress-controller \
+  "${IMAGE}:${TAG}" /nginx-ingress-controller \
   --update-status=false \
   --v=2 \
   --apiserver-host=http://0.0.0.0:8001 \
diff --git a/images/custom-error-pages/Makefile b/images/custom-error-pages/Makefile
index c816add8c2..79cfea3c2c 100644
--- a/images/custom-error-pages/Makefile
+++ b/images/custom-error-pages/Makefile
@@ -24,7 +24,6 @@ endif
 
 ARCH ?= $(shell go env GOARCH)
 GOARCH = ${ARCH}
-DUMB_ARCH = ${ARCH}
 
 BASEIMAGE?=alpine:3.9
 
diff --git a/internal/ingress/controller/nginx_test.go b/internal/ingress/controller/nginx_test.go
index 1921820e85..b2db2913dc 100644
--- a/internal/ingress/controller/nginx_test.go
+++ b/internal/ingress/controller/nginx_test.go
@@ -262,14 +262,14 @@ func TestConfigureDynamically(t *testing.T) {
 func TestConfigureCertificates(t *testing.T) {
 	listener, err := net.Listen("unix", nginx.StatusSocket)
 	if err != nil {
-		t.Errorf("crating unix listener: %s", err)
+		t.Fatalf("crating unix listener: %s", err)
 	}
 	defer listener.Close()
 	defer os.Remove(nginx.StatusSocket)
 
 	streamListener, err := net.Listen("unix", nginx.StreamSocket)
 	if err != nil {
-		t.Errorf("crating unix listener: %s", err)
+		t.Fatalf("crating unix listener: %s", err)
 	}
 	defer streamListener.Close()
 	defer os.Remove(nginx.StreamSocket)
diff --git a/test/e2e-image/Makefile b/test/e2e-image/Makefile
index f5ea8d88fa..1e385da6c5 100644
--- a/test/e2e-image/Makefile
+++ b/test/e2e-image/Makefile
@@ -1,9 +1,7 @@
-IMAGE=nginx-ingress-controller:e2e
-
-.PHONY: all container getbins clean
-
+.PHONY: all
 all: container
 
+.PHONY: container
 container:
 	make -C ../../ e2e-test-binary
 
@@ -12,8 +10,9 @@ container:
 	cp -r ../../deploy/cloud-generic .
 	cp -r ../../deploy/cluster-wide .
 
-	docker build -t $(IMAGE) .
+	docker build -t nginx-ingress-controller:e2e .
 
+.PHONY: clean
 clean:
 	rm -rf _cache e2e.test kubectl cluster ginkgo
-	docker rmi -f $(IMAGE) || true
+	docker rmi -f nginx-ingress-controller:e2e || true
diff --git a/test/e2e/run.sh b/test/e2e/run.sh
index 580a4b084f..a233560b84 100755
--- a/test/e2e/run.sh
+++ b/test/e2e/run.sh
@@ -56,6 +56,9 @@ echo "[dev-env] building container"
 make -C ${DIR}/../../ build container
 make -C ${DIR}/../../ e2e-test-image
 
+# Remove after https://github.com/kubernetes/ingress-nginx/pull/4271 is merged
+docker tag ${REGISTRY}/nginx-ingress-controller-${ARCH}:${TAG} ${REGISTRY}/nginx-ingress-controller:${TAG}
+
 echo "[dev-env] copying docker images to cluster..."
 kind load docker-image --name="${KIND_CLUSTER_NAME}" nginx-ingress-controller:e2e
 kind load docker-image --name="${KIND_CLUSTER_NAME}" ${REGISTRY}/nginx-ingress-controller:${TAG}