002 · Domain 6: Advanced Module Usage and Registry Practices · 10% of the exam
Task 6.4: Validate modules with Terraform unit tests
terraform test for module authors: run blocks with plan or apply, assertions, expect_failures, variables in test files, mock providers and overrides, and what each run leaves behind.
Study it
terraform test: run blocks, assertions, expected failures and mocks
Lesson coming
Sample questions
Try each one before opening the answer. Every option is explained, with the HashiCorp documentation page that proves it.
Question 1 · choose 1
A module test checks that an invalid instance type is rejected:
run "rejects_unknown_type" {
variables {
instance_type = "m1.xlarge"
}
expect_failures = [var.instance_type]
}
The variable's validation block rejects the value, yet terraform test
reports this run as failed. What should the author change?
AList the validation block itself, var.instance_type.validation, in expect_failures
BAdd an assert block whose condition is false to make the run expect an error
CUse a mock_provider for the run to keep the failure away from a real API
DAdd command = plan to the run; under the default command = apply, a condition failing at plan fails the run
Show the answer and why
AList the validation block itself, var.instance_type.validation, in expect_failures
Incorrect
expect_failures takes checkable objects such as the input variable; it is already listed correctly.
BAdd an assert block whose condition is false to make the run expect an error
Incorrect
A failing assertion is a test failure; it does not mark the validation failure as expected.
CUse a mock_provider for the run to keep the failure away from a real API
Incorrect
Variable validation runs during planning and does not depend on the provider; mocking does not change the outcome.
DAdd command = plan to the run; under the default command = apply, a condition failing at plan fails the run
Correct
A run with command = apply that expects a failure fails overall when the condition fails during plan; expected failures are best tested with command = plan.
Run blocks default to command = apply. Terraform only expects failures in the operation the run performs, so validation failures that occur at plan time are tested with command = plan.
A test file uses mock_provider "aws" {} so that tests run without credentials. One assertion checks that startswith(aws_s3_bucket.logs.arn, "arn:aws:s3:::") is true, and it fails. Nothing else is wrong. What explains the failure, and what fixes it?
AMocked providers leave computed attributes null; add override_during = plan so they are generated
BA computed string gets a random 8-character value; set arn in a mock_resource defaults block
CAssertions cannot read attributes of mocked resources; move the check into a check block
DThe arn is not known until apply; set command = apply in the run block
Show the answer and why
AMocked providers leave computed attributes null; add override_during = plan so they are generated
Incorrect
Mocked providers do generate values for computed attributes, so the arn is not null; it exists but has the wrong shape.
BA computed string gets a random 8-character value; set arn in a mock_resource defaults block
Correct
Mock providers do not know the format of computed attributes, so strings are random; mock_resource defaults supply a specific value.
CAssertions cannot read attributes of mocked resources; move the check into a check block
Incorrect
Assertions can reference values from mocked resources; they behave like real resources inside the test.
DThe arn is not known until apply; set command = apply in the run block
Incorrect
Run blocks already default to apply, and the generated value would still not match the format.
Mocked providers produce placeholder data: random strings, zero numbers, false booleans and empty collections. When a test depends on a value's shape, give it explicitly with mock_resource, mock_data or an override.
A test file sets bucket_prefix = "unit" in its variables block. The CI job also exports TF_VAR_bucket_prefix=ci and passes -var bucket_prefix=cli. Which value do the run blocks see?
Acli, from the command-line flag
Bci, from the environment variable
Cunit, from the .tftest.hcl
DTerraform errors on the conflict
Show the answer and why
Acli, from the command-line flag
Incorrect
Test file variables override command-line input.
Bci, from the environment variable
Incorrect
Environment variables rank lower still.
Cunit, from the .tftest.hcl
Correct
Variables defined in test files take the highest precedence.
DTerraform errors on the conflict
Incorrect
Precedence resolves it.
Precedence among other sources stays the same within tests.