hashicorp / terraform

Terraform enables you to safely and predictably create, change, and improve infrastructure. It is a source-available tool that codifies APIs into declarative configuration files that can be shared amongst team members, treated as code, edited, reviewed, and versioned.
https://www.terraform.io/
Other
42.4k stars 9.5k forks source link

Terraform Test - Lifecycle testing suite #35634

Closed deitChi closed 1 month ago

deitChi commented 1 month ago

Terraform Version

1.9.5

Use Cases

When developing modules, and iterative versions - as well as integration testing, it's important to do lifecycle testing.

i.e. We merge our PR, pass testing -> v1.0.0 is created.

Later, we change some locals - no real change to infrastructure, but we're going to release a patch -> v1.0.1.

We run the same tests, but don't really know if resources already created by v1.0.0 would be affected. Currently, we'd need to plan our development branch against live resources, which then informs us of the change.

My proposal, is that the so called 'lifecycle' suite get's an output namespace. This namespace will contain details about the test execution.

For example

run "test" {
  command = apply

  assert {
    condition = lifecycle.add == 2
    error_message = "created the wrong number of resources"
  }
}

run "idempotence" {
  command = apply

  assert {
    condition = lifecycle.changed == 0
    error_message = "module idempotence test failed"
  }
}

run "update" {
  command = apply
  source = "../path/to/new/version"

  assert {
    condition = lifecycle.added == 1
    error_message = "module added an unexpected number of resources"
  }
  assert {
    condition = lifecycle.changed== 1
    error_message = "module changed an unexpected number of resources"
  }
  assert {
    condition = lifecycle.destroyed== 1
    error_message = "module destroyed an unexpected number of resources"
  }
}

We could then use the testing output to drive the 'type' of release, i.e. patch, minor, breaking etc.

But at the very least - the idempotence check is quite an important one (terratest included it I believe)

Attempted Solutions

I suppose this can all be done without the terraform test framework.

cd old_version
terraform apply
idempotence="$(terraform apply | grep 'Plan:' )"
# Do idempotence check
cd ../new_version
output="$(terraform apply | grep 'Plan:' )"
terraform destroy
< do whatever with $output >

Proposal

Referenced in Use Cases

References

No response

crw commented 1 month ago

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!

liamcervante commented 1 month ago

Hi @deitChi, I'm going to close this as a duplicate as we have several other tickets tracking similar feature requests:

Feel free to browse the requests there and add any context you feel is missing or would be of particular value to you to help us with discovery. Alternatively, let me know if you think this request isn't actually covered by the existing ones, and we can tweak it slightly to make the additional requests clear and re-open it.

Thanks for filing this!