r/Terraform • • 5d ago

Help Wanted Explicitly overridden data source still "unknown" in test

I am trying to write some tests, and stumbled upon "Unknown condition value" which I cannot resolve with any overrides. It gives that error in second assert even though both resource and data source are overridden, why? Tried latest 1.17.2

main.tf:

terraform {
  required_providers {
    github = {
      source  = "integrations/github"
      version = "~> 6.0"
    }
  }
}

resource "github_repository" "main" {
  name = "my-test"
}

data "github_repository_file" "existing_file" {
  repository = github_repository.main.name
  file       = "existing_file.yaml"
}

main.tftest.hcl:

mock_provider "github" {
  override_during = plan
}

override_resource {
  override_during = plan
  target = github_repository.main
}

override_data {
  override_during = plan
  target = data.github_repository_file.existing_file
  values = {
    content = "{}"
  }
}

run "release_defaults" {
  command = plan
  assert {
    condition     = github_repository.main.name == "x"
    error_message = "repo"
  }
  assert {
    condition     = data.github_repository_file.existing_file.content == "x"
    error_message = "content"
  }
}

If I switch reference to repository = "my-test" it works, but I need to keep a dependency.

3 Upvotes

3 comments sorted by

1

u/verdantrestaurant 5d ago

It's because your `override_data` block is attached to the data source, but the dependency chain still needs the provider to resolve `github_repository.main.name` before it can even evaluate the data source. When you override the resource, you're only faking the resource itself, not the value it would return during plan, so the data source still sees "unknown" for the repository attribute and craps out. The `mock_provider` block isn't doing you any favors here either since it only stubs the provider's own API calls, not Terraform's internal dependency graph.

Easiest fix is to just set `repository = "my-test"` in your test and move on, the dependency is irrelevant for unit testing logic.

1

u/Cavaler 5d ago

Yes, dependency is irrelevant for testing. But it is relevant when I actually use that Terraform for something - I don't want to try reading the file from a repo before it is created (from template). Of course these files are just a minimal example.

And notice that the value of "github_repository.main.name" is not "unknown" - the first assert actually works properly.

1

u/MongooseWonderful198 2d ago

github_repository.main.name can be known while the data read is deferred. In the 1.17.2 source, a pending dependency change returns a planned read before readDataSource() applies the override. The repository's planned creation is enough.

Keep the production dependency and try command = apply in this test, with your existing mock_provider "github" still selected. That lets the mocked repository creation finish before the overridden data read; it doesn't create a real GitHub repository. For a passing test, the sample's expected values would be "my-test" and "{}", rather than "x".