-
Notifications
You must be signed in to change notification settings - Fork 91
/
readresource_test.go
191 lines (173 loc) · 5.8 KB
/
readresource_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
package fromproto5_test
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fromproto5"
"github.com/hashicorp/terraform-plugin-framework/internal/fwschema"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/privatestate"
"github.com/hashicorp/terraform-plugin-framework/resource"
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
)
func TestReadResourceRequest(t *testing.T) {
t.Parallel()
testProto5Type := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_attribute": tftypes.String,
},
}
testProto5Value := tftypes.NewValue(testProto5Type, map[string]tftypes.Value{
"test_attribute": tftypes.NewValue(tftypes.String, "test-value"),
})
testProto5DynamicValue, err := tfprotov5.NewDynamicValue(testProto5Type, testProto5Value)
if err != nil {
t.Fatalf("unexpected error calling tfprotov5.NewDynamicValue(): %s", err)
}
testFwSchema := schema.Schema{
Attributes: map[string]schema.Attribute{
"test_attribute": schema.StringAttribute{
Required: true,
},
},
}
testProviderKeyValue := privatestate.MustMarshalToJson(map[string][]byte{
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
})
testProviderData := privatestate.MustProviderData(context.Background(), testProviderKeyValue)
testEmptyProviderData := privatestate.EmptyProviderData(context.Background())
testCases := map[string]struct {
input *tfprotov5.ReadResourceRequest
resourceSchema fwschema.Schema
resource resource.Resource
providerMetaSchema fwschema.Schema
expected *fwserver.ReadResourceRequest
expectedDiagnostics diag.Diagnostics
}{
"nil": {
input: nil,
expected: nil,
},
"empty": {
input: &tfprotov5.ReadResourceRequest{},
expected: &fwserver.ReadResourceRequest{},
},
"currentstate-missing-schema": {
input: &tfprotov5.ReadResourceRequest{
CurrentState: &testProto5DynamicValue,
},
expected: &fwserver.ReadResourceRequest{},
expectedDiagnostics: diag.Diagnostics{
diag.NewErrorDiagnostic(
"Unable to Convert State",
"An unexpected error was encountered when converting the state from the protocol type. "+
"This is always an issue in terraform-plugin-framework used to implement the provider and should be reported to the provider developers.\n\n"+
"Please report this to the provider developer:\n\n"+
"Missing schema.",
),
},
},
"currentstate": {
input: &tfprotov5.ReadResourceRequest{
CurrentState: &testProto5DynamicValue,
},
resourceSchema: testFwSchema,
expected: &fwserver.ReadResourceRequest{
CurrentState: &tfsdk.State{
Raw: testProto5Value,
Schema: testFwSchema,
},
},
},
"private-malformed-json": {
input: &tfprotov5.ReadResourceRequest{
Private: []byte(`{`),
},
resourceSchema: testFwSchema,
expected: &fwserver.ReadResourceRequest{},
expectedDiagnostics: diag.Diagnostics{
diag.NewErrorDiagnostic(
"Error Decoding Private State",
"An error was encountered when decoding private state: unexpected end of JSON input.\n\n"+
"This is always a problem with Terraform or terraform-plugin-framework. Please report this to the provider developer.",
),
},
},
"private-empty-json": {
input: &tfprotov5.ReadResourceRequest{
Private: []byte("{}"),
},
resourceSchema: testFwSchema,
expected: &fwserver.ReadResourceRequest{
Private: &privatestate.Data{
Framework: map[string][]byte{},
Provider: testEmptyProviderData,
},
},
},
"private": {
input: &tfprotov5.ReadResourceRequest{
Private: privatestate.MustMarshalToJson(map[string][]byte{
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
"providerKeyOne": []byte(`{"pKeyOne": {"k0": "zero", "k1": 1}}`),
}),
},
resourceSchema: testFwSchema,
expected: &fwserver.ReadResourceRequest{
Private: &privatestate.Data{
Framework: map[string][]byte{
".frameworkKey": []byte(`{"fKeyOne": {"k0": "zero", "k1": 1}}`),
},
Provider: testProviderData,
},
},
},
"providermeta-missing-data": {
input: &tfprotov5.ReadResourceRequest{},
providerMetaSchema: testFwSchema,
expected: &fwserver.ReadResourceRequest{
ProviderMeta: &tfsdk.Config{
Raw: tftypes.NewValue(testProto5Type, nil),
Schema: testFwSchema,
},
},
},
"providermeta-missing-schema": {
input: &tfprotov5.ReadResourceRequest{
ProviderMeta: &testProto5DynamicValue,
},
expected: &fwserver.ReadResourceRequest{
// This intentionally should not include ProviderMeta
},
},
"providermeta": {
input: &tfprotov5.ReadResourceRequest{
ProviderMeta: &testProto5DynamicValue,
},
providerMetaSchema: testFwSchema,
expected: &fwserver.ReadResourceRequest{
ProviderMeta: &tfsdk.Config{
Raw: testProto5Value,
Schema: testFwSchema,
},
},
},
}
for name, testCase := range testCases {
name, testCase := name, testCase
t.Run(name, func(t *testing.T) {
t.Parallel()
got, diags := fromproto5.ReadResourceRequest(context.Background(), testCase.input, testCase.resource, testCase.resourceSchema, testCase.providerMetaSchema)
if diff := cmp.Diff(got, testCase.expected, cmp.AllowUnexported(privatestate.ProviderData{})); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}
if diff := cmp.Diff(diags, testCase.expectedDiagnostics); diff != "" {
t.Errorf("unexpected diagnostics difference: %s", diff)
}
})
}
}