/
serve_import_test.go
205 lines (181 loc) · 6.35 KB
/
serve_import_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
package tfsdk
import (
"context"
"testing"
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)
func TestServerImportResourceState(t *testing.T) {
t.Parallel()
type testCase struct {
req *tfprotov6.ImportResourceStateRequest
impl func(context.Context, ImportResourceStateRequest, *ImportResourceStateResponse)
resp *tfprotov6.ImportResourceStateResponse
}
tests := map[string]testCase{
"Set": {
req: &tfprotov6.ImportResourceStateRequest{
ID: "test",
TypeName: "test_import_state",
},
impl: func(ctx context.Context, req ImportResourceStateRequest, resp *ImportResourceStateResponse) {
state := testServeResourceImportStateData{
Id: req.ID,
}
diags := resp.State.Set(ctx, state)
resp.Diagnostics.Append(diags...)
},
resp: &tfprotov6.ImportResourceStateResponse{
ImportedResources: []*tfprotov6.ImportedResource{
{
State: func() *tfprotov6.DynamicValue {
val, err := tfprotov6.NewDynamicValue(
testServeResourceTypeImportStateTftype,
tftypes.NewValue(
testServeResourceTypeImportStateTftype,
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "test"),
"optional_string": tftypes.NewValue(tftypes.String, nil),
"required_string": tftypes.NewValue(tftypes.String, ""),
},
),
)
if err != nil {
panic(err)
}
return &val
}(),
TypeName: "test_import_state",
},
},
},
},
"SetAttribute": {
req: &tfprotov6.ImportResourceStateRequest{
ID: "test",
TypeName: "test_import_state",
},
impl: func(ctx context.Context, req ImportResourceStateRequest, resp *ImportResourceStateResponse) {
ResourceImportStatePassthroughID(ctx, tftypes.NewAttributePath().WithAttributeName("id"), req, resp)
},
resp: &tfprotov6.ImportResourceStateResponse{
ImportedResources: []*tfprotov6.ImportedResource{
{
State: func() *tfprotov6.DynamicValue {
val, err := tfprotov6.NewDynamicValue(
testServeResourceTypeImportStateTftype,
tftypes.NewValue(
testServeResourceTypeImportStateTftype,
map[string]tftypes.Value{
"id": tftypes.NewValue(tftypes.String, "test"),
"optional_string": tftypes.NewValue(tftypes.String, nil),
"required_string": tftypes.NewValue(tftypes.String, nil),
},
),
)
if err != nil {
panic(err)
}
return &val
}(),
TypeName: "test_import_state",
},
},
},
},
"ResourceImportStateNotImplemented": {
req: &tfprotov6.ImportResourceStateRequest{
ID: "test",
TypeName: "test_import_state",
},
impl: func(ctx context.Context, req ImportResourceStateRequest, resp *ImportResourceStateResponse) {
ResourceImportStateNotImplemented(ctx, "", resp)
},
resp: &tfprotov6.ImportResourceStateResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Summary: "Resource Import Not Implemented",
Severity: tfprotov6.DiagnosticSeverityError,
Detail: "This resource does not support import. Please contact the provider developer for additional information.",
},
},
},
},
"imported_resource_conversion_error": {
req: &tfprotov6.ImportResourceStateRequest{
ID: "test",
TypeName: "test_import_state",
},
impl: func(ctx context.Context, req ImportResourceStateRequest, resp *ImportResourceStateResponse) {
resp.State.Raw = tftypes.NewValue(tftypes.String, "this should never work")
},
resp: &tfprotov6.ImportResourceStateResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Summary: "Error converting imported resource response",
Severity: tfprotov6.DiagnosticSeverityError,
Detail: "An unexpected error was encountered when converting the imported resource response to a usable type. This is always a problem with the provider. Please give the following information to the provider developer:\n\n" +
`unexpected value type string, tftypes.Object["id":tftypes.String, "optional_string":tftypes.String, "required_string":tftypes.String] values must be of type map[string]tftypes.Value`,
},
},
},
},
"no_state": {
req: &tfprotov6.ImportResourceStateRequest{
ID: "test",
TypeName: "test_import_state",
},
impl: func(ctx context.Context, req ImportResourceStateRequest, resp *ImportResourceStateResponse) {},
resp: &tfprotov6.ImportResourceStateResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Summary: "Missing Resource Import State",
Severity: tfprotov6.DiagnosticSeverityError,
Detail: "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" +
"Resource ImportState method returned no State in response. If import is intentionally not supported, call the ResourceImportStateNotImplemented() function or return an error.",
},
},
},
},
"TypeName-ImportState-not-implemented": {
req: &tfprotov6.ImportResourceStateRequest{
ID: "test",
TypeName: "test_import_state_not_implemented",
},
resp: &tfprotov6.ImportResourceStateResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Summary: "Resource Import Not Implemented",
Severity: tfprotov6.DiagnosticSeverityError,
Detail: "This resource does not support import. Please contact the provider developer for additional information.",
},
},
},
},
}
for name, tc := range tests {
name, tc := name, tc
t.Run(name, func(t *testing.T) {
t.Parallel()
s := &testServeProvider{
importStateFunc: tc.impl,
}
testServer := &server{
p: s,
}
got, err := testServer.ImportResourceState(context.Background(), tc.req)
if err != nil {
t.Errorf("Unexpected error: %s", err)
return
}
if tc.req.TypeName == "test_import_state" && s.importResourceStateCalledResourceType != tc.req.TypeName {
t.Errorf("Called wrong resource. Expected to call %q, actually called %q", tc.req.TypeName, s.importResourceStateCalledResourceType)
return
}
if diff := cmp.Diff(got, tc.resp); diff != "" {
t.Errorf("Unexpected diff in response (+wanted, -got): %s", diff)
}
})
}
}