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

Support locals block inside tftest.hcl Terraform Test file. #34629

Open
sheeeng opened this issue Feb 7, 2024 · 2 comments
Open

Support locals block inside tftest.hcl Terraform Test file. #34629

sheeeng opened this issue Feb 7, 2024 · 2 comments
Labels
enhancement new new issue not yet triaged terraform test

Comments

@sheeeng
Copy link

sheeeng commented Feb 7, 2024

Terraform Version

terraform version
Terraform v1.7.2
on darwin_arm64

Use Cases

We don't want to repeat the same template string inside tftest.hcl Terraform Test file.

Please support locals block inside tftest.hcl Terraform Test file.

Attempted Solutions

locals {
  variable_name = "${run.setup_tests.variable_value_one}_${run.setup_tests.variable_value_two}"
}
$ terraform init -backend=false && terraform test --verbose --verbose
Initializing modules...

│ Error: Unsupported block type

│   on tests/main.tftest.hcl line 13:
│   13: locals {

│ Blocks of type "locals" are not expected here.

Proposal

No response

References

No response

@sheeeng sheeeng added enhancement new new issue not yet triaged labels Feb 7, 2024
@crw
Copy link
Collaborator

crw commented Feb 7, 2024

Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again!

@JohnEricson
Copy link

I want this feature as well to be able to define asserts once and then use them in several run blocks in the same *.tftest.hcl file. It's not possible to use variables { .. } for this as I need to use variables in the variable values resulting in Variables may not be used here. errors.

What I want to do:

variables {
  # Not a module variable. Used for reusing asserts in all runs.
  assert_test = {
    a = {
      expected = "test_a_correct_value"
      error_message = "Not the expected value in Test A"
    },
    b = {
      expected = "test_b_correct_value"
      error_message = "Not the expected value in Test B"
    }
  }
}

run "run_1" {
  command = plan

  variables {
    mode = "1"
  }

  assert {
    condition     = output.a == var.assert_test.a.expected
    error_message = var.assert_test.a.error_message
  }
}

run "run_2" {
  command = plan

  variables {
    mode = "2"
  }

  assert {
    condition     = output.a == var.assert_test.a.expected
    error_message = var.assert_test.a.error_message
  }
}

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new new issue not yet triaged terraform test
Projects
None yet
Development

No branches or pull requests

4 participants