Skip to content

Commit 16b337e

Browse files
authored
Merge pull request #32 from ingwarr/issue31
Update testing to new approach fixes #31
2 parents 70949e5 + 0f8e0e5 commit 16b337e

33 files changed

+405
-896
lines changed

.dockerignore

Lines changed: 10 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,10 @@
1+
.git
2+
.terraform
3+
.terraform.d
4+
.kitchen
5+
terraform.tfstate.d
6+
test/fixtures/*/.terraform
7+
test/fixtures/*/terraform.tfstate.d
8+
examples/.kitchen
9+
examples/*/.terraform
10+
examples/*/terraform.tfstate.d

CONTRIBUTING.md

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
# Contributing
2+
3+
This document provides guidelines for contributing to the module.
4+
5+
## Dependencies
6+
7+
The following dependencies must be installed on the development system:
8+
9+
- [Docker Engine][docker-engine]
10+
- [Google Cloud SDK][google-cloud-sdk]
11+
- [make]
12+
13+
## Generating Documentation for Inputs and Outputs
14+
15+
The Inputs and Outputs tables in the READMEs of the root module,
16+
submodules, and example modules are automatically generated based on
17+
the `variables` and `outputs` of the respective modules. These tables
18+
must be refreshed if the module interfaces are changed.
19+
20+
### Execution
21+
22+
Run `make generate_docs` to generate new Inputs and Outputs tables.
23+
24+
## Integration Testing
25+
26+
Integration tests are used to verify the behaviour of the root module,
27+
submodules, and example modules. Additions, changes, and fixes should
28+
be accompanied with tests.
29+
30+
The integration tests are run using [Kitchen][kitchen],
31+
[Kitchen-Terraform][kitchen-terraform], and [InSpec][inspec]. These
32+
tools are packaged within a Docker image for convenience.
33+
34+
The general strategy for these tests is to verify the behaviour of the
35+
[example modules](./examples/), thus ensuring that the root module,
36+
submodules, and example modules are all functionally correct.
37+
38+
### Test Environment
39+
The easiest way to test the module is in an isolated test project. The setup for such a project is defined in [test/setup](./test/setup/) directory.
40+
41+
To use this setup, you need a service account with Project Creator access on a folder. Export the Service Account credentials to your environment like so:
42+
43+
```
44+
export SERVICE_ACCOUNT_JSON=$(< credentials.json)
45+
```
46+
47+
You will also need to set a few environment variables:
48+
```
49+
export TF_VAR_org_id="your_org_id"
50+
export TF_VAR_folder_id="your_folder_id"
51+
export TF_VAR_billing_account="your_billing_account_id"
52+
```
53+
54+
With these settings in place, you can prepare a test project using Docker:
55+
```
56+
make docker_test_prepare
57+
```
58+
59+
### Noninteractive Execution
60+
61+
Run `make docker_test_integration` to test all of the example modules
62+
noninteractively, using the prepared test project.
63+
64+
### Interactive Execution
65+
66+
1. Run `make docker_run` to start the testing Docker container in
67+
interactive mode.
68+
69+
1. Run `kitchen_do create <EXAMPLE_NAME>` to initialize the working
70+
directory for an example module.
71+
72+
1. Run `kitchen_do converge <EXAMPLE_NAME>` to apply the example module.
73+
74+
1. Run `kitchen_do verify <EXAMPLE_NAME>` to test the example module.
75+
76+
1. Run `kitchen_do destroy <EXAMPLE_NAME>` to destroy the example module
77+
state.
78+
79+
## Linting and Formatting
80+
81+
Many of the files in the repository can be linted or formatted to
82+
maintain a standard of quality.
83+
84+
### Execution
85+
86+
Run `make docker_test_lint`.
87+
88+
[docker-engine]: https://www.docker.com/products/docker-engine
89+
[flake8]: http://flake8.pycqa.org/en/latest/
90+
[gofmt]: https://golang.org/cmd/gofmt/
91+
[google-cloud-sdk]: https://cloud.google.com/sdk/install
92+
[hadolint]: https://github.com/hadolint/hadolint
93+
[inspec]: https://inspec.io/
94+
[kitchen-terraform]: https://github.com/newcontext-oss/kitchen-terraform
95+
[kitchen]: https://kitchen.ci/
96+
[make]: https://en.wikipedia.org/wiki/Make_(software)
97+
[shellcheck]: https://www.shellcheck.net/
98+
[terraform-docs]: https://github.com/segmentio/terraform-docs
99+
[terraform]: https://terraform.io/

Gemfile

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,4 +14,4 @@
1414

1515
source 'https://rubygems.org/' do
1616
gem 'kitchen-terraform', '~> 4.9'
17-
end
17+
end

Makefile

Lines changed: 50 additions & 115 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google LLC
1+
# Copyright 2019 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -18,133 +18,68 @@
1818
# Make will use bash instead of sh
1919
SHELL := /usr/bin/env bash
2020

21-
# Docker build config variables
22-
CREDENTIALS_PATH ?= /cft/workdir/credentials.json
23-
DOCKER_ORG := gcr.io/cloud-foundation-cicd
24-
DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 2.3.0
25-
DOCKER_REPO_BASE_KITCHEN_TERRAFORM := ${DOCKER_ORG}/cft/kitchen-terraform:${DOCKER_TAG_BASE_KITCHEN_TERRAFORM}
21+
DOCKER_TAG_VERSION_DEVELOPER_TOOLS := 0.1.0
22+
DOCKER_IMAGE_DEVELOPER_TOOLS := cft/developer-tools
23+
REGISTRY_URL := gcr.io/cloud-foundation-cicd
2624

27-
28-
# All is the first target in the file so it will get picked up when you just run 'make' on its own
29-
.PHONY: all
30-
all: check generate_docs
31-
32-
# Run all available linters
33-
.PHONY: check
34-
check: check_shell check_python check_golang check_terraform check_base_files test_check_headers check_headers check_trailing_whitespace
35-
36-
# The .PHONY directive tells make that this isn't a real target and so
37-
# the presence of a file named 'check_shell' won't cause this target to stop
38-
# working
39-
.PHONY: check_shell
40-
check_shell:
41-
@source test/make.sh && check_shell
42-
43-
.PHONY: check_python
44-
check_python:
45-
@source test/make.sh && check_python
46-
47-
.PHONY: check_golang
48-
check_golang:
49-
@source test/make.sh && golang
50-
51-
.PHONY: check_terraform
52-
check_terraform:
53-
@source test/make.sh && check_terraform
54-
55-
.PHONY: check_base_files
56-
check_base_files:
57-
@source test/make.sh && basefiles
58-
59-
.PHONY: check_trailing_whitespace
60-
check_trailing_whitespace:
61-
@source test/make.sh && check_trailing_whitespace
62-
63-
.PHONY: test_check_headers
64-
test_check_headers:
65-
@echo "Testing the validity of the header check"
66-
@python test/test_verify_boilerplate.py
67-
68-
#.PHONY: check_headers
69-
#check_headers:
70-
# @source test/make.sh && check_headers
71-
.PHONY: check_headers
72-
check_headers: ## Check that source files have appropriate boilerplate
73-
@echo "Checking file headers"
74-
@python test/verify_boilerplate.py
75-
76-
77-
# Integration tests
78-
.PHONY: test_integration
79-
test_integration:
80-
test/ci_integration.sh
81-
82-
.PHONY: generate_docs
83-
generate_docs:
84-
@source test/make.sh && generate_docs
85-
86-
# Versioning
87-
.PHONY: version
88-
version:
89-
@source helpers/version-repo.sh
90-
91-
# Run docker
25+
# Enter docker container for local development
9226
.PHONY: docker_run
9327
docker_run:
9428
docker run --rm -it \
95-
-e PROJECT_ID \
9629
-e SERVICE_ACCOUNT_JSON \
97-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
98-
-v $(CURDIR):/cft/workdir \
99-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
100-
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && exec /bin/bash"
30+
-v "$(CURDIR)":/workspace \
31+
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
32+
/bin/bash
10133

102-
.PHONY: docker_create
103-
docker_create:
34+
# Execute prepare tests within the docker container
35+
.PHONY: docker_test_prepare
36+
docker_test_prepare:
10437
docker run --rm -it \
105-
-e PROJECT_ID \
10638
-e SERVICE_ACCOUNT_JSON \
107-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
108-
-v $(CURDIR):/cft/workdir \
109-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
110-
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && kitchen create"
111-
112-
.PHONY: docker_converge
113-
docker_converge:
39+
-e TF_VAR_org_id \
40+
-e TF_VAR_folder_id \
41+
-e TF_VAR_billing_account \
42+
-v "$(CURDIR)":/workspace \
43+
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
44+
/usr/local/bin/execute_with_credentials.sh prepare_environment
45+
46+
# Clean up test environment within the docker container
47+
.PHONY: docker_test_cleanup
48+
docker_test_cleanup:
11449
docker run --rm -it \
115-
-e PROJECT_ID \
11650
-e SERVICE_ACCOUNT_JSON \
117-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
118-
-v $(CURDIR):/cft/workdir \
119-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
120-
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && kitchen converge"
121-
122-
.PHONY: docker_verify
123-
docker_verify:
51+
-e TF_VAR_org_id \
52+
-e TF_VAR_folder_id \
53+
-e TF_VAR_billing_account \
54+
-v "$(CURDIR)":/workspace \
55+
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
56+
/usr/local/bin/execute_with_credentials.sh cleanup_environment
57+
58+
# Execute integration tests within the docker container
59+
.PHONY: docker_test_integration
60+
docker_test_integration:
12461
docker run --rm -it \
125-
-e PROJECT_ID \
12662
-e SERVICE_ACCOUNT_JSON \
127-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
128-
-v $(CURDIR):/cft/workdir \
129-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
130-
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && kitchen verify"
63+
-v "$(CURDIR)":/workspace \
64+
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
65+
/usr/local/bin/test_integration.sh
13166

132-
.PHONY: docker_destroy
133-
docker_destroy:
67+
# Execute lint tests within the docker container
68+
.PHONY: docker_test_lint
69+
docker_test_lint:
13470
docker run --rm -it \
135-
-e PROJECT_ID \
136-
-e SERVICE_ACCOUNT_JSON \
137-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
138-
-v $(CURDIR):/cft/workdir \
139-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
140-
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && kitchen destroy"
71+
-v "$(CURDIR)":/workspace \
72+
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
73+
/usr/local/bin/test_lint.sh
14174

142-
.PHONY: test_integration_docker
143-
test_integration_docker:
75+
# Generate documentation
76+
.PHONY: docker_generate_docs
77+
docker_generate_docs:
14478
docker run --rm -it \
145-
-e PROJECT_ID \
146-
-e SERVICE_ACCOUNT_JSON \
147-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
148-
-v $(CURDIR):/cft/workdir \
149-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
150-
/bin/bash -c "cd /cft/workdir && source test/ci_integration.sh && setup_environment && make test_integration"
79+
-v "$(CURDIR)":/workspace \
80+
$(REGISTRY_URL)/${DOCKER_IMAGE_DEVELOPER_TOOLS}:${DOCKER_TAG_VERSION_DEVELOPER_TOOLS} \
81+
/bin/bash -c 'source /usr/local/bin/task_helper_functions.sh && generate_docs'
82+
83+
# Alias for backwards compatibility
84+
.PHONY: generate_docs
85+
generate_docs: docker_generate_docs

build/int.cloudbuild.yaml

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
# Copyright 2019 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'
Lines changed: 11 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Copyright 2018 Google LLC
1+
# Copyright 2019 Google LLC
22
#
33
# Licensed under the Apache License, Version 2.0 (the "License");
44
# you may not use this file except in compliance with the License.
@@ -12,16 +12,13 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15-
ARG BASE_IMAGE
16-
17-
FROM $BASE_IMAGE
18-
19-
RUN apk add --no-cache \
20-
ca-certificates=20171114-r3
21-
22-
WORKDIR $APP_BASE_DIR/workdir
23-
COPY Gemfile .
24-
RUN bundle install
25-
26-
RUN gcloud components install beta --quiet
27-
RUN gcloud components install alpha --quiet
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'

0 commit comments

Comments
 (0)