Skip to content

Commit 50aaca9

Browse files
committed
Add unit tests for client capabilities
1 parent 2433dca commit 50aaca9

File tree

4 files changed

+390
-136
lines changed

4 files changed

+390
-136
lines changed

internal/fwserver/server_importresourcestate_test.go

Lines changed: 94 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -99,6 +99,67 @@ func TestServerImportResourceState(t *testing.T) {
9999
},
100100
expectedResponse: &fwserver.ImportResourceStateResponse{},
101101
},
102+
"request-client-capabilities-deferral-allowed": {
103+
server: &fwserver.Server{
104+
Provider: &testprovider.Provider{},
105+
},
106+
request: &fwserver.ImportResourceStateRequest{
107+
ClientCapabilities: testDeferral,
108+
EmptyState: *testEmptyState,
109+
ID: "test-id",
110+
Resource: &testprovider.ResourceWithImportState{
111+
Resource: &testprovider.Resource{},
112+
ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
113+
if req.ClientCapabilities.DeferralAllowed != true {
114+
resp.Diagnostics.AddError("Unexpected req.ClientCapabilities.DeferralAllowed value",
115+
"expected: true but got: false")
116+
}
117+
118+
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
119+
},
120+
},
121+
TypeName: "test_resource",
122+
},
123+
expectedResponse: &fwserver.ImportResourceStateResponse{
124+
ImportedResources: []fwserver.ImportedResource{
125+
{
126+
State: *testState,
127+
TypeName: "test_resource",
128+
Private: testEmptyPrivate,
129+
},
130+
},
131+
},
132+
},
133+
"request-client-capabilities-unset": {
134+
server: &fwserver.Server{
135+
Provider: &testprovider.Provider{},
136+
},
137+
request: &fwserver.ImportResourceStateRequest{
138+
EmptyState: *testEmptyState,
139+
ID: "test-id",
140+
Resource: &testprovider.ResourceWithImportState{
141+
Resource: &testprovider.Resource{},
142+
ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
143+
if req.ClientCapabilities.DeferralAllowed != false {
144+
resp.Diagnostics.AddError("Unexpected req.ClientCapabilities.DeferralAllowed value",
145+
"expected: false but got: true")
146+
}
147+
148+
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
149+
},
150+
},
151+
TypeName: "test_resource",
152+
},
153+
expectedResponse: &fwserver.ImportResourceStateResponse{
154+
ImportedResources: []fwserver.ImportedResource{
155+
{
156+
State: *testState,
157+
TypeName: "test_resource",
158+
Private: testEmptyPrivate,
159+
},
160+
},
161+
},
162+
},
102163
"request-id": {
103164
server: &fwserver.Server{
104165
Provider: &testprovider.Provider{},
@@ -249,7 +310,7 @@ func TestServerImportResourceState(t *testing.T) {
249310
},
250311
},
251312
},
252-
"response-importedresources-private": {
313+
"response-importedresources-deferral": {
253314
server: &fwserver.Server{
254315
Provider: &testprovider.Provider{},
255316
},
@@ -259,26 +320,33 @@ func TestServerImportResourceState(t *testing.T) {
259320
Resource: &testprovider.ResourceWithImportState{
260321
Resource: &testprovider.Resource{},
261322
ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
262-
diags := resp.Private.SetKey(ctx, "providerKeyOne", []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`))
323+
if req.ID != "test-id" {
324+
resp.Diagnostics.AddError("unexpected req.ID value: %s", req.ID)
325+
}
263326

264-
resp.Diagnostics.Append(diags...)
327+
resp.Deferred = &resource.Deferred{
328+
Reason: resource.DeferredReasonAbsentPrereq,
329+
}
265330

266331
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
332+
267333
},
268334
},
269-
TypeName: "test_resource",
335+
TypeName: "test_resource",
336+
ClientCapabilities: testDeferral,
270337
},
271338
expectedResponse: &fwserver.ImportResourceStateResponse{
272339
ImportedResources: []fwserver.ImportedResource{
273340
{
274341
State: *testState,
275342
TypeName: "test_resource",
276-
Private: testPrivate,
343+
Private: testEmptyPrivate,
277344
},
278345
},
346+
Deferred: &resource.Deferred{Reason: resource.DeferredReasonAbsentPrereq},
279347
},
280348
},
281-
"response-importedresources-empty-state": {
349+
"response-importedresources-private": {
282350
server: &fwserver.Server{
283351
Provider: &testprovider.Provider{},
284352
},
@@ -288,22 +356,26 @@ func TestServerImportResourceState(t *testing.T) {
288356
Resource: &testprovider.ResourceWithImportState{
289357
Resource: &testprovider.Resource{},
290358
ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
291-
// Intentionally empty
359+
diags := resp.Private.SetKey(ctx, "providerKeyOne", []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`))
360+
361+
resp.Diagnostics.Append(diags...)
362+
363+
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
292364
},
293365
},
294366
TypeName: "test_resource",
295367
},
296368
expectedResponse: &fwserver.ImportResourceStateResponse{
297-
Diagnostics: diag.Diagnostics{
298-
diag.NewErrorDiagnostic(
299-
"Missing Resource Import State",
300-
"An unexpected error was encountered when importing the resource. This is always a problem with the provider. Please give the following information to the provider developer:\n\n"+
301-
"Resource ImportState method returned no State in response. If import is intentionally not supported, remove the Resource type ImportState method or return an error.",
302-
),
369+
ImportedResources: []fwserver.ImportedResource{
370+
{
371+
State: *testState,
372+
TypeName: "test_resource",
373+
Private: testPrivate,
374+
},
303375
},
304376
},
305377
},
306-
"response-importedresources-deferral": {
378+
"response-importedresources-empty-state": {
307379
server: &fwserver.Server{
308380
Provider: &testprovider.Provider{},
309381
},
@@ -313,30 +385,19 @@ func TestServerImportResourceState(t *testing.T) {
313385
Resource: &testprovider.ResourceWithImportState{
314386
Resource: &testprovider.Resource{},
315387
ImportStateMethod: func(ctx context.Context, req resource.ImportStateRequest, resp *resource.ImportStateResponse) {
316-
if req.ID != "test-id" {
317-
resp.Diagnostics.AddError("unexpected req.ID value: %s", req.ID)
318-
}
319-
320-
resp.Deferred = &resource.Deferred{
321-
Reason: resource.DeferredReasonAbsentPrereq,
322-
}
323-
324-
resource.ImportStatePassthroughID(ctx, path.Root("id"), req, resp)
325-
388+
// Intentionally empty
326389
},
327390
},
328-
TypeName: "test_resource",
329-
ClientCapabilities: testDeferral,
391+
TypeName: "test_resource",
330392
},
331393
expectedResponse: &fwserver.ImportResourceStateResponse{
332-
ImportedResources: []fwserver.ImportedResource{
333-
{
334-
State: *testState,
335-
TypeName: "test_resource",
336-
Private: testEmptyPrivate,
337-
},
394+
Diagnostics: diag.Diagnostics{
395+
diag.NewErrorDiagnostic(
396+
"Missing Resource Import State",
397+
"An unexpected error was encountered when importing the resource. This is always a problem with the provider. Please give the following information to the provider developer:\n\n"+
398+
"Resource ImportState method returned no State in response. If import is intentionally not supported, remove the Resource type ImportState method or return an error.",
399+
),
338400
},
339-
Deferred: &resource.Deferred{Reason: resource.DeferredReasonAbsentPrereq},
340401
},
341402
},
342403
}

internal/fwserver/server_planresourcechange_test.go

Lines changed: 128 additions & 43 deletions
Original file line numberDiff line numberDiff line change
@@ -2783,6 +2783,91 @@ func TestServerPlanResourceChange(t *testing.T) {
27832783
PlannedPrivate: testEmptyPrivate,
27842784
},
27852785
},
2786+
"create-resourcewithmodifyplan-request-client-capabilities-deferral-allowed": {
2787+
server: &fwserver.Server{
2788+
Provider: &testprovider.Provider{},
2789+
},
2790+
request: &fwserver.PlanResourceChangeRequest{
2791+
ClientCapabilities: testDeferralAllowed,
2792+
Config: &tfsdk.Config{
2793+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
2794+
"test_computed": tftypes.NewValue(tftypes.String, nil),
2795+
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
2796+
}),
2797+
Schema: testSchema,
2798+
},
2799+
ProposedNewState: &tfsdk.Plan{
2800+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
2801+
"test_computed": tftypes.NewValue(tftypes.String, nil),
2802+
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
2803+
}),
2804+
Schema: testSchema,
2805+
},
2806+
PriorState: testEmptyState,
2807+
ResourceSchema: testSchema,
2808+
Resource: &testprovider.ResourceWithModifyPlan{
2809+
ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
2810+
if req.ClientCapabilities.DeferralAllowed != true {
2811+
resp.Diagnostics.AddError("Unexpected req.ClientCapabilities.DeferralAllowed value",
2812+
"expected: true but got: false")
2813+
}
2814+
2815+
},
2816+
},
2817+
},
2818+
expectedResponse: &fwserver.PlanResourceChangeResponse{
2819+
PlannedState: &tfsdk.State{
2820+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
2821+
"test_computed": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
2822+
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
2823+
}),
2824+
Schema: testSchema,
2825+
},
2826+
PlannedPrivate: testEmptyPrivate,
2827+
},
2828+
},
2829+
"create-resourcewithmodifyplan-request-client-capabilities-unset": {
2830+
server: &fwserver.Server{
2831+
Provider: &testprovider.Provider{},
2832+
},
2833+
request: &fwserver.PlanResourceChangeRequest{
2834+
Config: &tfsdk.Config{
2835+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
2836+
"test_computed": tftypes.NewValue(tftypes.String, nil),
2837+
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
2838+
}),
2839+
Schema: testSchema,
2840+
},
2841+
ProposedNewState: &tfsdk.Plan{
2842+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
2843+
"test_computed": tftypes.NewValue(tftypes.String, nil),
2844+
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
2845+
}),
2846+
Schema: testSchema,
2847+
},
2848+
PriorState: testEmptyState,
2849+
ResourceSchema: testSchema,
2850+
Resource: &testprovider.ResourceWithModifyPlan{
2851+
ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
2852+
if req.ClientCapabilities.DeferralAllowed != false {
2853+
resp.Diagnostics.AddError("Unexpected req.ClientCapabilities.DeferralAllowed value",
2854+
"expected: false but got: true")
2855+
}
2856+
2857+
},
2858+
},
2859+
},
2860+
expectedResponse: &fwserver.PlanResourceChangeResponse{
2861+
PlannedState: &tfsdk.State{
2862+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
2863+
"test_computed": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
2864+
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
2865+
}),
2866+
Schema: testSchema,
2867+
},
2868+
PlannedPrivate: testEmptyPrivate,
2869+
},
2870+
},
27862871
"create-resourcewithmodifyplan-request-config": {
27872872
server: &fwserver.Server{
27882873
Provider: &testprovider.Provider{},
@@ -2964,6 +3049,49 @@ func TestServerPlanResourceChange(t *testing.T) {
29643049
PlannedPrivate: testEmptyPrivate,
29653050
},
29663051
},
3052+
"create-resourcewithmodifyplan-response-deferral": {
3053+
server: &fwserver.Server{
3054+
Provider: &testprovider.Provider{},
3055+
},
3056+
request: &fwserver.PlanResourceChangeRequest{
3057+
ClientCapabilities: testDeferralAllowed,
3058+
Config: &tfsdk.Config{
3059+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
3060+
"test_computed": tftypes.NewValue(tftypes.String, nil),
3061+
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
3062+
}),
3063+
Schema: testSchema,
3064+
},
3065+
ProposedNewState: &tfsdk.Plan{
3066+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
3067+
"test_computed": tftypes.NewValue(tftypes.String, nil),
3068+
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
3069+
}),
3070+
Schema: testSchema,
3071+
},
3072+
PriorState: testEmptyState,
3073+
ResourceSchema: testSchema,
3074+
Resource: &testprovider.ResourceWithModifyPlan{
3075+
ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
3076+
if req.ClientCapabilities.DeferralAllowed == true {
3077+
resp.Deferred = &resource.Deferred{Reason: resource.DeferredReasonAbsentPrereq}
3078+
}
3079+
3080+
},
3081+
},
3082+
},
3083+
expectedResponse: &fwserver.PlanResourceChangeResponse{
3084+
Deferred: &resource.Deferred{Reason: resource.DeferredReasonAbsentPrereq},
3085+
PlannedState: &tfsdk.State{
3086+
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
3087+
"test_computed": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
3088+
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
3089+
}),
3090+
Schema: testSchema,
3091+
},
3092+
PlannedPrivate: testEmptyPrivate,
3093+
},
3094+
},
29673095
"create-resourcewithmodifyplan-response-diagnostics": {
29683096
server: &fwserver.Server{
29693097
Provider: &testprovider.Provider{},
@@ -6019,49 +6147,6 @@ func TestServerPlanResourceChange(t *testing.T) {
60196147
PlannedPrivate: testPrivateProvider,
60206148
},
60216149
},
6022-
"create-resourcewithmodifyplan-response-deferral": {
6023-
server: &fwserver.Server{
6024-
Provider: &testprovider.Provider{},
6025-
},
6026-
request: &fwserver.PlanResourceChangeRequest{
6027-
ClientCapabilities: testDeferralAllowed,
6028-
Config: &tfsdk.Config{
6029-
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
6030-
"test_computed": tftypes.NewValue(tftypes.String, nil),
6031-
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
6032-
}),
6033-
Schema: testSchema,
6034-
},
6035-
ProposedNewState: &tfsdk.Plan{
6036-
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
6037-
"test_computed": tftypes.NewValue(tftypes.String, nil),
6038-
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
6039-
}),
6040-
Schema: testSchema,
6041-
},
6042-
PriorState: testEmptyState,
6043-
ResourceSchema: testSchema,
6044-
Resource: &testprovider.ResourceWithModifyPlan{
6045-
ModifyPlanMethod: func(ctx context.Context, req resource.ModifyPlanRequest, resp *resource.ModifyPlanResponse) {
6046-
if req.ClientCapabilities.DeferralAllowed == true {
6047-
resp.Deferred = &resource.Deferred{Reason: resource.DeferredReasonAbsentPrereq}
6048-
}
6049-
6050-
},
6051-
},
6052-
},
6053-
expectedResponse: &fwserver.PlanResourceChangeResponse{
6054-
Deferred: &resource.Deferred{Reason: resource.DeferredReasonAbsentPrereq},
6055-
PlannedState: &tfsdk.State{
6056-
Raw: tftypes.NewValue(testSchemaType, map[string]tftypes.Value{
6057-
"test_computed": tftypes.NewValue(tftypes.String, tftypes.UnknownValue),
6058-
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
6059-
}),
6060-
Schema: testSchema,
6061-
},
6062-
PlannedPrivate: testEmptyPrivate,
6063-
},
6064-
},
60656150
}
60666151

60676152
for name, testCase := range testCases {

0 commit comments

Comments
 (0)