hashicorp / hashicorp/terraform-plugin-testing
Match Resources Using Key During Import State Acceptance Tests
- Dominant language
- Go
- Stars
- 68
- Forks
- 22
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 1
Description
### SDK version
```
github.com/hashicorp/terraform-plugin-sdk/v2 v2.14.0
```
### Use-cases
Allow multiple resources to be used in a single configuration within an acceptance test and have the import state test complete successfully. For instance, in the random provider, the testing of import state has been [separated out](https://github.com/hashicorp/terraform-provider-random/blob/main/internal/provider/resource_pasword_test.go) into its own test. The reason for this is that testing `import` using a `config` that contains multiple `random_password` resources produces unpredictable results because of the way in which equality [matching of resources is implemented](https://github.com/hashicorp/terraform-plugin-sdk/blob/main/helper/resource/testing_new_import_state.go#L177) within the SDK:
```go
if r2.Primary != nil && r2.Primary.ID == r.Primary.ID && r2.Type == r.Type && r2.Provider == r.Provider {
oldR = r2
break
}
```
Because the `random_password` resources all have the same `r.Primary.ID` and there is no matching on the "key" (e.g., `random_password.bar`) then, the first resource from the slice is considered a match and is returned. The ordering of the resources is non-deterministic so the results of a test using multiple resources in the same `config` would be unpredictable.
### Attempted Solutions
### Proposal
```go
if r2.Primary != nil && r2.Primary.ID == r.Primary.ID && r2.Type == r.Type && r2.Provider == r.Provider && r2Key == rKey{
oldR = r2
break
}
```
### References
https://github.com/hashicorp/terraform-plugin-sdk/issues/367
Contributor guide
Assessment
This issue has not been assessed yet.