How do mock providers work in `terraform test`?
AdvancedProductionTesting and Policy as Code
Answer
Since 1.7 you can declare mock_provider "aws" {} so tests run without credentials or cloud calls; computed attributes get generated values, and you can set specific ones with mock_resource, mock_data or override_resource.
mock_provider "aws" {
mock_data "aws_availability_zones" {
defaults = { names = ["ap-south-1a", "ap-south-1b"] }
}
}
They are great for logic tests (conditionals, for_each keys, validation) in seconds, but cannot prove the cloud accepts your config.
More on Testing and Policy as Code
How do you test infrastructure code, and what does the testing pyramid look like?BeginnerWhat does the static-analysis layer catch?BeginnerWhat does tflint do, and how is it configured?BeginnerHow do Checkov and Trivy differ?IntermediateHow does `terraform test` work?BeginnerWhen do you use Terratest instead of `terraform test`?Intermediate