skip to content

How much of a Terraform module's `terraform test` suite would you let run on `mock_provider` before a green result stops proving anything, and what rule would you set?

level: principalimportance: nice to knowfreq 22%

answer

  1. a percentage is the wrong unit
  2. who decides this assertion's result
  3. some assertions read back your own text
  4. what does the wrapper still forward
  5. thin wrappers differ from logic-heavy modules

basics

~20 s

No fixed percentage works: Terraform mocks can carry what a module's own logic decides, never what the provider decides at plan or apply. Assertions reading only authored values prove nothing, so keep one real-provider path per module.

solid answer

~50 s

I would not set a percentage; I would classify each assertion by who decides its result. If the module's own expressions decide it, such as `for_each` keys, conditionals, validation rules and wiring, mocks are fine and can carry most of a logic-heavy module's suite. If the provider's plan or apply decides it, a mock has no answer: Terraform's mock never asks the real plugin to plan, so replacements and provider-side diffs do not appear. An `assert` that reads back a value from `mock_resource` `defaults` proves nothing and gets deleted. So the rule I would set is this: every module keeps at least one real-provider path, thin resource wrappers lean on real runs, mock data lives in shared `.tfmock.hcl` files with values copied from real output and rechecked at provider upgrades, and `override_module` is allowed only where the child has its own tests.

code

hcl · 18 lines
hcl
# tests/mixed.tftest.hcl
provider "aws" {}

mock_provider "aws" {
  alias = "fake"
}

run "logic_on_the_mock" {
  providers = {
    aws = aws.fake
  }
}

run "accepted_by_the_real_provider" {
  providers = {
    aws = aws
  }
}

go deeper

for a junior

Know that a mocked pass means the configuration's logic behaved, not that a cloud accepted anything. Being able to say that plainly is already useful.

for a middle

Sort example assertions into two groups, those the module decides and those the provider decides, and say which group a mock can answer.

for a senior

Point at concrete mechanisms: no replacement reported, reads answered from state, validation still forwarded, child checks removed by a module override. Then show how you would catch a decorative test.

for a principal

Own the policy and its exceptions: a real path per module, separated files, an owner for mock data, and a ration on module overrides. Explain the trade against speed and cost, and how it varies by module type.

## Why a percentage is the wrong unit There is no single right number here, and a target such as "80 percent mocked" measures the wrong thing. A suite can be entirely mocked and useful, or half real and decorative. What matters is whether an assertion could fail for a reason you care about. So the decision is made per assertion: **who decides the result**, the module's own expressions or the provider? | Assertion about | Decided by | Does a mock suffice? | |---|---|---| | `for_each` keys, `count`, conditionals, locals | the module's expressions | yes | | `validation` rules and preconditions | the module, given realistic input | yes, with authored data of the right shape | | attribute names, types, configuration validators | the real plugin, still consulted | yes | | whether a change updates or replaces | the provider's plan response | no | | provider-side plan differences, real computed values | the provider | no | | a value read back from `defaults` | nobody; it is your own string | delete the test | ## What the mock actually calls A decision like this should rest on the mechanism, so know what the wrapper around the real plugin does: - **Forwarded to the real plugin:** the schemas, resource and data source configuration validation, and provider-defined function calls. - **Replaced:** planning and applying a resource. The mock's plan response carries no replacement paths, so a change the real provider would treat as a replacement shows as an in-place update. - **Replaced:** reading a resource, which returns what is already in state, so a mocked run never sees the object change outside Terraform. - **Refused:** importing. The mock returns an error telling you to use `override_resource` instead. - **Removed further by overrides:** `override_module` stops the child's resources, input validation and output preconditions from running at all. ## The policy I would set These are defaults for a platform team to adopt and then adjust: 1. **One real path per module.** At least one test creates the module's main case through a real provider. Without it, nobody has shown that the configuration is accepted. 2. **Separate the files.** Keep real runs in their own `.tftest.hcl` file, so a credential-free job can select the mocked files with `terraform test -filter=...`. Which tier runs on which event is a separate decision. 3. **Use an alias when one case needs both.** `provider "aws" {}` and `mock_provider "aws" { alias = "fake" }` can share a file, and each `run` block picks one through `providers`. Mocking a single alias also suits a second account the job cannot reach. 4. **Ban self-confirming assertions.** In review, reject any `assert` whose expected value is the same string as a default in the mock data. 5. **One owner for mock data.** Keep it in shared `.tfmock.hcl` files, copy values from real output, and recheck them on provider upgrades and whenever a real run disagrees. 6. **Ration `override_module`.** Allow it only where the child has its own tests, because it removes the child's checks from the parent's view. ## How the answer varies by module The same policy gives different ratios: - **A composition module** with heavy conditionals, generated names and wiring between children can sensibly be mostly mocked, since most of what can break is its own logic. - **A thin wrapper** around one resource has almost no logic of its own. Its value is how the provider behaves with those arguments, so a mocked pass says little and real runs should dominate. - **A module that enforces a safety property,** such as blocking public access, needs real evidence for that property, however cheap the mock is. - **A module with slow or costly resources** pushes more onto mocks. Accept that trade openly, and write down what is no longer proved. ## Signs the line is in the wrong place Review these periodically instead of a coverage number: - A green mocked suite was followed by a first real apply that failed, more than once. - Breaking an expression in the module on purpose leaves every test green. That is the cheapest honest measure of a suite. - A test change is mostly edits to mock data rather than assertions. - A provider major upgrade left mocked tests passing and nobody looked at the mock data. - Warnings about `Invalid override target` have become background noise. The trade itself is real on both sides. Mocks give speed, no credentials, no bill and no flakiness from a cloud. Real runs give the only evidence of how the provider plans and applies. A lead's job is to say which questions each is allowed to answer, not to pick a winner.

  • How would you mock only one provider alias in a module that uses two?
    Declare `provider "aws" {}` for the real one and `mock_provider "aws" { alias = "replica" }` for the other, matching the alias the configuration uses. Resources on `aws.replica` get mock data and the rest call the cloud, so the job still needs credentials for the real side. It fits a second account the job cannot reach.
  • How do you tell a decorative mocked suite from a useful one?
    Break the module on purpose: invert a conditional, drop a `for_each` key, rewire an output. A useful suite goes red. If it stays green, its assertions were reading authored values rather than exercising logic. This costs minutes, needs no credentials, and tells you more than any percentage of mocked tests.
  • What would make you accept a module with no real-provider test at all?
    A composition module that holds no resources of its own, whose children each have real tests. Its only logic is wiring, and that is what mocks and `override_module` can exercise. I would record the exception. A module holding a resource does not qualify, because nobody has then seen the provider accept its configuration.

  • Hitting a fixed share such as 80 percent mocked makes a suite healthy.
  • A green mocked suite shows the provider will accept the change.
  • Because the mock uses the real schema, a mocked plan shows real replacements.
  • Asserting on a mock_resource default is a valuable regression test.
  • Real-provider runs can be dropped once mocked coverage is high.
  • A thin single-resource wrapper is the best candidate for a fully mocked suite.