hashicorp / hashicorp/terraform-plugin-testing

Setup/Teardown hook on `resource.Test()` and `resource.TestStep` level

Open
#82 1 comment 2 reactions 0 assignees View on GitHub
enhancement
Dominant language
Go
Stars
68
Forks
22
Avg merge
2d 12h
Merged PRs (30d)
1

Description

### SDK version

```
v2.17.0
```

### Use-cases

In the [Terraform Provider GitLab](https://github.com/gitlabhq/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?

Contributor guide

Open the contributing guide

Assessment

This issue has not been assessed yet.

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.