Skip to content

ci: support prepare test #322

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Jun 6, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ TestRecordImport
TestRecordTfer
terraform.tfstate
terraform.tfstate.backup
**/terraform.tfvars
quickstarts/**/header.md
quickstarts/**/footer.md
quickstarts/**/plan.tftest.hcl
Expand Down
70 changes: 61 additions & 9 deletions scripts/terraform-test.sh
Original file line number Diff line number Diff line change
Expand Up @@ -2,12 +2,47 @@

f=$1
success=true
exitCode=0

f=$(echo $f | xargs echo -n)
f="${f%/}"

exitCode=0
echo ""
echo "====> Terraform testing in" $f

# set up prerequisite resources
varfile=""
if [ -d "$f/prepare" ]; then
echo ""
echo " ====> Found prepare directory. Creating prerequisite resources in $f/prepare"
terraform -chdir=$f/prepare init -upgrade >/dev/null
if [[ $? -ne 0 ]]; then
success=false
exitCode=1
echo -e "\033[31m[ERROR]\033[0m: running terraform init in prepare directory failed."
bash scripts/generate-test-record.sh $f "Prepare Init: running terraform init in prepare directory failed."
else
terraform -chdir=$f/prepare apply -auto-approve >/dev/null
if [[ $? -ne 0 ]]; then
success=false
exitCode=3
echo -e "\033[31m[ERROR]\033[0m: running terraform apply in prepare directory failed."
bash scripts/generate-test-record.sh $f "Prepare Apply: running terraform apply in prepare directory failed."
terraform -chdir=$f/prepare destroy -auto-approve >/dev/null
else
echo -e "\033[32m - prepare apply: success\033[0m"
terraform -chdir=$f/prepare output > $f/prepare/terraform.tfvars
varfile="-var-file=./prepare/terraform.tfvars"
fi
fi
fi
if [[ $success == "false" ]]; then
rm -rf $f/prepare/.terraform
rm -rf $f/prepare/.terraform.lock.hcl
exit $exitCode
fi


terraform -chdir=$f init -upgrade >/dev/null
if [[ $? -ne 0 ]]; then
success=false
Expand All @@ -17,7 +52,7 @@ if [[ $? -ne 0 ]]; then
else
echo ""
echo " ----> Plan Testing"
terraform -chdir=$f plan >/dev/null
terraform -chdir=$f plan $varfile >/dev/null
if [[ $? -ne 0 ]]; then
success=false
exitCode=2
Expand All @@ -27,7 +62,7 @@ else
echo -e "\033[32m - plan check: success\033[0m"
echo ""
echo " ----> Apply Testing"
terraform -chdir=$f apply -auto-approve >/dev/null
terraform -chdir=$f apply -auto-approve $varfile >/dev/null
if [[ $? -ne 0 ]]; then
success=false
exitCode=3
Expand All @@ -37,7 +72,7 @@ else
echo -e "\033[32m - apply check: success\033[0m"
echo ""
echo -e " ----> Apply Diff Checking\n"
terraform -chdir=$f plan -detailed-exitcode
terraform -chdir=$f plan $varfile -detailed-exitcode
if [[ $? -ne 0 ]]; then
success=false
exitCode=4
Expand All @@ -53,7 +88,7 @@ else
fi
echo ""
echo " ----> Destroying"
terraform -chdir=$f destroy -auto-approve >/dev/null
terraform -chdir=$f destroy $varfile -auto-approve >/dev/null
if [[ $? -ne 0 ]]; then
success=false
if [[ $exitCode -eq 0 ]]; then
Expand All @@ -66,14 +101,31 @@ else
fi
fi

if [[ $success == "true" ]]; then
bash scripts/generate-test-record.sh $f
fi

rm -rf $f/.terraform
rm -rf $f/.terraform.lock.hcl
fi

# destroy prerequisite resources
if [ -d "$f/prepare" ]; then
echo ""
echo " ====> Destroying prerequisite resources in $f/prepare"
terraform -chdir=$f/prepare destroy -auto-approve >/dev/null
if [[ $? -ne 0 ]]; then
success=false
exitCode=5
echo -e "\033[31m[ERROR]\033[0m: running terraform destroy in prepare directory failed."
bash scripts/generate-test-record.sh $f "Prepare Destroy: running terraform destroy in prepare directory failed."
else
echo -e "\033[32m - prepare destroy: success\033[0m"
fi
rm -rf $f/prepare/.terraform
rm -rf $f/prepare/.terraform.lock.hcl
fi


if [[ $success == "true" ]]; then
bash scripts/generate-test-record.sh $f
fi
echo -e "\n"

exit $exitCode