From 209917d10d959be0952821fd96b1b1a806e8756a Mon Sep 17 00:00:00 2001 From: tombuildsstuff Date: Thu, 14 Oct 2021 14:11:02 +0200 Subject: [PATCH] tooling: adding a script to validate the examples in the `./examples` folder are valid --- .gitignore | 1 + GNUmakefile | 4 +++- scripts/validate-examples.sh | 35 +++++++++++++++++++++++++++++++++++ 3 files changed, 39 insertions(+), 1 deletion(-) create mode 100755 scripts/validate-examples.sh diff --git a/.gitignore b/.gitignore index 766a22694240..9692e41cd9c8 100644 --- a/.gitignore +++ b/.gitignore @@ -25,6 +25,7 @@ website/node_modules *.iml *.test .terraform.tfstate.lock.info +.terraform.lock.hcl website/vendor diff --git a/GNUmakefile b/GNUmakefile index 1999805159ed..f114e74b2c02 100644 --- a/GNUmakefile +++ b/GNUmakefile @@ -134,5 +134,7 @@ teamcity-test: @$(MAKE) -C .teamcity tools @$(MAKE) -C .teamcity test +validate-examples: + ./scripts/validate-examples.sh -.PHONY: build build-docker test test-docker testacc vet fmt fmtcheck errcheck scaffold-website test-compile website website-test +.PHONY: build build-docker test test-docker testacc vet fmt fmtcheck errcheck scaffold-website test-compile website website-test validate-examples diff --git a/scripts/validate-examples.sh b/scripts/validate-examples.sh new file mode 100755 index 000000000000..c4e28a1a2c8a --- /dev/null +++ b/scripts/validate-examples.sh @@ -0,0 +1,35 @@ +#!/usr/bin/env bash + +echo "==> Checking examples validate with 'terraform validate'..." + +exampleDirs=$(find ./examples -mindepth 2 -maxdepth 2 -type d '!' -exec test -e "{}/*.tf" ';' -print | sort) +examplesWithErrors=() +hasError=false + +for d in $exampleDirs; do + echo "Validating $d.." + exampleHasErrors=false + terraform -chdir=$d init > /dev/null || exampleHasErrors=true + if ! ${exampleHasErrors}; then + terraform -chdir=$d validate > /dev/null || exampleHasErrors=true + fi + if ${exampleHasErrors}; then + examplesWithErrors[${#examplesWithErrors[@]}]=$d + hasError=true + fi +done + +if ${hasError}; then + echo "------------------------------------------------" + echo "" + echo "The directories listed below failed to validate using 'terraform validate'" + echo "Please fix the validation errors for these, these can be found by running" + echo "'terraform init' and then 'terraform validate':" + for exampleDir in "${examplesWithErrors[@]}" + do + echo "- $exampleDir" + done + exit 1 +fi + +exit 0