Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow mocked values on resources during the planning stage in terraform test #35851

Open
buehlmann opened this issue Oct 14, 2024 · 3 comments
Open
Labels
enhancement new new issue not yet triaged terraform test

Comments

@buehlmann
Copy link

Terraform Version

Terraform v1.9.6 on linux_amd64

Use Cases

I would like to mock calculated attributes on resources in unit tests. By unit tests I mean the command = plan in the run blocks set, because I just want to test conditionals and loops inside my Terraform manifests. In the assert condition of the run block, it should be possible to use the mocked values on the resources.

Attempted Solutions

The resource gitlab_user_runner exposes the computed attribute token, which I use as input for other resources. In my unit test I want to mock the value of this token.

The main Terraform manifest:

# main.tf
provider "gitlab" {
  base_url = "https://gitlab.com/"
}

resource "gitlab_user_runner" "test_runner" {
  runner_type     = "group_type"
  description     = "test-runner"
  group_id        = 42
  tag_list        = ["test-tag"]
}

Testfile where I want to override the token value on gitlab_user_runner.test_runner

# tests/gitlab.tftest.hcl

mock_provider "gitlab" {}

run "test_mock_token" {
  command = plan
  
  override_resource {
    target = gitlab_user_runner.test_runner
    values = {
      token = "mocked-token"
    }
  }
  
  assert {
    condition     = gitlab_user_runner.test_runner.token == "mocked-token"
    error_message = "tokens not matching"
  }
}

When executing the test, I get the following error:

│ Error: Unknown condition value
│ 
│   on tests/gitlab.tftest.hcl line 12, in run "test_mock_token":
│   12:     condition     = gitlab_user_runner.test_runner.token == "mocked-token"
│ 
│ 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.
│

When I run the test with command = plan everything works fine. But in this case I just want to have a unit test, without any resources provisioned.

Proposal

To write proper unit tests it would be a great benefit if one could supply mocked values already during the planning stage.

References

No response

@buehlmann buehlmann added enhancement new new issue not yet triaged labels Oct 14, 2024
@crw
Copy link
Collaborator

crw commented Oct 14, 2024

Thanks for this feature request! If you are viewing this issue and would like to indicate your interest, please use the 👍 reaction on the issue description to upvote this issue. We also welcome additional use case descriptions. Thanks again!

@mud5150
Copy link

mud5150 commented Oct 24, 2024

Is this an enhancement or a bug? The documentation leads me to believe this should already work.

@crw
Copy link
Collaborator

crw commented Nov 15, 2024

Mind linking the related documentation? I can follow-up from there.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
enhancement new new issue not yet triaged terraform test
Projects
None yet
Development

No branches or pull requests

4 participants