-
Notifications
You must be signed in to change notification settings - Fork 15
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
framework: Add corner tests for set defaulting bug in plugin framework #270
Merged
+634
−0
Merged
Changes from all commits
Commits
Show all changes
7 commits
Select commit
Hold shift + click to select a range
cd7699d
add tests for 783
austinvalle caaee1c
add nested attribute tests
austinvalle 0afa5da
comments
austinvalle f244948
comments
austinvalle a5c60f1
version checks
austinvalle 336ad20
Merge branch 'main' into av/set-bug-acceptance-tests
austinvalle 0ec4292
Merge branch 'main' into av/set-bug-acceptance-tests
austinvalle File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
99 changes: 99 additions & 0 deletions
99
internal/framework5provider/set_nested_block_with_defaults.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,99 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package framework | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var _ resource.Resource = SetNestedBlockWithDefaultsResource{} | ||
|
||
func NewSetNestedBlockWithDefaultsResource() resource.Resource { | ||
return &SetNestedBlockWithDefaultsResource{} | ||
} | ||
|
||
// SetNestedBlockWithDefaultsResource is used for a test asserting a bug that has yet to be fixed in plugin framework | ||
// with defaults being used in an attribute inside of a set. | ||
// | ||
// This bug can be observed with various different outcomes: producing duplicate set element errors, incorrect diffs during plan, | ||
// consistent diffs with values switching back and forth, etc. Example bug reports: | ||
// - https://github.com/hashicorp/terraform-plugin-framework/issues/783 | ||
// - https://github.com/hashicorp/terraform-plugin-framework/issues/867 | ||
// - https://github.com/hashicorp/terraform-plugin-framework/issues/1036 | ||
type SetNestedBlockWithDefaultsResource struct{} | ||
|
||
func (r SetNestedBlockWithDefaultsResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_set_nested_block_with_defaults" | ||
} | ||
|
||
func (r SetNestedBlockWithDefaultsResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Blocks: map[string]schema.Block{ | ||
"set": schema.SetNestedBlock{ | ||
NestedObject: schema.NestedBlockObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"value": schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
Default: stringdefault.StaticString("zero"), | ||
}, | ||
"default_value": schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
Default: stringdefault.StaticString("this is a default"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r SetNestedBlockWithDefaultsResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var data SetNestedBlockWithDefaultsResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r SetNestedBlockWithDefaultsResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var data SetNestedBlockWithDefaultsResourceModel | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r SetNestedBlockWithDefaultsResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var data SetNestedBlockWithDefaultsResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r SetNestedBlockWithDefaultsResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
} | ||
|
||
type SetNestedBlockWithDefaultsResourceModel struct { | ||
Set types.Set `tfsdk:"set"` | ||
} |
111 changes: 111 additions & 0 deletions
111
internal/framework5provider/set_nested_block_with_defaults_test.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,111 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package framework | ||
|
||
import ( | ||
"regexp" | ||
"testing" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/providerserver" | ||
"github.com/hashicorp/terraform-plugin-go/tfprotov5" | ||
"github.com/hashicorp/terraform-plugin-testing/helper/resource" | ||
"github.com/hashicorp/terraform-plugin-testing/knownvalue" | ||
"github.com/hashicorp/terraform-plugin-testing/plancheck" | ||
"github.com/hashicorp/terraform-plugin-testing/tfjsonpath" | ||
"github.com/hashicorp/terraform-plugin-testing/tfversion" | ||
) | ||
|
||
// This test asserts a bug that has yet to be fixed in plugin framework with defaults being used in an attribute inside of a set. | ||
// | ||
// This bug can be observed with various different outcomes: producing duplicate set element errors, incorrect diffs during plan, | ||
// consistent diffs with values switching back and forth, etc. Example bug reports: | ||
// - https://github.com/hashicorp/terraform-plugin-framework/issues/783 | ||
// - https://github.com/hashicorp/terraform-plugin-framework/issues/867 | ||
// - https://github.com/hashicorp/terraform-plugin-framework/issues/1036 | ||
// | ||
// They all originate from the same root cause, which is when using `Default` on multiple attributes inside of a set, when one default | ||
// value is applied, the other default values may also be applied due to the set element being modified during traversal. The reason this | ||
// results in differing behavior is because Terraform core can't apply data consistency rules to sets that contain objects, so instead of | ||
// a single consistent error message, we get a bunch of different errors/odd behavior depending on the exact result of the defaulting logic. | ||
// | ||
// This specific test will successfully apply with the correct data, then following refresh/plan/apply commands will all raise | ||
// a "duplicate set element" error. Since the framework logic defaults the set elements while traversing it and set elements are identified by their | ||
// value, follow-up path lookups for other set elements can't find the correct data, resulting in default values being applied incorrectly | ||
// for the "value" attributes. | ||
// | ||
// Error: Duplicate Set Element | ||
// | ||
// with framework_set_nested_block_with_defaults.test, | ||
// on terraform_plugin_test.tf line 11, in resource "framework_set_nested_block_with_defaults" "test": | ||
// 11: resource "framework_set_nested_block_with_defaults" "test" { | ||
// | ||
// This attribute contains duplicate values of: | ||
// tftypes.Object["default_value":tftypes.String, | ||
// "value":tftypes.String]<"default_value":tftypes.String<"this is a default">, | ||
// "value":tftypes.String<"zero">> | ||
// | ||
// Once this bug is fixed, the ExpectError regex in this test should be removed and the plan check should be switched to a state check. | ||
func TestSetNestedBlockWithDefaults(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
TerraformVersionChecks: []tfversion.TerraformVersionCheck{ | ||
// The "Duplicate Set Element" error was introduced in Terraform 1.4 | ||
tfversion.SkipBelow(tfversion.Version1_4_0), | ||
}, | ||
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){ | ||
"framework": providerserver.NewProtocol5WithError(New()), | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
Config: `resource "framework_set_nested_block_with_defaults" "test" { | ||
set { | ||
value = "one" | ||
} | ||
set { | ||
value = "two" | ||
} | ||
set { | ||
value = "three" | ||
} | ||
}`, | ||
ConfigPlanChecks: resource.ConfigPlanChecks{ | ||
PreApply: []plancheck.PlanCheck{ | ||
plancheck.ExpectKnownValue( | ||
"framework_set_nested_block_with_defaults.test", | ||
tfjsonpath.New("set"), | ||
knownvalue.SetExact( | ||
[]knownvalue.Check{ | ||
knownvalue.ObjectExact( | ||
map[string]knownvalue.Check{ | ||
// During plan and after the first apply, this value will be "one" | ||
// After the first refresh, the bug will cause this value to be defaulted to "zero" | ||
"value": knownvalue.StringExact("one"), | ||
"default_value": knownvalue.StringExact("this is a default"), | ||
}, | ||
), | ||
knownvalue.ObjectExact( | ||
map[string]knownvalue.Check{ | ||
// During plan and after the first apply, this value will be "two" | ||
// After the first refresh, the bug will cause this value to be defaulted to "zero" | ||
"value": knownvalue.StringExact("two"), | ||
"default_value": knownvalue.StringExact("this is a default"), | ||
}, | ||
), | ||
knownvalue.ObjectExact( | ||
map[string]knownvalue.Check{ | ||
// During plan and after the first apply, this value will be "three" | ||
// After the first refresh, the bug will cause this value to be defaulted to "zero" | ||
"value": knownvalue.StringExact("three"), | ||
"default_value": knownvalue.StringExact("this is a default"), | ||
}, | ||
), | ||
}, | ||
), | ||
), | ||
}, | ||
}, | ||
ExpectError: regexp.MustCompile(`Error: Duplicate Set Element`), | ||
}, | ||
}, | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
100 changes: 100 additions & 0 deletions
100
internal/framework6provider/set_nested_attribute_with_defaults.go
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,100 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package framework | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringdefault" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var _ resource.Resource = SetNestedAttributeWithDefaultsResource{} | ||
|
||
func NewSetNestedAttributeWithDefaultsResource() resource.Resource { | ||
return &SetNestedAttributeWithDefaultsResource{} | ||
} | ||
|
||
// SetNestedAttributeWithDefaultsResource is used for a test asserting a bug that has yet to be fixed in plugin framework | ||
// with defaults being used in an attribute inside of a set. | ||
// | ||
// This bug can be observed with various different outcomes: producing duplicate set element errors, incorrect diffs during plan, | ||
// consistent diffs with values switching back and forth, etc. Example bug reports: | ||
// - https://github.com/hashicorp/terraform-plugin-framework/issues/783 | ||
// - https://github.com/hashicorp/terraform-plugin-framework/issues/867 | ||
// - https://github.com/hashicorp/terraform-plugin-framework/issues/1036 | ||
type SetNestedAttributeWithDefaultsResource struct{} | ||
|
||
func (r SetNestedAttributeWithDefaultsResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_set_nested_attribute_with_defaults" | ||
} | ||
|
||
func (r SetNestedAttributeWithDefaultsResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"set": schema.SetNestedAttribute{ | ||
Required: true, | ||
NestedObject: schema.NestedAttributeObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"value": schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
Default: stringdefault.StaticString("zero"), | ||
}, | ||
"default_value": schema.StringAttribute{ | ||
Optional: true, | ||
Computed: true, | ||
Default: stringdefault.StaticString("this is a default"), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r SetNestedAttributeWithDefaultsResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var data SetNestedAttributeWithDefaultsResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r SetNestedAttributeWithDefaultsResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var data SetNestedAttributeWithDefaultsResourceModel | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r SetNestedAttributeWithDefaultsResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var data SetNestedAttributeWithDefaultsResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r SetNestedAttributeWithDefaultsResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
} | ||
|
||
type SetNestedAttributeWithDefaultsResourceModel struct { | ||
Set types.Set `tfsdk:"set"` | ||
} |
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Great explanation here!