Terraform Mock Data Test Fixtures Prompt
Build reusable mock fixtures for the native test framework's `mock_provider`/`override_data` so module tests run fast and offline — exercising logic, validation, and outputs without hitting real cloud APIs or creating billable resources.
- Target user
- Module authors writing fast, hermetic Terraform tests
- Difficulty
- Intermediate
- Tools
- Claude, ChatGPT
The prompt
You are a senior Terraform/IaC engineer who builds hermetic module tests with `mock_provider`, `override_resource`, and `override_data` so the suite runs in seconds with no credentials and no real infrastructure. I will provide: - The module under test, its variables, data sources, and outputs - Which data sources and computed attributes need mocking - The behaviors I want to assert (validation, output shape, conditional logic) Your job: 1. **Map the external surface** — list every data source and provider-computed attribute the module reads, since each is a candidate for mocking. 2. **Author the mock provider block** — define `mock_provider` with `mock_resource`/`mock_data` defaults, and `override_data`/`override_resource` for the specific values your assertions depend on. 3. **Make fixtures reusable** — structure mock values so multiple `run` blocks share them (via `tests/` fixture files or shared variables) instead of duplicating per test. 4. **Cover the cases** — write `run` blocks asserting on outputs and `expect_failures` for `validation`/`precondition` paths, including an edge case where a mocked value is null/empty. 5. **Keep mocks honest** — note where a mock could drift from real provider behavior and add at least one opt-in non-mocked acceptance run gated for CI. 6. **Wire into CI** — show the `terraform test` invocation, the fast (mocked) vs. slow (real) split, and how failures report. Output as: (a) the external-surface map, (b) the `.tftest.hcl` with mock_provider + overrides, (c) the shared fixture structure, (d) the assertion + expect_failures runs, (e) the CI split between mocked and real tests. Caution: mocks prove your logic, not the provider's — keep at least one real-apply acceptance test, and never let a green mocked suite be the only gate before applying to production.