From fa21eac42bd15369317ba0777d7d372864573fee Mon Sep 17 00:00:00 2001 From: wwwil Date: Mon, 3 Jun 2019 11:11:52 +0100 Subject: [PATCH 1/2] Ignore verify-terraform directory, used for testing only Signed-off-by: wwwil --- .gitignore | 1 + 1 file changed, 1 insertion(+) diff --git a/.gitignore b/.gitignore index 6af62f7..e2af0a8 100644 --- a/.gitignore +++ b/.gitignore @@ -3,3 +3,4 @@ terraform.tfvars terraform.tfstate terraform.tfstate.backup key.json +verify-terraform/ From 2faf47453e168592208209397b4a23cccb130874 Mon Sep 17 00:00:00 2001 From: wwwil Date: Mon, 3 Jun 2019 11:14:18 +0100 Subject: [PATCH 2/2] Expand verify-terraform script for more comprehensive testing Signed-off-by: wwwil --- hack/verify-terraform.sh | 28 ++++++++++++++++++++++++---- 1 file changed, 24 insertions(+), 4 deletions(-) diff --git a/hack/verify-terraform.sh b/hack/verify-terraform.sh index 3de6f23..aaf96cc 100755 --- a/hack/verify-terraform.sh +++ b/hack/verify-terraform.sh @@ -21,9 +21,14 @@ set -o errexit set -o nounset set -o pipefail +set -o xtrace REPO_ROOT=$(dirname "${BASH_SOURCE[0]}")/.. +# Capture the output of terraform fmt so that we can trigger the script to +# fail if formatting changes were made. terraform fmt does not consider +# applying formatting changes to be failure, however we want the files to be +# correctly formatted in version control. FMT=$(terraform fmt $REPO_ROOT) if [ "$FMT" != "" ]; then echo "$FMT" @@ -40,11 +45,26 @@ sed -i.bak 's|backend "gcs" {}|# backend "gcs" {}|g' main.tf # Use the local version of the module, not the Terraform Registry version sed -i.bak 's|source\s=\s"jetstack/gke-cluster/google"|source\s=\s"../"|g' main.tf sed -i.bak 's|"jetstack/gke-cluster/google"|"../"|g' main.tf + terraform init -VALIDATE=$(terraform validate) -if [ "$VALIDATE" != "" ]; then - echo "$VALIDATE" - exit 1 +terraform validate + +# TODO: Set up a GCP project and service account to run the following section +# in automated testing. + +# To make Terraform plan and apply the the following env vars are required: +# GOOGLE_APPLICATION_CREDENTIALS is the path of a key.json for a service account +# GCP_PROJECT_ID is the ID of a GCP project to use +if [ ! -z ${GCP_PROJECT_ID+x} ] || [ ! -z ${GOOGLE_APPLICATION_CREDENTIALS+x} ]; then + echo $GCP_PROJECT_ID + echo $GOOGLE_APPLICATION_CREDENTIALS + sed -i.bak "s|my-project|$GCP_PROJECT_ID|g" terraform.tfvars + terraform plan + terraform apply -auto-approve + terraform destroy -auto-approve +else + echo "Skipping Terraform plan and apply as GCP_PROJECT_ID and GOOGLE_APPLICATION_CREDENTIALS not set." fi + popd > /dev/null rm -rf $REPO_ROOT/verify-terraform