Skip to content

Commit

Permalink
Plugins: Wrap original check health error (#69944)
Browse files Browse the repository at this point in the history
Fixes #69765
  • Loading branch information
kousikmitra committed Jun 16, 2023
1 parent 0316350 commit 62ee1fa
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 1 deletion.
2 changes: 1 addition & 1 deletion pkg/plugins/manager/client/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,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
}

0 comments on commit 62ee1fa

Please sign in to comment.