hashicorp / hashicorp/terraform-plugin-testing
path not found error for `null` attributes in `plancheck.ExpectUnknownValue` and `plancheck.ExpectSensitiveValue`
- Dominant language
- Go
- Stars
- 68
- Forks
- 22
- Avg merge
- 2d 12h
- Merged PRs (30d)
- 1
Description
Ref: https://github.com/hashicorp/terraform-plugin-framework/issues/840#issuecomment-1716481991
The error message for an attribute that is null and being asserted as unknown or sensitive can be confusing for developers as it's the same error you'd receive if the attribute didn't exist in the schema.
```bash
--- FAIL: Test_ExpectUnknownValue_NullValue (233.24s)
path not found: specified key string_attribute not found in map
```
### terraform-plugin-testing version
```
1.5.1
```
### Relevant provider source code
_This recreation was done with the internal testing `plugin-go` provider in this Go module, but the same can be achieved with another TF SDK._
```go
Schema: map[string]*schema.Schema{
"string_attribute": {
Optional: true,
Type: schema.TypeString,
},
"list_attribute": {
Type: schema.TypeList,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
"set_attribute": {
Type: schema.TypeSet,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
"map_attribute": {
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
"root_map_attribute": {
Type: schema.TypeMap,
Elem: &schema.Schema{
Type: schema.TypeString,
},
Optional: true,
},
"list_nested_block": {
Type: schema.TypeList,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"list_nested_block_attribute": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
"set_nested_block": {
Type: schema.TypeSet,
Optional: true,
Elem: &schema.Resource{
Schema: map[string]*schema.Schema{
"set_nested_block_attribute": {
Type: schema.TypeString,
Optional: true,
},
},
},
},
},
```
### Expected Behavior
Given the following test
```go
func Test_ExpectUnknownValue_NullValue(t *testing.T) {
t.Parallel()
r.UnitTest(t, r.TestCase{
ProviderFactories: map[string]func() (*schema.Provider, error){
"test": func() (*schema.Provider, error) { //nolint:unparam // required signature
return testProvider(), nil
},
},
Steps: []r.TestStep{
{
Config: `
resource "test_resource" "one" {}
`,
ConfigPlanChecks: r.ConfigPlanChecks{
PreApply: []plancheck.PlanCheck{
plancheck.ExpectUnknownValue("test_resource.one", tfjsonpath.New("string_attribute")),
},
},
},
},
})
}
```
I would expect an error message similar to other assertion fails, like:
```
--- FAIL: Test_ExpectUnknownValue_NullValue (233.24s)
specified key string_attribute was null, but expected it to be unknown
```
```
--- FAIL: Test_ExpectUnknownValue_NullValue (233.24s)
specified key string_attribute was null, but expected it to be sensitive
```
### Potential Solutions
This is occurring because [`Change.AfterUnknown`](https://github.com/hashicorp/terraform-plugin-testing/blob/4da90ede6ba8912abcf165523ef01185e0a3e25d/plancheck/expect_unknown_value.go#L28) and [`Change.AfterSensitive`](https://github.com/hashicorp/terraform-plugin-testing/blob/4da90ede6ba8912abcf165523ef01185e0a3e25d/plancheck/expect_sensitive_value.go#L28) don't have the attribute value if it's `nil`. Perhaps we can cross-reference with the `After` property to provide a better error message?

Contributor guide
Assessment
This issue has not been assessed yet.