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

Support for CLI plan and apply args or nested command for test #35099

Open
brettcurtis opened this issue Apr 30, 2024 · 1 comment
Open

Support for CLI plan and apply args or nested command for test #35099

brettcurtis opened this issue Apr 30, 2024 · 1 comment
Labels
enhancement new new issue not yet triaged

Comments

@brettcurtis
Copy link

Terraform Version

Terraform v1.8.2
on linux_amd64

Use Cases

I'd like to be able to pass an argument for plan or apply to tests. In some case I may want to just run a plan based test to get faster feedback. For example maybe I want something like pre-commit-terraform to run plan tests on each commit but the feedback loop for apply is too long.

Let's assume this simple use-case:

main.tf:

resource "google_storage_bucket" "test" {
  name          = "foo-tf847gz7d"
  location      = "US"
  force_destroy = true
  project       = "my-tmp-tf94ab-testing"

  public_access_prevention = "enforced"
}

tests/my.tftest.hcl

run "tests" {
  command = apply

  # Can validate in plan
  assert {
    condition     = google_storage_bucket.test.name == "foo-tf847gz7d"
    error_message = "Invalid bucket name"
  }

  # Can't validate in plan
  assert {
    condition     = google_storage_bucket.test.self_link == "https://www.googleapis.com/storage/v1/b/foo-tf847gz7d"
    error_message = "Invalid self link"
  }
}

I'd like to keep both assertions in the same test and pass either a plan or apply at the CLI and have test run the respective assertion.

Or maybe we can nest the command arg:

run "tests" {

  # Can validate in plan
  assert {
    command = apply
    condition     = google_storage_bucket.test.name == "foo-tf847gz7d"
    error_message = "Invalid bucket name"
  }

  # Can't validate in plan
  assert {
    command = plan
    condition     = google_storage_bucket.test.self_link == "https://www.googleapis.com/storage/v1/b/foo-tf847gz7d"
    error_message = "Invalid self link"
  }
}

Attempted Solutions

I was thinking to write two tests and call the test with the -filter argument but then I'd be doing a lot of copy/paste of assertions assuming I would want to run all the "plan" assertions on the apply run seeing it's a more thorough test.

Proposal

No response

References

No response

@crw
Copy link
Collaborator

crw commented Apr 30, 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!

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
Projects
None yet
Development

No branches or pull requests

2 participants