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?