Scenario
You are operating a production Terraform estate. The CI pipeline
runs terraform test. The test fails after a configuration
change.
Error: test failure
on main.tf line 12, in resource "aws_instance" "web":
12: instance_type = "t3.medium"
The test expects "t3.small" but the configuration produces
"t3.medium".
FAIL: tests/main.tftest.hcl
The test was passing before the change.
Your task
Investigate the cause and remediate.
Evidence to discover
# Check the recent commits
git log --oneline -10
# Check the diff
git diff HEAD~1 main.tf
# Check the test
cat tests/main.tftest.hcl
Questions to answer
- What changed in the configuration?
- What does the test expect?
- What is the correct remediation?
Recovery procedure
-
Identify the cause. The recent commit changed the
instance_typefromt3.smalltot3.medium. The test expectst3.small. The test is now wrong. -
Decide the remediation. The configuration change is intentional. The test should be updated to match.
-
Apply the remediation.
# Update the test to match the new configuration
vim tests/main.tftest.hcl
# Run the test locally
terraform test
# Verify the test passes
echo "Test passed"
- Document the change.
git add tests/
git commit -m "Update test for new instance type"
git push
The CI pipeline re-runs the test. The test passes.
Remediation
The cause was the configuration change without a corresponding test update. The remediation was to update the test. The CI pipeline is green.
Prevention
- Update the test when the configuration changes.
- Run the test locally before pushing.
- Use the test to validate the change.
- Document the test update in the same commit as the configuration change.
What you learned
- A test failure after a change is a signal that the test is out of date.
- The test should be updated alongside the configuration.
- The CI pipeline is the production control.
- The local test is the first line of defence.