Plugins: Wrap original check health error (#69944)

Fixes #69765
This commit is contained in:
Kousik Mitra 2023-06-16 14:48:53 +05:30 committed by GitHub
parent 0316350d16
commit 62ee1fa05a
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
2 changed files with 67 additions and 1 deletions

View File

@ -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

View File

@ -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{
@ -321,6 +379,7 @@ func TestCallResource(t *testing.T) {
type fakePluginBackend struct {
qdr backend.QueryDataHandlerFunc
crr backend.CallResourceHandlerFunc
chr backend.CheckHealthHandlerFunc
backendplugin.Plugin
}
@ -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
}