Terraform test 1.6.x > Potential bug when command = apply "Unknown condition value"

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?

Hi @sebastien.latre, I think this is a bug as well. Thanks for finding it.

What’s happening is Terraform is trying to be clever and is removing values from that state that it thinks aren’t being used anywhere. The testing framework is failing to declare that it needs that output so it’s not available when it comes time to process the conditions within the run block.

I’m surprised no one has encountered this before - I’ll arrange for a fix to be included in the upcoming v1.7.x series. I’m not sure exactly where the state cleanup is happening yet, so I can’t point to exactly where the fix should be. It’s possible we’ve not seen this before because no one has tried this with a module that doesn’t have any resources, and doesn’t capture the outputs in an external module.

A potential workaround for you in the meantime is to create an output block in your root configuration that exposes the nested output you want to verify. You can then write your test assertion against that output instead of the nested output. Root module outputs are never tidied up in the same way that nested module outputs are.

I’ve captured this as a bug in `terraform test`: Modules with no resources aren't saving their output values · Issue #34476 · hashicorp/terraform · GitHub, if you’d like to track the fix.

Thanks a lot for tracking the bug and your workaround did the trick perfectly!