Hello everyone!
I have the impression to face a bug in the new terraform test framework 1.6.x.
I’m currently using Terraform 1.6.6 on Windows through Git bash with the following minified context (the real use case is much more complex):
module issue_project under test (./issue_project):
terraform {
required_providers {
azurerm = {
source = "hashicorp/azurerm"
version = "~> 3.85.0"
}
}
}
module "resourcegroups" {
source = "../issue_resourcegroups"
}
module issue_resourcegroups (./issue_resourcegroups):
module "resourcegroup_N" {
source = "../issue_resourcegroup"
}
output "resource_group" {
value = module.resourcegroup_N.resource_group
}
module issue_resourcegroup (./issue_resourcegroup):
resource "azurerm_resource_group" "resource_group" {
name = "CDLDA98GBL0"
location = "westeurope"
tags = {
environment = "DEV"
}
}
output "resource_group" {
value = azurerm_resource_group.resource_group
}
When I try to execute the following test (./issue_project/tests/apply_issue.tftest.hcl):
provider "azurerm" {
subscription_id = "xxxxxxxx-xxxx-xxxx-xxxx-fb71050d9c5c"
tenant_id = "xxxxxxxx-xxxx-xxxx-xxxx-1c7d8de6bf2c"
features {
}
}
run "apply" {
command = apply
assert {
condition = length(module.resourcegroups.resource_group) == 6
error_message = "Fail"
}
}
I’m getting the following issue:
$ terraform test
tests\apply_issue.tftest.hcl... in progress
run "apply"... fail
╷
│ Error: Unknown condition value
│
│ on tests\apply_issue.tftest.hcl line 14, in run "apply":
│ 14: condition = length(module.resourcegroups.resource_group) == 6
│
│ Condition expression could not be evaluated at this time. This means you
│ have executed a `run` block with `command = plan` and one of the values
│ your condition depended on is not known until after the plan has been
│ applied. Either remove this value from your condition, or execute an
│ `apply` command from this `run` block.
╵
tests\apply_issue.tftest.hcl... tearing down
tests\apply_issue.tftest.hcl... fail
Failure! 0 passed, 1 failed.
I’m already running the test in apply mode…
For information, it’s working perfectly with the exact same code and command = plan in the run:
$ terraform test
tests\apply_issue.tftest.hcl... in progress
run "apply"... pass
tests\apply_issue.tftest.hcl... tearing down
tests\apply_issue.tftest.hcl... pass
Success! 1 passed, 0 failed.
Any idea how to troubleshoot this?