Skip to content

Commit 67f8fe4

Browse files
SDKv2 Diff tests for primitive types
1 parent 3b7968c commit 67f8fe4

File tree

2 files changed

+180
-52
lines changed

2 files changed

+180
-52
lines changed

pkg/tests/diff_test/detailed_diff_test.go renamed to pkg/tests/diff_test/detailed_diff_map_test.go

Lines changed: 0 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -10,58 +10,6 @@ import (
1010
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
1111
)
1212

13-
func TestSDKv2DetailedDiffString(t *testing.T) {
14-
t.Parallel()
15-
16-
res := schema.Resource{
17-
Schema: map[string]*schema.Schema{
18-
"string_prop": {
19-
Type: schema.TypeString,
20-
Optional: true,
21-
},
22-
},
23-
}
24-
25-
valueOne := ref("val1")
26-
valueTwo := ref("val2")
27-
var noValue *string
28-
29-
ctyVal := func(v *string) map[string]cty.Value {
30-
if v == nil {
31-
return map[string]cty.Value{}
32-
}
33-
return map[string]cty.Value{
34-
"string_prop": cty.StringVal(*v),
35-
}
36-
}
37-
38-
scenarios := []struct {
39-
name string
40-
initialValue *string
41-
changeValue *string
42-
}{
43-
{"unchanged empty", noValue, noValue},
44-
{"unchanged non-empty", valueOne, valueOne},
45-
{"added", noValue, valueOne},
46-
{"removed", valueOne, noValue},
47-
{"changed", valueOne, valueTwo},
48-
}
49-
50-
for _, scenario := range scenarios {
51-
t.Run(scenario.name, func(t *testing.T) {
52-
t.Parallel()
53-
diff := crosstests.Diff(t, &res, ctyVal(scenario.initialValue), ctyVal(scenario.changeValue))
54-
autogold.ExpectFile(t, testOutput{
55-
initialValue: scenario.initialValue,
56-
changeValue: scenario.changeValue,
57-
tfOut: diff.TFOut,
58-
pulumiOut: diff.PulumiOut,
59-
detailedDiff: diff.PulumiDiff.DetailedDiff,
60-
})
61-
})
62-
}
63-
}
64-
6513
func TestSDKv2DetailedDiffMap(t *testing.T) {
6614
t.Parallel()
6715

Lines changed: 180 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,180 @@
1+
package tests
2+
3+
import (
4+
"context"
5+
"strings"
6+
"testing"
7+
8+
"github.com/hashicorp/terraform-plugin-sdk/v2/diag"
9+
"github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema"
10+
"github.com/hexops/autogold/v2"
11+
crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests"
12+
"github.com/zclconf/go-cty/cty"
13+
)
14+
15+
func TestSDKv2DetailedDiffString(t *testing.T) {
16+
t.Parallel()
17+
18+
optionalSchema := schema.Resource{
19+
Schema: map[string]*schema.Schema{
20+
"string_prop": {
21+
Type: schema.TypeString,
22+
Optional: true,
23+
},
24+
},
25+
}
26+
27+
optionalForceNewSchema := schema.Resource{
28+
Schema: map[string]*schema.Schema{
29+
"string_prop": {
30+
Type: schema.TypeString,
31+
Optional: true,
32+
ForceNew: true,
33+
},
34+
},
35+
}
36+
37+
requiredSchema := schema.Resource{
38+
Schema: map[string]*schema.Schema{
39+
"string_prop": {
40+
Type: schema.TypeString,
41+
Required: true,
42+
},
43+
},
44+
}
45+
46+
requiredForceNewSchema := schema.Resource{
47+
Schema: map[string]*schema.Schema{
48+
"string_prop": {
49+
Type: schema.TypeString,
50+
ForceNew: true,
51+
Required: true,
52+
},
53+
},
54+
}
55+
56+
setComputedFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics {
57+
if d.Get("string_prop") == nil {
58+
err := d.Set("string_prop", "computed")
59+
if err != nil {
60+
return diag.FromErr(err)
61+
}
62+
}
63+
return nil
64+
}
65+
66+
optionalComputedSchema := schema.Resource{
67+
Schema: map[string]*schema.Schema{
68+
"string_prop": {
69+
Type: schema.TypeString,
70+
Optional: true,
71+
Computed: true,
72+
},
73+
},
74+
CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
75+
rd.SetId("id")
76+
return setComputedFunc(ctx, rd, i)
77+
},
78+
UpdateContext: setComputedFunc,
79+
}
80+
81+
optionalComputedForceNewSchema := schema.Resource{
82+
Schema: map[string]*schema.Schema{
83+
"string_prop": {
84+
Type: schema.TypeString,
85+
Optional: true,
86+
Computed: true,
87+
ForceNew: true,
88+
},
89+
},
90+
CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics {
91+
rd.SetId("id")
92+
return setComputedFunc(ctx, rd, i)
93+
},
94+
UpdateContext: setComputedFunc,
95+
}
96+
97+
optionalDefaultSchema := schema.Resource{
98+
Schema: map[string]*schema.Schema{
99+
"string_prop": {
100+
Type: schema.TypeString,
101+
Optional: true,
102+
Default: "default",
103+
},
104+
},
105+
}
106+
107+
optionalDefaultForceNewSchema := schema.Resource{
108+
Schema: map[string]*schema.Schema{
109+
"string_prop": {
110+
Type: schema.TypeString,
111+
Optional: true,
112+
Default: "default",
113+
ForceNew: true,
114+
},
115+
},
116+
}
117+
118+
valueOne := ref("val1")
119+
valueTwo := ref("val2")
120+
var noValue *string
121+
122+
ctyVal := func(v *string) map[string]cty.Value {
123+
if v == nil {
124+
return map[string]cty.Value{}
125+
}
126+
return map[string]cty.Value{
127+
"string_prop": cty.StringVal(*v),
128+
}
129+
}
130+
131+
schemaValueMakerPairs := []struct {
132+
name string
133+
schema schema.Resource
134+
valueMaker func(v *string) map[string]cty.Value
135+
}{
136+
{"optional", optionalSchema, ctyVal},
137+
{"optionalForceNew", optionalForceNewSchema, ctyVal},
138+
{"required", requiredSchema, ctyVal},
139+
{"requiredForceNew", requiredForceNewSchema, ctyVal},
140+
{"optionalComputed", optionalComputedSchema, ctyVal},
141+
{"optionalComputedForceNew", optionalComputedForceNewSchema, ctyVal},
142+
{"optionalDefault", optionalDefaultSchema, ctyVal},
143+
{"optionalDefaultForceNew", optionalDefaultForceNewSchema, ctyVal},
144+
}
145+
146+
scenarios := []struct {
147+
name string
148+
initialValue *string
149+
changeValue *string
150+
}{
151+
{"unchanged empty", noValue, noValue},
152+
{"unchanged non-empty", valueOne, valueOne},
153+
{"added", noValue, valueOne},
154+
{"removed", valueOne, noValue},
155+
{"changed", valueOne, valueTwo},
156+
}
157+
158+
for _, schemaValueMakerPair := range schemaValueMakerPairs {
159+
t.Run(schemaValueMakerPair.name, func(t *testing.T) {
160+
t.Parallel()
161+
for _, scenario := range scenarios {
162+
t.Run(scenario.name, func(t *testing.T) {
163+
if strings.Contains(schemaValueMakerPair.name, "required") &&
164+
(scenario.initialValue == nil || scenario.changeValue == nil) {
165+
t.Skip("Required fields cannot be unset")
166+
}
167+
t.Parallel()
168+
diff := crosstests.Diff(t, &schemaValueMakerPair.schema, schemaValueMakerPair.valueMaker(scenario.initialValue), schemaValueMakerPair.valueMaker(scenario.changeValue))
169+
autogold.ExpectFile(t, testOutput{
170+
initialValue: scenario.initialValue,
171+
changeValue: scenario.changeValue,
172+
tfOut: diff.TFOut,
173+
pulumiOut: diff.PulumiOut,
174+
detailedDiff: diff.PulumiDiff.DetailedDiff,
175+
})
176+
})
177+
}
178+
})
179+
}
180+
}

0 commit comments

Comments
 (0)