tofu test: a run block's `variables` cannot reference the test file's top-level `variables`
Community note
[!TIP] Hi there, OpenTofu community! The OpenTofu team prioritizes issues based on upvotes. Please make sure to upvote this issue and describe how it affects you in detail in the comments to show your support.
OpenTofu Version
OpenTofu v1.12.6
OpenTofu v1.13.0-beta1Both affected. Terraform 1.8.3 and 1.16.2 accept the same configuration.
OpenTofu Configuration Files
main.tf:
variable "input" {
type = string
}
output "out" {
value = var.input
}main.tftest.hcl:
variables {
helper = "hello"
}
run "check" {
command = plan
variables {
input = var.helper
}
assert {
condition = output.out == "hello"
error_message = "expected hello"
}
}Note that helper is declared only in the test file's top-level variables block — it is
deliberately not a variable of the module under test.
Debug Output
main.tftest.hcl... fail
run "check"... fail
Error: No value for required variable
on main.tf line 1:
1: variable "input" {
The root module input variable "input" is not set, and has no default value.
Error: Unsupported attribute
on main.tftest.hcl line 9, in run "check":
9: input = var.helper
├────────────────
│ var is object with no attributes
This object does not have an attribute named "helper".
Failure! 0 passed, 1 failed.Expected Behavior
var.helper inside a run block's variables block resolves to the value defined in the
test file's top-level variables block, so the module receives input = "hello" and the test
passes. This is what Terraform does — terraform test reports Success! 1 passed, 0 failed.
on 1.8.3 and on 1.16.2.
Actual Behavior
var is an object with no attributes inside the run block's variables block, so the
reference fails and the module variable ends up unset.
Steps to Reproduce
tofu inittofu test
Additional Context
This pattern is useful for expressing a value once and reusing it in both the inputs of a run
block and its assertions, which is how we hit it: a shared module's test file defines a set of
expected backoff/retry values at file level and references them from several run blocks.
#1213 added support for var. references in the top-level variables block (resolving to
module input values supplied via tfvars/TF_VAR_). This report is the other direction:
referencing a top-level test variable from a run block. I could not find an existing issue
covering it.
References
- #1213 — related, but about
var.inside the top-levelvariablesblock
Workaround
Verified working on OpenTofu 1.12.6 and 1.13.0-beta1, and still passing on Terraform 1.8.3 and 1.16.2, so the reshaped file stays portable:
- Inline the value at each use site and delete the top-level
variablesblock, or - if the value is worth defining once, declare it as a variable of the module under test with
a default and reference
var.<name>— module variables resolve correctly in both tools.
The second option keeps the "define once" property but changes the module's public surface, which is unattractive for a shared module. Neither is a reason not to fix the reference, but both unblock today.
Source: opentofu/opentofu