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 testing framework: Allow to reference variables loaded from local tfvars files #34538

Closed
bew opened this issue Jan 18, 2024 · 3 comments · Fixed by #34699
Closed

Terraform testing framework: Allow to reference variables loaded from local tfvars files #34538

bew opened this issue Jan 18, 2024 · 3 comments · Fixed by #34699
Labels
enhancement new new issue not yet triaged terraform test

Comments

@bew
Copy link

bew commented Jan 18, 2024

Terraform Version

Terraform v1.7.0
on windows_amd64

Use Cases

I'm working on a 'complex' module requiring a number of resources pre-creation involving multiple but common variables.

In this case I have 2 modules:

  • test-setup (pre-create a few resources)
  • networking (module under test)

Both modules take a network_config variable as input, and in our case the variable cannot be hardcoded in the test and is given dynamically through terraform.tfvars (file that is generated before the test is run).

Attempted Solutions

I need to pass a variable from local terraform.tfvars file, to the test-setup module that is not the module under test at the moment, and there is no way to refer to dynamically loaded variables in the tftest file.

You can see the issue here:

# file: main.tf  (single file `networking` module)
# file: ../test-setup/main.tf (test setup module)
variable "network_config" {
  type = object({
    vnet_name = string
    subnet_cidr = string
  })
}
# use variable..
# file: tests/terraform.tfvars
network_config = {
  vnet_name = "vnet-hello-world"
  subnet_cidr = "1.2.3.4/24"
}
# file: tests/issue.tftest.hcl

# NOTE: variable `network_config` is auto-loaded from local tfvars file

run "setup" {
  module {
    source = "../test-setup"
  }
  variables {
    # Not specifying the variable doesn't auto-pass it from tfvars (since this is for another module, not the one under test).
    # And specifying it I have no way to reference auto-loaded var.
    network_config = var.network_config       # This does NOT currently work (saying `var.network_config` doesn't exist)
  }
}

run "do-test" {
  # variable `network_config` is auto-passed to the module under test

  # NOTE: If we could reference auto-loaded vars it would be interesting to transform the variable for a specific test
  variables {
    # This does NOT currently work (saying `var.network_config` doesn't exist)
    network_config = merge(var.network_config, {
      # For this call, change the vnet name but keep the generated subnet_cidr
      vnet_name = "some-other-vnet"
    })
  }
}

Proposal

Allow tftest files to reference auto-loaded variables.

For declarative-like approach it might be interesting to allow tftest files to declare external variables that are auto-loaded, so the evaluator / checker knows a given variable actually exists?

References

Might be related to #34534 for the ability to reference variables in variables block.

@crw
Copy link
Contributor

crw commented Jan 18, 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!

@novekm
Copy link

novekm commented Jan 28, 2024

+1, I believe I'm running into a similar issue (defined in this GitHub Issue - #34584 I created), but in my case Terraform itself is crashing (not sure if that's the same issue OP is describing). Will try again with hardcoded values

Copy link
Contributor

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 Mar 25, 2024
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
enhancement new new issue not yet triaged terraform test
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants