Skip to content

Commit

Permalink
Merge pull request #22 from mineiros-io/soerenmartius/tests
Browse files Browse the repository at this point in the history
Add unit tests
  • Loading branch information
soerenmartius authored Jun 9, 2022
2 parents 7141751 + cd7784c commit c4dc81a
Show file tree
Hide file tree
Showing 26 changed files with 2,179 additions and 112 deletions.
123 changes: 119 additions & 4 deletions .github/workflows/tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,11 @@ on:
push:
branches:
- main
- master
pull_request:

concurrency:
group: main
group: terraform-google-cloud-sql
cancel-in-progress: false

jobs:
Expand All @@ -16,7 +17,121 @@ jobs:
name: Static Analysis
steps:
- name: Checkout
uses: actions/checkout@v2
# for security reasons we pin commit ids and not tags.
# actions/[email protected] -> 2541b1294d2704b0964813337f33b291d3f8596b
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b
with:
fetch-depth: 0

- name: Run pre-commit
run: make test/pre-commit
# - name: Setup SSH to Private Modules
# # for security reasons we pin commit ids and not tags.
# # webfactory/[email protected] -> fc49353b67b2b7c1e0e6a600572d01a69f2672dd
# uses: webfactory/ssh-agent@fc49353b67b2b7c1e0e6a600572d01a69f2672dd
# with:
# ssh-private-key: |
# ${{ secrets.DEPLOY_KEY__REPOSITORY_A }}
# ${{ secrets.DEPLOY_KEY__REPOSITORY_B }}

- name: Set up Golang
# for security reasons we pin commit ids and not tags.
# actions/[email protected] -> b22fbbc2921299758641fab08929b4ac52b32923
uses: actions/setup-go@b22fbbc2921299758641fab08929b4ac52b32923
with:
go-version: 1.17.6

- name: Set up Terramate
# for security reasons we pin commit ids and not tags.
# mineiros-io/[email protected] -> bea6836fa912c982f39a2965a327476b15d06b45
run: go install github.com/mineiros-io/terramate/cmd/terramate@bea6836fa912c982f39a2965a327476b15d06b45
env:
GOPROXY: direct

- name: Set up Terradoc
# for security reasons we pin commit ids and not tags.
# mineiros-io/terradoc@main -> af1a7b3ae3635958adf5ee2f40e0c3e70fd0803a
run: go install github.com/mineiros-io/terradoc/cmd/terradoc@af1a7b3ae3635958adf5ee2f40e0c3e70fd0803a
env:
GOPROXY: direct

- name: Set up Terraform
# for security reasons we pin commit ids and not tags.
# hashicorp/[email protected] -> 17d4c9b8043b238f6f35641cdd8433da1e6f3867
uses: hashicorp/setup-terraform@17d4c9b8043b238f6f35641cdd8433da1e6f3867
with:
terraform_version: 1.2.1
terraform_wrapper: false

- name: Setup TFLint
# for security reasons we pin commit ids and not tags.
# terraform-linters/[email protected] -> 9e653e0f7de0839150e03280e0981e0c25eaf10a
uses: terraform-linters/setup-tflint@9e653e0f7de0839150e03280e0981e0c25eaf10a
with:
tflint_version: v0.29.0

- name: Setup and run pre-commit
# for security reasons we pin commit ids and not tags.
# pre-commit/[email protected] -> 9b88afc9cd57fd75b655d5c71bd38146d07135fe
uses: pre-commit/action@9b88afc9cd57fd75b655d5c71bd38146d07135fe

unit-tests:
# needs: pre-commit
runs-on: ubuntu-latest
name: Unit Tests
steps:
- name: Checkout
# for security reasons we pin commit ids and not tags.
# actions/[email protected] -> 2541b1294d2704b0964813337f33b291d3f8596b
uses: actions/checkout@2541b1294d2704b0964813337f33b291d3f8596b
with:
fetch-depth: 0

# - name: Setup SSH to Private Modules
# # for security reasons we pin commit ids and not tags.
# # webfactory/[email protected] -> fc49353b67b2b7c1e0e6a600572d01a69f2672dd
# uses: webfactory/ssh-agent@fc49353b67b2b7c1e0e6a600572d01a69f2672dd
# with:
# ssh-private-key: |
# ${{ secrets.DEPLOY_KEY__REPOSITORY_A }}
# ${{ secrets.DEPLOY_KEY__REPOSITORY_B }}

- name: Check for Terraform file changes
# for security reasons we pin commit ids and not tags.
# getsentry/[email protected] -> b2feaf19c27470162a626bd6fa8438ae5b263721
uses: getsentry/paths-filter@b2feaf19c27470162a626bd6fa8438ae5b263721
id: changes
with:
filters: |
terraform:
- '*.tf'
- 'test/**/*.tf'
- 'test/**/*.go'
- 'test/go.mod'
- 'test/go.sum'
- name: Set up Terraform
if: steps.changes.outputs.terraform == 'true'
# for security reasons we pin commit ids and not tags.
# hashicorp/[email protected] -> 17d4c9b8043b238f6f35641cdd8433da1e6f3867
uses: hashicorp/setup-terraform@17d4c9b8043b238f6f35641cdd8433da1e6f3867
with:
terraform_version: 1.2.1
terraform_wrapper: false

- name: Set up Golang
if: steps.changes.outputs.terraform == 'true'
# for security reasons we pin commit ids and not tags.
# actions/[email protected] -> b22fbbc2921299758641fab08929b4ac52b32923
uses: actions/setup-go@b22fbbc2921299758641fab08929b4ac52b32923
with:
go-version: 1.17.6

- name: Run Unit Tests
if: steps.changes.outputs.terraform == 'true'
run: make test/unit-tests
env:
AWS_SECRET_ACCESS_KEY: ${{ secrets.MINEIROS_TESTING_AWS_SECRET_ACCESS_KEY }}
AWS_ACCESS_KEY_ID: ${{ secrets.MINEIROS_TESTING_AWS_ACCESS_KEY_ID }}
GOOGLE_CREDENTIALS: ${{ secrets.MINEIROS_TESTING_GCP_SA_KEY_FILE }}
TEST_GCP_PROJECT: ${{ secrets.MINEIROS_TESTING_GCP_PROJECT }}
TEST_GCP_ORG_DOMAIN: ${{ secrets.MINEIROS_TESTING_GCP_ORG_DOMAIN }}
TEST_GCP_BILLING_ACCOUNT: ${{ secrets.MINEIROS_TESTING_GCP_BILLING_ACCOUNT }}
5 changes: 4 additions & 1 deletion .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,10 @@ out/
.DS_Store

# Local .terraform directories
**/.terraform/*
**/.terraform/*

# Local .terratest directories
**/.terratest/*

# .tfstate files
*.tfstate
Expand Down
60 changes: 0 additions & 60 deletions .golangci.yml

This file was deleted.

12 changes: 6 additions & 6 deletions .pre-commit-config.yaml
Original file line number Diff line number Diff line change
@@ -1,13 +1,13 @@
repos:
- repo: https://github.com/mineiros-io/pre-commit-hooks
rev: v0.3.1
rev: v0.4.1
hooks:
- id: phony-targets
- id: terradoc-validate
- id: terradoc-fmt
- id: terradoc-generate
- id: terramate-generate
- id: terraform-fmt
- id: terraform-validate
exclude: ^examples|.terraform/
- id: tflint
- id: golangci-lint
- id: phony-targets
- id: markdown-link-check
args: ['-p'] # When adding the -p flag, markdown-link-check will always with an exit code 0, even if dead links are found
verbose: true # Forces the output of the hook to be printed even when the hook passes.
13 changes: 12 additions & 1 deletion CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,16 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

## [Unreleased]

## [0.0.5]

## Added

- Add unit tests

## Removed

- Removed validation from `var.database_version`

## [0.0.4]

### BREAKING CHANGES
Expand Down Expand Up @@ -40,7 +50,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0

- Initial Implementation

[unreleased]: https://github.com/mineiros-io/terraform-google-cloud-sql/compare/v0.0.4...HEAD
[unreleased]: https://github.com/mineiros-io/terraform-google-cloud-sql/compare/v0.0.5...HEAD
[0.0.5]: https://github.com/mineiros-io/terraform-google-cloud-sql/compare/v0.0.4...v0.0.5
[0.0.4]: https://github.com/mineiros-io/terraform-google-cloud-sql/compare/v0.0.3...v0.0.4
[0.0.3]: https://github.com/mineiros-io/terraform-google-cloud-sql/compare/v0.0.2...v0.0.3
[0.0.2]: https://github.com/mineiros-io/terraform-google-cloud-sql/compare/v0.0.1...v0.0.2
Expand Down
51 changes: 37 additions & 14 deletions Makefile
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
# Set default shell to bash
SHELL := /bin/bash -o pipefail

BUILD_TOOLS_VERSION ?= v0.14.3
BUILD_TOOLS_VERSION ?= v0.15.2
BUILD_TOOLS_DOCKER_REPO ?= mineiros/build-tools
BUILD_TOOLS_DOCKER_IMAGE ?= ${BUILD_TOOLS_DOCKER_REPO}:${BUILD_TOOLS_VERSION}

Expand Down Expand Up @@ -64,6 +64,8 @@ endif
# the relevant environment variables (service-account key file).
ifdef GOOGLE_CREDENTIALS
DOCKER_GCP_FLAGS += -e GOOGLE_CREDENTIALS
DOCKER_GCP_FLAGS += -e TEST_GCP_PROJECT
DOCKER_GCP_FLAGS += -e TEST_GCP_ORG_DOMAIN
endif

# If GITHUB_OWNER is defined, we are likely running inside a GitHub provider
Expand All @@ -84,36 +86,58 @@ template/adjust:
@find . $(FILTER) -exec sed -i -e "s,terraform-module-template,$${PWD##*/},g" {} \;

## Run pre-commit hooks inside a build-tools docker container.
.PHONY: test/docker/pre-commit
test/docker/pre-commit: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
test/docker/pre-commit: DOCKER_FLAGS += -e NOCOLOR=1
test/docker/pre-commit:
$(call docker-run,make test/pre-commit)

## Run all Go tests inside a build-tools docker container. This is complementary to running 'go test ./test/...'.
.PHONY: test/docker/unit-tests
test/docker/unit-tests: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
test/docker/unit-tests: DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS}
test/docker/unit-tests: DOCKER_FLAGS += ${DOCKER_AWS_FLAGS}
test/docker/unit-tests: DOCKER_FLAGS += ${DOCKER_GCP_FLAGS}
test/docker/unit-tests: DOCKER_FLAGS += $(shell env | grep ^TF_VAR_ | cut -d = -f 1 | xargs -i printf ' -e {}')
test/docker/unit-tests: DOCKER_FLAGS += -e TF_DATA_DIR=.terratest
test/docker/unit-tests: DOCKER_FLAGS += -e NOCOLOR=1
test/docker/unit-tests: TEST ?= "TestUnit"
test/docker/unit-tests:
@echo "${YELLOW}[TEST] ${GREEN}Start Running Go Tests in Docker Container.${RESET}"
$(call docker-run,make test/unit-tests)

## Run pre-commit hooks.
.PHONY: test/pre-commit
test/pre-commit: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
test/pre-commit:
$(call docker-run,pre-commit run -a)
$(call quiet-command,pre-commit run -a)

## Run all Go tests inside a build-tools docker container. This is complementary to running 'go test ./test/...'.
.PHONY: test/unit-tests
test/unit-tests: DOCKER_FLAGS += ${DOCKER_SSH_FLAGS}
test/unit-tests: DOCKER_FLAGS += ${DOCKER_GITHUB_FLAGS}
test/unit-tests: DOCKER_FLAGS += ${DOCKER_AWS_FLAGS}
test/unit-tests: DOCKER_FLAGS += ${DOCKER_GCP_FLAGS}
test/unit-tests: DOCKER_FLAGS += $(shell env | grep ^TF_VAR_ | cut -d = -f 1 | xargs -i printf ' -e {}')
test/unit-tests: DOCKER_FLAGS += -e TF_DATA_DIR=.terratest
## Run all unit tests.
.PHONY: test/docker/unit-tests
test/unit-tests: TEST ?= "TestUnit"
test/unit-tests:
@echo "${YELLOW}[TEST] ${GREEN}Start Running Go Tests in Docker Container.${RESET}"
$(call go-test,./test -run $(TEST))
@echo "${YELLOW}[TEST] ${GREEN}Start Running unit tests.${RESET}"
$(call quiet-command,cd test ; go test -v -count 1 -timeout 45m -parallel 128 -run $(TEST))

## Generate README.md with Terradoc
.PHONY: terradoc
terradoc:
$(call quiet-command,terradoc -o README.md README.tfdoc.hcl)
$(call quiet-command,terradoc generate -o README.md README.tfdoc.hcl)

## Generate shared configuration for tests
.PHONY: terramate
terramate:
$(call quiet-command,terramate generate)

## Clean up cache and temporary files
.PHONY: clean
clean:
$(call rm-command,.terraform)
$(call rm-command,.terratest)
$(call rm-command,.terraform.lock.hcl)
$(call rm-command,*.tfplan)
$(call rm-command,*/*/.terraform)
$(call rm-command,*/*/.terratest)
$(call rm-command,*/*/*.tfplan)
$(call rm-command,*/*/.terraform.lock.hcl)

Expand All @@ -136,5 +160,4 @@ DOCKER_RUN_CMD = docker run ${DOCKER_FLAGS} ${BUILD_TOOLS_DOCKER_IMAGE}

quiet-command = $(if ${V},${1},$(if ${2},@echo ${2} && ${1}, @${1}))
docker-run = $(call quiet-command,${DOCKER_RUN_CMD} ${1} | cat,"${YELLOW}[DOCKER RUN] ${GREEN}${1}${RESET}")
go-test = $(call quiet-command,${DOCKER_RUN_CMD} go test -v -count 1 -timeout 45m -parallel 128 ${1} | cat,"${YELLOW}[TEST] ${GREEN}${1}${RESET}")
rm-command = $(call quiet-command,rm -rf ${1},"${YELLOW}[CLEAN] ${GREEN}${1}${RESET}")
8 changes: 2 additions & 6 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -51,7 +51,7 @@ Most basic usage just setting required arguments:

```hcl
module "terraform-google-cloud-sql" {
source = "github.com/mineiros-io/terraform-google-cloud-sql.git?ref=v0.0.4"
source = "github.com/mineiros-io/terraform-google-cloud-sql.git?ref=v0.0.5"
tier = "db-f1-micro"
database_version = "MYSQL_5_6"
Expand Down Expand Up @@ -92,7 +92,7 @@ See [variables.tf] and [examples/] for details and use-cases.

The machine type to use.

- [**`name`**](#var-name): *(Optional `number`)*<a name="var-name"></a>
- [**`name`**](#var-name): *(Optional `string`)*<a name="var-name"></a>

The name of the instance. If the name is left blank, Terraform will randomly generate one when the instance is first created. This is done because after a name is used, it cannot be reused for up to one week.

Expand Down Expand Up @@ -144,10 +144,6 @@ See [variables.tf] and [examples/] for details and use-cases.

Pricing plan for this instance, can only be `PER_USE`.

- [**`replication_type`**](#var-replication_type): *(Optional `string`)*<a name="var-replication_type"></a>

This property is only applicable to First Generation instances. Replication type for this instance, can be one of `ASYNCHRONOUS` or `SYNCHRONOUS`.

- [**`user_labels`**](#var-user_labels): *(Optional `map(string)`)*<a name="var-user_labels"></a>

A set of key/value user label pairs to assign to the instance.
Expand Down
Loading

0 comments on commit c4dc81a

Please sign in to comment.