Skip to content

Commit cc42117

Browse files
committed
Add a test for IncludeResource diagnostic
1 parent 9168f7f commit cc42117

File tree

3 files changed

+72
-19
lines changed

3 files changed

+72
-19
lines changed

internal/fwserver/server_listresource.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -113,15 +113,18 @@ func processListResult(req list.ListRequest, result list.ListResult) ListResult
113113
if !result.Diagnostics.HasError() && result.Identity == nil { // TODO: write a test
114114
return ListResult{
115115
Diagnostics: diag.Diagnostics{
116-
diag.NewErrorDiagnostic("Incomplete List Result", "ListResult.Identity is nil."),
116+
diag.NewErrorDiagnostic(
117+
"Incomplete List Result",
118+
"The provider did not populate the Identity field in the ListResourceResult. This may be due to an error in the provider's implementation.",
119+
),
117120
},
118121
}
119122
}
120123

121124
if !result.Diagnostics.HasError() && req.IncludeResource && result.Resource == nil { // TODO: write a test
122125
result.Diagnostics.AddWarning(
123126
"Incomplete List Result",
124-
"ListRequest.IncludeResource is true and ListResult.Resource is nil.",
127+
"The provider did not populate the Resource field in the ListResourceResult. This may be due to the provider not supporting this functionality or an error in the provider's implementation.",
125128
)
126129
}
127130

internal/proto6server/server_listresource_test.go

Lines changed: 66 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -91,14 +91,14 @@ func TestServerListResource(t *testing.T) {
9191
}
9292
},
9393
ListMethod: func(ctx context.Context, req list.ListRequest, resp *list.ListResultsStream) {
94-
results := []list.ListResult{}
9594
var config listConfig
9695
diags := req.Config.Get(ctx, &config)
9796
if len(diags) > 0 {
9897
t.Fatalf("unexpected diagnostics: %s", diags)
9998
}
10099

101-
for _, name := range []string{"plateau", "platinum", "platypus"} {
100+
results := []list.ListResult{}
101+
for name := range resources {
102102
if !strings.HasPrefix(name, config.Filter) {
103103
continue
104104
}
@@ -119,6 +119,25 @@ func TestServerListResource(t *testing.T) {
119119
}
120120
}
121121

122+
listResourceThatDoesNotPopulateResource := func() list.ListResource {
123+
r, ok := listResource().(*testprovider.ListResource)
124+
if !ok {
125+
t.Fatal("listResourceThatDoesNotPopulateResource must be a testprovider.ListResource")
126+
}
127+
128+
r.ListMethod = func(ctx context.Context, req list.ListRequest, resp *list.ListResultsStream) {
129+
result := req.ToResult(
130+
ctx,
131+
resources["plateau"].ThingResourceIdentity,
132+
nil,
133+
"plateau")
134+
135+
resp.Results = slices.Values([]list.ListResult{result})
136+
}
137+
138+
return r
139+
}
140+
122141
managedResource := func() resource.Resource {
123142
return &testprovider.ResourceWithIdentity{
124143
IdentitySchemaMethod: func(ctx context.Context, req resource.IdentitySchemaRequest, resp *resource.IdentitySchemaResponse) {
@@ -144,23 +163,27 @@ func TestServerListResource(t *testing.T) {
144163
}
145164
}
146165

147-
happyServer := &Server{
148-
FrameworkServer: fwserver.Server{
149-
Provider: &testprovider.Provider{
150-
ListResourcesMethod: func(ctx context.Context) []func() list.ListResource {
151-
return []func() list.ListResource{
152-
listResource,
153-
}
154-
},
155-
ResourcesMethod: func(ctx context.Context) []func() resource.Resource {
156-
return []func() resource.Resource{
157-
managedResource,
158-
}
166+
server := func(listResource func() list.ListResource, managedResource func() resource.Resource) *Server {
167+
return &Server{
168+
FrameworkServer: fwserver.Server{
169+
Provider: &testprovider.Provider{
170+
ListResourcesMethod: func(ctx context.Context) []func() list.ListResource {
171+
return []func() list.ListResource{
172+
listResource,
173+
}
174+
},
175+
ResourcesMethod: func(ctx context.Context) []func() resource.Resource {
176+
return []func() resource.Resource{
177+
managedResource,
178+
}
179+
},
159180
},
160181
},
161-
},
182+
}
162183
}
163184

185+
happyServer := server(listResource, managedResource)
186+
164187
testCases := map[string]struct {
165188
server *Server
166189
request *tfprotov6.ListResourceRequest
@@ -226,6 +249,29 @@ func TestServerListResource(t *testing.T) {
226249
},
227250
},
228251
},
252+
"result-with-include-resource-warning": {
253+
server: server(listResourceThatDoesNotPopulateResource, managedResource),
254+
request: &tfprotov6.ListResourceRequest{
255+
TypeName: "test_resource",
256+
Config: plateau,
257+
IncludeResource: true,
258+
},
259+
expectedError: nil,
260+
expectedDiagnostics: diag.Diagnostics{},
261+
expectedResults: []tfprotov6.ListResourceResult{
262+
{
263+
DisplayName: "plateau",
264+
Identity: expectedResourceIdentities["plateau"],
265+
Diagnostics: []*tfprotov6.Diagnostic{
266+
{
267+
Severity: tfprotov6.DiagnosticSeverityWarning,
268+
Summary: "Incomplete List Result",
269+
Detail: "The provider did not populate the Resource field in the ListResourceResult. This may be due to the provider not supporting this functionality or an error in the provider's implementation.",
270+
},
271+
},
272+
},
273+
},
274+
},
229275
}
230276

231277
for name, testCase := range testCases {
@@ -245,7 +291,11 @@ func TestServerListResource(t *testing.T) {
245291
t.Errorf("unexpected error difference: %s", diff)
246292
}
247293

248-
if diff := cmp.Diff(testCase.expectedResults, slices.Collect(got.Results), cmpopts.EquateEmpty()); diff != "" {
294+
sortResults := cmpopts.SortSlices(func(a, b tfprotov6.ListResourceResult) bool {
295+
return a.DisplayName < b.DisplayName
296+
})
297+
298+
if diff := cmp.Diff(testCase.expectedResults, slices.Collect(got.Results), sortResults, cmpopts.EquateEmpty()); diff != "" {
249299
t.Errorf("unexpected results difference: %s", diff)
250300
}
251301
})

list/list_resource.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -140,7 +140,7 @@ func (r ListRequest) ToResult(ctx context.Context, identityVal any, resourceVal
140140
}
141141

142142
var resource *tfsdk.Resource
143-
if r.IncludeResource {
143+
if r.IncludeResource && resourceVal != nil {
144144
resource, d = r.ToResource(ctx, resourceVal)
145145
diags.Append(d...)
146146
if diags.HasError() {

0 commit comments

Comments
 (0)