mirror of
https://github.com/grafana/grafana.git
synced 2025-02-25 18:55:37 -06:00
Plugins: Re-use plugin registry mocks from fakes package (#69840)
re-use mocks from fakes package
This commit is contained in:
parent
3dc8255639
commit
9f18e0ccf3
@ -10,11 +10,12 @@ import (
|
|||||||
"github.com/grafana/grafana/pkg/plugins"
|
"github.com/grafana/grafana/pkg/plugins"
|
||||||
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
"github.com/grafana/grafana/pkg/plugins/backendplugin"
|
||||||
"github.com/grafana/grafana/pkg/plugins/log"
|
"github.com/grafana/grafana/pkg/plugins/log"
|
||||||
|
"github.com/grafana/grafana/pkg/plugins/manager/fakes"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestProcessManager_Start(t *testing.T) {
|
func TestProcessManager_Start(t *testing.T) {
|
||||||
t.Run("Plugin not found in registry", func(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")
|
err := m.Start(context.Background(), "non-existing-datasource")
|
||||||
require.ErrorIs(t, err, backendplugin.ErrPluginNotRegistered)
|
require.ErrorIs(t, err, backendplugin.ErrPluginNotRegistered)
|
||||||
})
|
})
|
||||||
@ -63,9 +64,11 @@ func TestProcessManager_Start(t *testing.T) {
|
|||||||
plugin.SignatureError = tc.signatureError
|
plugin.SignatureError = tc.signatureError
|
||||||
})
|
})
|
||||||
|
|
||||||
m := NewManager(newFakePluginRegistry(map[string]*plugins.Plugin{
|
m := NewManager(&fakes.FakePluginRegistry{
|
||||||
|
Store: map[string]*plugins.Plugin{
|
||||||
p.ID: p,
|
p.ID: p,
|
||||||
}))
|
}},
|
||||||
|
)
|
||||||
|
|
||||||
err := m.Start(context.Background(), p.ID)
|
err := m.Start(context.Background(), p.ID)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
@ -83,7 +86,7 @@ func TestProcessManager_Start(t *testing.T) {
|
|||||||
|
|
||||||
func TestProcessManager_Stop(t *testing.T) {
|
func TestProcessManager_Stop(t *testing.T) {
|
||||||
t.Run("Plugin not found in registry", func(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")
|
err := m.Stop(context.Background(), "non-existing-datasource")
|
||||||
require.ErrorIs(t, err, backendplugin.ErrPluginNotRegistered)
|
require.ErrorIs(t, err, backendplugin.ErrPluginNotRegistered)
|
||||||
})
|
})
|
||||||
@ -97,9 +100,11 @@ func TestProcessManager_Stop(t *testing.T) {
|
|||||||
plugin.Backend = true
|
plugin.Backend = true
|
||||||
})
|
})
|
||||||
|
|
||||||
m := NewManager(newFakePluginRegistry(map[string]*plugins.Plugin{
|
m := NewManager(&fakes.FakePluginRegistry{
|
||||||
|
Store: map[string]*plugins.Plugin{
|
||||||
pluginID: p,
|
pluginID: p,
|
||||||
}))
|
}},
|
||||||
|
)
|
||||||
err := m.Stop(context.Background(), pluginID)
|
err := m.Stop(context.Background(), pluginID)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
@ -116,9 +121,11 @@ func TestProcessManager_ManagedBackendPluginLifecycle(t *testing.T) {
|
|||||||
plugin.Backend = true
|
plugin.Backend = true
|
||||||
})
|
})
|
||||||
|
|
||||||
m := NewManager(newFakePluginRegistry(map[string]*plugins.Plugin{
|
m := NewManager(&fakes.FakePluginRegistry{
|
||||||
|
Store: map[string]*plugins.Plugin{
|
||||||
p.ID: p,
|
p.ID: p,
|
||||||
}))
|
}},
|
||||||
|
)
|
||||||
|
|
||||||
err := m.Start(context.Background(), p.ID)
|
err := m.Start(context.Background(), p.ID)
|
||||||
require.NoError(t, err)
|
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 {
|
type fakeBackendPlugin struct {
|
||||||
managed bool
|
managed bool
|
||||||
|
|
||||||
|
@ -54,10 +54,12 @@ func TestStore_Plugin(t *testing.T) {
|
|||||||
p1.RegisterClient(&DecommissionedPlugin{})
|
p1.RegisterClient(&DecommissionedPlugin{})
|
||||||
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-panel"}}
|
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-panel"}}
|
||||||
|
|
||||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
ps := New(&fakes.FakePluginRegistry{
|
||||||
|
Store: map[string]*plugins.Plugin{
|
||||||
p1.ID: p1,
|
p1.ID: p1,
|
||||||
p2.ID: p2,
|
p2.ID: p2,
|
||||||
}))
|
},
|
||||||
|
})
|
||||||
|
|
||||||
p, exists := ps.Plugin(context.Background(), p1.ID)
|
p, exists := ps.Plugin(context.Background(), p1.ID)
|
||||||
require.False(t, exists)
|
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 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "e-test-panel", Type: plugins.TypePanel}}
|
||||||
p5.RegisterClient(&DecommissionedPlugin{})
|
p5.RegisterClient(&DecommissionedPlugin{})
|
||||||
|
|
||||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
ps := New(&fakes.FakePluginRegistry{
|
||||||
|
Store: map[string]*plugins.Plugin{
|
||||||
p1.ID: p1,
|
p1.ID: p1,
|
||||||
p2.ID: p2,
|
p2.ID: p2,
|
||||||
p3.ID: p3,
|
p3.ID: p3,
|
||||||
p4.ID: p4,
|
p4.ID: p4,
|
||||||
p5.ID: p5,
|
p5.ID: p5,
|
||||||
}))
|
},
|
||||||
|
})
|
||||||
|
|
||||||
pss := ps.Plugins(context.Background())
|
pss := ps.Plugins(context.Background())
|
||||||
require.Equal(t, pss, []plugins.PluginDTO{p1.ToDTO(), p2.ToDTO(), p3.ToDTO(), p4.ToDTO()})
|
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 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "f-test-app", Type: plugins.TypeApp}}
|
||||||
p6.RegisterClient(&DecommissionedPlugin{})
|
p6.RegisterClient(&DecommissionedPlugin{})
|
||||||
|
|
||||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
ps := New(&fakes.FakePluginRegistry{
|
||||||
|
Store: map[string]*plugins.Plugin{
|
||||||
p1.ID: p1,
|
p1.ID: p1,
|
||||||
p2.ID: p2,
|
p2.ID: p2,
|
||||||
p3.ID: p3,
|
p3.ID: p3,
|
||||||
p4.ID: p4,
|
p4.ID: p4,
|
||||||
p5.ID: p5,
|
p5.ID: p5,
|
||||||
p6.ID: p6,
|
p6.ID: p6,
|
||||||
}))
|
},
|
||||||
|
})
|
||||||
|
|
||||||
sr := func(p *plugins.Plugin) *plugins.StaticRoute {
|
sr := func(p *plugins.Plugin) *plugins.StaticRoute {
|
||||||
return &plugins.StaticRoute{PluginID: p.ID, Directory: p.FS.Base()}
|
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}}
|
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-panel", Type: plugins.TypePanel}}
|
||||||
p3 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-app", Type: plugins.TypeApp}}
|
p3 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-app", Type: plugins.TypeApp}}
|
||||||
|
|
||||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
ps := New(&fakes.FakePluginRegistry{
|
||||||
|
Store: map[string]*plugins.Plugin{
|
||||||
p1.ID: p1,
|
p1.ID: p1,
|
||||||
p2.ID: p2,
|
p2.ID: p2,
|
||||||
p3.ID: p3,
|
p3.ID: p3,
|
||||||
}))
|
},
|
||||||
|
})
|
||||||
|
|
||||||
r := ps.Renderer(context.Background())
|
r := ps.Renderer(context.Background())
|
||||||
require.Equal(t, p1, r)
|
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}}
|
p3 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-secrets", Type: plugins.TypeSecretsManager}}
|
||||||
p4 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource", Type: plugins.TypeDataSource}}
|
p4 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-datasource", Type: plugins.TypeDataSource}}
|
||||||
|
|
||||||
ps := New(newFakePluginRegistry(map[string]*plugins.Plugin{
|
ps := New(&fakes.FakePluginRegistry{
|
||||||
|
Store: map[string]*plugins.Plugin{
|
||||||
p1.ID: p1,
|
p1.ID: p1,
|
||||||
p2.ID: p2,
|
p2.ID: p2,
|
||||||
p3.ID: p3,
|
p3.ID: p3,
|
||||||
p4.ID: p4,
|
p4.ID: p4,
|
||||||
}))
|
},
|
||||||
|
})
|
||||||
|
|
||||||
r := ps.SecretsManager(context.Background())
|
r := ps.SecretsManager(context.Background())
|
||||||
require.Equal(t, p3, r)
|
require.Equal(t, p3, r)
|
||||||
@ -173,12 +183,12 @@ func TestStore_availablePlugins(t *testing.T) {
|
|||||||
p1.RegisterClient(&DecommissionedPlugin{})
|
p1.RegisterClient(&DecommissionedPlugin{})
|
||||||
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-app"}}
|
p2 := &plugins.Plugin{JSONData: plugins.JSONData{ID: "test-app"}}
|
||||||
|
|
||||||
ps := New(
|
ps := New(&fakes.FakePluginRegistry{
|
||||||
newFakePluginRegistry(map[string]*plugins.Plugin{
|
Store: map[string]*plugins.Plugin{
|
||||||
p1.ID: p1,
|
p1.ID: p1,
|
||||||
p2.ID: p2,
|
p2.ID: p2,
|
||||||
}),
|
},
|
||||||
)
|
})
|
||||||
|
|
||||||
aps := ps.availablePlugins(context.Background())
|
aps := ps.availablePlugins(context.Background())
|
||||||
require.Len(t, aps, 1)
|
require.Len(t, aps, 1)
|
||||||
@ -197,36 +207,3 @@ func (p *DecommissionedPlugin) Decommission() error {
|
|||||||
func (p *DecommissionedPlugin) IsDecommissioned() bool {
|
func (p *DecommissionedPlugin) IsDecommissioned() bool {
|
||||||
return true
|
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
|
|
||||||
}
|
|
||||||
|
Loading…
Reference in New Issue
Block a user