skip to content

Under Terraform's `mock_provider`, why does a format-checking validation reject a computed ARN in a credential-free CI job running a module's `.tftest.hcl` tests, and how do you fix it?

level: seniorimportance: should knowfreq 30%

answer

  1. type known, format unknown
  2. skipped while unknown, checked once known
  3. fix the producer, not the consumer
  4. when versus what
  5. whose string does the pass now prove

basics

~20 s

Terraform's mock_provider knows an attribute's type, not its format, so a computed arn arrives as a random 8-character string and fails the regex at apply. Set an ARN-shaped value with mock_resource defaults or override_resource on the producing resource.

solid answer

~50 s

The generator knows types but not formats, so `arn` becomes a random 8-character string. During plan that attribute is unknown, so a `variable` `validation` reading it is skipped; at apply the string is known, the rule runs again and the regex fails. A provider's own configuration validators can fail the same way, because the mock forwards validation to the real plugin. The fix goes on the resource that produces the value: a `mock_resource` `defaults` entry for the type, or an `override_resource` for one address, holding something like `arn:aws:s3:::name`. Add `override_during = plan` if a `command = plan` run must see it, otherwise that run never evaluates the rule. Do not loosen the validation. And be honest about the result: the pass now proves the wiring and the rule against a string you typed, not that AWS returns that format.

code

hcl · 16 lines
hcl
mock_provider "aws" {
  mock_resource "aws_s3_bucket" {
    override_during = plan
    defaults = {
      arn = "arn:aws:s3:::name"
    }
  }
}

run "reader_accepts_the_bucket_arn" {
  command = plan

  variables {
    bucket_name = "logs"
  }
}

go deeper

for a junior

Remember that a mock invents a random 8-character string for an ARN. It looks wrong because the mock knows the attribute's type and not its format.

for a middle

Trace the two steps: unknown during plan so the rule is skipped, known during apply so the rule fails. Then name mock_resource defaults as the place the value belongs.

for a senior

Refuse the tempting fixes. Loosening the rule, going plan-only and overriding the child all turn the job green while checking less. Fix the producer, and state exactly what the pass now proves.

for a principal

Use this failure as evidence of a cost: every format check under mocks needs authored data, and that data can agree with a wrong rule. Plan for at least one real-provider run to catch that.

## The failure A CI job has no cloud credentials, so a module's tests use `mock_provider "aws" {}`. A **computed attribute** is one the provider normally supplies, and here the root module passes one into a child whose input has a **validation rule**, a condition Terraform checks against the value: ```hcl # main.tf resource "aws_s3_bucket" "logs" { bucket = var.bucket_name } module "reader" { source = "./modules/reader" bucket_arn = aws_s3_bucket.logs.arn } # modules/reader/variables.tf variable "bucket_arn" { type = string validation { condition = can(regex("^arn:aws:s3:::", var.bucket_arn)) error_message = "bucket_arn must be an S3 ARN." } } ``` The test fails with that `error_message`, although the code is correct and passes against a real provider. ## Why it fails where it does The sequence inside one `run` block with the default `command = apply` is: 1. **Plan.** The mock returns `arn` as unknown, as a real provider would. The child's input is unknown, the condition is unknown, and Terraform skips the rule for now. 2. **Apply.** The mock fills `arn` by its generation rules. A string attribute gets a random 8-character value, since the mock has no idea what an ARN looks like. 3. **Validation again.** The input is now known, the rule is evaluated during apply, and the regex rejects the string. The same shape appears elsewhere: - **Provider validators.** A mock forwards resource configuration validation to the real plugin. Terraform repeats that validation when it plans a resource, including the re-plan during apply, so a consuming resource whose provider checks the attribute's format can reject the same string. - **Data sources.** A mocked data source is filled in one step, so its generated value is known during plan, and the failure moves from apply to plan. ## The fix Give the **producer** a value with the right shape. The rule and the consumer are correct, and only the mock's data is wrong. Any of these places the value upstream, so everything that reads it sees the same string: | Fix | Scope | Use it when | |---|---|---| | `mock_resource` with `defaults` | every resource of that type under this mock provider | one shape suits them all | | `mock_data` with `defaults` | every data source of that type | the ARN comes from a lookup | | `override_resource` with `target` | one address | resources of one type need different values | | a `.tfmock.hcl` file through `source` | every test file that names it | several test files share the data | ## `override_during` and plan-only runs A `command = plan` run changes the picture: - With no plan-time value, the rule stays unknown and is skipped, so the run can go green having checked nothing. - An `assert` reading the attribute errors with `Unknown condition value`. - `override_during = plan` on the `mock_resource` makes the value known during plan and reuses it at apply. The value is a bare keyword, `plan` or `apply`. - It changes **when**, never **what**. Without `defaults` the value is still random. In the source, a type with no `mock_resource` block stays unknown during plan even when the `mock_provider` sets `override_during = plan`, although the docs page reads more broadly. - File-level and run-level override blocks do not inherit the setting from `mock_provider`, so set it on the block itself. ## Fixes that make it worse - **Loosening the regex.** This removes a production check to please a test double. - **Switching to `command = plan`.** The test goes green because the rule never saw a value. - **Overriding the consumer.** An `override_resource` on the consuming resource still has its configuration validated, and the bad value still comes from upstream. - **Using `override_module` on the child.** The child's validation stops running, so the test is green and the rule is untested. ## What the green test proves After the fix, the test proves two things: the bucket's `arn` is wired into the child, and the rule accepts a string you typed. It does not prove that the real provider returns that format. If the regex were wrong about real ARNs, the mock would agree with it, because you wrote both. Keep at least one run against a real provider for that question.

  • Why does the suite go green if you switch that `run` block to `command = plan` and change nothing else?
    Because nothing produces a value during plan. The `arn` stays unknown, so the validation condition is unknown and Terraform skips the rule. The run passes without evaluating it. An `assert` reading the same attribute would error with `Unknown condition value` instead of passing.
  • What changes if the ARN comes from a data source rather than a managed resource?
    The failure moves earlier. A mocked data source with fully known configuration is read during plan and filled in one step, so the random string is known at plan and the rule rejects it there. The fix is `mock_data` `defaults` or an `override_data`, and `override_during` is not needed for that read.
  • How do you handle two buckets of the same type that need different ARNs?
    `mock_resource` works per type, so it can only give them one value. Use one `override_resource` per address, with `target` naming each resource or instance. Remember that an override bypasses mock defaults, so list every computed attribute the test or the module reads in its `values`.

A mock's generated ARN is like a prop passport on a film set: it fills the right slot in the scene, but it fails any check that actually reads it. Making a better prop gets it past that check, and it still says nothing about what a real passport looks like.

  • A mock provider skips all validation, so a validation block cannot fail under mocks.
  • The random string means the provider plugin failed to install.
  • Loosening the validation regex is the right response to a mock's string.
  • Switching the run block to command = plan fixes the validation failure.
  • override_during = plan makes a generated arn look like a real ARN.
  • Once the ARN default is in place, the test proves AWS returns that format.