Skip to content

Commit 1dca8f8

Browse files
authored
Merge pull request #61 from terraform-google-modules/feature/add-cloud-build
Run tests in Cloud Build
2 parents 3c06b04 + da96367 commit 1dca8f8

File tree

24 files changed

+361
-272
lines changed

24 files changed

+361
-272
lines changed

CONTRIBUTING.md

Lines changed: 137 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,137 @@
1+
## File structure
2+
The project has the following folders and files:
3+
4+
- /: root folder
5+
- /examples: examples for using this module
6+
- /test: Folders with files for testing the module (see Testing section on this file)
7+
- /main.tf: main file for this module, contains all the resources to create
8+
- /variables.tf: all the variables for the module
9+
- /output.tf: the outputs of the module
10+
- /README.md: this file
11+
12+
## Testing and documentation generation
13+
14+
### Requirements
15+
- [docker](https://docker.com/)
16+
17+
### Integration testing
18+
##### Terraform integration tests
19+
20+
The module's integration tests are designed to be run within a Docker
21+
container containing all the dependencies required for testing. The
22+
`docker_test_integration` make target wraps this behavior but requires the
23+
following configuration to execute properly:
24+
25+
- Configure a service account with the roles documented above and export the JSON key to the `SERVICE_ACCOUNT_JSON` environment variable
26+
27+
export SERVICE_ACCOUNT_JSON=$(< /path/to/credentials.json)
28+
29+
- Create `test/fixtures/shared/terraform.tfvars` and populate with the required Terraform input variables (see `test/fixtures/shared/terraform.tfvars.sample` for more information)
30+
31+
Once those steps have been completed run `make docker_test_integration` from
32+
the root of the repository to execute the tests within the `project_id`
33+
provided. Infrastructure from `test/fixtures/*` will be provisioned,
34+
integration tests from `test/integration/*` will be executed, and the
35+
infrastructure will be deprovisioned to complete the process.
36+
37+
### Autogeneration of documentation from .tf files
38+
Run
39+
```
40+
make generate_docs
41+
```
42+
43+
### Lint testing
44+
45+
Lint testing is also performed within a Docker container containing all the
46+
dependencies required for lint tests. Execute those tests by running `make
47+
docker_test_lint` from the root of the repository.
48+
49+
Successful output looks similar to the following:
50+
51+
```
52+
Checking for trailing whitespace
53+
Checking for missing newline at end of file
54+
Running shellcheck
55+
Checking file headers
56+
Running flake8
57+
Running terraform fmt
58+
terraform fmt -diff -check=true -write=false .
59+
terraform fmt -diff -check=true -write=false ./codelabs/simple
60+
terraform fmt -diff -check=true -write=false ./examples/delete_default_gateway_routes
61+
terraform fmt -diff -check=true -write=false ./examples/multi_vpc
62+
terraform fmt -diff -check=true -write=false ./examples/secondary_ranges
63+
terraform fmt -diff -check=true -write=false ./examples/simple_project
64+
terraform fmt -diff -check=true -write=false ./examples/simple_project_with_regional_network
65+
terraform fmt -diff -check=true -write=false ./examples/submodule_firewall
66+
terraform fmt -diff -check=true -write=false ./examples/submodule_svpc_access
67+
terraform fmt -diff -check=true -write=false ./modules/fabric-net-firewall
68+
terraform fmt -diff -check=true -write=false ./modules/fabric-net-svpc-access
69+
terraform fmt -diff -check=true -write=false ./test/fixtures/all_examples
70+
terraform fmt -diff -check=true -write=false ./test/fixtures/delete_default_gateway_routes
71+
terraform fmt -diff -check=true -write=false ./test/fixtures/multi_vpc
72+
terraform fmt -diff -check=true -write=false ./test/fixtures/secondary_ranges
73+
terraform fmt -diff -check=true -write=false ./test/fixtures/shared
74+
terraform fmt -diff -check=true -write=false ./test/fixtures/simple_project
75+
terraform fmt -diff -check=true -write=false ./test/fixtures/simple_project_with_regional_network
76+
terraform fmt -diff -check=true -write=false ./test/fixtures/simulated_ci_environment
77+
terraform fmt -diff -check=true -write=false ./test/fixtures/submodule_firewall
78+
Running terraform validate
79+
terraform_validate .
80+
Success! The configuration is valid.
81+
82+
terraform_validate ./codelabs/simple
83+
Success! The configuration is valid.
84+
85+
terraform_validate ./examples/delete_default_gateway_routes
86+
Success! The configuration is valid.
87+
88+
terraform_validate ./examples/multi_vpc
89+
Success! The configuration is valid.
90+
91+
terraform_validate ./examples/secondary_ranges
92+
Success! The configuration is valid.
93+
94+
terraform_validate ./examples/simple_project
95+
Success! The configuration is valid.
96+
97+
terraform_validate ./examples/simple_project_with_regional_network
98+
Success! The configuration is valid.
99+
100+
terraform_validate ./examples/submodule_firewall
101+
Success! The configuration is valid.
102+
103+
terraform_validate ./examples/submodule_svpc_access
104+
Success! The configuration is valid.
105+
106+
terraform_validate ./modules/fabric-net-firewall
107+
Success! The configuration is valid.
108+
109+
terraform_validate ./modules/fabric-net-svpc-access
110+
Success! The configuration is valid.
111+
112+
terraform_validate ./test/fixtures/all_examples
113+
Success! The configuration is valid.
114+
115+
terraform_validate ./test/fixtures/delete_default_gateway_routes
116+
Success! The configuration is valid.
117+
118+
terraform_validate ./test/fixtures/multi_vpc
119+
Success! The configuration is valid.
120+
121+
terraform_validate ./test/fixtures/secondary_ranges
122+
Success! The configuration is valid.
123+
124+
terraform_validate ./test/fixtures/simple_project
125+
Success! The configuration is valid.
126+
127+
terraform_validate ./test/fixtures/simple_project_with_regional_network
128+
Success! The configuration is valid.
129+
130+
terraform_validate ./test/fixtures/simulated_ci_environment
131+
Success! The configuration is valid.
132+
133+
terraform_validate ./test/fixtures/submodule_firewall
134+
Success! The configuration is valid.
135+
```
136+
137+
[terraform-provider-google]: https://github.com/terraform-providers/terraform-provider-google

Makefile

Lines changed: 25 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,7 @@
1515
# Make will use bash instead of sh
1616
SHELL := /usr/bin/env bash
1717

18-
DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.0.1
18+
DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.1.0
1919
DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools
2020
REGISTRY_URL := gcr.io/cloud-foundation-cicd
2121

@@ -28,6 +28,30 @@ docker_run:
2828
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
2929
/bin/bash
3030

31+
# Execute prepare tests within the docker container
32+
.PHONY: docker_test_prepare
33+
docker_test_prepare:
34+
docker run --rm -it \
35+
-e SERVICE_ACCOUNT_JSON \
36+
-e TF_VAR_org_id \
37+
-e TF_VAR_folder_id \
38+
-e TF_VAR_billing_account \
39+
-v $(CURDIR):/workspace \
40+
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
41+
/usr/local/bin/execute_with_credentials.sh prepare_environment
42+
43+
# Clean up test environment within the docker container
44+
.PHONY: docker_test_cleanup
45+
docker_test_prepare:
46+
docker run --rm -it \
47+
-e SERVICE_ACCOUNT_JSON \
48+
-e TF_VAR_org_id \
49+
-e TF_VAR_folder_id \
50+
-e TF_VAR_billing_account \
51+
-v $(CURDIR):/workspace \
52+
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
53+
/usr/local/bin/execute_with_credentials.sh cleanup_environment
54+
3155
# Execute integration tests within the docker container
3256
.PHONY: docker_test_integration
3357
docker_test_integration:

README.md

Lines changed: 0 additions & 138 deletions
Original file line numberDiff line numberDiff line change
@@ -158,141 +158,3 @@ If you are going to manage a Shared VPC, you must have either:
158158
In order to operate with the Service Account you must activate the following API on the project where the Service Account was created:
159159

160160
- Compute Engine API - compute.googleapis.com
161-
162-
## File structure
163-
The project has the following folders and files:
164-
165-
- /: root folder
166-
- /examples: examples for using this module
167-
- /test: Folders with files for testing the module (see Testing section on this file)
168-
- /main.tf: main file for this module, contains all the resources to create
169-
- /variables.tf: all the variables for the module
170-
- /output.tf: the outputs of the module
171-
- /README.md: this file
172-
173-
## Testing and documentation generation
174-
175-
### Requirements
176-
- [docker](https://docker.com/)
177-
178-
### Integration testing
179-
##### Terraform integration tests
180-
181-
The module's integration tests are designed to be run within a Docker
182-
container containing all the dependencies required for testing. The
183-
`docker_test_integration` make target wraps this behavior but requires the
184-
following configuration to execute properly:
185-
186-
- Configure a service account with the roles documented above and export the JSON key to the `SERVICE_ACCOUNT_JSON` environment variable
187-
188-
export SERVICE_ACCOUNT_JSON=$(< /path/to/credentials.json)
189-
190-
- Create `test/fixtures/shared/terraform.tfvars` and populate with the required Terraform input variables (see `test/fixtures/shared/terraform.tfvars.sample` for more information)
191-
192-
Once those steps have been completed run `make docker_test_integration` from
193-
the root of the repository to execute the tests within the `project_id`
194-
provided. Infrastructure from `test/fixtures/*` will be provisioned,
195-
integration tests from `test/integration/*` will be executed, and the
196-
infrastructure will be deprovisioned to complete the process.
197-
198-
### Autogeneration of documentation from .tf files
199-
Run
200-
```
201-
make generate_docs
202-
```
203-
204-
### Lint testing
205-
206-
Lint testing is also performed within a Docker container containing all the
207-
dependencies required for lint tests. Execute those tests by running `make
208-
docker_test_lint` from the root of the repository.
209-
210-
Successful output looks similar to the following:
211-
212-
```
213-
Checking for trailing whitespace
214-
Checking for missing newline at end of file
215-
Running shellcheck
216-
Checking file headers
217-
Running flake8
218-
Running terraform fmt
219-
terraform fmt -diff -check=true -write=false .
220-
terraform fmt -diff -check=true -write=false ./codelabs/simple
221-
terraform fmt -diff -check=true -write=false ./examples/delete_default_gateway_routes
222-
terraform fmt -diff -check=true -write=false ./examples/multi_vpc
223-
terraform fmt -diff -check=true -write=false ./examples/secondary_ranges
224-
terraform fmt -diff -check=true -write=false ./examples/simple_project
225-
terraform fmt -diff -check=true -write=false ./examples/simple_project_with_regional_network
226-
terraform fmt -diff -check=true -write=false ./examples/submodule_firewall
227-
terraform fmt -diff -check=true -write=false ./examples/submodule_svpc_access
228-
terraform fmt -diff -check=true -write=false ./modules/fabric-net-firewall
229-
terraform fmt -diff -check=true -write=false ./modules/fabric-net-svpc-access
230-
terraform fmt -diff -check=true -write=false ./test/fixtures/all_examples
231-
terraform fmt -diff -check=true -write=false ./test/fixtures/delete_default_gateway_routes
232-
terraform fmt -diff -check=true -write=false ./test/fixtures/multi_vpc
233-
terraform fmt -diff -check=true -write=false ./test/fixtures/secondary_ranges
234-
terraform fmt -diff -check=true -write=false ./test/fixtures/shared
235-
terraform fmt -diff -check=true -write=false ./test/fixtures/simple_project
236-
terraform fmt -diff -check=true -write=false ./test/fixtures/simple_project_with_regional_network
237-
terraform fmt -diff -check=true -write=false ./test/fixtures/simulated_ci_environment
238-
terraform fmt -diff -check=true -write=false ./test/fixtures/submodule_firewall
239-
Running terraform validate
240-
terraform_validate .
241-
Success! The configuration is valid.
242-
243-
terraform_validate ./codelabs/simple
244-
Success! The configuration is valid.
245-
246-
terraform_validate ./examples/delete_default_gateway_routes
247-
Success! The configuration is valid.
248-
249-
terraform_validate ./examples/multi_vpc
250-
Success! The configuration is valid.
251-
252-
terraform_validate ./examples/secondary_ranges
253-
Success! The configuration is valid.
254-
255-
terraform_validate ./examples/simple_project
256-
Success! The configuration is valid.
257-
258-
terraform_validate ./examples/simple_project_with_regional_network
259-
Success! The configuration is valid.
260-
261-
terraform_validate ./examples/submodule_firewall
262-
Success! The configuration is valid.
263-
264-
terraform_validate ./examples/submodule_svpc_access
265-
Success! The configuration is valid.
266-
267-
terraform_validate ./modules/fabric-net-firewall
268-
Success! The configuration is valid.
269-
270-
terraform_validate ./modules/fabric-net-svpc-access
271-
Success! The configuration is valid.
272-
273-
terraform_validate ./test/fixtures/all_examples
274-
Success! The configuration is valid.
275-
276-
terraform_validate ./test/fixtures/delete_default_gateway_routes
277-
Success! The configuration is valid.
278-
279-
terraform_validate ./test/fixtures/multi_vpc
280-
Success! The configuration is valid.
281-
282-
terraform_validate ./test/fixtures/secondary_ranges
283-
Success! The configuration is valid.
284-
285-
terraform_validate ./test/fixtures/simple_project
286-
Success! The configuration is valid.
287-
288-
terraform_validate ./test/fixtures/simple_project_with_regional_network
289-
Success! The configuration is valid.
290-
291-
terraform_validate ./test/fixtures/simulated_ci_environment
292-
Success! The configuration is valid.
293-
294-
terraform_validate ./test/fixtures/submodule_firewall
295-
Success! The configuration is valid.
296-
```
297-
298-
[terraform-provider-google]: https://github.com/terraform-providers/terraform-provider-google

build/int.cloudbuild.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
timeout: 3600s
16+
steps:
17+
- id: prepare
18+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
19+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && prepare_environment']
20+
env:
21+
- 'TF_VAR_org_id=$_ORG_ID'
22+
- 'TF_VAR_folder_id=$_FOLDER_ID'
23+
- 'TF_VAR_billing_account=$_BILLING_ACCOUNT'
24+
- id: create
25+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
26+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do create']
27+
- id: converge
28+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
29+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do converge']
30+
- id: verify
31+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
32+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do verify']
33+
- id: destroy
34+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
35+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy']
36+
tags:
37+
- 'ci'
38+
- 'integration'
39+
substitutions:
40+
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
41+
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.1.0'

build/lint.cloudbuild.yaml

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
# Copyright 2018 Google LLC
2+
#
3+
# Licensed under the Apache License, Version 2.0 (the "License");
4+
# you may not use this file except in compliance with the License.
5+
# You may obtain a copy of the License at
6+
#
7+
# https://www.apache.org/licenses/LICENSE-2.0
8+
#
9+
# Unless required by applicable law or agreed to in writing, software
10+
# distributed under the License is distributed on an "AS IS" BASIS,
11+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
# See the License for the specific language governing permissions and
13+
# limitations under the License.
14+
15+
steps:
16+
- name: 'gcr.io/cloud-foundation-cicd/cft/developer-tools:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
17+
id: 'lint'
18+
args: ['/usr/local/bin/test_lint.sh']
19+
tags:
20+
- 'ci'
21+
- 'lint'
22+
substitutions:
23+
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
24+
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.1.0'

test/.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
source.sh

test/fixtures/delete_default_gateway_routes/terraform.tfvars

Lines changed: 0 additions & 1 deletion
This file was deleted.

0 commit comments

Comments
 (0)