In a Terraform `.tftest.hcl` file using `mock_provider`, what values do computed attributes receive, and how do `mock_resource` and `mock_data` defaults change them?
answer
- only computed, only when unset
- the value depends on the attribute's type
- types known, formats not
- defaults are per type, not per address
- source names a directory of mock files
basics
~10 sTerraform's mock_provider generates only computed attributes left unset: random 8-character strings, 0, false and empty collections. Defaults in mock_resource and mock_data replace those with chosen values, per resource or data source type.
solid answer
~40 sA `mock_provider` keeps the real provider's schema but makes up the values the cloud would have returned. Only computed attributes with no configured value are filled: strings get a random 8-character value, numbers `0`, booleans `false`, collections stay empty, and objects follow the same rules for each sub-attribute. A managed resource's values stay `(known after apply)` during plan and appear at apply; a data source gets them when it is read. The generator knows types and not formats, so an `arn` will not look like an ARN. `mock_resource` and `mock_data` blocks set `defaults` for one type, and any attribute you leave out still falls back to generation. A `source` argument loads the same blocks from `.tfmock.hcl` files in a directory, and a nested block wins. Terraform 1.17 added `mock_ephemeral` for ephemeral resources.
code
hcl · 17 lines# testing/aws/data.tfmock.hcl
mock_resource "aws_s3_bucket" {
defaults = {
arn = "arn:aws:s3:::name"
}
}
mock_data "aws_s3_bucket" {
defaults = {
arn = "arn:aws:s3:::name"
}
}
# tests/bucket.tftest.hcl
mock_provider "aws" {
source = "./testing/aws"
}go deeper
Recall the table: random 8-character strings, zero, false, empty collections. Say that only computed attributes are invented and that required arguments must still be written.
Explain plan versus apply timing, the per-type scope of defaults, the fall-back for unlisted attributes, and how a source directory of mock data files combines with nested blocks.
Show you predict breakage from these rules: an index into an empty computed list, a regex over an 8-character string, an assertion comparing against a value that changes every execution.
Treat mock data as a maintained copy of provider behaviour. Decide who owns the shared files, where the values come from, and how they get rechecked when the provider changes.
## What a mock provider has to invent A **computed attribute** is one the provider, rather than your configuration, normally supplies: an `arn`, an `id`, a generated name. A `mock_provider` block in a Terraform `.tftest.hcl` file stops the provider's create and read calls from reaching a cloud, so nothing is left to return those values. Terraform makes them up and records them in the state that `terraform test` holds in memory for the test. The mock still wraps the real provider plugin. It takes the resource and data source schemas from that plugin, so `terraform init` must still install the provider, and the generator always knows each attribute's type. It knows nothing about format: the documentation warns that generated data will rarely match the syntax a real provider returns. ## The generation rules | Attribute type | Generated value | |---|---| | string | a random 8-character alphanumeric string | | number | `0` | | bool | `false` | | list, set, map | an empty collection | | object | each sub-attribute, by these same rules | | dynamic type | `null`, because no valid value can be guaranteed | Four consequences catch people out: - **Only computed attributes.** Required arguments must still be set in the configuration or the test variables. Terraform does not invent them. - **Only when unset.** An optional computed attribute you set keeps your value. Generation and defaults fire only on values that are still null or unknown. - **Random means random.** The string differs from one execution to the next, so an assertion must never compare against a generated value. - **Empty means empty.** An expression that takes `[0]` of a computed list works against a real provider that returns items, and can fail under a mock that returns none. ## When the values appear The two kinds of object behave differently: - A **managed resource** imitates a real provider. During plan its computed attributes are `(known after apply)`, and the mock fills them during apply. The `override_during` attribute can move that to plan. - A **data source** is filled in one step, with no unknown stage. If its configuration is fully known, Terraform reads it during plan and the values are there at once. ## Choosing values with defaults Inside `mock_provider` you can write any number of `mock_resource` and `mock_data` blocks. The label is the resource or data source type, and `defaults` is an object of attributes: ```hcl mock_provider "aws" { mock_resource "aws_s3_bucket" { defaults = { arn = "arn:aws:s3:::name" } } } ``` That block answers the common complaint that a generated `arn` looks wrong, and the rules around it are narrow: 1. A default applies to **every** resource of that type handled by this mock provider. Two aliased mock providers hold separate data. 2. An attribute you do not list falls back to the generation rules. 3. A default fills the same slots as the generator, so it cannot overrule a configured value, and a key that names no computed attribute is never read. 4. A value of the wrong type fails the test with a `Failed to compute attribute` error. 5. Repeated blocks and nested collections take **one** set of values for all their instances. You cannot give each `replica` block of a table its own `arn`. ## Sharing data with `.tfmock.hcl` When several test files need the same values, move the blocks into a mock data file and point `source` at its directory: ```hcl mock_provider "aws" { source = "./testing/aws" } ``` Terraform loads every `.tfmock.hcl` and `.tfmock.json` file there. Such a file may hold `mock_resource`, `mock_data`, `override_resource` and `override_data` blocks, but not `override_module`. Two collisions behave differently. Two files in the directory defining one type is an error. A block nested in `mock_provider` and a file's block for the same type is allowed, and the nested block is used whole: the attributes are not merged. ## The version note Mocking arrived in Terraform 1.7.0. Terraform 1.17.0 added ephemeral resources to it, through a `mock_ephemeral` block that works like `mock_data`. The 1.17 docs page on mocking does not mention it yet, but the 1.17 changelog and source do, so an answer listing only two block types is one release behind.
- Does `mock_provider` remove the need to run `terraform init` for that provider?No. The mock wraps an instance of the real plugin, which supplies the schemas and validates resource configuration, so `terraform init` must still install the provider. What goes away is credentials: the mock's configure step does nothing, so the plugin never tries to authenticate against a cloud.
- If a `mock_resource` default and an `override_resource` both cover one resource, what does an attribute listed only in the default get?A generated value, not the default. An override sends that resource down its own path, which never consults the mock's data: attributes in `values` are used and any other computed attribute falls back to the generation rules. List everything the test reads in the override.
- A mocked computed string comes back empty or null, like a mocked list.
- The mock reads the provider schema and produces realistically formatted ARNs.
- A mock_resource default can overwrite a value the configuration sets.
- With no credentials needed, the provider plugin no longer has to be installed.
- Generated values stay the same between executions, so assertions can compare against them.
- A mock_resource block targets one resource address.
Assumes Terraform 1.17: mock_ephemeral for ephemeral resources arrived in 1.17.0, while test mocking itself dates from 1.7.0.