Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

terraform test is not applying defaults to variables referenced in assertions #33864

Closed
liamcervante opened this issue Sep 11, 2023 · 1 comment · Fixed by #33867
Closed

terraform test is not applying defaults to variables referenced in assertions #33864

liamcervante opened this issue Sep 11, 2023 · 1 comment · Fixed by #33867
Assignees
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code

Comments

@liamcervante
Copy link
Member

Terraform Version

`v1.6.0-beta1`

Terraform Configuration Files

variable "input" {

  type = object({
    required = string
    optional = optional(string)
    default = optional(string, "default")
  })

  default = {
    required = "required"
  }

}

resource "test_resource" "resource" {
  value = var.input.default
}

output "computed" {
  value = test_resource.resource.value
}
# main.tftest.hcl


run "stacked" {
  variables {
    input = {
      required = "required"
      optional = "optional"
      default = "overridden"
    }
  }

  assert {
    condition = output.computed == "overridden"
    error_message = "did not override default value"
  }
}

run "defaults" {
  assert {
    condition = output.computed == "default"
    error_message = "didn't set default value"
  }
}

run "custom_defined_apply_defaults" {
  variables {
    input = {
      required = "required"
    }
  }

  assert {
    condition = output.computed == "default"
    error_message = "didn't set default value"
  }
}

Debug Output

N/A

Expected Behavior

Tests should apply defaults and pass.

Actual Behavior

        main.tftest.hcl... in progress
          run "stacked"... pass
          run "defaults"... pass
          run "custom_defined_apply_defaults"... fail
        ╷
        │ Error: Invalid value for input variable
        │ 
        │ The given value is not suitable for var.input declared at main.tf:2,1-17:
        │ attributes "default" and "optional" are required.
        ╵
        main.tftest.hcl... tearing down
        main.tftest.hcl... fail
        
        Failure! 2 passed, 1 failed.

Steps to Reproduce

terraform init
terraform test

Additional Context

We're missing a call to apply the defaults before we convert the variables here.

I've looked at this again, and I think we're taking the wrong approach. We should have the computed input values from the plan already available - so let's just use that? Will see if possible.

References

First discovered by here.

@liamcervante liamcervante added bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code labels Sep 11, 2023
@liamcervante liamcervante self-assigned this Sep 11, 2023
Copy link

I'm going to lock this issue because it has been closed for 30 days ⏳. This helps our maintainers find and focus on the active issues.
If you have found a problem that seems similar to this, please open a new issue and complete the issue template so we can capture all the details necessary to investigate further.

@github-actions github-actions bot locked as resolved and limited conversation to collaborators Dec 10, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
bug confirmed a Terraform Core team member has reproduced this issue explained a Terraform Core team member has described the root cause of this issue in code
Projects
None yet
Development

Successfully merging a pull request may close this issue.

1 participant