Skip to content

terraform test parses all global variables instead of just what it needs #34070

@CodeBehind

Description

@CodeBehind

Terraform Version

Terraform v1.6.1

Terraform Configuration Files

  variable "myvar_a" {
      type = string
  }
  
  output "my_output" {
      value = var.myvar_a
  }

Debug Output

│ Error: Variables not allowed
│
│   on <value for var.myvar_b> line 1:
│   (source code not available)
│
│ Variables may not be used here.

Expected Behavior

test.tftest.hcl... in progress

run "run"... pass
test.tftest.hcl... tearing down
test.tftest.hcl... pass

Success! 1 passed, 0 failed.

Actual Behavior

Testing with

Just add this as "test.tftest.hcl" to the root folder.

  run "run" {
      command = plan 
  
      assert {
          condition = length(var.myvar_a) > 1 
          error_message = "Is not greater than 1"
      }
  }

When i run a "terraform plan" or a "terraform test " while having "TF_VAR_myvar_a" set in my environment, both commands are doing the same (they succeed like expected).

When i now add TF_VAR_myvar_b (it's not declared in my main.tf file) terraform plan ignores the variable and still succeeds, while terraform test is failing with the mentioned debug output.

I think terraform test should ignore unused variables as terraform plan does, due to having multiple projects with different variables and secrets set via environment variables.

Steps to Reproduce

  1. Create main.tf with the content of the terraform configuration.
  2. Create test.tftest.hcl with the content of the tftest.hcl configuration from above.
  3. Add TF_VAR_myvar_a to your environment variables with any text
  4. Run terraform init
  5. Run terraform plan
  6. Run terraform test
  7. Now add TF_VAR_myvar_b to your environment with any text
  8. Run terraform plan
  9. Run terraform test <--- this produces the error

Additional Context

No response

References

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    bugconfirmeda Terraform Core team member has reproduced this issueexplaineda Terraform Core team member has described the root cause of this issue in codeterraform test

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions