From b2642d4d2f1bc737982a108cae6eb5ae3ae1525d Mon Sep 17 00:00:00 2001 From: Aaron Lane Date: Thu, 21 Feb 2019 13:25:43 -0500 Subject: [PATCH 1/7] Update integration.sh to use standardized logic --- test/integration.sh | 67 +++++++++++++++++++++++++++------------------ 1 file changed, 41 insertions(+), 26 deletions(-) diff --git a/test/integration.sh b/test/integration.sh index 7e780c7..4009ccc 100755 --- a/test/integration.sh +++ b/test/integration.sh @@ -14,34 +14,49 @@ # See the License for the specific language governing permissions and # limitations under the License. -set -e -set -x +# Always clean up. +DELETE_AT_EXIT="$(mktemp -d)" +finish() { + echo 'BEGIN: finish() trap handler' >&2 + kitchen destroy + [[ -d "${DELETE_AT_EXIT}" ]] && rm -rf "${DELETE_AT_EXIT}" + echo 'END: finish() trap handler' >&2 +} -if [ -z "${PROJECT_ID}" ]; then - echo "The PROJECT_ID ENV variable must be set to proceed. Aborting." - exit 1 -fi +# Map the input parameters provided by Concourse CI, or whatever mechanism is +# running the tests to Terraform input variables. Also setup credentials for +# use with kitchen-terraform, inspec, and gcloud. +setup_environment() { + local tmpfile + tmpfile="$(mktemp)" + echo "${SERVICE_ACCOUNT_JSON}" > "${tmpfile}" -if [ -z "${SERVICE_ACCOUNT_JSON}" ]; then - echo "The SERVICE_ACCOUNT_JSON ENV variable must be set to proceed. Aborting." - exit 1 -fi + # gcloud variables + export CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="${tmpfile}" + # Application default credentials (Terraform google provider and inspec-gcp) + export GOOGLE_APPLICATION_CREDENTIALS="${tmpfile}" + + # Terraform variables + export TF_VAR_project_id="${PROJECT_ID}" + export TF_VAR_region="${REGION:-us-east1}" + export TF_VAR_zone="${ZONE:-us-east1-b}" +} -export TF_VAR_project_id="${PROJECT_ID}" -export TF_VAR_region="${REGION:-us-east1}" -export TF_VAR_zone="${ZONE:-us-east1-b}" +main() { + set -eu + # Setup trap handler to auto-cleanup + export TMPDIR="${DELETE_AT_EXIT}" + trap finish EXIT -DELETE_AT_EXIT="$(mktemp -d)" -finish() { - [[ -d "${DELETE_AT_EXIT}" ]] && rm -rf "${DELETE_AT_EXIT}" + # Setup environment variables + setup_environment + set -x + + # Execute the test lifecycle + kitchen verify } -trap finish EXIT -CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE="$(TMPDIR="${DELETE_AT_EXIT}" mktemp)" -set +x -echo "${SERVICE_ACCOUNT_JSON}" > "${CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE}" -set -x -GOOGLE_APPLICATION_CREDENTIALS="${CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE}" -declare -rx CLOUDSDK_AUTH_CREDENTIAL_FILE_OVERRIDE GOOGLE_APPLICATION_CREDENTIALS -set +e -bundle install -bundle exec kitchen test --destroy=always + +# if script is being executed and not sourced. +if [[ "${BASH_SOURCE[0]}" == "${0}" ]]; then + main "$@" +fi From 27652030eff0c3672537d8e597014c08d2fb9c1f Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Thu, 21 Feb 2019 14:05:34 -0500 Subject: [PATCH 2/7] Update test/integration.sh Replace hard tab with spaces Co-Authored-By: aaron-lane --- test/integration.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration.sh b/test/integration.sh index 4009ccc..71b9e35 100755 --- a/test/integration.sh +++ b/test/integration.sh @@ -38,7 +38,7 @@ setup_environment() { # Terraform variables export TF_VAR_project_id="${PROJECT_ID}" - export TF_VAR_region="${REGION:-us-east1}" + export TF_VAR_region="${REGION:-us-east1}" export TF_VAR_zone="${ZONE:-us-east1-b}" } From c59f8d21f5c92273e996ba536c1c1a99fee150ef Mon Sep 17 00:00:00 2001 From: Adrien Thebo Date: Thu, 21 Feb 2019 14:05:42 -0500 Subject: [PATCH 3/7] Update test/integration.sh Replace hard tab with spaces Co-Authored-By: aaron-lane --- test/integration.sh | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/integration.sh b/test/integration.sh index 71b9e35..4d7b64a 100755 --- a/test/integration.sh +++ b/test/integration.sh @@ -39,7 +39,7 @@ setup_environment() { # Terraform variables export TF_VAR_project_id="${PROJECT_ID}" export TF_VAR_region="${REGION:-us-east1}" - export TF_VAR_zone="${ZONE:-us-east1-b}" + export TF_VAR_zone="${ZONE:-us-east1-b}" } main() { From 1541a6796538fb177622da6d50ba8518ee394663 Mon Sep 17 00:00:00 2001 From: Aaron Lane Date: Thu, 21 Feb 2019 14:14:59 -0500 Subject: [PATCH 4/7] Move providers to test fixture --- examples/automatic_labelling/main.tf | 12 ------------ test/fixtures/automatic_labelling/main.tf | 12 ++++++++++++ 2 files changed, 12 insertions(+), 12 deletions(-) diff --git a/examples/automatic_labelling/main.tf b/examples/automatic_labelling/main.tf index c6ef76a..e39247d 100644 --- a/examples/automatic_labelling/main.tf +++ b/examples/automatic_labelling/main.tf @@ -14,18 +14,6 @@ * limitations under the License. */ -provider "archive" { - version = "~> 1.1" -} - -provider "google" { - version = "~> 1.20" -} - -provider "random" { - version = "~> 2.0" -} - resource "random_pet" "main" { separator = "-" } diff --git a/test/fixtures/automatic_labelling/main.tf b/test/fixtures/automatic_labelling/main.tf index f0bf003..7e2874e 100644 --- a/test/fixtures/automatic_labelling/main.tf +++ b/test/fixtures/automatic_labelling/main.tf @@ -14,6 +14,18 @@ * limitations under the License. */ +provider "archive" { + version = "~> 1.1" +} + +provider "google" { + version = "~> 1.20" +} + +provider "random" { + version = "~> 2.0" +} + provider "null" { version = "~> 1.0" } From 2713cd17c762b42ab870c5789068f272ed0a7e0d Mon Sep 17 00:00:00 2001 From: Aaron Lane Date: Thu, 21 Feb 2019 14:15:35 -0500 Subject: [PATCH 5/7] Add name var to example; randomly name in fixture --- examples/automatic_labelling/main.tf | 6 +----- examples/automatic_labelling/variables.tf | 5 +++++ test/fixtures/automatic_labelling/main.tf | 5 +++++ 3 files changed, 11 insertions(+), 5 deletions(-) diff --git a/examples/automatic_labelling/main.tf b/examples/automatic_labelling/main.tf index e39247d..850c798 100644 --- a/examples/automatic_labelling/main.tf +++ b/examples/automatic_labelling/main.tf @@ -14,10 +14,6 @@ * limitations under the License. */ -resource "random_pet" "main" { - separator = "-" -} - module "event_function" { source = "../../" @@ -41,7 +37,7 @@ module "event_function" { ) }" - name = "${random_pet.main.id}" + name = "${var.name}" project_id = "${var.project_id}" region = "${var.region}" } diff --git a/examples/automatic_labelling/variables.tf b/examples/automatic_labelling/variables.tf index 0579e01..b3102f9 100644 --- a/examples/automatic_labelling/variables.tf +++ b/examples/automatic_labelling/variables.tf @@ -14,6 +14,11 @@ * limitations under the License. */ +variable "name" { + type = "string" + description = "The name to apply to any nameable resources." +} + variable "project_id" { type = "string" description = "The ID of the project to which resources will be applied." diff --git a/test/fixtures/automatic_labelling/main.tf b/test/fixtures/automatic_labelling/main.tf index 7e2874e..9de0018 100644 --- a/test/fixtures/automatic_labelling/main.tf +++ b/test/fixtures/automatic_labelling/main.tf @@ -30,10 +30,15 @@ provider "null" { version = "~> 1.0" } +resource "random_pet" "main" { + separator = "-" +} + module "automatic_labelling" { source = "../../../examples/automatic_labelling" project_id = "${var.project_id}" + name = "automatic-labelling-${random_pet.main.id}" region = "${var.region}" } From 8f290cbfa5fd3d1cbaee60a3eedf7cf16227b02a Mon Sep 17 00:00:00 2001 From: Aaron Lane Date: Thu, 21 Feb 2019 14:15:57 -0500 Subject: [PATCH 6/7] Update null provider to 2.0 --- test/fixtures/automatic_labelling/main.tf | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/test/fixtures/automatic_labelling/main.tf b/test/fixtures/automatic_labelling/main.tf index 9de0018..06385d8 100644 --- a/test/fixtures/automatic_labelling/main.tf +++ b/test/fixtures/automatic_labelling/main.tf @@ -27,7 +27,7 @@ provider "random" { } provider "null" { - version = "~> 1.0" + version = "~> 2.0" } resource "random_pet" "main" { From 79a6430dec4fa475c58d35e3182c2acb1f0a795b Mon Sep 17 00:00:00 2001 From: Aaron Lane Date: Thu, 21 Feb 2019 14:16:05 -0500 Subject: [PATCH 7/7] Generate documentation --- README.md | 3 +-- examples/automatic_labelling/README.md | 1 + 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index 2ace5da..5b8d075 100644 --- a/README.md +++ b/README.md @@ -29,11 +29,10 @@ use this module. | function\_labels | A set of key/value label pairs to assign to the function. | map | `` | no | | function\_runtime | The runtime in which the function will be executed. | string | `"nodejs6"` | no | | function\_source\_archive\_bucket\_labels | A set of key/value label pairs to assign to the function source archive bucket. | map | `` | no | -| function\_source\_archive\_bucket\_location | The Google Cloud Storage location in which to create the function source archive bucket. | string | `"US"` | no | | function\_source\_directory | The contents of this directory will be archived and used as the function source. | string | n/a | yes | | function\_timeout\_s | The amount of time in seconds allotted for the execution of the function. | string | `"60"` | no | | log\_export\_filter | The filter to apply when exporting logs to the Pub/Sub topic. | string | n/a | yes | -| name | The name to apply to any nameable resources. | string | `"event-function"` | no | +| name | The name to apply to any nameable resources. | string | n/a | yes | | project\_id | The ID of the project to which resources will be applied. | string | n/a | yes | | region | The region in which resources will be applied. | string | n/a | yes | diff --git a/examples/automatic_labelling/README.md b/examples/automatic_labelling/README.md index b65fa58..ac9c13d 100644 --- a/examples/automatic_labelling/README.md +++ b/examples/automatic_labelling/README.md @@ -35,6 +35,7 @@ The project against which this module will be invoked requires no additional API | Name | Description | Type | Default | Required | |------|-------------|:----:|:-----:|:-----:| +| name | The name to apply to any nameable resources. | string | n/a | yes | | project\_id | The ID of the project to which resources will be applied. | string | n/a | yes | | region | The region in which resources will be applied. | string | n/a | yes |