-
Notifications
You must be signed in to change notification settings - Fork 97
/
Copy pathconfig_test.go
286 lines (264 loc) · 7.82 KB
/
config_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
// Copyright (c) HashiCorp, Inc.
// SPDX-License-Identifier: MPL-2.0
package tfsdk_test
import (
"context"
"reflect"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
intreflect "github.com/hashicorp/terraform-plugin-framework/internal/reflect"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testschema"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testtypes"
"github.com/hashicorp/terraform-plugin-framework/path"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func TestConfigGet(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
config tfsdk.Config
target any
expected any
expectedDiags diag.Diagnostics
}{
// Refer to fwschemadata.TestDataGet for more exhaustive unit testing.
// These test cases are to ensure Plan schema and data values are
// passed appropriately to the shared implementation.
"valid": {
config: tfsdk.Config{
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"string": tftypes.String,
},
},
map[string]tftypes.Value{
"string": tftypes.NewValue(tftypes.String, "test"),
},
),
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"string": testschema.Attribute{
Optional: true,
Type: types.StringType,
},
},
},
},
target: new(struct {
String types.String `tfsdk:"string"`
}),
expected: &struct {
String types.String `tfsdk:"string"`
}{
String: types.StringValue("test"),
},
},
"diagnostic": {
config: tfsdk.Config{
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"bool": tftypes.Bool,
},
},
map[string]tftypes.Value{
"bool": tftypes.NewValue(tftypes.Bool, nil),
},
),
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"bool": testschema.Attribute{
Optional: true,
Type: types.BoolType,
},
},
},
},
target: new(struct {
String types.String `tfsdk:"bool"`
}),
expected: &struct {
String types.String `tfsdk:"bool"`
}{
String: types.String{},
},
expectedDiags: diag.Diagnostics{
diag.WithPath(
path.Root("bool"),
intreflect.DiagNewAttributeValueIntoWrongType{
ValType: reflect.TypeOf(types.Bool{}),
TargetType: reflect.TypeOf(types.String{}),
SchemaType: types.BoolType,
},
),
},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
diags := testCase.config.Get(context.Background(), testCase.target)
if diff := cmp.Diff(diags, testCase.expectedDiags); diff != "" {
t.Errorf("unexpected diagnostics (+wanted, -got): %s", diff)
}
if diff := cmp.Diff(testCase.target, testCase.expected); diff != "" {
t.Errorf("unexpected value (+wanted, -got): %s", diff)
}
})
}
}
func TestConfigGetAttribute(t *testing.T) {
t.Parallel()
type testCase struct {
config tfsdk.Config
target interface{}
expected interface{}
expectedDiags diag.Diagnostics
}
testCases := map[string]testCase{
// Refer to fwschemadata.TestDataGetAtPath for more exhaustive unit
// testing. These test cases are to ensure Plan schema and data values
// are passed appropriately to the shared implementation.
"valid": {
config: tfsdk.Config{
Raw: tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
}, map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "namevalue"),
}),
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"name": testschema.Attribute{
Type: types.StringType,
Required: true,
},
},
},
},
target: new(string),
expected: pointer("namevalue"),
},
"diagnostics": {
config: tfsdk.Config{
Raw: tftypes.NewValue(tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"name": tftypes.String,
},
}, map[string]tftypes.Value{
"name": tftypes.NewValue(tftypes.String, "namevalue"),
}),
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"name": testschema.Attribute{
Type: testtypes.StringTypeWithValidateWarning{},
Required: true,
},
},
},
},
target: new(testtypes.String),
expected: &testtypes.String{InternalString: types.StringValue("namevalue"), CreatedBy: testtypes.StringTypeWithValidateWarning{}},
expectedDiags: diag.Diagnostics{testtypes.TestWarningDiagnostic(path.Root("name"))},
},
}
for name, tc := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
diags := tc.config.GetAttribute(context.Background(), path.Root("name"), tc.target)
if diff := cmp.Diff(diags, tc.expectedDiags); diff != "" {
t.Errorf("unexpected diagnostics (+wanted, -got): %s", diff)
}
if diff := cmp.Diff(tc.target, tc.expected, cmp.Transformer("testtypes", func(in *testtypes.String) testtypes.String { return *in }), cmp.Transformer("types", func(in *types.String) types.String { return *in })); diff != "" {
t.Errorf("unexpected value (+wanted, -got): %s", diff)
}
})
}
}
func TestConfigPathMatches(t *testing.T) {
t.Parallel()
testCases := map[string]struct {
config tfsdk.Config
expression path.Expression
expected path.Paths
expectedDiags diag.Diagnostics
}{
// Refer to TestPathMatches for more exhaustive unit testing.
// These test cases are to ensure Config schema and data values are
// passed appropriately to the shared implementation.
"AttributeNameExact-match": {
config: tfsdk.Config{
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"test": testschema.Attribute{
Type: types.StringType,
},
},
},
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.String,
},
},
map[string]tftypes.Value{
"test": tftypes.NewValue(tftypes.String, "test-value"),
},
),
},
expression: path.MatchRoot("test"),
expected: path.Paths{
path.Root("test"),
},
},
"AttributeNameExact-mismatch": {
config: tfsdk.Config{
Schema: testschema.Schema{
Attributes: map[string]fwschema.Attribute{
"test": testschema.Attribute{
Type: types.StringType,
},
},
},
Raw: tftypes.NewValue(
tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test": tftypes.String,
},
},
map[string]tftypes.Value{
"test": tftypes.NewValue(tftypes.String, "test-value"),
},
),
},
expression: path.MatchRoot("not-test"),
expected: nil,
expectedDiags: diag.Diagnostics{
diag.NewErrorDiagnostic(
"Invalid Path Expression for Schema",
"The Terraform Provider unexpectedly provided a path expression that does not match the current schema. "+
"This can happen if the path expression does not correctly follow the schema in structure or types. "+
"Please report this to the provider developers.\n\n"+
"Path Expression: not-test",
),
},
},
}
for name, testCase := range testCases {
t.Run(name, func(t *testing.T) {
t.Parallel()
got, diags := testCase.config.PathMatches(context.Background(), testCase.expression)
if diff := cmp.Diff(got, testCase.expected); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
if diff := cmp.Diff(diags, testCase.expectedDiags); diff != "" {
t.Errorf("unexpected diagnostics difference: %s", diff)
}
})
}
}