terraform
terraform copied to clipboard
Terraform testing framework: Allow to reference variables loaded from local tfvars files
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.
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!
+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
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.