terraform-plugin-sdk
terraform-plugin-sdk copied to clipboard
Setup/Teardown hook on `resource.Test()` and `resource.TestStep` level
SDK version
v2.17.0
Use-cases
In the Terraform Provider GitLab we have to setup testing resources (e.g. a project the resource under test resides in) in each test case.
At the moment we are creating them in each test case just before calling resource.Test() or resource.ParallelTest(). However, that's a problem because when using resource.ParallelTest() this setup code isn't parallelized.
We have a similar scenario for resource.TestStep where sometimes we have to introduce errors or other things and currently misuse PreConfig, but don't have a way for a proper teardown (t.Cleanup() doesn't help if a subsequent TestStep required that to happen)
Attempted Solutions
Misusing PreConfig for TestStep and defining setup / teardown outside of resource.Test() / resource.ParallelTest() with be above mentioned limitations.
Proposal
I don't have a concrete proposal, but envision some sort of Setup and Teardown field registering some function on resource.Test() / resource.ParallelTest() and resource.TestStep() level.
The biggest challenge I see is how to pass data from Setup to Teardown - this data would be necessary to cleanup properly.
Another one is how to pass data from Setup() - from resource.Test() and resource.TestStep() to e.g. the Config or Check ?
Questions
- Have there been similar proposals?
- How are other providers doing this?
- Is there another clean solution how to achieve such things?