1
1
# Set default shell to bash
2
2
SHELL := /bin/bash -o pipefail
3
3
4
- BUILD_TOOLS_VERSION ?= v0.12.0
4
+ BUILD_TOOLS_VERSION ?= v0.15.2
5
5
BUILD_TOOLS_DOCKER_REPO ?= mineiros/build-tools
6
6
BUILD_TOOLS_DOCKER_IMAGE ?= ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION}
7
7
@@ -32,29 +32,45 @@ endif
32
32
33
33
GIT_TOPLEVEl = $(shell git rev-parse --show-toplevel)
34
34
35
- # generic docker run flags
35
+ # Generic docker run flags
36
36
DOCKER_RUN_FLAGS += -v ${GIT_TOPLEVEl}:/build
37
37
DOCKER_RUN_FLAGS += --rm
38
38
DOCKER_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).
43
48
ifdef SSH_AUTH_SOCK
44
49
DOCKER_SSH_FLAGS += -e SSH_AUTH_SOCK=/ssh-agent
45
50
DOCKER_SSH_FLAGS += -v ${SSH_AUTH_SOCK}:/ssh-agent
46
51
endif
47
52
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.
50
56
ifdef AWS_ACCESS_KEY_ID
51
57
DOCKER_AWS_FLAGS += -e AWS_ACCESS_KEY_ID
52
58
DOCKER_AWS_FLAGS += -e AWS_SECRET_ACCESS_KEY
53
59
DOCKER_AWS_FLAGS += -e AWS_SESSION_TOKEN
54
60
endif
55
61
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.
58
74
ifdef GITHUB_OWNER
59
75
DOCKER_GITHUB_FLAGS += -e GITHUB_TOKEN
60
76
DOCKER_GITHUB_FLAGS += -e GITHUB_OWNER
@@ -70,28 +86,58 @@ template/adjust:
70
86
@find . $(FILTER ) -exec sed -i -e " s,terraform-module-template,$$ {PWD##*/},g" {} \;
71
87
72
88
# # 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.
73
110
.PHONY : test/pre-commit
74
111
test/pre-commit : DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
75
112
test/pre-commit :
76
- $(call docker-run ,pre-commit run -a)
113
+ $(call quiet-command ,pre-commit run -a)
77
114
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
83
117
test/unit-tests : TEST ?= "TestUnit"
84
118
test/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)
87
131
88
132
# # Clean up cache and temporary files
89
133
.PHONY : clean
90
134
clean :
91
135
$(call rm-command,.terraform)
136
+ $(call rm-command,.terratest)
92
137
$(call rm-command,.terraform.lock.hcl)
93
138
$(call rm-command,* .tfplan)
94
139
$(call rm-command,* /* /.terraform)
140
+ $(call rm-command,* /* /.terratest)
95
141
$(call rm-command,* /* /* .tfplan)
96
142
$(call rm-command,* /* /.terraform.lock.hcl)
97
143
@@ -108,16 +154,10 @@ help:
108
154
} \
109
155
{ lastLine = $$ 0 }' $(MAKEFILE_LIST)
110
156
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
117
158
DOCKER_FLAGS += ${DOCKER_RUN_FLAGS}
118
159
DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE}
119
160
120
161
quiet-command = $(if ${V},${1},$(if ${2},@echo ${2} && ${1}, @${1}) )
121
162
docker-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}")
123
163
rm-command = $(call quiet-command,rm -rf ${1},"${YELLOW}[CLEAN] ${GREEN}${1}${RESET}")
0 commit comments