Skip to content

Commit a45f1d1

Browse files
committed
Event function: update testing to new approach
To support zone and region params of examples added custom code to main.tf to get random available Fixes #26
1 parent 32a9ef5 commit a45f1d1

22 files changed

+428
-821
lines changed

.gitignore

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,9 @@
1212
\#*\#
1313
.\#*
1414

15+
# IDE-related fiels
16+
.idea
17+
1518
# Vim-related files
1619
[._]*.s[a-w][a-z]
1720
[._]s[a-w][a-z]

CONTRIBUTING.md

Lines changed: 119 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,119 @@
1+
CONTRIBUTING# 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+
- [Terraform][terraform-site] v0.12.Z
10+
- [Docker Engine][docker-engine]
11+
- [Google Cloud SDK][google-cloud-sdk]
12+
- [make]
13+
14+
### IAM Roles
15+
16+
The Service Account which will be used to invoke this module must have
17+
the following IAM roles:
18+
19+
- Cloud Functions Developer: `roles/cloudfunctions.developer`
20+
- Storage Admin: `roles/storage.admin`
21+
22+
### APIs
23+
24+
The project against which this module will be invoked must have the
25+
following APIs enabled:
26+
27+
- Cloud Functions API: `cloudfunctions.googleapis.com`
28+
- Cloud Storage API: `storage-component.googleapis.com`
29+
30+
The [Project Factory module][project-factory-module-site] can be used to
31+
provision projects with specific APIs activated.
32+
33+
## Generating Documentation for Inputs and Outputs
34+
35+
The Inputs and Outputs tables in the READMEs of the root module,
36+
submodules, and example modules are automatically generated based on
37+
the `variables` and `outputs` of the respective modules. These tables
38+
must be refreshed if the module interfaces are changed.
39+
40+
### Execution
41+
42+
Run `make generate_docs` to generate new Inputs and Outputs tables.
43+
44+
## Integration Testing
45+
46+
Integration tests are used to verify the behaviour of the root module,
47+
submodules, and example modules. Additions, changes, and fixes should
48+
be accompanied with tests.
49+
50+
The integration tests are run using [Kitchen][kitchen],
51+
[Kitchen-Terraform][kitchen-terraform], and [InSpec][inspec]. These
52+
tools are packaged within a Docker image for convenience.
53+
54+
The general strategy for these tests is to verify the behaviour of the
55+
[example modules](./examples/), thus ensuring that the root module,
56+
submodules, and example modules are all functionally correct.
57+
58+
### Test Environment
59+
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.
60+
61+
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:
62+
63+
```
64+
export SERVICE_ACCOUNT_JSON=$(< credentials.json)
65+
```
66+
67+
You will also need to set a few environment variables:
68+
```
69+
export TF_VAR_org_id="your_org_id"
70+
export TF_VAR_folder_id="your_folder_id"
71+
export TF_VAR_billing_account="your_billing_account_id"
72+
```
73+
74+
With these settings in place, you can prepare a test project using Docker:
75+
```
76+
make docker_test_prepare
77+
```
78+
79+
### Noninteractive Execution
80+
81+
Run `make docker_test_integration` to test all of the example modules
82+
noninteractively, using the prepared test project.
83+
84+
### Interactive Execution
85+
86+
1. Run `make docker_run` to start the testing Docker container in
87+
interactive mode.
88+
89+
1. Run `kitchen_do create <EXAMPLE_NAME>` to initialize the working
90+
directory for an example module.
91+
92+
1. Run `kitchen_do converge <EXAMPLE_NAME>` to apply the example module.
93+
94+
1. Run `kitchen_do verify <EXAMPLE_NAME>` to test the example module.
95+
96+
1. Run `kitchen_do destroy <EXAMPLE_NAME>` to destroy the example module
97+
state.
98+
99+
## Linting and Formatting
100+
101+
Many of the files in the repository can be linted or formatted to
102+
maintain a standard of quality.
103+
104+
### Execution
105+
106+
Run `make docker_test_lint`.
107+
108+
[docker-engine]: https://www.docker.com/products/docker-engine
109+
[flake8]: http://flake8.pycqa.org/en/latest/
110+
[gofmt]: https://golang.org/cmd/gofmt/
111+
[google-cloud-sdk]: https://cloud.google.com/sdk/install
112+
[hadolint]: https://github.com/hadolint/hadolint
113+
[inspec]: https://inspec.io/
114+
[kitchen-terraform]: https://github.com/newcontext-oss/kitchen-terraform
115+
[kitchen]: https://kitchen.ci/
116+
[make]: https://en.wikipedia.org/wiki/Make_(software)
117+
[shellcheck]: https://www.shellcheck.net/
118+
[terraform-docs]: https://github.com/segmentio/terraform-docs
119+
[terraform]: https://terraform.io/

Makefile

Lines changed: 49 additions & 98 deletions
Original file line numberDiff line numberDiff line change
@@ -18,117 +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-
all: check generate_docs
28-
29-
.PHONY: check
30-
check: check_shell check_python check_terraform check_base_files test_check_headers check_headers check_trailing_whitespace
31-
32-
.PHONY: check_shell
33-
check_shell:
34-
@source test/make.sh && check_shell
35-
36-
.PHONY: check_python
37-
check_python:
38-
@source test/make.sh && check_python
39-
40-
.PHONY: check_terraform
41-
check_terraform:
42-
@source test/make.sh && check_terraform
43-
44-
.PHONY: check_base_files
45-
check_base_files:
46-
@source test/make.sh && basefiles
47-
48-
.PHONY: check_trailing_whitespace
49-
check_trailing_whitespace:
50-
@source test/make.sh && check_trailing_whitespace
51-
52-
.PHONY: test_check_headers
53-
test_check_headers:
54-
@echo "Testing the validity of the header check"
55-
@python test/test_verify_boilerplate.py
56-
57-
.PHONY: check_headers
58-
check_headers:
59-
@source test/make.sh && check_headers
60-
61-
# Integration tests
62-
.PHONY: test_integration
63-
test_integration:
64-
test/integration.sh
65-
66-
.PHONY: generate_docs
67-
generate_docs:
68-
@source test/make.sh && generate_docs
69-
70-
# Versioning
71-
.PHONY: version
72-
version:
73-
@source helpers/version-repo.sh
74-
75-
# Run docker
25+
# Enter docker container for local development
7626
.PHONY: docker_run
7727
docker_run:
7828
docker run --rm -it \
79-
-e PROJECT_ID \
8029
-e SERVICE_ACCOUNT_JSON \
81-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
82-
-v $(CURDIR):/cft/workdir \
83-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
84-
/bin/bash -c "source test/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
8533

86-
.PHONY: docker_create
87-
docker_create:
34+
# Execute prepare tests within the docker container
35+
.PHONY: docker_test_prepare
36+
docker_test_prepare:
8837
docker run --rm -it \
89-
-e PROJECT_ID \
9038
-e SERVICE_ACCOUNT_JSON \
91-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
92-
-v $(CURDIR):/cft/workdir \
93-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
94-
/bin/bash -c "source test/integration.sh && setup_environment && kitchen create"
95-
96-
.PHONY: docker_converge
97-
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:
9849
docker run --rm -it \
99-
-e PROJECT_ID \
10050
-e SERVICE_ACCOUNT_JSON \
101-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
102-
-v $(CURDIR):/cft/workdir \
103-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
104-
/bin/bash -c "source test/integration.sh && setup_environment && kitchen converge"
105-
106-
.PHONY: docker_verify
107-
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:
10861
docker run --rm -it \
109-
-e PROJECT_ID \
11062
-e SERVICE_ACCOUNT_JSON \
111-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
112-
-v $(CURDIR):/cft/workdir \
113-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
114-
/bin/bash -c "source test/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
11566

116-
.PHONY: docker_destroy
117-
docker_destroy:
67+
# Execute lint tests within the docker container
68+
.PHONY: docker_test_lint
69+
docker_test_lint:
11870
docker run --rm -it \
119-
-e PROJECT_ID \
120-
-e SERVICE_ACCOUNT_JSON \
121-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
122-
-v $(CURDIR):/cft/workdir \
123-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
124-
/bin/bash -c "source test/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
12574

126-
.PHONY: test_integration_docker
127-
test_integration_docker:
75+
# Generate documentation
76+
.PHONY: docker_generate_docs
77+
docker_generate_docs:
12878
docker run --rm -it \
129-
-e PROJECT_ID \
130-
-e SERVICE_ACCOUNT_JSON \
131-
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
132-
-v $(CURDIR):/cft/workdir \
133-
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
134-
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: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
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: echo
25+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
26+
args: ['/bin/bash', '-c', 'cat test/source.sh']
27+
- id: create
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 create']
30+
- id: converge
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 converge']
33+
- id: verify
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 verify']
36+
- id: destroy
37+
name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_DEVELOPER_TOOLS:$_DOCKER_TAG_VERSION_DEVELOPER_TOOLS'
38+
args: ['/bin/bash', '-c', 'source /usr/local/bin/task_helper_functions.sh && kitchen_do destroy']
39+
tags:
40+
- 'ci'
41+
- 'integration'
42+
substitutions:
43+
_DOCKER_IMAGE_DEVELOPER_TOOLS: 'cft/developer-tools'
44+
_DOCKER_TAG_VERSION_DEVELOPER_TOOLS: '0.1.0'

test/boilerplate/boilerplate.Dockerfile.txt renamed to build/lint.cloudbuild.yaml

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -11,3 +11,14 @@
1111
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
14+
15+
steps:
16+
- name: 'gcr.io/cloud-foundation-cicd/$_DOCKER_IMAGE_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/boilerplate/boilerplate.Makefile.txt

Lines changed: 0 additions & 13 deletions
This file was deleted.

test/boilerplate/boilerplate.go.txt

Lines changed: 0 additions & 15 deletions
This file was deleted.

0 commit comments

Comments
 (0)