Plugins: Refactor plugin repository API (#69063)

* support grafana wildcard version

* undo go.mod changes

* tidy

* flesh out tests

* refactor

* add tests

* tidy naming

* undo some changes

* split interfaces

* separation

* update new signature

* simplify

* update var namings

* unexport types

* introduce opts pattern

* reorder test

* fix compat checks

* middle ground

* unexport client

* move back

* fix tests

* inline logger

* make client usable

* use fake logger

* tidy errors

* remove unused types

* fix test

* review fixes

* rework compatibility

* adjust installer

* fix tests

* opts => cfg

* remove unused var

* fix var name
This commit is contained in:
Will Browne
2023-05-30 11:48:52 +02:00
committed by GitHub
parent e7e70dbac6
commit 12dc56ad0c
18 changed files with 724 additions and 358 deletions
+13 -8
View File
@@ -4,6 +4,7 @@ import (
"archive/zip"
"context"
"fmt"
"runtime"
"testing"
"github.com/stretchr/testify/require"
@@ -62,7 +63,7 @@ func TestPluginManager_Add_Remove(t *testing.T) {
}
inst := New(fakes.NewFakePluginRegistry(), loader, pluginRepo, fs)
err := inst.Add(context.Background(), pluginID, v1, plugins.CompatOpts{})
err := inst.Add(context.Background(), pluginID, v1, testCompatOpts())
require.NoError(t, err)
t.Run("Won't add if already exists", func(t *testing.T) {
@@ -72,7 +73,7 @@ func TestPluginManager_Add_Remove(t *testing.T) {
},
}
err = inst.Add(context.Background(), pluginID, v1, plugins.CompatOpts{})
err = inst.Add(context.Background(), pluginID, v1, testCompatOpts())
require.Equal(t, plugins.DuplicateError{
PluginID: pluginV1.ID,
}, err)
@@ -96,9 +97,9 @@ func TestPluginManager_Add_Remove(t *testing.T) {
require.Equal(t, []string{zipNameV2}, src.PluginURIs(ctx))
return []*plugins.Plugin{pluginV2}, nil
}
pluginRepo.GetPluginDownloadOptionsFunc = func(_ context.Context, pluginID, version string, _ repo.CompatOpts) (*repo.PluginDownloadOptions, error) {
return &repo.PluginDownloadOptions{
PluginZipURL: "https://grafanaplugins.com",
pluginRepo.GetPluginArchiveInfoFunc = func(_ context.Context, _, _ string, _ repo.CompatOpts) (*repo.PluginArchiveInfo, error) {
return &repo.PluginArchiveInfo{
URL: "https://grafanaplugins.com",
}, nil
}
pluginRepo.GetPluginArchiveByURLFunc = func(_ context.Context, pluginZipURL string, _ repo.CompatOpts) (*repo.PluginArchive, error) {
@@ -115,7 +116,7 @@ func TestPluginManager_Add_Remove(t *testing.T) {
}, nil
}
err = inst.Add(context.Background(), pluginID, v2, plugins.CompatOpts{})
err = inst.Add(context.Background(), pluginID, v2, testCompatOpts())
require.NoError(t, err)
})
@@ -168,10 +169,10 @@ func TestPluginManager_Add_Remove(t *testing.T) {
}
pm := New(reg, &fakes.FakeLoader{}, &fakes.FakePluginRepo{}, &fakes.FakePluginStorage{})
err := pm.Add(context.Background(), p.ID, "3.2.0", plugins.CompatOpts{})
err := pm.Add(context.Background(), p.ID, "3.2.0", testCompatOpts())
require.ErrorIs(t, err, plugins.ErrInstallCorePlugin)
err = pm.Add(context.Background(), testPluginID, "", plugins.CompatOpts{})
err = pm.Add(context.Background(), testPluginID, "", testCompatOpts())
require.Equal(t, plugins.ErrInstallCorePlugin, err)
t.Run(fmt.Sprintf("Can't uninstall %s plugin", tc.class), func(t *testing.T) {
@@ -206,3 +207,7 @@ func createPlugin(t *testing.T, pluginID string, class plugins.Class, managed, b
return p
}
func testCompatOpts() plugins.CompatOpts {
return plugins.NewCompatOpts("10.0.0", runtime.GOOS, runtime.GOARCH)
}