Skip to content

Commit 6d48307

Browse files
authored
Merge pull request #29 from kopachevsky/fix/26/event-function
Event function: update testing to new approach
2 parents 32a9ef5 + a111499 commit 6d48307

23 files changed

+399
-939
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: 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/

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

0 commit comments

Comments
 (0)