Skip to content
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

Ensure GetProviderSchema Response Error/Diagnostics are Unit Tested #177

Merged
merged 3 commits into from
Jul 14, 2023
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
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230714-094529.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'tf5muxserver: Ensure `GetProviderSchema` RPC diagnostics are properly returned
to Terraform'
time: 2023-07-14T09:45:29.631825+01:00
custom:
Issue: "176"
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230714-094547.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'tf6muxserver: Ensure `GetProviderSchema` RPC diagnostics are properly returned
to Terraform'
time: 2023-07-14T09:45:47.81872+01:00
custom:
Issue: "176"
26 changes: 3 additions & 23 deletions internal/tf5testserver/tf5testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ import (
var _ tfprotov5.ProviderServer = &TestServer{}

type TestServer struct {
DataSourceSchemas map[string]*tfprotov5.Schema
ProviderMetaSchema *tfprotov5.Schema
ProviderSchema *tfprotov5.Schema
ResourceSchemas map[string]*tfprotov5.Schema
ServerCapabilities *tfprotov5.ServerCapabilities

ApplyResourceChangeCalled map[string]bool

ConfigureProviderCalled bool

GetProviderSchemaCalled bool
GetProviderSchemaCalled bool
GetProviderSchemaResponse *tfprotov5.GetProviderSchemaResponse

ImportResourceStateCalled map[string]bool

Expand Down Expand Up @@ -65,22 +60,7 @@ func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov5.Configure

func (s *TestServer) GetProviderSchema(_ context.Context, _ *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
s.GetProviderSchemaCalled = true

if s.DataSourceSchemas == nil {
s.DataSourceSchemas = make(map[string]*tfprotov5.Schema)
}

if s.ResourceSchemas == nil {
s.ResourceSchemas = make(map[string]*tfprotov5.Schema)
}

return &tfprotov5.GetProviderSchemaResponse{
Provider: s.ProviderSchema,
ProviderMeta: s.ProviderMetaSchema,
ResourceSchemas: s.ResourceSchemas,
DataSourceSchemas: s.DataSourceSchemas,
ServerCapabilities: s.ServerCapabilities,
}, nil
return s.GetProviderSchemaResponse, nil
}

func (s *TestServer) ImportResourceState(_ context.Context, req *tfprotov5.ImportResourceStateRequest) (*tfprotov5.ImportResourceStateResponse, error) {
Expand Down
26 changes: 3 additions & 23 deletions internal/tf6testserver/tf6testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,17 +12,12 @@ import (
var _ tfprotov6.ProviderServer = &TestServer{}

type TestServer struct {
DataSourceSchemas map[string]*tfprotov6.Schema
ProviderMetaSchema *tfprotov6.Schema
ProviderSchema *tfprotov6.Schema
ResourceSchemas map[string]*tfprotov6.Schema
ServerCapabilities *tfprotov6.ServerCapabilities

ApplyResourceChangeCalled map[string]bool

ConfigureProviderCalled bool

GetProviderSchemaCalled bool
GetProviderSchemaCalled bool
GetProviderSchemaResponse *tfprotov6.GetProviderSchemaResponse

ImportResourceStateCalled map[string]bool

Expand Down Expand Up @@ -65,22 +60,7 @@ func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov6.Configure

func (s *TestServer) GetProviderSchema(_ context.Context, _ *tfprotov6.GetProviderSchemaRequest) (*tfprotov6.GetProviderSchemaResponse, error) {
s.GetProviderSchemaCalled = true

if s.DataSourceSchemas == nil {
s.DataSourceSchemas = make(map[string]*tfprotov6.Schema)
}

if s.ResourceSchemas == nil {
s.ResourceSchemas = make(map[string]*tfprotov6.Schema)
}

return &tfprotov6.GetProviderSchemaResponse{
Provider: s.ProviderSchema,
ProviderMeta: s.ProviderMetaSchema,
ResourceSchemas: s.ResourceSchemas,
DataSourceSchemas: s.DataSourceSchemas,
ServerCapabilities: s.ServerCapabilities,
}, nil
return s.GetProviderSchemaResponse, nil
}

func (s *TestServer) ImportResourceState(_ context.Context, req *tfprotov6.ImportResourceStateRequest) (*tfprotov6.ImportResourceStateResponse, error) {
Expand Down
13 changes: 9 additions & 4 deletions tf5muxserver/mux_server_ApplyResourceChange_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"

"github.com/hashicorp/terraform-plugin-mux/internal/tf5testserver"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
)
Expand All @@ -17,13 +18,17 @@ func TestMuxServerApplyResourceChange(t *testing.T) {

ctx := context.Background()
testServer1 := &tf5testserver.TestServer{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_resource_server1": {},
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_resource_server1": {},
},
},
}
testServer2 := &tf5testserver.TestServer{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_resource_server2": {},
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{
ResourceSchemas: map[string]*tfprotov5.Schema{
"test_resource_server2": {},
},
},
}

Expand Down
19 changes: 18 additions & 1 deletion tf5muxserver/mux_server_ConfigureProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"testing"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"

"github.com/hashicorp/terraform-plugin-mux/internal/tf5testserver"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
)
Expand All @@ -16,7 +17,23 @@ func TestMuxServerConfigureProvider(t *testing.T) {
t.Parallel()

ctx := context.Background()
testServers := [5]*tf5testserver.TestServer{{}, {}, {}, {}, {}}
testServers := [5]*tf5testserver.TestServer{
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
}

servers := []func() tfprotov5.ProviderServer{
testServers[0].ProviderServer,
Expand Down
3 changes: 3 additions & 0 deletions tf5muxserver/mux_server_GetProviderSchema.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@ import (
"fmt"

"github.com/hashicorp/terraform-plugin-go/tfprotov5"

"github.com/hashicorp/terraform-plugin-mux/internal/logging"
)

Expand Down Expand Up @@ -42,6 +43,8 @@ func (s *muxServer) GetProviderSchema(ctx context.Context, req *tfprotov5.GetPro
return resp, fmt.Errorf("error calling GetProviderSchema for %T: %w", server, err)
}

resp.Diagnostics = append(resp.Diagnostics, serverResp.Diagnostics...)

if serverResp.Provider != nil {
if resp.Provider != nil && !schemaEquals(serverResp.Provider, resp.Provider) {
resp.Diagnostics = append(resp.Diagnostics, &tfprotov5.Diagnostic{
Expand Down
Loading
Loading