|
1 | 1 | package tests |
2 | 2 |
|
3 | 3 | import ( |
4 | | - "context" |
5 | 4 | "strings" |
6 | 5 | "testing" |
7 | 6 |
|
8 | | - "github.com/hashicorp/terraform-plugin-sdk/v2/diag" |
9 | 7 | "github.com/hashicorp/terraform-plugin-sdk/v2/helper/schema" |
10 | 8 | "github.com/hexops/autogold/v2" |
11 | 9 | "github.com/zclconf/go-cty/cty" |
12 | 10 |
|
13 | 11 | crosstests "github.com/pulumi/pulumi-terraform-bridge/v3/pkg/internal/tests/cross-tests" |
14 | 12 | ) |
15 | 13 |
|
16 | | -func generatePrimitiveSchemaValueMakerPairs[T any]( |
17 | | - typ schema.ValueType, ctyMaker func(v T) cty.Value, val1, val2, computedVal, defaultVal, nilVal T, |
18 | | -) ([]diffSchemaValueMakerPair[T], []diffScenario[T]) { |
19 | | - valueOne := ref(val1) |
20 | | - valueTwo := ref(val2) |
21 | | - noValue := ref(nilVal) |
22 | | - |
23 | | - ctyVal := func(v *T) map[string]cty.Value { |
24 | | - if v == nil { |
25 | | - return map[string]cty.Value{} |
26 | | - } |
27 | | - return map[string]cty.Value{ |
28 | | - "prop": ctyMaker(*v), |
29 | | - } |
30 | | - } |
31 | | - |
32 | | - optionalSchema := schema.Resource{ |
33 | | - Schema: map[string]*schema.Schema{ |
34 | | - "prop": { |
35 | | - Type: typ, |
36 | | - Optional: true, |
37 | | - }, |
38 | | - }, |
39 | | - } |
40 | | - |
41 | | - optionalForceNewSchema := schema.Resource{ |
42 | | - Schema: map[string]*schema.Schema{ |
43 | | - "prop": { |
44 | | - Type: typ, |
45 | | - Optional: true, |
46 | | - ForceNew: true, |
47 | | - }, |
48 | | - }, |
49 | | - } |
50 | | - |
51 | | - requiredSchema := schema.Resource{ |
52 | | - Schema: map[string]*schema.Schema{ |
53 | | - "prop": { |
54 | | - Type: typ, |
55 | | - Required: true, |
56 | | - }, |
57 | | - }, |
58 | | - } |
59 | | - |
60 | | - requiredForceNewSchema := schema.Resource{ |
61 | | - Schema: map[string]*schema.Schema{ |
62 | | - "prop": { |
63 | | - Type: typ, |
64 | | - ForceNew: true, |
65 | | - Required: true, |
66 | | - }, |
67 | | - }, |
68 | | - } |
69 | | - |
70 | | - setComputedFunc := func(ctx context.Context, d *schema.ResourceData, meta interface{}) diag.Diagnostics { |
71 | | - if _, ok := d.GetOk("prop"); !ok { |
72 | | - err := d.Set("prop", computedVal) |
73 | | - if err != nil { |
74 | | - return diag.FromErr(err) |
75 | | - } |
76 | | - } |
77 | | - return nil |
78 | | - } |
79 | | - |
80 | | - optionalComputedSchema := schema.Resource{ |
81 | | - Schema: map[string]*schema.Schema{ |
82 | | - "prop": { |
83 | | - Type: typ, |
84 | | - Optional: true, |
85 | | - Computed: true, |
86 | | - }, |
87 | | - }, |
88 | | - CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { |
89 | | - rd.SetId("id") |
90 | | - return setComputedFunc(ctx, rd, i) |
91 | | - }, |
92 | | - UpdateContext: setComputedFunc, |
93 | | - } |
94 | | - |
95 | | - optionalComputedForceNewSchema := schema.Resource{ |
96 | | - Schema: map[string]*schema.Schema{ |
97 | | - "prop": { |
98 | | - Type: typ, |
99 | | - Optional: true, |
100 | | - Computed: true, |
101 | | - ForceNew: true, |
102 | | - }, |
103 | | - }, |
104 | | - CreateContext: func(ctx context.Context, rd *schema.ResourceData, i interface{}) diag.Diagnostics { |
105 | | - rd.SetId("id") |
106 | | - return setComputedFunc(ctx, rd, i) |
107 | | - }, |
108 | | - UpdateContext: setComputedFunc, |
109 | | - } |
110 | | - |
111 | | - optionalDefaultSchema := schema.Resource{ |
112 | | - Schema: map[string]*schema.Schema{ |
113 | | - "prop": { |
114 | | - Type: typ, |
115 | | - Optional: true, |
116 | | - Default: defaultVal, |
117 | | - }, |
118 | | - }, |
119 | | - } |
120 | | - |
121 | | - optionalDefaultForceNewSchema := schema.Resource{ |
122 | | - Schema: map[string]*schema.Schema{ |
123 | | - "prop": { |
124 | | - Type: typ, |
125 | | - Optional: true, |
126 | | - Default: defaultVal, |
127 | | - ForceNew: true, |
128 | | - }, |
129 | | - }, |
130 | | - } |
131 | | - |
132 | | - return []diffSchemaValueMakerPair[T]{ |
133 | | - {"optional", optionalSchema, ctyVal}, |
134 | | - {"optionalForceNew", optionalForceNewSchema, ctyVal}, |
135 | | - {"required", requiredSchema, ctyVal}, |
136 | | - {"requiredForceNew", requiredForceNewSchema, ctyVal}, |
137 | | - {"optionalComputed", optionalComputedSchema, ctyVal}, |
138 | | - {"optionalComputedForceNew", optionalComputedForceNewSchema, ctyVal}, |
139 | | - {"optionalDefault", optionalDefaultSchema, ctyVal}, |
140 | | - {"optionalDefaultForceNew", optionalDefaultForceNewSchema, ctyVal}, |
141 | | - }, []diffScenario[T]{ |
142 | | - {"unchanged empty", noValue, noValue}, |
143 | | - {"unchanged non-empty", valueOne, valueOne}, |
144 | | - {"added", noValue, valueOne}, |
145 | | - {"removed", valueOne, noValue}, |
146 | | - {"changed", valueOne, valueTwo}, |
147 | | - } |
148 | | -} |
149 | | - |
150 | 14 | func TestSDKv2DetailedDiffString(t *testing.T) { |
151 | 15 | t.Parallel() |
152 | 16 |
|
153 | 17 | var nilVal string |
154 | | - schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs( |
| 18 | + schemaValueMakerPairs, scenarios := generateBaseTests( |
155 | 19 | schema.TypeString, cty.StringVal, "val1", "val2", "computed", "default", nilVal) |
156 | 20 |
|
157 | 21 | for _, schemaValueMakerPair := range schemaValueMakerPairs { |
@@ -182,7 +46,7 @@ func TestSDKv2DetailedDiffBool(t *testing.T) { |
182 | 46 | t.Parallel() |
183 | 47 |
|
184 | 48 | var nilVal bool |
185 | | - schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs( |
| 49 | + schemaValueMakerPairs, scenarios := generateBaseTests( |
186 | 50 | schema.TypeBool, cty.BoolVal, true, false, true, false, nilVal) |
187 | 51 |
|
188 | 52 | for _, schemaValueMakerPair := range schemaValueMakerPairs { |
@@ -213,7 +77,7 @@ func TestSDKv2DetailedDiffInt(t *testing.T) { |
213 | 77 | t.Parallel() |
214 | 78 |
|
215 | 79 | var nilVal int64 |
216 | | - schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs( |
| 80 | + schemaValueMakerPairs, scenarios := generateBaseTests( |
217 | 81 | schema.TypeInt, cty.NumberIntVal, 1, 2, 3, 4, nilVal) |
218 | 82 |
|
219 | 83 | for _, schemaValueMakerPair := range schemaValueMakerPairs { |
@@ -244,7 +108,7 @@ func TestSDKv2DetailedDiffFloat(t *testing.T) { |
244 | 108 | t.Parallel() |
245 | 109 |
|
246 | 110 | var nilVal float64 |
247 | | - schemaValueMakerPairs, scenarios := generatePrimitiveSchemaValueMakerPairs( |
| 111 | + schemaValueMakerPairs, scenarios := generateBaseTests( |
248 | 112 | schema.TypeFloat, cty.NumberFloatVal, 1.0, 2.0, 3.0, 4.0, nilVal) |
249 | 113 |
|
250 | 114 | for _, schemaValueMakerPair := range schemaValueMakerPairs { |
|
0 commit comments