Tf test 1.8 > Cannot test custom data source output?

I’m testing a module defining in it a custom datasource (with external provider).
I would like to be able to test the output of it from the terraform test framework with a pretty simple configuration but that isn’t working as I would expect.
I re-read the documentation several times and seems to be possible to use that so I’m a bit puzzled.

Context:

terraform version
Terraform v1.8.3
on windows_amd64

module_to_test/main.tf

terraform {
  required_providers {
    external = {
      source  = "hashicorp/external"
      version = "~> 2.3.2"
    }
}

variable "git_configuration" {
  type = object({
    name    = string
    web_url = string
  })
}

data "external" "azuredevops_credential" {
  count = var.git_configuration != null ? 1 : 0

  program     = ["bash", "configure_git_credentials.sh", ...]
  working_dir = "${path.module}/assets"
}
[...]

And I would like to test its output in that way:
module_to_test/tests/unit.tftest.hcl

[...]
run "test_run" {
  command = apply

  assert {
    condition     = length(data.external.azuredevops_credential) == 1
    error_message = "A datasource data.external.azuredevops_credential should have an item"
  }
  assert {
    condition     = data.external.azuredevops_credential[0].git_username == "terraform"
    error_message = "A datasource data.external.azuredevops_credential[0].git_username should have the right value. Actual=${data.external.azuredevops_credential[0].git_username}, Expected=_"
  }
}

But after running
terraform init
terraform test

it gives me the following error:

  run "databrick_dbk_with_azdo"... fail
╷
│ Error: Unknown variable
│
│   on tests\unit.tftest.hcl line 280, in run "test_run":
│  280:     condition     = data.external.azuredevops_credential[0].git_username == "terraform"
│
│ There is no variable named "data".

Any idea on what I’m doing wrong?

Hi @sebastien.latre, it looks like Terraform is producing the wrong error message here.

I think you are missing the result attribute in your reference, your condition should read:

data.external.azuredevops_credential[0].result.git_username

I’ve been able to replicate this, but in my replication I do see an additional correct error message - do you see that as well? This is my full output:

main.tftest.hcl... in progress
  run "test"... fail
╷
│ Error: Unknown variable
│ 
│   on main.tftest.hcl line 8, in run "test":
│    8:         condition = data.external.resource[0].hello == "world"
│ 
│ There is no variable named "data".
╵
╷
│ Error: Unsupported attribute
│ 
│   on main.tftest.hcl line 8, in run "test":
│    8:         condition = data.external.resource[0].hello == "world"
│ 
│ This object has no argument, nested block, or exported attribute named "hello".
╵
main.tftest.hcl... tearing down
main.tftest.hcl... fail

The second unsupported attribute error is correct, and should be the only one Terraform displays.

If you fix the reference, this should unblock you. I’ve filed `terraform test`: Additional erroneous error message when referencing invalid attributes · Issue #35265 · hashicorp/terraform · GitHub to track removing the additional error message.

Indeed, there is an additional message but was so confused by the first one that I didn’t even fully read the second one.
I’m too much used in “fix the first error you find before going further”.
So this one was a bit stupid.
Sorry for that and thanks to fix it for future users :slight_smile: