Terraform test framework mock_provider vs provider plugin: computed attribute fidelity boundary
0 reputation · 05 Jan 2024, 13:26 UTC
0 reputation · 05 Jan 2024, 13:26 UTC
The native terraform test framework with mock_provider blocks enables plan and apply assertions without production credentials by satisfying provider schemas with generated or overridden values. This creates an integration boundary between Terraform core's test harness and the provider plugin's actual runtime behavior.
Mocked resources return computed attributes as placeholder values unless explicitly overridden in the mock block. Schema validation is enforced, but provider-side API constraints — quotas, eventual consistency windows, server-side defaults, and permission checks — are not simulated. A test that passes with mocks may still fail against the real provider when computed values diverge or API-level validation rejects the request.
Determine how to structure test assertions so that mocked computed attributes meaningfully approximate real provider output, without relying on provider-specific endpoint overrides or local emulators.
Which computed attribute patterns are safe to assert against generated placeholders versus requiring explicit overrides? How can test authors detect when a mock's schema-level validation passes but the real provider would reject the same configuration at apply time? What strategy keeps the test suite credible without introducing credential-dependent steps?
31275 reputation · 06 Jan 2024, 01:22 UTC
Which computed attributes can safely rely on generated placeholders, and which need explicit overrides?
mock_provider block with a fixed sentinel value so assertions are meaningful and stable.How to detect when a mock passes schema validation but the real provider would reject the configuration?
mock_provider and once against the real provider in a sandbox or dedicated test account.Strategy for a credible suite without credential-dependent steps
mock_provider for all resources; override only API-dependent attributes with sentinels."0000-00-00T00:00:00Z" to avoid flaky assertions.mock_provider "example" {
mock_resource "example_thing" {
defaults = {
id = "sentinel-id" # API-dependent
fqdn = "placeholder.example.com" # deterministic
}
}
}
terraform test -var-file=real.tfvars
terraform show -no-color > real_output.txt
diff real_output.txt mock_output.txt
This guidance assumes Terraform 1.6+ where terraform test and mock_provider are available. Mocks cannot simulate quotas, permission checks, or eventual consistency, so mock-only suites will always carry residual risk. One detail that would refine this answer: the specific computed attributes of your provider and any documented server-side defaults, which determine the deterministic vs. API-dependent split.
Use comments to ask for clarification. Post a solution as an answer.
No question comments on this page.