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

Plugins: Wrap original check health error #70227

Merged
merged 1 commit into from
Jun 16, 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.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/plugins/manager/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -191,7 +191,7 @@ func (s *Service) CheckHealth(ctx context.Context, req *backend.CheckHealthReque
return nil, err
}

return nil, fmt.Errorf("%v: %w", "failed to check plugin health", backendplugin.ErrHealthCheckFailed)
return nil, fmt.Errorf("%w: %w", backendplugin.ErrHealthCheckFailed, err)
}

return resp, nil
Expand Down
66 changes: 66 additions & 0 deletions pkg/plugins/manager/client/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,64 @@ func TestQueryData(t *testing.T) {
})
}

func TestCheckHealth(t *testing.T) {
t.Run("empty plugin registry should return plugin not registered error", func(t *testing.T) {
registry := fakes.NewFakePluginRegistry()
client := ProvideService(registry, &config.Cfg{})
_, err := client.CheckHealth(context.Background(), &backend.CheckHealthRequest{})
require.Error(t, err)
require.ErrorIs(t, err, backendplugin.ErrPluginNotRegistered)
})

t.Run("non-empty plugin registry", func(t *testing.T) {
tcs := []struct {
err error
expectedError error
}{
{
err: backendplugin.ErrPluginUnavailable,
expectedError: backendplugin.ErrPluginUnavailable,
},
{

err: backendplugin.ErrMethodNotImplemented,
expectedError: backendplugin.ErrMethodNotImplemented,
},
{
err: errors.New("surprise surprise"),
expectedError: backendplugin.ErrHealthCheckFailed,
},
}

for _, tc := range tcs {
t.Run(fmt.Sprintf("Plugin client error %q should return expected error", tc.err), func(t *testing.T) {
registry := fakes.NewFakePluginRegistry()
p := &plugins.Plugin{
JSONData: plugins.JSONData{
ID: "grafana",
},
}
p.RegisterClient(&fakePluginBackend{
chr: func(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
return nil, tc.err
},
})
err := registry.Add(context.Background(), p)
require.NoError(t, err)

client := ProvideService(registry, &config.Cfg{})
_, err = client.CheckHealth(context.Background(), &backend.CheckHealthRequest{
PluginContext: backend.PluginContext{
PluginID: "grafana",
},
})
require.Error(t, err)
require.ErrorIs(t, err, tc.expectedError)
})
}
})
}

func TestCallResource(t *testing.T) {
registry := fakes.NewFakePluginRegistry()
p := &plugins.Plugin{
Expand Down Expand Up @@ -321,6 +379,7 @@ func TestCallResource(t *testing.T) {
type fakePluginBackend struct {
qdr backend.QueryDataHandlerFunc
crr backend.CallResourceHandlerFunc
chr backend.CheckHealthHandlerFunc

backendplugin.Plugin
}
Expand All @@ -343,3 +402,10 @@ func (f *fakePluginBackend) CallResource(ctx context.Context, req *backend.CallR
func (f *fakePluginBackend) IsDecommissioned() bool {
return false
}

func (f *fakePluginBackend) CheckHealth(ctx context.Context, req *backend.CheckHealthRequest) (*backend.CheckHealthResult, error) {
if f.chr != nil {
return f.chr(ctx, req)
}
return &backend.CheckHealthResult{}, nil
}