Skip to content

Commit 7ec37c2

Browse files
committed
from/to plan tests
1 parent 5d5960a commit 7ec37c2

File tree

4 files changed

+438
-4
lines changed

4 files changed

+438
-4
lines changed

internal/fromproto5/planaction_test.go

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,142 @@
33

44
package fromproto5_test
55

6-
// TODO:Actions: Add unit tests once this mapping logic is complete
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
12+
"github.com/hashicorp/terraform-plugin-go/tftypes"
13+
14+
"github.com/hashicorp/terraform-plugin-framework/action"
15+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
16+
"github.com/hashicorp/terraform-plugin-framework/diag"
17+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto5"
18+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
19+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
20+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
21+
)
22+
23+
func TestPlanActionRequest(t *testing.T) {
24+
t.Parallel()
25+
26+
testProto5Type := tftypes.Object{
27+
AttributeTypes: map[string]tftypes.Type{
28+
"test_attribute": tftypes.String,
29+
},
30+
}
31+
32+
testProto5Value := tftypes.NewValue(testProto5Type, map[string]tftypes.Value{
33+
"test_attribute": tftypes.NewValue(tftypes.String, "test-value"),
34+
})
35+
36+
testProto5DynamicValue, err := tfprotov5.NewDynamicValue(testProto5Type, testProto5Value)
37+
38+
if err != nil {
39+
t.Fatalf("unexpected error calling tfprotov5.NewDynamicValue(): %s", err)
40+
}
41+
42+
testUnlinkedSchema := schema.UnlinkedSchema{
43+
Attributes: map[string]schema.Attribute{
44+
"test_attribute": schema.StringAttribute{
45+
Required: true,
46+
},
47+
},
48+
}
49+
50+
testCases := map[string]struct {
51+
input *tfprotov5.PlanActionRequest
52+
actionSchema fwschema.Schema
53+
actionImpl action.Action
54+
providerMetaSchema fwschema.Schema
55+
expected *fwserver.PlanActionRequest
56+
expectedDiagnostics diag.Diagnostics
57+
}{
58+
"nil": {
59+
input: nil,
60+
expected: nil,
61+
},
62+
"empty": {
63+
input: &tfprotov5.PlanActionRequest{},
64+
expected: nil,
65+
expectedDiagnostics: diag.Diagnostics{
66+
diag.NewErrorDiagnostic(
67+
"Missing Action Schema",
68+
"An unexpected error was encountered when handling the request. "+
69+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
70+
"Please report this to the provider developer:\n\n"+
71+
"Missing schema.",
72+
),
73+
},
74+
},
75+
"config-missing-schema": {
76+
input: &tfprotov5.PlanActionRequest{
77+
Config: &testProto5DynamicValue,
78+
},
79+
expected: nil,
80+
expectedDiagnostics: diag.Diagnostics{
81+
diag.NewErrorDiagnostic(
82+
"Missing Action Schema",
83+
"An unexpected error was encountered when handling the request. "+
84+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
85+
"Please report this to the provider developer:\n\n"+
86+
"Missing schema.",
87+
),
88+
},
89+
},
90+
"config": {
91+
input: &tfprotov5.PlanActionRequest{
92+
Config: &testProto5DynamicValue,
93+
},
94+
actionSchema: testUnlinkedSchema,
95+
expected: &fwserver.PlanActionRequest{
96+
Config: &tfsdk.Config{
97+
Raw: testProto5Value,
98+
Schema: testUnlinkedSchema,
99+
},
100+
ActionSchema: testUnlinkedSchema,
101+
},
102+
},
103+
"client-capabilities": {
104+
input: &tfprotov5.PlanActionRequest{
105+
ClientCapabilities: &tfprotov5.PlanActionClientCapabilities{
106+
DeferralAllowed: true,
107+
},
108+
},
109+
actionSchema: testUnlinkedSchema,
110+
expected: &fwserver.PlanActionRequest{
111+
ActionSchema: testUnlinkedSchema,
112+
ClientCapabilities: action.ModifyPlanClientCapabilities{
113+
DeferralAllowed: true,
114+
},
115+
},
116+
},
117+
"client-capabilities-unset": {
118+
input: &tfprotov5.PlanActionRequest{},
119+
actionSchema: testUnlinkedSchema,
120+
expected: &fwserver.PlanActionRequest{
121+
ActionSchema: testUnlinkedSchema,
122+
ClientCapabilities: action.ModifyPlanClientCapabilities{
123+
DeferralAllowed: false,
124+
},
125+
},
126+
},
127+
}
128+
129+
for name, testCase := range testCases {
130+
t.Run(name, func(t *testing.T) {
131+
t.Parallel()
132+
133+
got, diags := fromproto5.PlanActionRequest(context.Background(), testCase.input, testCase.actionImpl, testCase.actionSchema)
134+
135+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
136+
t.Errorf("unexpected difference: %s", diff)
137+
}
138+
139+
if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" {
140+
t.Errorf("unexpected diagnostics difference: %s", diff)
141+
}
142+
})
143+
}
144+
}

internal/fromproto6/planaction_test.go

Lines changed: 139 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,142 @@
33

44
package fromproto6_test
55

6-
// TODO:Actions: Add unit tests once this mapping logic is complete
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
12+
"github.com/hashicorp/terraform-plugin-go/tftypes"
13+
14+
"github.com/hashicorp/terraform-plugin-framework/action"
15+
"github.com/hashicorp/terraform-plugin-framework/action/schema"
16+
"github.com/hashicorp/terraform-plugin-framework/diag"
17+
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto6"
18+
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
19+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
20+
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
21+
)
22+
23+
func TestPlanActionRequest(t *testing.T) {
24+
t.Parallel()
25+
26+
testProto6Type := tftypes.Object{
27+
AttributeTypes: map[string]tftypes.Type{
28+
"test_attribute": tftypes.String,
29+
},
30+
}
31+
32+
testProto6Value := tftypes.NewValue(testProto6Type, map[string]tftypes.Value{
33+
"test_attribute": tftypes.NewValue(tftypes.String, "test-value"),
34+
})
35+
36+
testProto6DynamicValue, err := tfprotov6.NewDynamicValue(testProto6Type, testProto6Value)
37+
38+
if err != nil {
39+
t.Fatalf("unexpected error calling tfprotov6.NewDynamicValue(): %s", err)
40+
}
41+
42+
testUnlinkedSchema := schema.UnlinkedSchema{
43+
Attributes: map[string]schema.Attribute{
44+
"test_attribute": schema.StringAttribute{
45+
Required: true,
46+
},
47+
},
48+
}
49+
50+
testCases := map[string]struct {
51+
input *tfprotov6.PlanActionRequest
52+
actionSchema fwschema.Schema
53+
actionImpl action.Action
54+
providerMetaSchema fwschema.Schema
55+
expected *fwserver.PlanActionRequest
56+
expectedDiagnostics diag.Diagnostics
57+
}{
58+
"nil": {
59+
input: nil,
60+
expected: nil,
61+
},
62+
"empty": {
63+
input: &tfprotov6.PlanActionRequest{},
64+
expected: nil,
65+
expectedDiagnostics: diag.Diagnostics{
66+
diag.NewErrorDiagnostic(
67+
"Missing Action Schema",
68+
"An unexpected error was encountered when handling the request. "+
69+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
70+
"Please report this to the provider developer:\n\n"+
71+
"Missing schema.",
72+
),
73+
},
74+
},
75+
"config-missing-schema": {
76+
input: &tfprotov6.PlanActionRequest{
77+
Config: &testProto6DynamicValue,
78+
},
79+
expected: nil,
80+
expectedDiagnostics: diag.Diagnostics{
81+
diag.NewErrorDiagnostic(
82+
"Missing Action Schema",
83+
"An unexpected error was encountered when handling the request. "+
84+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
85+
"Please report this to the provider developer:\n\n"+
86+
"Missing schema.",
87+
),
88+
},
89+
},
90+
"config": {
91+
input: &tfprotov6.PlanActionRequest{
92+
Config: &testProto6DynamicValue,
93+
},
94+
actionSchema: testUnlinkedSchema,
95+
expected: &fwserver.PlanActionRequest{
96+
Config: &tfsdk.Config{
97+
Raw: testProto6Value,
98+
Schema: testUnlinkedSchema,
99+
},
100+
ActionSchema: testUnlinkedSchema,
101+
},
102+
},
103+
"client-capabilities": {
104+
input: &tfprotov6.PlanActionRequest{
105+
ClientCapabilities: &tfprotov6.PlanActionClientCapabilities{
106+
DeferralAllowed: true,
107+
},
108+
},
109+
actionSchema: testUnlinkedSchema,
110+
expected: &fwserver.PlanActionRequest{
111+
ActionSchema: testUnlinkedSchema,
112+
ClientCapabilities: action.ModifyPlanClientCapabilities{
113+
DeferralAllowed: true,
114+
},
115+
},
116+
},
117+
"client-capabilities-unset": {
118+
input: &tfprotov6.PlanActionRequest{},
119+
actionSchema: testUnlinkedSchema,
120+
expected: &fwserver.PlanActionRequest{
121+
ActionSchema: testUnlinkedSchema,
122+
ClientCapabilities: action.ModifyPlanClientCapabilities{
123+
DeferralAllowed: false,
124+
},
125+
},
126+
},
127+
}
128+
129+
for name, testCase := range testCases {
130+
t.Run(name, func(t *testing.T) {
131+
t.Parallel()
132+
133+
got, diags := fromproto6.PlanActionRequest(context.Background(), testCase.input, testCase.actionImpl, testCase.actionSchema)
134+
135+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
136+
t.Errorf("unexpected difference: %s", diff)
137+
}
138+
139+
if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" {
140+
t.Errorf("unexpected diagnostics difference: %s", diff)
141+
}
142+
})
143+
}
144+
}

internal/toproto5/planaction_test.go

Lines changed: 80 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,4 +3,83 @@
33

44
package toproto5_test
55

6-
// TODO:Actions: Add unit tests once this mapping logic is complete
6+
import (
7+
"context"
8+
"testing"
9+
10+
"github.com/google/go-cmp/cmp"
11+
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
12+
13+
"github.com/hashicorp/terraform-plugin-framework/action"
14+
"github.com/hashicorp/terraform-plugin-framework/diag"
15+
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
16+
"github.com/hashicorp/terraform-plugin-framework/internal/toproto5"
17+
)
18+
19+
func TestPlanActionResponse(t *testing.T) {
20+
t.Parallel()
21+
22+
testDeferral := &action.Deferred{
23+
Reason: action.DeferredReasonAbsentPrereq,
24+
}
25+
26+
testProto5Deferred := &tfprotov5.Deferred{
27+
Reason: tfprotov5.DeferredReasonAbsentPrereq,
28+
}
29+
30+
testCases := map[string]struct {
31+
input *fwserver.PlanActionResponse
32+
expected *tfprotov5.PlanActionResponse
33+
}{
34+
"nil": {
35+
input: nil,
36+
expected: nil,
37+
},
38+
"empty": {
39+
input: &fwserver.PlanActionResponse{},
40+
expected: &tfprotov5.PlanActionResponse{},
41+
},
42+
"diagnostics": {
43+
input: &fwserver.PlanActionResponse{
44+
Diagnostics: diag.Diagnostics{
45+
diag.NewWarningDiagnostic("test warning summary", "test warning details"),
46+
diag.NewErrorDiagnostic("test error summary", "test error details"),
47+
},
48+
},
49+
expected: &tfprotov5.PlanActionResponse{
50+
Diagnostics: []*tfprotov5.Diagnostic{
51+
{
52+
Severity: tfprotov5.DiagnosticSeverityWarning,
53+
Summary: "test warning summary",
54+
Detail: "test warning details",
55+
},
56+
{
57+
Severity: tfprotov5.DiagnosticSeverityError,
58+
Summary: "test error summary",
59+
Detail: "test error details",
60+
},
61+
},
62+
},
63+
},
64+
"deferral": {
65+
input: &fwserver.PlanActionResponse{
66+
Deferred: testDeferral,
67+
},
68+
expected: &tfprotov5.PlanActionResponse{
69+
Deferred: testProto5Deferred,
70+
},
71+
},
72+
}
73+
74+
for name, testCase := range testCases {
75+
t.Run(name, func(t *testing.T) {
76+
t.Parallel()
77+
78+
got := toproto5.PlanActionResponse(context.Background(), testCase.input)
79+
80+
if diff := cmp.Diff(got, testCase.expected); diff != "" {
81+
t.Errorf("unexpected difference: %s", diff)
82+
}
83+
})
84+
}
85+
}

0 commit comments

Comments
 (0)