Skip to content

Commit 9c7727a

Browse files
authored
Merge pull request #48 from kopachevsky/feature/int-test-standartize
Standardize integration testing
2 parents ca73754 + eb7be90 commit 9c7727a

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

44 files changed

+418
-113
lines changed

.gitignore

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,9 @@
11
**/terraform.tfstate*
22
**/.terraform*
33
**/backend.tf
4-
**/terraform.tfvars
54
**/terraform.tfplan
65
**/values-*.yaml
6+
7+
.idea
8+
.kitchen
9+
credentials.json

CHANGELOG.md

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
# Change Log
2+
3+
All notable changes to this project will be documented in this file.
4+
5+
The format is based on [Keep a Changelog](http://keepachangelog.com/) and this
6+
project adheres to [Semantic Versioning](http://semver.org/).
7+
8+
## [Unreleased]
9+
10+
### Added
11+
12+
13+
14+
### Changed
15+

Makefile

Lines changed: 88 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,26 @@
1212
# See the License for the specific language governing permissions and
1313
# limitations under the License.
1414

15+
# Please note that this file was generated from [terraform-google-module-template](https://github.com/terraform-google-modules/terraform-google-module-template).
16+
# Please make sure to contribute relevant changes upstream!
17+
1518
# Make will use bash instead of sh
1619
SHELL := /usr/bin/env bash
1720

21+
# Docker build config variables
22+
CREDENTIALS_PATH ?= /cft/workdir/credentials.json
23+
24+
25+
# Docker build config variables
26+
DOCKER_ORG := gcr.io/cloud-foundation-cicd
27+
DOCKER_TAG_BASE_KITCHEN_TERRAFORM ?= 1.0.1
28+
DOCKER_REPO_BASE_KITCHEN_TERRAFORM := ${DOCKER_ORG}/cft/kitchen-terraform:${DOCKER_TAG_BASE_KITCHEN_TERRAFORM}
29+
1830
# All is the first target in the file so it will get picked up when you just run 'make' on its own
19-
all: check_shell check_python check_golang check_terraform check_docker check_base_files check_trailing_whitespace generate_docs
31+
all: check generate_docs
32+
33+
# Run all available linters
34+
check: check_shell check_python check_golang check_terraform check_docker check_base_files test_check_headers check_headers check_trailing_whitespace
2035

2136
# The .PHONY directive tells make that this isn't a real target and so
2237
# the presence of a file named 'check_shell' won't cause this target to stop
@@ -45,26 +60,90 @@ check_docker:
4560
check_base_files:
4661
@source test/make.sh && basefiles
4762

48-
.PHONY: check_shebangs
49-
check_shebangs:
50-
@source test/make.sh && check_bash
51-
5263
.PHONY: check_trailing_whitespace
5364
check_trailing_whitespace:
5465
@source test/make.sh && check_trailing_whitespace
5566

5667
.PHONY: test_check_headers
5768
test_check_headers:
69+
@echo "Testing the validity of the header check"
5870
@python test/test_verify_boilerplate.py
5971

6072
.PHONY: check_headers
6173
check_headers:
62-
@python test/verify_boilerplate.py
74+
@source test/make.sh && check_headers
75+
76+
# Integration tests
77+
.PHONY: test_integration
78+
test_integration:
79+
test/ci_integration.sh
6380

6481
.PHONY: generate_docs
6582
generate_docs:
6683
@source test/make.sh && generate_docs
6784

68-
.PHONY: test_integration
69-
test_integration:
70-
@source test/test.sh
85+
# Versioning
86+
.PHONY: version
87+
version:
88+
@source helpers/version-repo.sh
89+
90+
# Run docker
91+
.PHONY: docker_run
92+
docker_run:
93+
docker run --rm -it \
94+
-e PROJECT_ID \
95+
-e SERVICE_ACCOUNT_JSON \
96+
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
97+
-v $(CURDIR):/cft/workdir \
98+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
99+
/bin/bash -c "source test/ci_integration.sh && setup_environment && exec /bin/bash"
100+
101+
.PHONY: docker_create
102+
docker_create:
103+
docker run --rm -it \
104+
-e PROJECT_ID \
105+
-e SERVICE_ACCOUNT_JSON \
106+
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
107+
-v $(CURDIR):/cft/workdir \
108+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
109+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen create"
110+
111+
.PHONY: docker_converge
112+
docker_converge:
113+
docker run --rm -it \
114+
-e PROJECT_ID \
115+
-e SERVICE_ACCOUNT_JSON \
116+
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
117+
-v $(CURDIR):/cft/workdir \
118+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
119+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen converge"
120+
121+
.PHONY: docker_verify
122+
docker_verify:
123+
docker run --rm -it \
124+
-e PROJECT_ID \
125+
-e SERVICE_ACCOUNT_JSON \
126+
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
127+
-v $(CURDIR):/cft/workdir \
128+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
129+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen verify"
130+
131+
.PHONY: docker_destroy
132+
docker_destroy:
133+
docker run --rm -it \
134+
-e PROJECT_ID \
135+
-e SERVICE_ACCOUNT_JSON \
136+
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
137+
-v $(CURDIR):/cft/workdir \
138+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
139+
/bin/bash -c "source test/ci_integration.sh && setup_environment && kitchen destroy"
140+
141+
.PHONY: test_integration_docker
142+
test_integration_docker:
143+
docker run --rm -it \
144+
-e PROJECT_ID \
145+
-e SERVICE_ACCOUNT_JSON \
146+
-e GOOGLE_APPLICATION_CREDENTIALS=${CREDENTIALS_PATH} \
147+
-v $(CURDIR):/cft/workdir \
148+
${DOCKER_REPO_BASE_KITCHEN_TERRAFORM} \
149+
make test_integration

test/ci_integration.sh

Lines changed: 64 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,64 @@
1+
#!/usr/bin/env bash
2+
3+
# Copyright 2018 Google LLC
4+
#
5+
# Licensed under the Apache License, Version 2.0 (the "License");
6+
# you may not use this file except in compliance with the License.
7+
# You may obtain a copy of the License at
8+
#
9+
# http://www.apache.org/licenses/LICENSE-2.0
10+
#
11+
# Unless required by applicable law or agreed to in writing, software
12+
# distributed under the License is distributed on an "AS IS" BASIS,
13+
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
# See the License for the specific language governing permissions and
15+
# limitations under the License.
16+
17+
# Always clean up.
18+
DELETE_AT_EXIT="$(mktemp -d)"
19+
finish() {
20+
echo 'BEGIN: finish() trap handler' >&2
21+
kitchen destroy "$SUITE"
22+
[[ -d "${DELETE_AT_EXIT}" ]] && rm -rf "${DELETE_AT_EXIT}"
23+
echo 'END: finish() trap handler' >&2
24+
}
25+
26+
# Map the input parameters provided by Concourse CI, or whatever mechanism is
27+
# running the tests to Terraform input variables. Also setup credentials for
28+
# use with kitchen-terraform, inspec, and gcloud.
29+
setup_environment() {
30+
local tmpfile
31+
tmpfile="$(mktemp)"
32+
echo "${SERVICE_ACCOUNT_JSON}" > "${tmpfile}"
33+
34+
# gcloud variables
35+
export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="${tmpfile}"
36+
# Application default credentials (Terraform google provider and inspec-gcp)
37+
export GOOGLE_APPLICATION_CREDENTIALS="${tmpfile}"
38+
39+
# Terraform variables
40+
export TF_VAR_project="$PROJECT_ID"
41+
}
42+
43+
main() {
44+
export SUITE="${SUITE:-}"
45+
46+
set -eu
47+
# Setup trap handler to auto-cleanup
48+
export TMPDIR="${DELETE_AT_EXIT}"
49+
trap finish EXIT
50+
51+
# Setup environment variables
52+
setup_environment
53+
set -x
54+
55+
# Execute the test lifecycle
56+
kitchen create "$SUITE"
57+
kitchen converge "$SUITE"
58+
kitchen verify "$SUITE"
59+
}
60+
61+
# if script is being executed and not sourced.
62+
if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then
63+
main "$@"
64+
fi

test/fixtures/mysql-ha/main.tf

Lines changed: 15 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,25 @@
1414
* limitations under the License.
1515
*/
1616

17-
provider "google" {
18-
credentials = "${file(var.credentials_file_path)}"
17+
provider "google" {}
18+
19+
resource "random_id" "instance_name_suffix" {
20+
byte_length = 5
21+
}
22+
23+
locals {
24+
/*
25+
Random instance name needed because:
26+
"You cannot reuse an instance name for up to a week after you have deleted an instance."
27+
See https://cloud.google.com/sql/docs/mysql/delete-instance for details.
28+
*/
29+
instance_name = "${var.mysql_ha_name}-${random_id.instance_name_suffix.hex}"
1930
}
2031

32+
2133
module "mysql" {
2234
source = "../../../modules/mysql"
23-
name = "${var.mysql_ha_name}"
35+
name = "${local.instance_name}"
2436
project_id = "${var.project}"
2537
database_version = "MYSQL_5_7"
2638
region = "us-central1"

test/fixtures/mysql-ha/outputs.tf

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ output "project_id" {
1919
}
2020

2121
output "name" {
22-
value = "${var.mysql_ha_name}"
22+
value = "${local.instance_name}"
2323
}
2424

2525
output "authorized_network" {
Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1 @@
1+
../shared/terraform.tfvars

test/fixtures/mysql-ha/terraform.tfvars.example

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@
1919

2020
## These values you *MUST* modify to match your environment
2121

22-
credentials_file_path="../../../credentials.json"
2322
project="tf-proj"
2423
mysql_ha_name="tf-mysql-ha"
2524
mysql_ha_external_ip_range="x.x.x.x/32"

test/fixtures/mysql-ha/variables.tf

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -14,10 +14,6 @@
1414
* limitations under the License.
1515
*/
1616

17-
variable "credentials_file_path" {
18-
description = "Service account json auth path"
19-
}
20-
2117
variable "project" {
2218
description = "The project to run tests against"
2319
}

test/fixtures/mysql-simple/main.tf

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,13 +14,24 @@
1414
* limitations under the License.
1515
*/
1616

17-
provider "google" {
18-
credentials = "${file(var.credentials_file_path)}"
17+
provider "google" {}
18+
19+
resource "random_id" "instance_name_suffix" {
20+
byte_length = 5
21+
}
22+
23+
locals {
24+
/*
25+
Random instance name needed because:
26+
"You cannot reuse an instance name for up to a week after you have deleted an instance."
27+
See https://cloud.google.com/sql/docs/mysql/delete-instance for details.
28+
*/
29+
instance_name = "${var.mysql_simple_name}-${random_id.instance_name_suffix.hex}"
1930
}
2031

2132
module "mysql" {
2233
source = "../../../modules/mysql"
23-
name = "${var.mysql_simple_name}"
34+
name = "${local.instance_name}"
2435
project_id = "${var.project}"
2536
database_version = "MYSQL_5_7"
2637
region = "us-central1"

0 commit comments

Comments
 (0)