Skip to content

Commit

Permalink
internal/fwserver: Prevent PlanResourceChange panic during changed pa…
Browse files Browse the repository at this point in the history
…th logging (#797)

Reference: #783

Prior to the logic update:

```
--- FAIL: TestServerPlanResourceChange (0.01s)
    --- FAIL: TestServerPlanResourceChange/update-set-default-values (0.00s)
panic: runtime error: invalid memory address or nil pointer dereference [recovered]
	panic: runtime error: invalid memory address or nil pointer dereference
[signal SIGSEGV: segmentation violation code=0x2 addr=0x18 pc=0x1043b0484]

goroutine 32 [running]:
testing.tRunner.func1.2({0x1048d4e60, 0x104c08f90})
	/opt/homebrew/Cellar/go/1.20.5/libexec/src/testing/testing.go:1526 +0x1c8
testing.tRunner.func1()
	/opt/homebrew/Cellar/go/1.20.5/libexec/src/testing/testing.go:1529 +0x384
panic({0x1048d4e60, 0x104c08f90})
	/opt/homebrew/Cellar/go/1.20.5/libexec/src/runtime/panic.go:884 +0x204
github.com/hashicorp/terraform-plugin-framework/internal/fwserver.(*Server).PlanResourceChange(0x140001f4a00, {0x10494ddb0, 0x14000098008}, 0x140001eeeb0, 0x14000592540)
	/Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange.go:219 +0x19e4
github.com/hashicorp/terraform-plugin-framework/internal/fwserver_test.TestServerPlanResourceChange.func37(0x14000231860)
	/Users/bflad/src/github.com/hashicorp/terraform-plugin-framework/internal/fwserver/server_planresourcechange_test.go:5849 +0x70
testing.tRunner(0x14000231860, 0x140000ccde0)
	/opt/homebrew/Cellar/go/1.20.5/libexec/src/testing/testing.go:1576 +0x10c
created by testing.(*T).Run
	/opt/homebrew/Cellar/go/1.20.5/libexec/src/testing/testing.go:1629 +0x368
FAIL	github.com/hashicorp/terraform-plugin-framework/internal/fwserver	0.457s
```

This change verifies and handles the more critical bug fix of the panic report. The handling of defaults for set nested attributes will require further logic updates, which will be handled in a future change.
  • Loading branch information
bflad committed Jul 12, 2023
1 parent e036d9f commit a46d263
Show file tree
Hide file tree
Showing 4 changed files with 2,634 additions and 482 deletions.
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230711-161747.yaml
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'resource: Prevented panic during planning caused by `SetNestedAttribute` with
nested attribute `Default` and multiple configured elements'
time: 2023-07-11T16:17:47.990142-04:00
custom:
Issue: "783"
57 changes: 57 additions & 0 deletions internal/fwschemadata/data_get_at_path_test.go
Expand Up @@ -34,6 +34,40 @@ func TestDataGetAtPath(t *testing.T) {
expected any
expectedDiags diag.Diagnostics
}{
"invalid-path": {
data: fwschemadata.Data{
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"test": testschema.Attribute{
Optional: true,
Type: types.StringType,
},
},
},
TerraformValue: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.String,
},
},
map[string]tftypes.Value{
"test": tftypes.NewValue(tftypes.String, "test-value"),
},
),
},
path: path.Root("not-test"),
target: new(string),
expected: new(string),
expectedDiags: diag.Diagnostics{
diag.NewAttributeErrorDiagnostic(
path.Root("not-test"),
"Data Read Error",
"An unexpected error was encountered trying to retrieve type information at a given path. "+
"This is always an error in the provider. Please report the following to the provider developer:\n\n"+
"Error: AttributeName(\"not-test\") still remains in the path: could not find attribute or block \"not-test\" in schema",
),
},
},
"invalid-target": {
data: fwschemadata.Data{
Schema: testschema.Schema{
Expand Down Expand Up @@ -104,6 +138,29 @@ func TestDataGetAtPath(t *testing.T) {
),
},
},
"TerraformValue-null": {
data: fwschemadata.Data{
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"test": testschema.Attribute{
Optional: true,
Type: types.StringType,
},
},
},
TerraformValue: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.String,
},
},
nil,
),
},
path: path.Root("test"),
target: new(types.String),
expected: pointer(types.StringNull()),
},
"AttrTypeWithValidateError": {
data: fwschemadata.Data{
Schema: testschema.Schema{
Expand Down
8 changes: 8 additions & 0 deletions internal/fwserver/server_planresourcechange.go
Expand Up @@ -207,6 +207,14 @@ func (s *Server) PlanResourceChange(ctx context.Context, req *PlanResourceChange
_ = resp.PlannedState.GetAttribute(ctx, p, &plannedState)
_ = req.PriorState.GetAttribute(ctx, p, &priorState)

// Due to ignoring diagnostics, the value may not be populated.
// Prevent the panic and show the path as changed.
if plannedState == nil {
changedPaths.Append(p)

continue
}

if plannedState.Equal(priorState) {
continue
}
Expand Down

0 comments on commit a46d263

Please sign in to comment.