11# Set default shell to bash
22SHELL := /bin/bash -o pipefail
33
4- BUILD_TOOLS_VERSION ?= v0.12.0
4+ BUILD_TOOLS_VERSION ?= v0.15.2
55BUILD_TOOLS_DOCKER_REPO ?= mineiros/build-tools
66BUILD_TOOLS_DOCKER_IMAGE ?= ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION}
77
@@ -32,29 +32,45 @@ endif
3232
3333GIT_TOPLEVEl = $(shell git rev-parse --show-toplevel)
3434
35- # generic docker run flags
35+ # Generic docker run flags
3636DOCKER_RUN_FLAGS += -v ${GIT_TOPLEVEl}:/build
3737DOCKER_RUN_FLAGS += --rm
3838DOCKER_RUN_FLAGS += -e TF_IN_AUTOMATION
39-
40- # if SSH_AUTH_SOCK is defined we are likely referencing private repositories
41- # for depending terrfaorm modules or other depdendencies
42- # so we pass credentials to the docker container when running tests or pre-commit hooks
39+ # If TF_VERSION is defined, TFSwitch will switch to the desired version on
40+ # container startup. If TF_VERSION is omitted, the default version installed
41+ # inside the docker image will be used.
42+ DOCKER_RUN_FLAGS += -e TF_VERSION
43+
44+ # If SSH_AUTH_SOCK is set, we forward the SSH agent of the host system into
45+ # the docker container. This is useful when working with private repositories
46+ # and dependencies that might need to be cloned inside the container (e.g.
47+ # private Terraform modules).
4348ifdef SSH_AUTH_SOCK
4449 DOCKER_SSH_FLAGS += -e SSH_AUTH_SOCK=/ssh-agent
4550 DOCKER_SSH_FLAGS += -v ${SSH_AUTH_SOCK}:/ssh-agent
4651endif
4752
48- # if AWS_ACCESS_KEY_ID is defined we are likely running inside an AWS provider module
49- # so we pass credentials to the docker container when running tests
53+ # If AWS_ACCESS_KEY_ID is defined, we are likely running inside an AWS provider
54+ # module. To enable AWS authentication inside the docker container, we inject
55+ # the relevant environment variables.
5056ifdef AWS_ACCESS_KEY_ID
5157 DOCKER_AWS_FLAGS += -e AWS_ACCESS_KEY_ID
5258 DOCKER_AWS_FLAGS += -e AWS_SECRET_ACCESS_KEY
5359 DOCKER_AWS_FLAGS += -e AWS_SESSION_TOKEN
5460endif
5561
56- # if GITHUB_OWNER is defined we are running inside a github provider module
57- # so we pass credentials to the docker container when running tests
62+ # If GOOGLE_CREDENTIALS is defined, we are likely running inside a GCP provider
63+ # module. To enable GCP authentication inside the docker container, we inject
64+ # the relevant environment variables (service-account key file).
65+ ifdef GOOGLE_CREDENTIALS
66+ DOCKER_GCP_FLAGS += -e GOOGLE_CREDENTIALS
67+ DOCKER_GCP_FLAGS += -e TEST_GCP_PROJECT
68+ DOCKER_GCP_FLAGS += -e TEST_GCP_ORG_DOMAIN
69+ endif
70+
71+ # If GITHUB_OWNER is defined, we are likely running inside a GitHub provider
72+ # module. To enable GitHub authentication inside the docker container,
73+ # we inject the relevant environment variables.
5874ifdef GITHUB_OWNER
5975 DOCKER_GITHUB_FLAGS += -e GITHUB_TOKEN
6076 DOCKER_GITHUB_FLAGS += -e GITHUB_OWNER
@@ -70,28 +86,58 @@ template/adjust:
7086 @find . $(FILTER ) -exec sed -i -e " s,terraform-module-template,$$ {PWD##*/},g" {} \;
7187
7288# # Run pre-commit hooks inside a build-tools docker container.
89+ .PHONY : test/docker/pre-commit
90+ test/docker/pre-commit : DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
91+ test/docker/pre-commit : DOCKER_FLAGS += -e NOCOLOR=1
92+ test/docker/pre-commit :
93+ $(call docker-run,make test/pre-commit)
94+
95+ # # Run all Go tests inside a build-tools docker container. This is complementary to running 'go test ./test/...'.
96+ .PHONY : test/docker/unit-tests
97+ test/docker/unit-tests : DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
98+ test/docker/unit-tests : DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS}
99+ test/docker/unit-tests : DOCKER_FLAGS += ${DOCKER_AWS_FLAGS}
100+ test/docker/unit-tests : DOCKER_FLAGS += ${DOCKER_GCP_FLAGS}
101+ test/docker/unit-tests : DOCKER_FLAGS += $(shell env | grep ^TF_VAR_ | cut -d = -f 1 | xargs -i printf ' -e {}')
102+ test/docker/unit-tests : DOCKER_FLAGS += -e TF_DATA_DIR=.terratest
103+ test/docker/unit-tests : DOCKER_FLAGS += -e NOCOLOR=1
104+ test/docker/unit-tests : TEST ?= "TestUnit"
105+ test/docker/unit-tests :
106+ @echo " ${YELLOW} [TEST] ${GREEN} Start Running Go Tests in Docker Container.${RESET} "
107+ $(call docker-run,make test/unit-tests)
108+
109+ # # Run pre-commit hooks.
73110.PHONY : test/pre-commit
74111test/pre-commit : DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
75112test/pre-commit :
76- $(call docker-run ,pre-commit run -a)
113+ $(call quiet-command ,pre-commit run -a)
77114
78- # # Run all Go tests inside a build-tools docker container. This is complementary to running 'go test ./test/...'.
79- .PHONY : test/unit-tests
80- test/unit-tests : DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
81- test/unit-tests : DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS}
82- test/unit-tests : DOCKER_FLAGS += ${DOCKER_AWS_FLAGS}
115+ # # Run all unit tests.
116+ .PHONY : test/docker/unit-tests
83117test/unit-tests : TEST ?= "TestUnit"
84118test/unit-tests :
85- @echo " ${YELLOW} [TEST] ${GREEN} Start Running Go Tests in Docker Container.${RESET} "
86- $(call go-test,./test -run $(TEST ) )
119+ @echo " ${YELLOW} [TEST] ${GREEN} Start Running unit tests.${RESET} "
120+ $(call quiet-command,cd test ; go test -v -count 1 -timeout 45m -parallel 128 -run $(TEST ) )
121+
122+ # # Generate README.md with Terradoc
123+ .PHONY : terradoc
124+ terradoc :
125+ $(call quiet-command,terradoc generate -o README.md README.tfdoc.hcl)
126+
127+ # # Generate shared configuration for tests
128+ .PHONY : terramate
129+ terramate :
130+ $(call quiet-command,terramate generate)
87131
88132# # Clean up cache and temporary files
89133.PHONY : clean
90134clean :
91135 $(call rm-command,.terraform)
136+ $(call rm-command,.terratest)
92137 $(call rm-command,.terraform.lock.hcl)
93138 $(call rm-command,* .tfplan)
94139 $(call rm-command,* /* /.terraform)
140+ $(call rm-command,* /* /.terratest)
95141 $(call rm-command,* /* /* .tfplan)
96142 $(call rm-command,* /* /.terraform.lock.hcl)
97143
@@ -108,16 +154,10 @@ help:
108154 } \
109155 { lastLine = $$ 0 }' $(MAKEFILE_LIST)
110156
111- # # Generate README.md with Terradoc
112- .PHONY : terradoc
113- terradoc :
114- $(call quiet-command,terradoc -o README.md README.tfdoc.hcl)
115-
116- # define helper functions
157+ # Define helper functions
117158DOCKER_FLAGS += ${DOCKER_RUN_FLAGS}
118159DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE}
119160
120161quiet-command = $(if ${V},${1},$(if ${2},@echo ${2} && ${1}, @${1}) )
121162docker-run = $(call quiet-command,${DOCKER_RUN_CMD} ${1} | cat,"${YELLOW}[DOCKER RUN] ${GREEN}${1}${RESET}")
122- go-test = $(call quiet-command,${DOCKER_RUN_CMD} go test -v -count 1 -timeout 45m -parallel 128 ${1} | cat,"${YELLOW}[TEST] ${GREEN}${1}${RESET}")
123163rm-command = $(call quiet-command,rm -rf ${1},"${YELLOW}[CLEAN] ${GREEN}${1}${RESET}")
0 commit comments