Skip to content

internal: Implement fwserver ReadDataSource testing and update proto6server testing #359

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Jun 6, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
17 changes: 16 additions & 1 deletion internal/fromproto6/readdatasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,23 @@ func ReadDataSourceRequest(ctx context.Context, proto6 *tfprotov6.ReadDataSource

var diags diag.Diagnostics

// Panic prevention here to simplify the calling implementations.
// This should not happen, but just in case.
if dataSourceSchema == nil {
diags.AddError(
"Missing DataSource Schema",
"An unexpected error was encountered when handling the request. "+
"This is always an issue in the Terraform Provider SDK 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.",
)

return nil, diags
}

fw := &fwserver.ReadDataSourceRequest{
DataSourceType: dataSourceType,
DataSourceSchema: *dataSourceSchema,
DataSourceType: dataSourceType,
}

config, configDiags := Config(ctx, proto6.Config, dataSourceSchema)
Expand Down
24 changes: 20 additions & 4 deletions internal/fromproto6/readdatasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,17 +56,26 @@ func TestReadDataSourceRequest(t *testing.T) {
},
"empty": {
input: &tfprotov6.ReadDataSourceRequest{},
expected: &fwserver.ReadDataSourceRequest{},
expected: nil,
expectedDiagnostics: diag.Diagnostics{
diag.NewErrorDiagnostic(
"Missing DataSource Schema",
"An unexpected error was encountered when handling the request. "+
"This is always an issue in the Terraform Provider SDK 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.",
),
},
},
"config-missing-schema": {
input: &tfprotov6.ReadDataSourceRequest{
Config: &testProto6DynamicValue,
},
expected: &fwserver.ReadDataSourceRequest{},
expected: nil,
expectedDiagnostics: diag.Diagnostics{
diag.NewErrorDiagnostic(
"Unable to Convert Configuration",
"An unexpected error was encountered when converting the configuration from the protocol type. "+
"Missing DataSource Schema",
"An unexpected error was encountered when handling the request. "+
"This is always an issue in the Terraform Provider SDK 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.",
Expand All @@ -83,12 +92,15 @@ func TestReadDataSourceRequest(t *testing.T) {
Raw: testProto6Value,
Schema: *testFwSchema,
},
DataSourceSchema: *testFwSchema,
},
},
"providermeta-missing-data": {
input: &tfprotov6.ReadDataSourceRequest{},
dataSourceSchema: testFwSchema,
providerMetaSchema: testFwSchema,
expected: &fwserver.ReadDataSourceRequest{
DataSourceSchema: *testFwSchema,
ProviderMeta: &tfsdk.Config{
Raw: tftypes.NewValue(testProto6Type, nil),
Schema: *testFwSchema,
Expand All @@ -99,16 +111,20 @@ func TestReadDataSourceRequest(t *testing.T) {
input: &tfprotov6.ReadDataSourceRequest{
ProviderMeta: &testProto6DynamicValue,
},
dataSourceSchema: testFwSchema,
expected: &fwserver.ReadDataSourceRequest{
DataSourceSchema: *testFwSchema,
// This intentionally should not include ProviderMeta
},
},
"providermeta": {
input: &tfprotov6.ReadDataSourceRequest{
ProviderMeta: &testProto6DynamicValue,
},
dataSourceSchema: testFwSchema,
providerMetaSchema: testFwSchema,
expected: &fwserver.ReadDataSourceRequest{
DataSourceSchema: *testFwSchema,
ProviderMeta: &tfsdk.Config{
Raw: testProto6Value,
Schema: *testFwSchema,
Expand Down
15 changes: 10 additions & 5 deletions internal/fwserver/server_readdatasource.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,9 +11,10 @@ import (
// ReadDataSourceRequest is the framework server request for the
// ReadDataSource RPC.
type ReadDataSourceRequest struct {
Config *tfsdk.Config
DataSourceType tfsdk.DataSourceType
ProviderMeta *tfsdk.Config
Config *tfsdk.Config
DataSourceSchema tfsdk.Schema
DataSourceType tfsdk.DataSourceType
ProviderMeta *tfsdk.Config
}

// ReadDataSourceResponse is the framework server response for the
Expand All @@ -40,10 +41,14 @@ func (s *Server) ReadDataSource(ctx context.Context, req *ReadDataSourceRequest,
return
}

readReq := tfsdk.ReadDataSourceRequest{}
readReq := tfsdk.ReadDataSourceRequest{
Config: tfsdk.Config{
Schema: req.DataSourceSchema,
},
}
readResp := tfsdk.ReadDataSourceResponse{
State: tfsdk.State{
Schema: req.Config.Schema,
Schema: req.DataSourceSchema,
},
}

Expand Down
198 changes: 187 additions & 11 deletions internal/fwserver/server_readdatasource_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -5,32 +5,208 @@ import (
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-framework/diag"
"github.com/hashicorp/terraform-plugin-framework/internal/fwserver"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/emptyprovider"
"github.com/hashicorp/terraform-plugin-framework/internal/testing/testprovider"
"github.com/hashicorp/terraform-plugin-framework/tfsdk"
"github.com/hashicorp/terraform-plugin-framework/types"
"github.com/hashicorp/terraform-plugin-go/tftypes"
)

// TODO: Migrate tfsdk.Provider bits of proto6server.testProviderServer to
// new internal/testing/provider.Provider that allows customization of all
// method implementations via struct fields. Then, create additional test
// cases in this unit test.
//
// For now this testing is covered by proto6server.ReadDataSource.
//
// Reference: https://github.com/hashicorp/terraform-plugin-framework/issues/215
func TestServerReadDataSource(t *testing.T) {
t.Parallel()

testType := tftypes.Object{
AttributeTypes: map[string]tftypes.Type{
"test_computed": tftypes.String,
"test_required": tftypes.String,
},
}

testConfigValue := tftypes.NewValue(testType, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, nil),
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
})

testStateValue := tftypes.NewValue(testType, map[string]tftypes.Value{
"test_computed": tftypes.NewValue(tftypes.String, "test-state-value"),
"test_required": tftypes.NewValue(tftypes.String, "test-config-value"),
})

testSchema := tfsdk.Schema{
Attributes: map[string]tfsdk.Attribute{
"test_computed": {
Computed: true,
Type: types.StringType,
},
"test_required": {
Required: true,
Type: types.StringType,
},
},
}

testConfig := &tfsdk.Config{
Raw: testConfigValue,
Schema: testSchema,
}

testStateUnchanged := &tfsdk.State{
Raw: testConfigValue,
Schema: testSchema,
}

testState := &tfsdk.State{
Raw: testStateValue,
Schema: testSchema,
}

testCases := map[string]struct {
server *fwserver.Server
request *fwserver.ReadDataSourceRequest
expectedResponse *fwserver.ReadDataSourceResponse
}{
"empty-provider": {
"nil": {
server: &fwserver.Server{
Provider: &emptyprovider.Provider{},
Provider: &testprovider.Provider{},
},
expectedResponse: &fwserver.ReadDataSourceResponse{},
},
"request-config": {
server: &fwserver.Server{
Provider: &testprovider.Provider{},
},
request: &fwserver.ReadDataSourceRequest{
Config: testConfig,
DataSourceSchema: testSchema,
DataSourceType: &testprovider.DataSourceType{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testSchema, nil
},
NewDataSourceMethod: func(_ context.Context, _ tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
return &testprovider.DataSource{
ReadMethod: func(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
var config struct {
TestComputed types.String `tfsdk:"test_computed"`
TestRequired types.String `tfsdk:"test_required"`
}

resp.Diagnostics.Append(req.Config.Get(ctx, &config)...)

if config.TestRequired.Value != "test-config-value" {
resp.Diagnostics.AddError("unexpected req.Config value: %s", config.TestRequired.Value)
}
},
}, nil
},
},
},
expectedResponse: &fwserver.ReadDataSourceResponse{
State: testStateUnchanged,
},
},
"request-providermeta": {
server: &fwserver.Server{
Provider: &testprovider.Provider{},
},
request: &fwserver.ReadDataSourceRequest{
Config: testConfig,
DataSourceSchema: testSchema,
DataSourceType: &testprovider.DataSourceType{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testSchema, nil
},
NewDataSourceMethod: func(_ context.Context, _ tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
return &testprovider.DataSource{
ReadMethod: func(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
var config struct {
TestComputed types.String `tfsdk:"test_computed"`
TestRequired types.String `tfsdk:"test_required"`
}

resp.Diagnostics.Append(req.ProviderMeta.Get(ctx, &config)...)

if config.TestRequired.Value != "test-config-value" {
resp.Diagnostics.AddError("unexpected req.ProviderMeta value: %s", config.TestRequired.Value)
}
},
}, nil
},
},
ProviderMeta: testConfig,
},
expectedResponse: &fwserver.ReadDataSourceResponse{
State: testStateUnchanged,
},
},
"response-diagnostics": {
server: &fwserver.Server{
Provider: &testprovider.Provider{},
},
request: &fwserver.ReadDataSourceRequest{
Config: testConfig,
DataSourceSchema: testSchema,
DataSourceType: &testprovider.DataSourceType{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testSchema, nil
},
NewDataSourceMethod: func(_ context.Context, _ tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
return &testprovider.DataSource{
ReadMethod: func(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
resp.Diagnostics.AddWarning("warning summary", "warning detail")
resp.Diagnostics.AddError("error summary", "error detail")
},
}, nil
},
},
},
expectedResponse: &fwserver.ReadDataSourceResponse{
Diagnostics: diag.Diagnostics{
diag.NewWarningDiagnostic(
"warning summary",
"warning detail",
),
diag.NewErrorDiagnostic(
"error summary",
"error detail",
),
},
State: testStateUnchanged,
},
},
"response-state": {
server: &fwserver.Server{
Provider: &testprovider.Provider{},
},
request: &fwserver.ReadDataSourceRequest{
Config: testConfig,
DataSourceSchema: testSchema,
DataSourceType: &testprovider.DataSourceType{
GetSchemaMethod: func(_ context.Context) (tfsdk.Schema, diag.Diagnostics) {
return testSchema, nil
},
NewDataSourceMethod: func(_ context.Context, _ tfsdk.Provider) (tfsdk.DataSource, diag.Diagnostics) {
return &testprovider.DataSource{
ReadMethod: func(ctx context.Context, req tfsdk.ReadDataSourceRequest, resp *tfsdk.ReadDataSourceResponse) {
var data struct {
TestComputed types.String `tfsdk:"test_computed"`
TestRequired types.String `tfsdk:"test_required"`
}

resp.Diagnostics.Append(req.Config.Get(ctx, &data)...)

data.TestComputed = types.String{Value: "test-state-value"}

resp.Diagnostics.Append(resp.State.Set(ctx, &data)...)
},
}, nil
},
},
},
expectedResponse: &fwserver.ReadDataSourceResponse{
State: testState,
},
},
}

for name, testCase := range testCases {
Expand Down
Loading