-
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 semantic equality tests for set bug fix #297
Open
austinvalle
wants to merge
4
commits into
main
Choose a base branch
from
av/set-semantic-equality
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+531
−3
Open
Changes from 3 commits
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
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
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
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
137 changes: 137 additions & 0 deletions
137
internal/framework5provider/set_semantic_equality_resource.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,137 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package framework | ||
|
||
import ( | ||
"context" | ||
|
||
"github.com/hashicorp/terraform-plugin-framework-nettypes/iptypes" | ||
"github.com/hashicorp/terraform-plugin-framework-validators/setvalidator" | ||
"github.com/hashicorp/terraform-plugin-framework/attr" | ||
"github.com/hashicorp/terraform-plugin-framework/resource" | ||
"github.com/hashicorp/terraform-plugin-framework/resource/schema" | ||
"github.com/hashicorp/terraform-plugin-framework/schema/validator" | ||
"github.com/hashicorp/terraform-plugin-framework/types" | ||
) | ||
|
||
var _ resource.Resource = SetSemanticEqualityResource{} | ||
|
||
func NewSetSemanticEqualityResource() resource.Resource { | ||
return &SetSemanticEqualityResource{} | ||
} | ||
|
||
// This resource tests that semantic equality for elements inside of a set are correctly executed | ||
// Original bug: https://github.com/hashicorp/terraform-plugin-framework/issues/1061 | ||
type SetSemanticEqualityResource struct{} | ||
|
||
func (r SetSemanticEqualityResource) Metadata(_ context.Context, req resource.MetadataRequest, resp *resource.MetadataResponse) { | ||
resp.TypeName = req.ProviderTypeName + "_set_semantic_equality" | ||
} | ||
|
||
func (r SetSemanticEqualityResource) Schema(_ context.Context, _ resource.SchemaRequest, resp *resource.SchemaResponse) { | ||
resp.Schema = schema.Schema{ | ||
Attributes: map[string]schema.Attribute{ | ||
"set_of_ipv6": schema.SetAttribute{ | ||
ElementType: iptypes.IPv6AddressType{}, | ||
Required: true, | ||
}, | ||
}, | ||
Blocks: map[string]schema.Block{ | ||
"set_nested_block": schema.SetNestedBlock{ | ||
Validators: []validator.Set{ | ||
setvalidator.IsRequired(), | ||
}, | ||
NestedObject: schema.NestedBlockObject{ | ||
Attributes: map[string]schema.Attribute{ | ||
"ipv6": schema.StringAttribute{ | ||
CustomType: iptypes.IPv6AddressType{}, | ||
Required: true, | ||
}, | ||
}, | ||
}, | ||
}, | ||
}, | ||
} | ||
} | ||
|
||
func (r SetSemanticEqualityResource) Create(ctx context.Context, req resource.CreateRequest, resp *resource.CreateResponse) { | ||
var data SetSemanticEqualityResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Simulate remote API returning semantically equivalent IPv6 addresses | ||
data.shiftAndShorten() | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r SetSemanticEqualityResource) Read(ctx context.Context, req resource.ReadRequest, resp *resource.ReadResponse) { | ||
var data SetSemanticEqualityResourceModel | ||
|
||
resp.Diagnostics.Append(req.State.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Simulate remote API returning semantically equivalent IPv6 addresses | ||
data.shiftAndShorten() | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r SetSemanticEqualityResource) Update(ctx context.Context, req resource.UpdateRequest, resp *resource.UpdateResponse) { | ||
var data SetSemanticEqualityResourceModel | ||
|
||
resp.Diagnostics.Append(req.Plan.Get(ctx, &data)...) | ||
|
||
if resp.Diagnostics.HasError() { | ||
return | ||
} | ||
|
||
// Simulate remote API returning semantically equivalent IPv6 addresses | ||
data.shiftAndShorten() | ||
|
||
resp.Diagnostics.Append(resp.State.Set(ctx, &data)...) | ||
} | ||
|
||
func (r SetSemanticEqualityResource) Delete(ctx context.Context, req resource.DeleteRequest, resp *resource.DeleteResponse) { | ||
} | ||
|
||
type SetSemanticEqualityResourceModel struct { | ||
SetOfIPv6 types.Set `tfsdk:"set_of_ipv6"` | ||
SetNestedBlock types.Set `tfsdk:"set_nested_block"` | ||
} | ||
|
||
var setObjectWithIPv6 = types.ObjectType{ | ||
AttrTypes: map[string]attr.Type{ | ||
"ipv6": iptypes.IPv6AddressType{}, | ||
}, | ||
} | ||
|
||
// Shifts + switches data to shortened IPv6 addresses, but is semantically equal to test config | ||
func (m *SetSemanticEqualityResourceModel) shiftAndShorten() { | ||
m.SetOfIPv6 = types.SetValueMust(iptypes.IPv6AddressType{}, []attr.Value{ | ||
iptypes.NewIPv6AddressValue("2001:DB8::8:800:200C:417A"), | ||
iptypes.NewIPv6AddressValue("::FFFF:192.168.255.255"), | ||
iptypes.NewIPv6AddressValue("::"), | ||
iptypes.NewIPv6AddressValue("::101"), | ||
}) | ||
|
||
m.SetNestedBlock = types.SetValueMust(setObjectWithIPv6, []attr.Value{ | ||
types.ObjectValueMust(setObjectWithIPv6.AttributeTypes(), map[string]attr.Value{ | ||
"ipv6": iptypes.NewIPv6AddressValue("::FFFF:192.168.255.255"), | ||
}), | ||
types.ObjectValueMust(setObjectWithIPv6.AttributeTypes(), map[string]attr.Value{ | ||
"ipv6": iptypes.NewIPv6AddressValue("FF01::"), | ||
}), | ||
types.ObjectValueMust(setObjectWithIPv6.AttributeTypes(), map[string]attr.Value{ | ||
"ipv6": iptypes.NewIPv6AddressValue("2001:DB8::8:800:200C:417A"), | ||
}), | ||
}) | ||
} |
99 changes: 99 additions & 0 deletions
99
internal/framework5provider/set_semantic_equality_resource_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,99 @@ | ||
// Copyright (c) HashiCorp, Inc. | ||
// SPDX-License-Identifier: MPL-2.0 | ||
|
||
package framework | ||
|
||
import ( | ||
"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/plancheck" | ||
) | ||
|
||
// This resource tests that semantic equality for elements inside of a set are correctly executed | ||
// Original bug: https://github.com/hashicorp/terraform-plugin-framework/issues/1061 | ||
func TestSetSemanticEqualityResource(t *testing.T) { | ||
resource.UnitTest(t, resource.TestCase{ | ||
ProtoV5ProviderFactories: map[string]func() (tfprotov5.ProviderServer, error){ | ||
"framework": providerserver.NewProtocol5WithError(New()), | ||
}, | ||
Steps: []resource.TestStep{ | ||
{ | ||
// The resource Create/Read will return semantically equal data that will cause a diff if returned to Terraform. | ||
// The semantic equality logic in iptypes.IPv6Address allows this configuration to successfully apply. | ||
Config: `resource "framework_set_semantic_equality" "test" { | ||
set_of_ipv6 = [ | ||
"0:0:0:0:0:0:0:0", | ||
"2001:0DB8:0000:0000:0008:0800:200C:417A", | ||
"0:0:0:0:0:0:0:101", | ||
"0:0:0:0:0:FFFF:192.168.255.255", | ||
] | ||
|
||
set_nested_block { | ||
ipv6 = "FF01:0:0:0:0:0:0:0" | ||
} | ||
set_nested_block { | ||
ipv6 = "2001:db8::8:800:200c:417a" | ||
} | ||
set_nested_block { | ||
ipv6 = "0:0:0:0:0:FFFF:192.168.255.255" | ||
} | ||
}`, | ||
}, | ||
{ | ||
// Re-ordering the set doesn't produce a diff with semantically equal hardcoded data | ||
Config: `resource "framework_set_semantic_equality" "test" { | ||
set_of_ipv6 = [ | ||
"0:0:0:0:0:FFFF:192.168.255.255", | ||
"0:0:0:0:0:0:0:0", | ||
"2001:0DB8:0000:0000:0008:0800:200C:417A", | ||
"0:0:0:0:0:0:0:101", | ||
] | ||
|
||
set_nested_block { | ||
ipv6 = "0:0:0:0:0:FFFF:192.168.255.255" | ||
} | ||
set_nested_block { | ||
ipv6 = "FF01:0:0:0:0:0:0:0" | ||
} | ||
set_nested_block { | ||
ipv6 = "2001:db8::8:800:200c:417a" | ||
} | ||
}`, | ||
ConfigPlanChecks: resource.ConfigPlanChecks{ | ||
PreApply: []plancheck.PlanCheck{ | ||
plancheck.ExpectResourceAction("framework_set_semantic_equality.test", plancheck.ResourceActionNoop), | ||
}, | ||
}, | ||
}, | ||
{ | ||
// User config changes will still produce a diff, but the apply will be successful with semantically equal data | ||
Config: `resource "framework_set_semantic_equality" "test" { | ||
set_of_ipv6 = [ | ||
"0:0:0:0:0:FFFF:192.168.255.255", | ||
"::", # <----------- This update will cause a diff | ||
"2001:0DB8:0000:0000:0008:0800:200C:417A", | ||
"0:0:0:0:0:0:0:101", | ||
] | ||
|
||
set_nested_block { | ||
ipv6 = "0:0:0:0:0:FFFF:192.168.255.255" | ||
} | ||
set_nested_block { | ||
ipv6 = "FF01::" # <----------- This update will cause a diff | ||
} | ||
set_nested_block { | ||
ipv6 = "2001:db8::8:800:200c:417a" | ||
} | ||
}`, | ||
ConfigPlanChecks: resource.ConfigPlanChecks{ | ||
PreApply: []plancheck.PlanCheck{ | ||
plancheck.ExpectResourceAction("framework_set_semantic_equality.test", plancheck.ResourceActionUpdate), | ||
}, | ||
}, | ||
}, | ||
}, | ||
}) | ||
} |
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
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.
TODO: Update with released framework version, currently pointing to commit from linked PR