Skip to content

Commit

Permalink
tf5muxserver+tf6muxserver: Ensure unit testing of ConfigureProvider a…
Browse files Browse the repository at this point in the history
…nd StopProvider response diagnostics/errors (#179)

Reference: #178
  • Loading branch information
bflad authored Jul 14, 2023
1 parent 3c22b0a commit b3f16da
Show file tree
Hide file tree
Showing 6 changed files with 152 additions and 24 deletions.
18 changes: 11 additions & 7 deletions internal/tf5testserver/tf5testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var _ tfprotov5.ProviderServer = &TestServer{}
type TestServer struct {
ApplyResourceChangeCalled map[string]bool

ConfigureProviderCalled bool
ConfigureProviderCalled bool
ConfigureProviderResponse *tfprotov5.ConfigureProviderResponse

GetProviderSchemaCalled bool
GetProviderSchemaResponse *tfprotov5.GetProviderSchemaResponse
Expand All @@ -30,8 +31,8 @@ type TestServer struct {

ReadResourceCalled map[string]bool

StopProviderCalled bool
StopProviderError string
StopProviderCalled bool
StopProviderResponse *tfprotov5.StopProviderResponse

UpgradeResourceStateCalled map[string]bool

Expand All @@ -55,6 +56,11 @@ func (s *TestServer) ApplyResourceChange(_ context.Context, req *tfprotov5.Apply

func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov5.ConfigureProviderRequest) (*tfprotov5.ConfigureProviderResponse, error) {
s.ConfigureProviderCalled = true

if s.ConfigureProviderResponse != nil {
return s.ConfigureProviderResponse, nil
}

return &tfprotov5.ConfigureProviderResponse{}, nil
}

Expand Down Expand Up @@ -102,10 +108,8 @@ func (s *TestServer) ReadResource(_ context.Context, req *tfprotov5.ReadResource
func (s *TestServer) StopProvider(_ context.Context, _ *tfprotov5.StopProviderRequest) (*tfprotov5.StopProviderResponse, error) {
s.StopProviderCalled = true

if s.StopProviderError != "" {
return &tfprotov5.StopProviderResponse{
Error: s.StopProviderError,
}, nil
if s.StopProviderResponse != nil {
return s.StopProviderResponse, nil
}

return &tfprotov5.StopProviderResponse{}, nil
Expand Down
18 changes: 11 additions & 7 deletions internal/tf6testserver/tf6testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,8 @@ var _ tfprotov6.ProviderServer = &TestServer{}
type TestServer struct {
ApplyResourceChangeCalled map[string]bool

ConfigureProviderCalled bool
ConfigureProviderCalled bool
ConfigureProviderResponse *tfprotov6.ConfigureProviderResponse

GetProviderSchemaCalled bool
GetProviderSchemaResponse *tfprotov6.GetProviderSchemaResponse
Expand All @@ -27,8 +28,8 @@ type TestServer struct {

ReadResourceCalled map[string]bool

StopProviderCalled bool
StopProviderError string
StopProviderCalled bool
StopProviderResponse *tfprotov6.StopProviderResponse

UpgradeResourceStateCalled map[string]bool

Expand All @@ -55,6 +56,11 @@ func (s *TestServer) ApplyResourceChange(_ context.Context, req *tfprotov6.Apply

func (s *TestServer) ConfigureProvider(_ context.Context, _ *tfprotov6.ConfigureProviderRequest) (*tfprotov6.ConfigureProviderResponse, error) {
s.ConfigureProviderCalled = true

if s.ConfigureProviderResponse != nil {
return s.ConfigureProviderResponse, nil
}

return &tfprotov6.ConfigureProviderResponse{}, nil
}

Expand Down Expand Up @@ -102,10 +108,8 @@ func (s *TestServer) ReadResource(_ context.Context, req *tfprotov6.ReadResource
func (s *TestServer) StopProvider(_ context.Context, _ *tfprotov6.StopProviderRequest) (*tfprotov6.StopProviderResponse, error) {
s.StopProviderCalled = true

if s.StopProviderError != "" {
return &tfprotov6.StopProviderResponse{
Error: s.StopProviderError,
}, nil
if s.StopProviderResponse != nil {
return s.StopProviderResponse, nil
}

return &tfprotov6.StopProviderResponse{}, nil
Expand Down
51 changes: 49 additions & 2 deletions tf5muxserver/mux_server_ConfigureProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"testing"

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

"github.com/hashicorp/terraform-plugin-mux/internal/tf5testserver"
Expand All @@ -23,15 +24,42 @@ func TestMuxServerConfigureProvider(t *testing.T) {
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
ConfigureProviderResponse: &tfprotov5.ConfigureProviderResponse{
Diagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityWarning,
Summary: "warning summary",
Detail: "warning detail",
},
},
},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
ConfigureProviderResponse: &tfprotov5.ConfigureProviderResponse{
Diagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "error summary",
Detail: "error detail",
},
},
},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
ConfigureProviderResponse: &tfprotov5.ConfigureProviderResponse{
Diagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "unexpected error summary",
Detail: "unexpected error detail",
},
},
},
},
}

Expand All @@ -56,14 +84,33 @@ func TestMuxServerConfigureProvider(t *testing.T) {
t.Fatalf("unexpected error calling GetProviderSchema: %s", err)
}

_, err = muxServer.ProviderServer().ConfigureProvider(ctx, &tfprotov5.ConfigureProviderRequest{})
resp, err := muxServer.ProviderServer().ConfigureProvider(ctx, &tfprotov5.ConfigureProviderRequest{})

if err != nil {
t.Fatalf("error calling ConfigureProvider: %s", err)
}

expectedResp := &tfprotov5.ConfigureProviderResponse{
Diagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityWarning,
Summary: "warning summary",
Detail: "warning detail",
},
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "error summary",
Detail: "error detail",
},
},
}

if diff := cmp.Diff(resp, expectedResp); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}

for num, testServer := range testServers {
if !testServer.ConfigureProviderCalled {
if num < 4 && !testServer.ConfigureProviderCalled {
t.Errorf("configure not called on server%d", num+1)
}
}
Expand Down
19 changes: 16 additions & 3 deletions tf5muxserver/mux_server_StopProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"testing"

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

"github.com/hashicorp/terraform-plugin-mux/internal/tf5testserver"
Expand All @@ -23,14 +24,18 @@ func TestMuxServerStopProvider(t *testing.T) {
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
StopProviderError: "error in server2",
StopProviderResponse: &tfprotov5.StopProviderResponse{
Error: "error in server2",
},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
StopProviderError: "error in server4",
StopProviderResponse: &tfprotov5.StopProviderResponse{
Error: "error in server4",
},
},
{
GetProviderSchemaResponse: &tfprotov5.GetProviderSchemaResponse{},
Expand Down Expand Up @@ -58,12 +63,20 @@ func TestMuxServerStopProvider(t *testing.T) {
t.Fatalf("unexpected error calling GetProviderSchema: %s", err)
}

_, err = muxServer.ProviderServer().StopProvider(ctx, &tfprotov5.StopProviderRequest{})
resp, err := muxServer.ProviderServer().StopProvider(ctx, &tfprotov5.StopProviderRequest{})

if err != nil {
t.Fatalf("error calling StopProvider: %s", err)
}

expectedResp := &tfprotov5.StopProviderResponse{
Error: "error in server2\nerror in server4",
}

if diff := cmp.Diff(resp, expectedResp); diff != "" {
t.Errorf("unexpected response Error difference: %s", diff)
}

for num, testServer := range testServers {
if !testServer.StopProviderCalled {
t.Errorf("StopProvider not called on server%d", num+1)
Expand Down
51 changes: 49 additions & 2 deletions tf6muxserver/mux_server_ConfigureProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"

"github.com/hashicorp/terraform-plugin-mux/internal/tf6testserver"
Expand All @@ -23,15 +24,42 @@ func TestMuxServerConfigureProvider(t *testing.T) {
},
{
GetProviderSchemaResponse: &tfprotov6.GetProviderSchemaResponse{},
ConfigureProviderResponse: &tfprotov6.ConfigureProviderResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "warning summary",
Detail: "warning detail",
},
},
},
},
{
GetProviderSchemaResponse: &tfprotov6.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov6.GetProviderSchemaResponse{},
ConfigureProviderResponse: &tfprotov6.ConfigureProviderResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "error summary",
Detail: "error detail",
},
},
},
},
{
GetProviderSchemaResponse: &tfprotov6.GetProviderSchemaResponse{},
ConfigureProviderResponse: &tfprotov6.ConfigureProviderResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "unexpected error summary",
Detail: "unexpected error detail",
},
},
},
},
}

Expand All @@ -56,14 +84,33 @@ func TestMuxServerConfigureProvider(t *testing.T) {
t.Fatalf("unexpected error calling GetProviderSchema: %s", err)
}

_, err = muxServer.ProviderServer().ConfigureProvider(ctx, &tfprotov6.ConfigureProviderRequest{})
resp, err := muxServer.ProviderServer().ConfigureProvider(ctx, &tfprotov6.ConfigureProviderRequest{})

if err != nil {
t.Fatalf("error calling ConfigureProvider: %s", err)
}

expectedResp := &tfprotov6.ConfigureProviderResponse{
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityWarning,
Summary: "warning summary",
Detail: "warning detail",
},
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "error summary",
Detail: "error detail",
},
},
}

if diff := cmp.Diff(resp, expectedResp); diff != "" {
t.Errorf("unexpected difference: %s", diff)
}

for num, testServer := range testServers {
if !testServer.ConfigureProviderCalled {
if num < 4 && !testServer.ConfigureProviderCalled {
t.Errorf("configure not called on server%d", num+1)
}
}
Expand Down
19 changes: 16 additions & 3 deletions tf6muxserver/mux_server_StopProvider_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"context"
"testing"

"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"

"github.com/hashicorp/terraform-plugin-mux/internal/tf6testserver"
Expand All @@ -22,14 +23,18 @@ func TestMuxServerStopProvider(t *testing.T) {
GetProviderSchemaResponse: &tfprotov6.GetProviderSchemaResponse{},
}, {
GetProviderSchemaResponse: &tfprotov6.GetProviderSchemaResponse{},
StopProviderError: "error in server2",
StopProviderResponse: &tfprotov6.StopProviderResponse{
Error: "error in server2",
},
},
{
GetProviderSchemaResponse: &tfprotov6.GetProviderSchemaResponse{},
},
{
GetProviderSchemaResponse: &tfprotov6.GetProviderSchemaResponse{},
StopProviderError: "error in server4",
StopProviderResponse: &tfprotov6.StopProviderResponse{
Error: "error in server4",
},
},
{
GetProviderSchemaResponse: &tfprotov6.GetProviderSchemaResponse{},
Expand Down Expand Up @@ -57,12 +62,20 @@ func TestMuxServerStopProvider(t *testing.T) {
t.Fatalf("unexpected error calling GetProviderSchema: %s", err)
}

_, err = muxServer.ProviderServer().StopProvider(ctx, &tfprotov6.StopProviderRequest{})
resp, err := muxServer.ProviderServer().StopProvider(ctx, &tfprotov6.StopProviderRequest{})

if err != nil {
t.Fatalf("error calling StopProvider: %s", err)
}

expectedResp := &tfprotov6.StopProviderResponse{
Error: "error in server2\nerror in server4",
}

if diff := cmp.Diff(resp, expectedResp); diff != "" {
t.Errorf("unexpected response Error difference: %s", diff)
}

for num, testServer := range testServers {
if !testServer.StopProviderCalled {
t.Errorf("StopProvider not called on server%d", num+1)
Expand Down

0 comments on commit b3f16da

Please sign in to comment.