mirror of https://github.com/grafana/grafana
Chore: Replace core plugins as external warning (#81877)
parent
114e9e90f3
commit
26bc87b60e
@ -0,0 +1,43 @@ |
|||||||
|
package pluginexternal |
||||||
|
|
||||||
|
import ( |
||||||
|
"context" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/plugins/log" |
||||||
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" |
||||||
|
"github.com/grafana/grafana/pkg/setting" |
||||||
|
) |
||||||
|
|
||||||
|
type Service struct { |
||||||
|
cfg *setting.Cfg |
||||||
|
logger log.Logger |
||||||
|
pluginStore pluginstore.Store |
||||||
|
} |
||||||
|
|
||||||
|
func ProvideService( |
||||||
|
cfg *setting.Cfg, pluginStore pluginstore.Store, |
||||||
|
) (*Service, error) { |
||||||
|
logger := log.New("datasources") |
||||||
|
s := &Service{ |
||||||
|
cfg: cfg, |
||||||
|
logger: logger, |
||||||
|
pluginStore: pluginStore, |
||||||
|
} |
||||||
|
return s, nil |
||||||
|
} |
||||||
|
|
||||||
|
func (s *Service) Run(ctx context.Context) error { |
||||||
|
s.validateExternal() |
||||||
|
return ctx.Err() |
||||||
|
} |
||||||
|
|
||||||
|
func (s *Service) validateExternal() { |
||||||
|
for pluginID, pluginCfg := range s.cfg.PluginSettings { |
||||||
|
if pluginCfg["as_external"] == "true" { |
||||||
|
_, exists := s.pluginStore.Plugin(context.Background(), pluginID) |
||||||
|
if !exists { |
||||||
|
s.logger.Error("Core plugin expected to be loaded as external, but it is missing", "pluginID", pluginID) |
||||||
|
} |
||||||
|
} |
||||||
|
} |
||||||
|
} |
@ -0,0 +1,53 @@ |
|||||||
|
package pluginexternal |
||||||
|
|
||||||
|
import ( |
||||||
|
"testing" |
||||||
|
|
||||||
|
"github.com/grafana/grafana/pkg/plugins" |
||||||
|
"github.com/grafana/grafana/pkg/plugins/log" |
||||||
|
"github.com/grafana/grafana/pkg/services/pluginsintegration/pluginstore" |
||||||
|
"github.com/grafana/grafana/pkg/setting" |
||||||
|
"github.com/stretchr/testify/require" |
||||||
|
) |
||||||
|
|
||||||
|
func TestService_validateExternal(t *testing.T) { |
||||||
|
cfg := setting.NewCfg() |
||||||
|
cfg.PluginSettings = setting.PluginSettings{ |
||||||
|
"grafana-testdata-datasource": map[string]string{ |
||||||
|
"as_external": "true", |
||||||
|
}, |
||||||
|
} |
||||||
|
|
||||||
|
t.Run("should not log error if core plugin is loaded as external", func(t *testing.T) { |
||||||
|
l := log.NewTestLogger() |
||||||
|
s := &Service{ |
||||||
|
cfg: cfg, |
||||||
|
logger: l, |
||||||
|
pluginStore: &pluginstore.FakePluginStore{ |
||||||
|
PluginList: []pluginstore.Plugin{ |
||||||
|
{ |
||||||
|
JSONData: plugins.JSONData{ |
||||||
|
ID: "grafana-testdata-datasource", |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
}, |
||||||
|
} |
||||||
|
s.validateExternal() |
||||||
|
require.Equal(t, l.ErrorLogs.Calls, 0) |
||||||
|
}) |
||||||
|
|
||||||
|
t.Run("should log error if a core plugin is missing", func(t *testing.T) { |
||||||
|
l := log.NewTestLogger() |
||||||
|
s := &Service{ |
||||||
|
cfg: cfg, |
||||||
|
logger: l, |
||||||
|
pluginStore: &pluginstore.FakePluginStore{ |
||||||
|
PluginList: []pluginstore.Plugin{}, |
||||||
|
}, |
||||||
|
} |
||||||
|
s.validateExternal() |
||||||
|
require.Equal(t, l.ErrorLogs.Calls, 1) |
||||||
|
require.Contains(t, l.ErrorLogs.Message, "Core plugin expected to be loaded as external") |
||||||
|
}) |
||||||
|
} |
Loading…
Reference in new issue