Skip to content
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

PF Diff cross-tests for sets with defaults #2640

Merged
merged 1 commit into from
Nov 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions pkg/pf/tests/diff_set_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,12 @@ import (
"github.com/hashicorp/terraform-plugin-framework/attr"
"github.com/hashicorp/terraform-plugin-framework/resource"
rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
"github.com/hexops/autogold/v2"
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
"github.com/zclconf/go-cty/cty"
Expand All @@ -19,6 +21,24 @@ import (
pb "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/internal/providerbuilder"
)

type setDefault string

var _ defaults.Set = setDefault("default")

func (s setDefault) DefaultSet(ctx context.Context, req defaults.SetRequest, resp *defaults.SetResponse) {
resp.PlanValue = basetypes.NewSetValueMust(types.StringType, []attr.Value{
basetypes.NewStringValue("value"),
})
}

func (s setDefault) Description(ctx context.Context) string {
return "description"
}

func (s setDefault) MarkdownDescription(ctx context.Context) string {
return "markdown description"
}

func TestDetailedDiffSet(t *testing.T) {
t.Parallel()

Expand All @@ -33,6 +53,22 @@ func TestDetailedDiffSet(t *testing.T) {
},
})

attributeSchemaWithDefault := pb.NewResource(pb.NewResourceArgs{
ResourceSchema: rschema.Schema{
Attributes: map[string]rschema.Attribute{
"key": rschema.SetAttribute{
Optional: true,
Computed: true,
ElementType: types.StringType,
Default: setDefault("default"),
PlanModifiers: []planmodifier.Set{
setplanmodifier.UseStateForUnknown(),
},
},
},
},
})

attributeReplaceSchema := pb.NewResource(pb.NewResourceArgs{
ResourceSchema: rschema.Schema{
Attributes: map[string]rschema.Attribute{
Expand Down Expand Up @@ -114,6 +150,27 @@ func TestDetailedDiffSet(t *testing.T) {
},
})

blockSchemaWithDefault := pb.NewResource(pb.NewResourceArgs{
ResourceSchema: rschema.Schema{
Blocks: map[string]rschema.Block{
"key": rschema.SetNestedBlock{
NestedObject: rschema.NestedBlockObject{
Attributes: map[string]rschema.Attribute{
"nested": rschema.StringAttribute{
Optional: true,
Default: stringDefault("default"),
Computed: true,
PlanModifiers: []planmodifier.String{
stringplanmodifier.UseStateForUnknown(),
},
},
},
},
},
},
},
})

blockReplaceSchema := pb.NewResource(pb.NewResourceArgs{
ResourceSchema: rschema.Schema{
Blocks: map[string]rschema.Block{
Expand Down Expand Up @@ -467,6 +524,10 @@ func TestDetailedDiffSet(t *testing.T) {
{"block requires replace", blockReplaceSchema, nestedAttrList},
{"block nested requires replace", blockNestedReplaceSchema, nestedAttrList},

// Defaults
{"attribute with default", attributeSchemaWithDefault, attrList},
{"block with default", blockSchemaWithDefault, nestedAttrList},

// Computed attributes
{"attribute with computed no replace", computedSetAttributeSchema, attrList},
{"attribute with computed requires replace", computedSetAttributeReplaceSchema, attrList},
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
tfbridgetests.testOutput{
initialValue: &[]string{},
changeValue: &[]string{"value"},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# testprovider_test.res will be updated in-place
~ resource "testprovider_test" "res" {
id = "test-id"
~ key = [
+ "value",
]
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ testprovider:index/test:Test: (update)
[id=test-id]
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
~ keys: [
+ [0]: "value"
]
Resources:
~ 1 to update
1 unchanged
`,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
tfbridgetests.testOutput{
initialValue: &[]string{
"val1",
"val2",
},
changeValue: &[]string{
"val1",
"val2",
"val3",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# testprovider_test.res will be updated in-place
~ resource "testprovider_test" "res" {
id = "test-id"
~ key = [
+ "val3",
# (2 unchanged elements hidden)
]
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ testprovider:index/test:Test: (update)
[id=test-id]
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
~ keys: [
[0]: "val1"
[1]: "val2"
+ [2]: "val3"
]
Resources:
~ 1 to update
1 unchanged
`,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
tfbridgetests.testOutput{
initialValue: &[]string{
"val2",
"val3",
},
changeValue: &[]string{
"val2",
"val3",
"val1",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# testprovider_test.res will be updated in-place
~ resource "testprovider_test" "res" {
id = "test-id"
~ key = [
+ "val1",
# (2 unchanged elements hidden)
]
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ testprovider:index/test:Test: (update)
[id=test-id]
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
~ keys: [
[0]: "val2"
[1]: "val3"
+ [2]: "val1"
]
Resources:
~ 1 to update
1 unchanged
`,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
tfbridgetests.testOutput{
initialValue: &[]string{
"val2",
"val3",
},
changeValue: &[]string{
"val1",
"val2",
"val3",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# testprovider_test.res will be updated in-place
~ resource "testprovider_test" "res" {
id = "test-id"
~ key = [
+ "val1",
# (2 unchanged elements hidden)
]
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ testprovider:index/test:Test: (update)
[id=test-id]
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
~ keys: [
~ [0]: "val2" => "val1"
~ [1]: "val3" => "val2"
+ [2]: "val3"
]
Resources:
~ 1 to update
1 unchanged
`,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
tfbridgetests.testOutput{
initialValue: &[]string{
"val3",
"val1",
},
changeValue: &[]string{
"val2",
"val3",
"val1",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# testprovider_test.res will be updated in-place
~ resource "testprovider_test" "res" {
id = "test-id"
~ key = [
+ "val2",
# (2 unchanged elements hidden)
]
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ testprovider:index/test:Test: (update)
[id=test-id]
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
~ keys: [
~ [0]: "val3" => "val2"
~ [1]: "val1" => "val3"
+ [2]: "val1"
]
Resources:
~ 1 to update
1 unchanged
`,
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
tfbridgetests.testOutput{
initialValue: &[]string{
"val1",
"val3",
},
changeValue: &[]string{
"val1",
"val2",
"val3",
},
tfOut: `
Terraform used the selected providers to generate the following execution
plan. Resource actions are indicated with the following symbols:
~ update in-place

Terraform will perform the following actions:

# testprovider_test.res will be updated in-place
~ resource "testprovider_test" "res" {
id = "test-id"
~ key = [
+ "val2",
# (2 unchanged elements hidden)
]
}

Plan: 0 to add, 1 to change, 0 to destroy.

`,
pulumiOut: `Previewing update (test):
pulumi:pulumi:Stack: (same)
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
~ testprovider:index/test:Test: (update)
[id=test-id]
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
~ keys: [
[0]: "val1"
~ [1]: "val3" => "val2"
+ [2]: "val3"
]
Resources:
~ 1 to update
1 unchanged
`,
}
Loading