2021-03-08 00:02:49 -06:00
|
|
|
package manager
|
2015-02-27 06:45:00 -06:00
|
|
|
|
|
|
|
import (
|
2022-08-23 04:50:50 -05:00
|
|
|
"archive/zip"
|
2020-05-04 03:57:55 -05:00
|
|
|
"context"
|
2022-09-23 07:27:01 -05:00
|
|
|
"fmt"
|
2023-05-30 04:48:52 -05:00
|
|
|
"runtime"
|
2015-02-27 06:45:00 -06:00
|
|
|
"testing"
|
2021-05-12 13:05:16 -05:00
|
|
|
|
2023-02-28 09:10:27 -06:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
|
2021-03-08 00:02:49 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins"
|
2023-02-28 09:10:27 -06:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/log"
|
2022-08-30 10:30:43 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/manager/fakes"
|
2022-08-23 04:50:50 -05:00
|
|
|
"github.com/grafana/grafana/pkg/plugins/repo"
|
|
|
|
"github.com/grafana/grafana/pkg/plugins/storage"
|
2015-02-27 06:45:00 -06:00
|
|
|
)
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
const testPluginID = "test-plugin"
|
2021-05-12 13:05:16 -05:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
func TestPluginManager_Add_Remove(t *testing.T) {
|
|
|
|
t.Run("Adding a new plugin", func(t *testing.T) {
|
|
|
|
const (
|
|
|
|
pluginID, v1 = "test-panel", "1.0.0"
|
|
|
|
zipNameV1 = "test-panel-1.0.0.zip"
|
|
|
|
)
|
2021-05-19 08:42:50 -05:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
// mock a plugin to be returned automatically by the plugin loader
|
2023-06-08 05:21:19 -05:00
|
|
|
pluginV1 := createPlugin(t, pluginID, plugins.ClassExternal, true, true, func(plugin *plugins.Plugin) {
|
2022-08-30 10:30:43 -05:00
|
|
|
plugin.Info.Version = v1
|
2022-08-23 04:50:50 -05:00
|
|
|
})
|
2022-08-30 10:30:43 -05:00
|
|
|
mockZipV1 := &zip.ReadCloser{Reader: zip.Reader{File: []*zip.File{{
|
|
|
|
FileHeader: zip.FileHeader{Name: zipNameV1},
|
|
|
|
}}}}
|
2021-05-19 08:42:50 -05:00
|
|
|
|
2022-09-23 07:27:01 -05:00
|
|
|
var loadedPaths []string
|
2022-08-30 10:30:43 -05:00
|
|
|
loader := &fakes.FakeLoader{
|
2023-03-20 08:35:49 -05:00
|
|
|
LoadFunc: func(ctx context.Context, src plugins.PluginSource) ([]*plugins.Plugin, error) {
|
|
|
|
loadedPaths = append(loadedPaths, src.PluginURIs(ctx)...)
|
|
|
|
require.Equal(t, []string{zipNameV1}, src.PluginURIs(ctx))
|
2022-08-30 10:30:43 -05:00
|
|
|
return []*plugins.Plugin{pluginV1}, nil
|
|
|
|
},
|
2023-08-16 08:44:20 -05:00
|
|
|
UnloadFunc: func(_ context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
|
|
|
|
return p, nil
|
|
|
|
},
|
2021-11-01 04:53:33 -05:00
|
|
|
}
|
2021-05-19 08:42:50 -05:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
pluginRepo := &fakes.FakePluginRepo{
|
2022-09-23 07:27:01 -05:00
|
|
|
GetPluginArchiveFunc: func(_ context.Context, id, version string, _ repo.CompatOpts) (*repo.PluginArchive, error) {
|
|
|
|
require.Equal(t, pluginID, id)
|
2022-08-30 10:30:43 -05:00
|
|
|
require.Equal(t, v1, version)
|
|
|
|
return &repo.PluginArchive{
|
|
|
|
File: mockZipV1,
|
|
|
|
}, nil
|
|
|
|
},
|
2021-11-01 04:53:33 -05:00
|
|
|
}
|
2016-01-08 13:57:58 -06:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
fs := &fakes.FakePluginStorage{
|
2023-07-14 04:49:05 -05:00
|
|
|
ExtractFunc: func(_ context.Context, id string, _ storage.DirNameGeneratorFunc, z *zip.ReadCloser) (*storage.ExtractedPluginArchive, error) {
|
2022-09-23 07:27:01 -05:00
|
|
|
require.Equal(t, pluginID, id)
|
2022-08-30 10:30:43 -05:00
|
|
|
require.Equal(t, mockZipV1, z)
|
|
|
|
return &storage.ExtractedPluginArchive{
|
|
|
|
Path: zipNameV1,
|
|
|
|
}, nil
|
|
|
|
},
|
2021-11-01 04:53:33 -05:00
|
|
|
}
|
|
|
|
|
2023-11-13 06:18:13 -06:00
|
|
|
inst := New(fakes.NewFakePluginRegistry(), loader, pluginRepo, fs, storage.SimpleDirNameGeneratorFunc, &fakes.FakeAuthService{})
|
2023-05-30 04:48:52 -05:00
|
|
|
err := inst.Add(context.Background(), pluginID, v1, testCompatOpts())
|
2020-05-04 10:39:20 -05:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
t.Run("Won't add if already exists", func(t *testing.T) {
|
2022-09-23 07:27:01 -05:00
|
|
|
inst.pluginRegistry = &fakes.FakePluginRegistry{
|
|
|
|
Store: map[string]*plugins.Plugin{
|
|
|
|
pluginID: pluginV1,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2023-05-30 04:48:52 -05:00
|
|
|
err = inst.Add(context.Background(), pluginID, v1, testCompatOpts())
|
2022-08-30 10:30:43 -05:00
|
|
|
require.Equal(t, plugins.DuplicateError{
|
2022-12-02 06:46:55 -06:00
|
|
|
PluginID: pluginV1.ID,
|
2021-11-01 04:53:33 -05:00
|
|
|
}, err)
|
2021-03-10 05:41:29 -06:00
|
|
|
})
|
2021-11-01 04:53:33 -05:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
t.Run("Update plugin to different version", func(t *testing.T) {
|
|
|
|
const (
|
2023-03-07 09:47:02 -06:00
|
|
|
v2 = "2.0.0"
|
|
|
|
zipNameV2 = "test-panel-2.0.0.zip"
|
2022-08-30 10:30:43 -05:00
|
|
|
)
|
|
|
|
// mock a plugin to be returned automatically by the plugin loader
|
2023-06-08 05:21:19 -05:00
|
|
|
pluginV2 := createPlugin(t, pluginID, plugins.ClassExternal, true, true, func(plugin *plugins.Plugin) {
|
2022-08-30 10:30:43 -05:00
|
|
|
plugin.Info.Version = v2
|
2022-08-23 04:50:50 -05:00
|
|
|
})
|
2021-11-01 04:53:33 -05:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
mockZipV2 := &zip.ReadCloser{Reader: zip.Reader{File: []*zip.File{{
|
|
|
|
FileHeader: zip.FileHeader{Name: zipNameV2},
|
|
|
|
}}}}
|
2023-03-20 08:35:49 -05:00
|
|
|
loader.LoadFunc = func(ctx context.Context, src plugins.PluginSource) ([]*plugins.Plugin, error) {
|
2023-06-08 05:21:19 -05:00
|
|
|
require.Equal(t, plugins.ClassExternal, src.PluginClass(ctx))
|
2023-03-20 08:35:49 -05:00
|
|
|
require.Equal(t, []string{zipNameV2}, src.PluginURIs(ctx))
|
2022-08-30 10:30:43 -05:00
|
|
|
return []*plugins.Plugin{pluginV2}, nil
|
2021-05-12 13:05:16 -05:00
|
|
|
}
|
2023-05-30 04:48:52 -05:00
|
|
|
pluginRepo.GetPluginArchiveInfoFunc = func(_ context.Context, _, _ string, _ repo.CompatOpts) (*repo.PluginArchiveInfo, error) {
|
|
|
|
return &repo.PluginArchiveInfo{
|
|
|
|
URL: "https://grafanaplugins.com",
|
2022-08-30 10:30:43 -05:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
pluginRepo.GetPluginArchiveByURLFunc = func(_ context.Context, pluginZipURL string, _ repo.CompatOpts) (*repo.PluginArchive, error) {
|
|
|
|
require.Equal(t, "https://grafanaplugins.com", pluginZipURL)
|
|
|
|
return &repo.PluginArchive{
|
|
|
|
File: mockZipV2,
|
|
|
|
}, nil
|
|
|
|
}
|
2023-07-14 04:49:05 -05:00
|
|
|
fs.ExtractFunc = func(_ context.Context, pluginID string, _ storage.DirNameGeneratorFunc, z *zip.ReadCloser) (*storage.ExtractedPluginArchive, error) {
|
2022-08-30 10:30:43 -05:00
|
|
|
require.Equal(t, pluginV1.ID, pluginID)
|
|
|
|
require.Equal(t, mockZipV2, z)
|
|
|
|
return &storage.ExtractedPluginArchive{
|
|
|
|
Path: zipNameV2,
|
2022-08-23 04:50:50 -05:00
|
|
|
}, nil
|
|
|
|
}
|
2021-05-12 13:05:16 -05:00
|
|
|
|
2023-05-30 04:48:52 -05:00
|
|
|
err = inst.Add(context.Background(), pluginID, v2, testCompatOpts())
|
2022-08-30 10:30:43 -05:00
|
|
|
require.NoError(t, err)
|
2021-11-01 04:53:33 -05:00
|
|
|
})
|
2021-05-12 13:05:16 -05:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
t.Run("Removing an existing plugin", func(t *testing.T) {
|
2022-09-23 07:27:01 -05:00
|
|
|
inst.pluginRegistry = &fakes.FakePluginRegistry{
|
|
|
|
Store: map[string]*plugins.Plugin{
|
|
|
|
pluginID: pluginV1,
|
|
|
|
},
|
|
|
|
}
|
2021-05-12 13:05:16 -05:00
|
|
|
|
2022-09-23 07:27:01 -05:00
|
|
|
var unloadedPlugins []string
|
|
|
|
inst.pluginLoader = &fakes.FakeLoader{
|
2023-08-16 08:44:20 -05:00
|
|
|
UnloadFunc: func(_ context.Context, p *plugins.Plugin) (*plugins.Plugin, error) {
|
|
|
|
unloadedPlugins = append(unloadedPlugins, p.ID)
|
|
|
|
return p, nil
|
2022-09-23 07:27:01 -05:00
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
err = inst.Remove(context.Background(), pluginID)
|
|
|
|
require.NoError(t, err)
|
2021-05-12 13:05:16 -05:00
|
|
|
|
2022-09-23 07:27:01 -05:00
|
|
|
require.Equal(t, []string{pluginID}, unloadedPlugins)
|
2021-11-01 04:53:33 -05:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
t.Run("Won't remove if not exists", func(t *testing.T) {
|
2022-09-23 07:27:01 -05:00
|
|
|
inst.pluginRegistry = fakes.NewFakePluginRegistry()
|
|
|
|
|
|
|
|
err = inst.Remove(context.Background(), pluginID)
|
2021-11-01 04:53:33 -05:00
|
|
|
require.Equal(t, plugins.ErrPluginNotInstalled, err)
|
|
|
|
})
|
2021-05-12 13:05:16 -05:00
|
|
|
})
|
2020-12-11 05:57:57 -06:00
|
|
|
})
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
t.Run("Can't update core or bundled plugin", func(t *testing.T) {
|
|
|
|
tcs := []struct {
|
|
|
|
class plugins.Class
|
|
|
|
}{
|
2023-06-08 05:21:19 -05:00
|
|
|
{class: plugins.ClassCore},
|
|
|
|
{class: plugins.ClassBundled},
|
2021-11-01 04:53:33 -05:00
|
|
|
}
|
2020-12-11 05:57:57 -06:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
for _, tc := range tcs {
|
|
|
|
p := createPlugin(t, testPluginID, tc.class, true, true, func(plugin *plugins.Plugin) {
|
|
|
|
plugin.Info.Version = "1.0.0"
|
|
|
|
})
|
2020-12-11 05:57:57 -06:00
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
reg := &fakes.FakePluginRegistry{
|
|
|
|
Store: map[string]*plugins.Plugin{
|
|
|
|
testPluginID: p,
|
|
|
|
},
|
|
|
|
}
|
2021-06-09 02:57:32 -05:00
|
|
|
|
2023-11-13 06:18:13 -06:00
|
|
|
pm := New(reg, &fakes.FakeLoader{}, &fakes.FakePluginRepo{}, &fakes.FakePluginStorage{}, storage.SimpleDirNameGeneratorFunc, &fakes.FakeAuthService{})
|
2023-05-30 04:48:52 -05:00
|
|
|
err := pm.Add(context.Background(), p.ID, "3.2.0", testCompatOpts())
|
2022-08-30 10:30:43 -05:00
|
|
|
require.ErrorIs(t, err, plugins.ErrInstallCorePlugin)
|
2021-06-09 02:57:32 -05:00
|
|
|
|
2023-05-30 04:48:52 -05:00
|
|
|
err = pm.Add(context.Background(), testPluginID, "", testCompatOpts())
|
2022-08-30 10:30:43 -05:00
|
|
|
require.Equal(t, plugins.ErrInstallCorePlugin, err)
|
2021-11-01 04:53:33 -05:00
|
|
|
|
2022-09-23 07:27:01 -05:00
|
|
|
t.Run(fmt.Sprintf("Can't uninstall %s plugin", tc.class), func(t *testing.T) {
|
2022-08-30 10:30:43 -05:00
|
|
|
err = pm.Remove(context.Background(), p.ID)
|
|
|
|
require.Equal(t, plugins.ErrUninstallCorePlugin, err)
|
|
|
|
})
|
2021-11-01 04:53:33 -05:00
|
|
|
}
|
2022-03-30 08:05:12 -05:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
func createPlugin(t *testing.T, pluginID string, class plugins.Class, managed, backend bool, cbs ...func(*plugins.Plugin)) *plugins.Plugin {
|
2022-03-30 08:05:12 -05:00
|
|
|
t.Helper()
|
|
|
|
|
2021-11-01 04:53:33 -05:00
|
|
|
p := &plugins.Plugin{
|
|
|
|
Class: class,
|
|
|
|
JSONData: plugins.JSONData{
|
2022-08-30 10:30:43 -05:00
|
|
|
ID: pluginID,
|
2023-06-08 05:21:19 -05:00
|
|
|
Type: plugins.TypeDataSource,
|
2022-08-30 10:30:43 -05:00
|
|
|
Backend: backend,
|
2021-11-01 04:53:33 -05:00
|
|
|
},
|
|
|
|
}
|
2023-02-28 09:10:27 -06:00
|
|
|
p.SetLogger(log.NewTestLogger())
|
2022-08-30 10:30:43 -05:00
|
|
|
p.RegisterClient(&fakes.FakePluginClient{
|
|
|
|
ID: pluginID,
|
|
|
|
Managed: managed,
|
|
|
|
Log: p.Logger(),
|
|
|
|
})
|
2021-05-12 13:05:16 -05:00
|
|
|
|
2022-03-30 08:05:12 -05:00
|
|
|
for _, cb := range cbs {
|
|
|
|
cb(p)
|
|
|
|
}
|
|
|
|
|
2022-08-30 10:30:43 -05:00
|
|
|
return p
|
2022-08-23 04:50:50 -05:00
|
|
|
}
|
2023-05-30 04:48:52 -05:00
|
|
|
|
|
|
|
func testCompatOpts() plugins.CompatOpts {
|
|
|
|
return plugins.NewCompatOpts("10.0.0", runtime.GOOS, runtime.GOARCH)
|
|
|
|
}
|