Skip to content

Commit 02d9ef9

Browse files
PF cross-tests for sets with defaults
1 parent 7ecd0a5 commit 02d9ef9

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

49 files changed

+1860
-0
lines changed

pkg/pf/tests/diff_set_test.go

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,12 @@ import (
77
"github.com/hashicorp/terraform-plugin-framework/attr"
88
"github.com/hashicorp/terraform-plugin-framework/resource"
99
rschema "github.com/hashicorp/terraform-plugin-framework/resource/schema"
10+
"github.com/hashicorp/terraform-plugin-framework/resource/schema/defaults"
1011
"github.com/hashicorp/terraform-plugin-framework/resource/schema/planmodifier"
1112
"github.com/hashicorp/terraform-plugin-framework/resource/schema/setplanmodifier"
1213
"github.com/hashicorp/terraform-plugin-framework/resource/schema/stringplanmodifier"
1314
"github.com/hashicorp/terraform-plugin-framework/types"
15+
"github.com/hashicorp/terraform-plugin-framework/types/basetypes"
1416
"github.com/hexops/autogold/v2"
1517
"github.com/pulumi/pulumi/sdk/v3/go/common/util/contract"
1618
"github.com/zclconf/go-cty/cty"
@@ -19,6 +21,24 @@ import (
1921
pb "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/pf/tests/internal/providerbuilder"
2022
)
2123

24+
type setDefault string
25+
26+
var _ defaults.Set = setDefault("default")
27+
28+
func (s setDefault) DefaultSet(ctx context.Context, req defaults.SetRequest, resp *defaults.SetResponse) {
29+
resp.PlanValue = basetypes.NewSetValueMust(types.StringType, []attr.Value{
30+
basetypes.NewStringValue("value"),
31+
})
32+
}
33+
34+
func (s setDefault) Description(ctx context.Context) string {
35+
return "description"
36+
}
37+
38+
func (s setDefault) MarkdownDescription(ctx context.Context) string {
39+
return "markdown description"
40+
}
41+
2242
func TestDetailedDiffSet(t *testing.T) {
2343
t.Parallel()
2444

@@ -33,6 +53,22 @@ func TestDetailedDiffSet(t *testing.T) {
3353
},
3454
})
3555

56+
attributeSchemaWithDefault := pb.NewResource(pb.NewResourceArgs{
57+
ResourceSchema: rschema.Schema{
58+
Attributes: map[string]rschema.Attribute{
59+
"key": rschema.SetAttribute{
60+
Optional: true,
61+
Computed: true,
62+
ElementType: types.StringType,
63+
Default: setDefault("default"),
64+
PlanModifiers: []planmodifier.Set{
65+
setplanmodifier.UseStateForUnknown(),
66+
},
67+
},
68+
},
69+
},
70+
})
71+
3672
attributeReplaceSchema := pb.NewResource(pb.NewResourceArgs{
3773
ResourceSchema: rschema.Schema{
3874
Attributes: map[string]rschema.Attribute{
@@ -114,6 +150,27 @@ func TestDetailedDiffSet(t *testing.T) {
114150
},
115151
})
116152

153+
blockSchemaWithDefault := pb.NewResource(pb.NewResourceArgs{
154+
ResourceSchema: rschema.Schema{
155+
Blocks: map[string]rschema.Block{
156+
"key": rschema.SetNestedBlock{
157+
NestedObject: rschema.NestedBlockObject{
158+
Attributes: map[string]rschema.Attribute{
159+
"nested": rschema.StringAttribute{
160+
Optional: true,
161+
Default: stringDefault("default"),
162+
Computed: true,
163+
PlanModifiers: []planmodifier.String{
164+
stringplanmodifier.UseStateForUnknown(),
165+
},
166+
},
167+
},
168+
},
169+
},
170+
},
171+
},
172+
})
173+
117174
blockReplaceSchema := pb.NewResource(pb.NewResourceArgs{
118175
ResourceSchema: rschema.Schema{
119176
Blocks: map[string]rschema.Block{
@@ -467,6 +524,10 @@ func TestDetailedDiffSet(t *testing.T) {
467524
{"block requires replace", blockReplaceSchema, nestedAttrList},
468525
{"block nested requires replace", blockNestedReplaceSchema, nestedAttrList},
469526

527+
// Defaults
528+
{"attribute with default", attributeSchemaWithDefault, attrList},
529+
{"block with default", blockSchemaWithDefault, nestedAttrList},
530+
470531
// Computed attributes
471532
{"attribute with computed no replace", computedSetAttributeSchema, attrList},
472533
{"attribute with computed requires replace", computedSetAttributeReplaceSchema, attrList},
Lines changed: 35 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,35 @@
1+
tfbridgetests.testOutput{
2+
initialValue: &[]string{},
3+
changeValue: &[]string{"value"},
4+
tfOut: `
5+
Terraform used the selected providers to generate the following execution
6+
plan. Resource actions are indicated with the following symbols:
7+
~ update in-place
8+
9+
Terraform will perform the following actions:
10+
11+
# testprovider_test.res will be updated in-place
12+
~ resource "testprovider_test" "res" {
13+
id = "test-id"
14+
~ key = [
15+
+ "value",
16+
]
17+
}
18+
19+
Plan: 0 to add, 1 to change, 0 to destroy.
20+
21+
`,
22+
pulumiOut: `Previewing update (test):
23+
pulumi:pulumi:Stack: (same)
24+
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
25+
~ testprovider:index/test:Test: (update)
26+
[id=test-id]
27+
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
28+
~ keys: [
29+
+ [0]: "value"
30+
]
31+
Resources:
32+
~ 1 to update
33+
1 unchanged
34+
`,
35+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
tfbridgetests.testOutput{
2+
initialValue: &[]string{
3+
"val1",
4+
"val2",
5+
},
6+
changeValue: &[]string{
7+
"val1",
8+
"val2",
9+
"val3",
10+
},
11+
tfOut: `
12+
Terraform used the selected providers to generate the following execution
13+
plan. Resource actions are indicated with the following symbols:
14+
~ update in-place
15+
16+
Terraform will perform the following actions:
17+
18+
# testprovider_test.res will be updated in-place
19+
~ resource "testprovider_test" "res" {
20+
id = "test-id"
21+
~ key = [
22+
+ "val3",
23+
# (2 unchanged elements hidden)
24+
]
25+
}
26+
27+
Plan: 0 to add, 1 to change, 0 to destroy.
28+
29+
`,
30+
pulumiOut: `Previewing update (test):
31+
pulumi:pulumi:Stack: (same)
32+
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
33+
~ testprovider:index/test:Test: (update)
34+
[id=test-id]
35+
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
36+
~ keys: [
37+
[0]: "val1"
38+
[1]: "val2"
39+
+ [2]: "val3"
40+
]
41+
Resources:
42+
~ 1 to update
43+
1 unchanged
44+
`,
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
tfbridgetests.testOutput{
2+
initialValue: &[]string{
3+
"val2",
4+
"val3",
5+
},
6+
changeValue: &[]string{
7+
"val2",
8+
"val3",
9+
"val1",
10+
},
11+
tfOut: `
12+
Terraform used the selected providers to generate the following execution
13+
plan. Resource actions are indicated with the following symbols:
14+
~ update in-place
15+
16+
Terraform will perform the following actions:
17+
18+
# testprovider_test.res will be updated in-place
19+
~ resource "testprovider_test" "res" {
20+
id = "test-id"
21+
~ key = [
22+
+ "val1",
23+
# (2 unchanged elements hidden)
24+
]
25+
}
26+
27+
Plan: 0 to add, 1 to change, 0 to destroy.
28+
29+
`,
30+
pulumiOut: `Previewing update (test):
31+
pulumi:pulumi:Stack: (same)
32+
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
33+
~ testprovider:index/test:Test: (update)
34+
[id=test-id]
35+
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
36+
~ keys: [
37+
[0]: "val2"
38+
[1]: "val3"
39+
+ [2]: "val1"
40+
]
41+
Resources:
42+
~ 1 to update
43+
1 unchanged
44+
`,
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
tfbridgetests.testOutput{
2+
initialValue: &[]string{
3+
"val2",
4+
"val3",
5+
},
6+
changeValue: &[]string{
7+
"val1",
8+
"val2",
9+
"val3",
10+
},
11+
tfOut: `
12+
Terraform used the selected providers to generate the following execution
13+
plan. Resource actions are indicated with the following symbols:
14+
~ update in-place
15+
16+
Terraform will perform the following actions:
17+
18+
# testprovider_test.res will be updated in-place
19+
~ resource "testprovider_test" "res" {
20+
id = "test-id"
21+
~ key = [
22+
+ "val1",
23+
# (2 unchanged elements hidden)
24+
]
25+
}
26+
27+
Plan: 0 to add, 1 to change, 0 to destroy.
28+
29+
`,
30+
pulumiOut: `Previewing update (test):
31+
pulumi:pulumi:Stack: (same)
32+
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
33+
~ testprovider:index/test:Test: (update)
34+
[id=test-id]
35+
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
36+
~ keys: [
37+
~ [0]: "val2" => "val1"
38+
~ [1]: "val3" => "val2"
39+
+ [2]: "val3"
40+
]
41+
Resources:
42+
~ 1 to update
43+
1 unchanged
44+
`,
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
tfbridgetests.testOutput{
2+
initialValue: &[]string{
3+
"val3",
4+
"val1",
5+
},
6+
changeValue: &[]string{
7+
"val2",
8+
"val3",
9+
"val1",
10+
},
11+
tfOut: `
12+
Terraform used the selected providers to generate the following execution
13+
plan. Resource actions are indicated with the following symbols:
14+
~ update in-place
15+
16+
Terraform will perform the following actions:
17+
18+
# testprovider_test.res will be updated in-place
19+
~ resource "testprovider_test" "res" {
20+
id = "test-id"
21+
~ key = [
22+
+ "val2",
23+
# (2 unchanged elements hidden)
24+
]
25+
}
26+
27+
Plan: 0 to add, 1 to change, 0 to destroy.
28+
29+
`,
30+
pulumiOut: `Previewing update (test):
31+
pulumi:pulumi:Stack: (same)
32+
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
33+
~ testprovider:index/test:Test: (update)
34+
[id=test-id]
35+
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
36+
~ keys: [
37+
~ [0]: "val3" => "val2"
38+
~ [1]: "val1" => "val3"
39+
+ [2]: "val1"
40+
]
41+
Resources:
42+
~ 1 to update
43+
1 unchanged
44+
`,
45+
}
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
tfbridgetests.testOutput{
2+
initialValue: &[]string{
3+
"val1",
4+
"val3",
5+
},
6+
changeValue: &[]string{
7+
"val1",
8+
"val2",
9+
"val3",
10+
},
11+
tfOut: `
12+
Terraform used the selected providers to generate the following execution
13+
plan. Resource actions are indicated with the following symbols:
14+
~ update in-place
15+
16+
Terraform will perform the following actions:
17+
18+
# testprovider_test.res will be updated in-place
19+
~ resource "testprovider_test" "res" {
20+
id = "test-id"
21+
~ key = [
22+
+ "val2",
23+
# (2 unchanged elements hidden)
24+
]
25+
}
26+
27+
Plan: 0 to add, 1 to change, 0 to destroy.
28+
29+
`,
30+
pulumiOut: `Previewing update (test):
31+
pulumi:pulumi:Stack: (same)
32+
[urn=urn:pulumi:test::project::pulumi:pulumi:Stack::project-test]
33+
~ testprovider:index/test:Test: (update)
34+
[id=test-id]
35+
[urn=urn:pulumi:test::project::testprovider:index/test:Test::p]
36+
~ keys: [
37+
[0]: "val1"
38+
~ [1]: "val3" => "val2"
39+
+ [2]: "val3"
40+
]
41+
Resources:
42+
~ 1 to update
43+
1 unchanged
44+
`,
45+
}

0 commit comments

Comments
 (0)