Skip to content

Commit 051bdd7

Browse files
authored
internal: Implement fwserver ValidateDataSourceConfig/ValidateResourceConfig testing and update proto6server testing (#355)
Reference: #215
1 parent b9dde4f commit 051bdd7

15 files changed

+968
-720
lines changed

internal/fwserver/server_validatedatasourceconfig_test.go

+270-11
Original file line numberDiff line numberDiff line change
@@ -5,32 +5,291 @@ import (
55
"testing"
66

77
"github.com/google/go-cmp/cmp"
8+
"github.com/hashicorp/terraform-plugin-framework/diag"
89
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
9-
"github.com/hashicorp/terraform-plugin-framework/internal/testing/emptyprovider"
10+
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
11+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
12+
"github.com/hashicorp/terraform-plugin-framework/types"
13+
"github.com/hashicorp/terraform-plugin-go/tftypes"
1014
)
1115

12-
// TODO: Migrate tfsdk.Provider bits of proto6server.testProviderServer to
13-
// new internal/testing/provider.Provider that allows customization of all
14-
// method implementations via struct fields. Then, create additional test
15-
// cases in this unit test.
16-
//
17-
// For now this testing is covered by proto6server.ValidateDataSourceConfig.
18-
//
19-
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/215
2016
func TestServerValidateDataSourceConfig(t *testing.T) {
2117
t.Parallel()
2218

19+
testType := tftypes.Object{
20+
AttributeTypes: map[string]tftypes.Type{
21+
"test": tftypes.String,
22+
},
23+
}
24+
25+
testValue := tftypes.NewValue(testType, map[string]tftypes.Value{
26+
"test": tftypes.NewValue(tftypes.String, "test-value"),
27+
})
28+
29+
testSchema := tfsdk.Schema{
30+
Attributes: map[string]tfsdk.Attribute{
31+
"test": {
32+
Required: true,
33+
Type: types.StringType,
34+
},
35+
},
36+
}
37+
38+
testConfig := tfsdk.Config{
39+
Raw: testValue,
40+
Schema: testSchema,
41+
}
42+
43+
testSchemaAttributeValidator := tfsdk.Schema{
44+
Attributes: map[string]tfsdk.Attribute{
45+
"test": {
46+
Required: true,
47+
Type: types.StringType,
48+
Validators: []tfsdk.AttributeValidator{
49+
&testprovider.AttributeValidator{
50+
ValidateMethod: func(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {
51+
var got types.String
52+
53+
resp.Diagnostics.Append(tfsdk.ValueAs(ctx, req.AttributeConfig, &got)...)
54+
55+
if resp.Diagnostics.HasError() {
56+
return
57+
}
58+
59+
if got.Value != "test-value" {
60+
resp.Diagnostics.AddError("Incorrect req.AttributeConfig", "expected test-value, got "+got.Value)
61+
}
62+
},
63+
},
64+
},
65+
},
66+
},
67+
}
68+
69+
testConfigAttributeValidator := tfsdk.Config{
70+
Raw: testValue,
71+
Schema: testSchemaAttributeValidator,
72+
}
73+
74+
testSchemaAttributeValidatorError := tfsdk.Schema{
75+
Attributes: map[string]tfsdk.Attribute{
76+
"test": {
77+
Required: true,
78+
Type: types.StringType,
79+
Validators: []tfsdk.AttributeValidator{
80+
&testprovider.AttributeValidator{
81+
ValidateMethod: func(ctx context.Context, req tfsdk.ValidateAttributeRequest, resp *tfsdk.ValidateAttributeResponse) {
82+
resp.Diagnostics.AddAttributeError(req.AttributePath, "error summary", "error detail")
83+
},
84+
},
85+
},
86+
},
87+
},
88+
}
89+
90+
testConfigAttributeValidatorError := tfsdk.Config{
91+
Raw: testValue,
92+
Schema: testSchemaAttributeValidatorError,
93+
}
94+
2395
testCases := map[string]struct {
2496
server *fwserver.Server
2597
request *fwserver.ValidateDataSourceConfigRequest
2698
expectedResponse *fwserver.ValidateDataSourceConfigResponse
2799
}{
28-
"empty-provider": {
100+
"nil": {
101+
server: &fwserver.Server{
102+
Provider: &testprovider.Provider{},
103+
},
104+
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{},
105+
},
106+
"request-config": {
107+
server: &fwserver.Server{
108+
Provider: &testprovider.Provider{},
109+
},
110+
request: &fwserver.ValidateDataSourceConfigRequest{
111+
Config: &testConfig,
112+
DataSourceType: &testprovider.DataSourceType{
113+
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
114+
return testSchema, nil
115+
},
116+
},
117+
},
118+
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{},
119+
},
120+
"request-config-AttributeValidator": {
121+
server: &fwserver.Server{
122+
Provider: &testprovider.Provider{},
123+
},
124+
request: &fwserver.ValidateDataSourceConfigRequest{
125+
Config: &testConfigAttributeValidator,
126+
DataSourceType: &testprovider.DataSourceType{
127+
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
128+
return testSchemaAttributeValidator, nil
129+
},
130+
},
131+
},
132+
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{},
133+
},
134+
"request-config-AttributeValidator-diagnostic": {
135+
server: &fwserver.Server{
136+
Provider: &testprovider.Provider{},
137+
},
138+
request: &fwserver.ValidateDataSourceConfigRequest{
139+
Config: &testConfigAttributeValidatorError,
140+
DataSourceType: &testprovider.DataSourceType{
141+
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
142+
return testSchemaAttributeValidatorError, nil
143+
},
144+
},
145+
},
146+
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{
147+
Diagnostics: diag.Diagnostics{
148+
diag.NewAttributeErrorDiagnostic(
149+
tftypes.NewAttributePath().WithAttributeName("test"),
150+
"error summary",
151+
"error detail",
152+
),
153+
},
154+
},
155+
},
156+
"request-config-DataSourceWithConfigValidators": {
157+
server: &fwserver.Server{
158+
Provider: &testprovider.Provider{},
159+
},
160+
request: &fwserver.ValidateDataSourceConfigRequest{
161+
Config: &testConfig,
162+
DataSourceType: &testprovider.DataSourceType{
163+
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
164+
return testSchema, nil
165+
},
166+
NewDataSourceMethod: func(_ context.Context, _ tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
167+
return &testprovider.DataSourceWithConfigValidators{
168+
DataSource: &testprovider.DataSource{},
169+
ConfigValidatorsMethod: func(ctx context.Context) []tfsdk.DataSourceConfigValidator {
170+
return []tfsdk.DataSourceConfigValidator{
171+
&testprovider.DataSourceConfigValidator{
172+
ValidateMethod: func(ctx context.Context, req tfsdk.ValidateDataSourceConfigRequest, resp *tfsdk.ValidateDataSourceConfigResponse) {
173+
var got types.String
174+
175+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, tftypes.NewAttributePath().WithAttributeName("test"), &got)...)
176+
177+
if resp.Diagnostics.HasError() {
178+
return
179+
}
180+
181+
if got.Value != "test-value" {
182+
resp.Diagnostics.AddError("Incorrect req.Config", "expected test-value, got "+got.Value)
183+
}
184+
},
185+
},
186+
}
187+
},
188+
}, nil
189+
},
190+
},
191+
},
192+
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{},
193+
},
194+
"request-config-DataSourceWithConfigValidators-diagnostic": {
195+
server: &fwserver.Server{
196+
Provider: &testprovider.Provider{},
197+
},
198+
request: &fwserver.ValidateDataSourceConfigRequest{
199+
Config: &testConfig,
200+
DataSourceType: &testprovider.DataSourceType{
201+
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
202+
return testSchema, nil
203+
},
204+
NewDataSourceMethod: func(_ context.Context, _ tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
205+
return &testprovider.DataSourceWithConfigValidators{
206+
DataSource: &testprovider.DataSource{},
207+
ConfigValidatorsMethod: func(ctx context.Context) []tfsdk.DataSourceConfigValidator {
208+
return []tfsdk.DataSourceConfigValidator{
209+
&testprovider.DataSourceConfigValidator{
210+
ValidateMethod: func(ctx context.Context, req tfsdk.ValidateDataSourceConfigRequest, resp *tfsdk.ValidateDataSourceConfigResponse) {
211+
resp.Diagnostics.AddError("error summary", "error detail")
212+
},
213+
},
214+
}
215+
},
216+
}, nil
217+
},
218+
},
219+
},
220+
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{
221+
Diagnostics: diag.Diagnostics{
222+
diag.NewErrorDiagnostic(
223+
"error summary",
224+
"error detail",
225+
),
226+
}},
227+
},
228+
"request-config-DataSourceWithValidateConfig": {
29229
server: &fwserver.Server{
30-
Provider: &emptyprovider.Provider{},
230+
Provider: &testprovider.Provider{},
231+
},
232+
request: &fwserver.ValidateDataSourceConfigRequest{
233+
Config: &testConfig,
234+
DataSourceType: &testprovider.DataSourceType{
235+
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
236+
return testSchema, nil
237+
},
238+
NewDataSourceMethod: func(_ context.Context, _ tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
239+
return &testprovider.DataSourceWithValidateConfig{
240+
DataSource: &testprovider.DataSource{},
241+
ValidateConfigMethod: func(ctx context.Context, req tfsdk.ValidateDataSourceConfigRequest, resp *tfsdk.ValidateDataSourceConfigResponse) {
242+
var got types.String
243+
244+
resp.Diagnostics.Append(req.Config.GetAttribute(ctx, tftypes.NewAttributePath().WithAttributeName("test"), &got)...)
245+
246+
if resp.Diagnostics.HasError() {
247+
return
248+
}
249+
250+
if got.Value != "test-value" {
251+
resp.Diagnostics.AddError("Incorrect req.Config", "expected test-value, got "+got.Value)
252+
}
253+
},
254+
}, nil
255+
},
256+
},
31257
},
32258
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{},
33259
},
260+
"request-config-DataSourceWithValidateConfig-diagnostic": {
261+
server: &fwserver.Server{
262+
Provider: &testprovider.Provider{},
263+
},
264+
request: &fwserver.ValidateDataSourceConfigRequest{
265+
Config: &testConfig,
266+
DataSourceType: &testprovider.DataSourceType{
267+
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
268+
return testSchema, nil
269+
},
270+
NewDataSourceMethod: func(_ context.Context, _ tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
271+
return &testprovider.DataSourceWithValidateConfig{
272+
DataSource: &testprovider.DataSource{},
273+
ValidateConfigMethod: func(ctx context.Context, req tfsdk.ValidateDataSourceConfigRequest, resp *tfsdk.ValidateDataSourceConfigResponse) {
274+
resp.Diagnostics.AddWarning("warning summary", "warning detail")
275+
resp.Diagnostics.AddError("error summary", "error detail")
276+
},
277+
}, nil
278+
},
279+
},
280+
},
281+
expectedResponse: &fwserver.ValidateDataSourceConfigResponse{
282+
Diagnostics: diag.Diagnostics{
283+
diag.NewWarningDiagnostic(
284+
"warning summary",
285+
"warning detail",
286+
),
287+
diag.NewErrorDiagnostic(
288+
"error summary",
289+
"error detail",
290+
),
291+
}},
292+
},
34293
}
35294

36295
for name, testCase := range testCases {

0 commit comments

Comments
 (0)