How do you write unit tests for policy rules without deploying?
AdvancedScenarioTesting Upgrades and Large-Scale Operations
Answer
Situation read → Policies need tests like code.
What I'd do →
- Keep a folder of sample plan JSONs (good and bad cases).
- Run
conftest verifyfor Rego unit tests, oropa test policy/. - Generate sample plans from small fixtures using
terraform show -json. - Run policy tests in CI whenever policies change.
Why → Untested policies either block everything or nothing.
Interview tip → Test both the deny and the allow path.
More on Testing Upgrades and Large-Scale Operations
What does each of `terraform validate`, `plan` and `terraform test` actually check?BeginnerWrite a native test for a module that creates an encrypted S3 bucket.IntermediateCompare Terratest with `terraform test`.IntermediateReal-cloud integration tests leave orphaned resources when they fail midway. How do you handle that?AdvancedHow do you upgrade from Terraform 1.5 to 1.9 safely?IntermediateMajor AWS provider upgrade from v4 to v5 (or v5 to v6) across many stacks. How do you plan it?Advanced