hashicorp / hashicorp/terraform-plugin-testing
Add exported function to retrieve attribute from state for use during tests
- Dominant language
- Go
- Stars
- 68
- Forks
- 22
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 1
Description
### SDK version
```
v2.20.0
```
### Use-cases
Occasionally, when writing acceptance tests it is useful to be able to extract and store a value from state in order to use in subsequent test steps. For instance, when using the `Taint` field in a `TestStep` it is useful to be able to verify that a resource has been destroyed and recreated.
### Attempted Solutions
Adding a function along the following lines to each provider which needs to be able to compare attributes before and after test steps.
```go
func testExtractResourceAttr(resourceName string, attributeName string, attributeValue *string) r.TestCheckFunc {
return func(s *terraform.State) error {
rs, ok := s.RootModule().Resources[resourceName]
if !ok {
return fmt.Errorf("resource name %s not found in state", resourceName)
}
attrValue, ok := rs.Primary.Attributes[attributeName]
if !ok {
return fmt.Errorf("attribute %s not found in resource %s state", attributeName, resourceName)
}
*attributeValue = attrValue
return nil
}
}
```
### Proposal
Add an exported function to `/helper/resource` that exposes this functionality to avoid code duplication in providers.
### References
Contributor guide
Assessment
This issue has not been assessed yet.