hashicorp / hashicorp/terraform-plugin-sdk
helper/resource: Checking Collection Attribute Length Ignores Missing/Null Attributes
- Dominant language
- Go
- Stars
- 485
- Forks
- 244
- Avg merge
- 19h 57m
- Merged PRs (30d)
- 4
Description
### SDK version
```
v2.24.0
```
### Relevant provider source code
```go
// sdk resource logic
d.Set("list", nil) // schema.TypeList
d.Set("map", nil) // schema.TypeMap
d.Set("set", nil) // schema.TypeSet
// framework resource logic
ListNull(/* ... */)
MapNull(/* ... */)
SetNull(/* ... */)
// acceptance testing TestCheckFunc
resource.TestCheckResourceAttr("examplecloud_thing.test", "list.#", "0")
resource.TestCheckResourceAttr("examplecloud_thing.test", "map.%", "0")
resource.TestCheckResourceAttr("examplecloud_thing.test", "set.#", "0")
```
### Terraform Configuration Files
```hcl
resource "examplecloud_thing" "example" {
# attributes not configured
}
```
### Expected Behavior
Test checks fail that the attribute doesn't exist with the framework code.
### Actual Behavior
Test checks pass with the framework code. It specifically ignores returning an error in this situation:
https://github.com/hashicorp/terraform-plugin-sdk/blob/b5b7dd0ab159303da4a64c94d64aeaea884c2a23/helper/resource/testing.go#L997-L1005
This rule was intentionally added because provider developers were already depending on this type of testing logic years ago:
https://github.com/hashicorp/terraform-plugin-sdk/commit/57c8f9629a2fb72ab75f7ff1d702bbcfee763ba4
### Steps to Reproduce
1. `TF_ACC=1 go test -count=1 -v ./...`
### References
- https://github.com/hashicorp/terraform-provider-aws/pull/27221
- https://github.com/hashicorp/terraform-provider-aws/issues/27372
- https://github.com/hashicorp/terraform-provider-aws/pull/27377
Contributor guide
Research direction
Start at helper/resource/testing.go around lines 997-1005 and run TF_ACC=1 go test -count=1 -v ./... to reproduce the collection-attribute checks. Compare the SDK and framework behavior for missing or null list, map, and set attributes, then add regression coverage showing the intended TestCheckResourceAttr result for each case.
Written by the indexing model from the issue text.
Assessment
- Tech stack
- go, terraform
- Domain
- testing-qa
- Issue type
- Bug
- Difficulty
- 4/5
- Estimated time
- 3-5 days
- Activity status
- Stale
- Clarity
- Mostly clear
- Newbie friendliness
- 45/100