From 9f18e0ccf3531af837e10581d38fc5ea711af362 Mon Sep 17 00:00:00 2001 From: Will Browne Date: Fri, 9 Jun 2023 12:08:26 +0200 Subject: [PATCH] Plugins: Re-use plugin registry mocks from fakes package (#69840) re-use mocks from fakes package --- pkg/plugins/manager/process/process_test.go | 63 ++++------- pkg/plugins/manager/store/store_test.go | 111 ++++++++------------ 2 files changed, 62 insertions(+), 112 deletions(-) diff --git a/pkg/plugins/manager/process/process_test.go b/pkg/plugins/manager/process/process_test.go index 275b743bd0c..1a1d6849a86 100644 --- a/pkg/plugins/manager/process/process_test.go +++ b/pkg/plugins/manager/process/process_test.go @@ -10,11 +10,12 @@ import ( "github.com/grafana/grafana/pkg/plugins" "github.com/grafana/grafana/pkg/plugins/backendplugin" "github.com/grafana/grafana/pkg/plugins/log" + "github.com/grafana/grafana/pkg/plugins/manager/fakes" ) func TestProcessManager_Start(t *testing.T) { t.Run("Plugin not found in registry", func(t *testing.T) { - m := NewManager(newFakePluginRegistry(map[string]*plugins.Plugin{})) + m := NewManager(fakes.NewFakePluginRegistry()) err := m.Start(context.Background(), "non-existing-datasource") require.ErrorIs(t, err, backendplugin.ErrPluginNotRegistered) }) @@ -63,9 +64,11 @@ func TestProcessManager_Start(t *testing.T) { plugin.SignatureError = tc.signatureError }) - m := NewManager(newFakePluginRegistry(map[string]*plugins.Plugin{ - p.ID: p, - })) + m := NewManager(&fakes.FakePluginRegistry{ + Store: map[string]*plugins.Plugin{ + p.ID: p, + }}, + ) err := m.Start(context.Background(), p.ID) require.NoError(t, err) @@ -83,7 +86,7 @@ func TestProcessManager_Start(t *testing.T) { func TestProcessManager_Stop(t *testing.T) { t.Run("Plugin not found in registry", func(t *testing.T) { - m := NewManager(newFakePluginRegistry(map[string]*plugins.Plugin{})) + m := NewManager(fakes.NewFakePluginRegistry()) err := m.Stop(context.Background(), "non-existing-datasource") require.ErrorIs(t, err, backendplugin.ErrPluginNotRegistered) }) @@ -97,9 +100,11 @@ func TestProcessManager_Stop(t *testing.T) { plugin.Backend = true }) - m := NewManager(newFakePluginRegistry(map[string]*plugins.Plugin{ - pluginID: p, - })) + m := NewManager(&fakes.FakePluginRegistry{ + Store: map[string]*plugins.Plugin{ + pluginID: p, + }}, + ) err := m.Stop(context.Background(), pluginID) require.NoError(t, err) @@ -116,9 +121,11 @@ func TestProcessManager_ManagedBackendPluginLifecycle(t *testing.T) { plugin.Backend = true }) - m := NewManager(newFakePluginRegistry(map[string]*plugins.Plugin{ - p.ID: p, - })) + m := NewManager(&fakes.FakePluginRegistry{ + Store: map[string]*plugins.Plugin{ + p.ID: p, + }}, + ) err := m.Start(context.Background(), p.ID) require.NoError(t, err) @@ -162,40 +169,6 @@ func TestProcessManager_ManagedBackendPluginLifecycle(t *testing.T) { }) } -type fakePluginRegistry struct { - store map[string]*plugins.Plugin -} - -func newFakePluginRegistry(m map[string]*plugins.Plugin) *fakePluginRegistry { - return &fakePluginRegistry{ - store: m, - } -} - -func (f *fakePluginRegistry) Plugin(_ context.Context, id string) (*plugins.Plugin, bool) { - p, exists := f.store[id] - return p, exists -} - -func (f *fakePluginRegistry) Plugins(_ context.Context) []*plugins.Plugin { - var res []*plugins.Plugin - - for _, p := range f.store { - res = append(res, p) - } - return res -} - -func (f *fakePluginRegistry) Add(_ context.Context, p *plugins.Plugin) error { - f.store[p.ID] = p - return nil -} - -func (f *fakePluginRegistry) Remove(_ context.Context, id string) error { - delete(f.store, id) - return nil -} - type fakeBackendPlugin struct { managed bool diff --git a/pkg/plugins/manager/store/store_test.go b/pkg/plugins/manager/store/store_test.go index 6ff8bea4eaa..8772ca9824f 100644 --- a/pkg/plugins/manager/store/store_test.go +++ b/pkg/plugins/manager/store/store_test.go @@ -54,10 +54,12 @@ func TestStore_Plugin(t *testing.T) { p1.RegisterClient(&DecommissionedPlugin{}) p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-panel"}} - ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{ - p1.ID: p1, - p2.ID: p2, - })) + ps := New(&fakes.FakePluginRegistry{ + Store: map[string]*plugins.Plugin{ + p1.ID: p1, + p2.ID: p2, + }, + }) p, exists := ps.Plugin(context.Background(), p1.ID) require.False(t, exists) @@ -78,13 +80,15 @@ func TestStore_Plugins(t *testing.T) { p5 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "e-test-panel", Type: plugins.TypePanel}} p5.RegisterClient(&DecommissionedPlugin{}) - ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{ - p1.ID: p1, - p2.ID: p2, - p3.ID: p3, - p4.ID: p4, - p5.ID: p5, - })) + ps := New(&fakes.FakePluginRegistry{ + Store: map[string]*plugins.Plugin{ + p1.ID: p1, + p2.ID: p2, + p3.ID: p3, + p4.ID: p4, + p5.ID: p5, + }, + }) pss := ps.Plugins(context.Background()) require.Equal(t, pss, []plugins.PluginDTO{p1.ToDTO(), p2.ToDTO(), p3.ToDTO(), p4.ToDTO()}) @@ -113,14 +117,16 @@ func TestStore_Routes(t *testing.T) { p6 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "f-test-app", Type: plugins.TypeApp}} p6.RegisterClient(&DecommissionedPlugin{}) - ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{ - p1.ID: p1, - p2.ID: p2, - p3.ID: p3, - p4.ID: p4, - p5.ID: p5, - p6.ID: p6, - })) + ps := New(&fakes.FakePluginRegistry{ + Store: map[string]*plugins.Plugin{ + p1.ID: p1, + p2.ID: p2, + p3.ID: p3, + p4.ID: p4, + p5.ID: p5, + p6.ID: p6, + }, + }) sr := func(p *plugins.Plugin) *plugins.StaticRoute { return &plugins.StaticRoute{PluginID: p.ID, Directory: p.FS.Base()} @@ -137,11 +143,13 @@ func TestStore_Renderer(t *testing.T) { p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-panel", Type: plugins.TypePanel}} p3 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-app", Type: plugins.TypeApp}} - ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{ - p1.ID: p1, - p2.ID: p2, - p3.ID: p3, - })) + ps := New(&fakes.FakePluginRegistry{ + Store: map[string]*plugins.Plugin{ + p1.ID: p1, + p2.ID: p2, + p3.ID: p3, + }, + }) r := ps.Renderer(context.Background()) require.Equal(t, p1, r) @@ -155,12 +163,14 @@ func TestStore_SecretsManager(t *testing.T) { p3 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-secrets", Type: plugins.TypeSecretsManager}} p4 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource", Type: plugins.TypeDataSource}} - ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{ - p1.ID: p1, - p2.ID: p2, - p3.ID: p3, - p4.ID: p4, - })) + ps := New(&fakes.FakePluginRegistry{ + Store: map[string]*plugins.Plugin{ + p1.ID: p1, + p2.ID: p2, + p3.ID: p3, + p4.ID: p4, + }, + }) r := ps.SecretsManager(context.Background()) require.Equal(t, p3, r) @@ -173,12 +183,12 @@ func TestStore_availablePlugins(t *testing.T) { p1.RegisterClient(&DecommissionedPlugin{}) p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-app"}} - ps := New( - newFakePluginRegistry(map[string]*plugins.Plugin{ + ps := New(&fakes.FakePluginRegistry{ + Store: map[string]*plugins.Plugin{ p1.ID: p1, p2.ID: p2, - }), - ) + }, + }) aps := ps.availablePlugins(context.Background()) require.Len(t, aps, 1) @@ -197,36 +207,3 @@ func (p *DecommissionedPlugin) Decommission() error { func (p *DecommissionedPlugin) IsDecommissioned() bool { return true } - -type fakePluginRegistry struct { - store map[string]*plugins.Plugin -} - -func newFakePluginRegistry(m map[string]*plugins.Plugin) *fakePluginRegistry { - return &fakePluginRegistry{ - store: m, - } -} - -func (f *fakePluginRegistry) Plugin(_ context.Context, id string) (*plugins.Plugin, bool) { - p, exists := f.store[id] - return p, exists -} - -func (f *fakePluginRegistry) Plugins(_ context.Context) []*plugins.Plugin { - var res []*plugins.Plugin - for _, p := range f.store { - res = append(res, p) - } - return res -} - -func (f *fakePluginRegistry) Add(_ context.Context, p *plugins.Plugin) error { - f.store[p.ID] = p - return nil -} - -func (f *fakePluginRegistry) Remove(_ context.Context, id string) error { - delete(f.store, id) - return nil -}