Skip to content

Commit 0861c97

Browse files
authored
add configure data tests for list resource (#1204)
1 parent 3a3e409 commit 0861c97

File tree

2 files changed

+54
-0
lines changed

2 files changed

+54
-0
lines changed

internal/fwserver/server_configureprovider_test.go

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -206,6 +206,20 @@ func TestServerConfigureProvider(t *testing.T) {
206206
ActionData: "test-provider-configure-value",
207207
},
208208
},
209+
"response-listresourcedata": {
210+
server: &fwserver.Server{
211+
Provider: &testprovider.Provider{
212+
SchemaMethod: func(_ context.Context, _ provider.SchemaRequest, resp *provider.SchemaResponse) {},
213+
ConfigureMethod: func(ctx context.Context, req provider.ConfigureRequest, resp *provider.ConfigureResponse) {
214+
resp.ListResourceData = "test-provider-configure-value"
215+
},
216+
},
217+
},
218+
request: &provider.ConfigureRequest{},
219+
expectedResponse: &provider.ConfigureResponse{
220+
ListResourceData: "test-provider-configure-value",
221+
},
222+
},
209223
"response-invalid-deferral-diagnostic": {
210224
server: &fwserver.Server{
211225
Provider: &testprovider.Provider{

internal/fwserver/server_listresource_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ package fwserver_test
55

66
import (
77
"context"
8+
"fmt"
89
"slices"
910
"testing"
1011

@@ -13,6 +14,7 @@ import (
1314
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
1415
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
1516
"github.com/hashicorp/terraform-plugin-framework/list"
17+
"github.com/hashicorp/terraform-plugin-framework/resource"
1618
"github.com/hashicorp/terraform-plugin-framework/resource/identityschema"
1719
"github.com/hashicorp/terraform-plugin-framework/resource/schema"
1820
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
@@ -182,6 +184,44 @@ func TestServerListResource(t *testing.T) {
182184
expectedStreamEvents: []fwserver.ListResult{},
183185
expectedError: "config cannot be nil",
184186
},
187+
"listresource-configure-data": {
188+
server: &fwserver.Server{
189+
ListResourceConfigureData: "test-provider-configure-value",
190+
Provider: &testprovider.Provider{},
191+
},
192+
request: &fwserver.ListRequest{
193+
Config: &tfsdk.Config{},
194+
ListResource: &testprovider.ListResourceWithConfigure{
195+
ConfigureMethod: func(ctx context.Context, req resource.ConfigureRequest, resp *resource.ConfigureResponse) {
196+
providerData, ok := req.ProviderData.(string)
197+
198+
if !ok {
199+
resp.Diagnostics.AddError(
200+
"Unexpected ConfigureRequest.ProviderData",
201+
fmt.Sprintf("Expected string, got: %T", req.ProviderData),
202+
)
203+
return
204+
}
205+
206+
if providerData != "test-provider-configure-value" {
207+
resp.Diagnostics.AddError(
208+
"Unexpected ConfigureRequest.ProviderData",
209+
fmt.Sprintf("Expected test-provider-configure-value, got: %q", providerData),
210+
)
211+
}
212+
},
213+
ListResource: &testprovider.ListResource{
214+
ListMethod: func(ctx context.Context, req list.ListRequest, resp *list.ListResultsStream) {
215+
// In practice, the Configure method would save the
216+
// provider data to the ListResource implementation and
217+
// use it here. The fact that Configure is able to
218+
// read the data proves this can work.
219+
},
220+
},
221+
},
222+
},
223+
expectedStreamEvents: []fwserver.ListResult{},
224+
},
185225
}
186226

187227
for name, testCase := range testCases {

0 commit comments

Comments
 (0)