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 2 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-20230713-155423.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'tf5muxserver: Include diagnostics returned from `server.GetProviderSchema()`
in `muxServer.GetProviderSchemaResponse`'
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Nit: I think we can tailor this message to developers without talking about the internal details, e.g.

tf5muxserver: Ensure GetProviderSchema RPC diagnostics are properly returned to Terraform

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Makes sense, have updated.

time: 2023-07-13T15:54:23.555736+01:00
custom:
Issue: "176"
6 changes: 6 additions & 0 deletions .changes/unreleased/BUG FIXES-20230713-155511.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
kind: BUG FIXES
body: 'tf6muxserver: Include diagnostics returned from `server.GetProviderSchema()`
in `muxServer.GetProviderSchemaResponse`'
time: 2023-07-13T15:55:11.144049+01:00
custom:
Issue: "176"
17 changes: 17 additions & 0 deletions internal/tf5testserver/tf5testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,20 @@ func (s *TestServer) PrepareProviderConfig(_ context.Context, req *tfprotov5.Pre
s.PrepareProviderConfigCalled = true
return s.PrepareProviderConfigResponse, nil
}

type TestServerDiags struct {
*TestServer
Diagnostics []*tfprotov5.Diagnostic
}

func (s *TestServerDiags) GetProviderSchema(ctx context.Context, req *tfprotov5.GetProviderSchemaRequest) (*tfprotov5.GetProviderSchemaResponse, error) {
resp, err := s.TestServer.GetProviderSchema(ctx, req)

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

return resp, err
}
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Instead of setting up a new test server type, how do you feel about updating the existing TestServer to store a full GetProviderSchemaResponse *tfprotov5.GetProviderSchemaResponse field similar to PrepareProviderConfigResponse *tfprotov5.PrepareProviderConfigResponse?

We can migrate all the other GetProviderSchema related fields to be under that as well, rather than individually calling them out:

DataSourceSchemas  map[string]*tfprotov5.Schema
	ProviderMetaSchema *tfprotov5.Schema
	ProviderSchema     *tfprotov5.Schema
	ResourceSchemas    map[string]*tfprotov5.Schema
	ServerCapabilities *tfprotov5.ServerCapabilities

I'm going to peek at the other RPC logic and create an issue if it makes sense to do something similar for the other RPCs.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Good call. Have refactored.


func (s *TestServerDiags) ProviderServer() tfprotov5.ProviderServer {
return s
}
17 changes: 17 additions & 0 deletions internal/tf6testserver/tf6testserver.go
Original file line number Diff line number Diff line change
Expand Up @@ -162,3 +162,20 @@ func (s *TestServer) ValidateProviderConfig(_ context.Context, req *tfprotov6.Va
s.ValidateProviderConfigCalled = true
return s.ValidateProviderConfigResponse, nil
}

type TestServerDiags struct {
*TestServer
Diagnostics []*tfprotov6.Diagnostic
}

func (s *TestServerDiags) GetProviderSchema(ctx context.Context, req *tfprotov6.GetProviderSchemaRequest) (*tfprotov6.GetProviderSchemaResponse, error) {
resp, err := s.TestServer.GetProviderSchema(ctx, req)

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

return resp, err
}

func (s *TestServerDiags) ProviderServer() tfprotov6.ProviderServer {
return s
}
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
24 changes: 24 additions & 0 deletions tf5muxserver/mux_server_GetProviderSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov5"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-mux/internal/tf5testserver"
"github.com/hashicorp/terraform-plugin-mux/tf5muxserver"
)
Expand Down Expand Up @@ -486,6 +487,29 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
PlanDestroy: true,
},
},
"get-provider-schema-diagnostics": {
servers: []func() tfprotov5.ProviderServer{
(&tf5testserver.TestServerDiags{
TestServer: &tf5testserver.TestServer{},
Diagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Summary",
Detail: "Detail",
},
},
}).ProviderServer,
},
expectedDiagnostics: []*tfprotov5.Diagnostic{
{
Severity: tfprotov5.DiagnosticSeverityError,
Summary: "Summary",
Detail: "Detail",
},
},
expectedDataSourceSchemas: map[string]*tfprotov5.Schema{},
expectedResourceSchemas: map[string]*tfprotov5.Schema{},
},
"provider-mismatch": {
servers: []func() tfprotov5.ProviderServer{
(&tf5testserver.TestServer{
Expand Down
3 changes: 3 additions & 0 deletions tf6muxserver/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/tfprotov6"

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

Expand Down Expand Up @@ -42,6 +43,8 @@ func (s *muxServer) GetProviderSchema(ctx context.Context, req *tfprotov6.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, &tfprotov6.Diagnostic{
Expand Down
24 changes: 24 additions & 0 deletions tf6muxserver/mux_server_GetProviderSchema_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/google/go-cmp/cmp"
"github.com/hashicorp/terraform-plugin-go/tfprotov6"
"github.com/hashicorp/terraform-plugin-go/tftypes"

"github.com/hashicorp/terraform-plugin-mux/internal/tf6testserver"
"github.com/hashicorp/terraform-plugin-mux/tf6muxserver"
)
Expand Down Expand Up @@ -486,6 +487,29 @@ func TestMuxServerGetProviderSchema(t *testing.T) {
PlanDestroy: true,
},
},
"get-provider-schema-diagnostics": {
servers: []func() tfprotov6.ProviderServer{
(&tf6testserver.TestServerDiags{
TestServer: &tf6testserver.TestServer{},
Diagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Summary",
Detail: "Detail",
},
},
}).ProviderServer,
},
expectedDiagnostics: []*tfprotov6.Diagnostic{
{
Severity: tfprotov6.DiagnosticSeverityError,
Summary: "Summary",
Detail: "Detail",
},
},
expectedDataSourceSchemas: map[string]*tfprotov6.Schema{},
expectedResourceSchemas: map[string]*tfprotov6.Schema{},
},
"provider-mismatch": {
servers: []func() tfprotov6.ProviderServer{
(&tf6testserver.TestServer{
Expand Down