mirror of
https://github.com/grafana/grafana.git
synced 2025-08-02 23:53:10 +08:00
Preinstall: Allow to set a download URL (#96535)
This commit is contained in:

committed by
GitHub

parent
b544b8afff
commit
e0935246a3
@ -3,6 +3,7 @@ package manager
|
||||
import (
|
||||
"archive/zip"
|
||||
"context"
|
||||
"errors"
|
||||
"fmt"
|
||||
"runtime"
|
||||
"testing"
|
||||
@ -62,7 +63,9 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
require.Equal(t, pluginID, id)
|
||||
require.Equal(t, mockZipV1, z)
|
||||
return &storage.ExtractedPluginArchive{
|
||||
Path: zipNameV1,
|
||||
ID: pluginID,
|
||||
Version: v1,
|
||||
Path: zipNameV1,
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
@ -84,6 +87,22 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
}, err)
|
||||
})
|
||||
|
||||
t.Run("Add from URL", func(t *testing.T) {
|
||||
url := "https://grafanaplugins.com"
|
||||
pluginRepo := &fakes.FakePluginRepo{
|
||||
GetPluginArchiveByURLFunc: func(_ context.Context, archiveURL string, _ repo.CompatOpts) (*repo.PluginArchive, error) {
|
||||
require.Equal(t, pluginID, pluginID)
|
||||
require.Equal(t, url, archiveURL)
|
||||
return &repo.PluginArchive{
|
||||
File: mockZipV1,
|
||||
}, nil
|
||||
},
|
||||
}
|
||||
inst := New(fakes.NewFakePluginRegistry(), loader, pluginRepo, fs, storage.SimpleDirNameGeneratorFunc, &fakes.FakeAuthService{})
|
||||
err := inst.Add(context.Background(), pluginID, v1, plugins.NewAddOpts(v1, runtime.GOOS, runtime.GOARCH, url))
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Update plugin to different version", func(t *testing.T) {
|
||||
// mock a plugin to be returned automatically by the plugin loader
|
||||
pluginV2 := createPlugin(t, pluginID, plugins.ClassExternal, true, true, func(plugin *plugins.Plugin) {
|
||||
@ -113,7 +132,9 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
require.Equal(t, pluginV1.ID, pluginID)
|
||||
require.Equal(t, mockZipV2, z)
|
||||
return &storage.ExtractedPluginArchive{
|
||||
Path: zipNameV2,
|
||||
ID: pluginID,
|
||||
Version: v2,
|
||||
Path: zipNameV2,
|
||||
}, nil
|
||||
}
|
||||
|
||||
@ -121,6 +142,47 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
require.NoError(t, err)
|
||||
})
|
||||
|
||||
t.Run("Update plugin from url", func(t *testing.T) {
|
||||
url := "https://grafanaplugins.com"
|
||||
// mock a plugin to be returned automatically by the plugin loader
|
||||
pluginV2 := createPlugin(t, pluginID, plugins.ClassExternal, true, true, func(plugin *plugins.Plugin) {
|
||||
plugin.Info.Version = v2
|
||||
})
|
||||
|
||||
mockZipV2 := &zip.ReadCloser{Reader: zip.Reader{File: []*zip.File{{
|
||||
FileHeader: zip.FileHeader{Name: zipNameV2},
|
||||
}}}}
|
||||
loader.LoadFunc = func(ctx context.Context, src plugins.PluginSource) ([]*plugins.Plugin, error) {
|
||||
require.Equal(t, plugins.ClassExternal, src.PluginClass(ctx))
|
||||
require.Equal(t, []string{zipNameV2}, src.PluginURIs(ctx))
|
||||
return []*plugins.Plugin{pluginV2}, nil
|
||||
}
|
||||
pluginRepo.GetPluginArchiveInfoFunc = func(_ context.Context, _, _ string, _ repo.CompatOpts) (*repo.PluginArchiveInfo, error) {
|
||||
return nil, errors.New("shouldn't be called")
|
||||
}
|
||||
getPluginArchiveByURLCalled := false
|
||||
pluginRepo.GetPluginArchiveByURLFunc = func(_ context.Context, pluginZipURL string, _ repo.CompatOpts) (*repo.PluginArchive, error) {
|
||||
require.Equal(t, url, pluginZipURL)
|
||||
getPluginArchiveByURLCalled = true
|
||||
return &repo.PluginArchive{
|
||||
File: mockZipV2,
|
||||
}, nil
|
||||
}
|
||||
fs.ExtractFunc = func(_ context.Context, pluginID string, _ storage.DirNameGeneratorFunc, z *zip.ReadCloser) (*storage.ExtractedPluginArchive, error) {
|
||||
require.Equal(t, pluginV1.ID, pluginID)
|
||||
require.Equal(t, mockZipV2, z)
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: pluginID,
|
||||
Version: v2,
|
||||
Path: zipNameV2,
|
||||
}, nil
|
||||
}
|
||||
|
||||
err = inst.Add(context.Background(), pluginID, v2, plugins.NewAddOpts(v2, runtime.GOOS, runtime.GOARCH, url))
|
||||
require.NoError(t, err)
|
||||
require.True(t, getPluginArchiveByURLCalled)
|
||||
})
|
||||
|
||||
t.Run("Removing an existing plugin", func(t *testing.T) {
|
||||
inst.pluginRegistry = &fakes.FakePluginRegistry{
|
||||
Store: map[string]*plugins.Plugin{
|
||||
@ -210,14 +272,19 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
ExtractFunc: func(_ context.Context, id string, _ storage.DirNameGeneratorFunc, z *zip.ReadCloser) (*storage.ExtractedPluginArchive, error) {
|
||||
switch id {
|
||||
case p1:
|
||||
return &storage.ExtractedPluginArchive{Path: p1Zip}, nil
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p1,
|
||||
Path: p1Zip,
|
||||
}, nil
|
||||
case p2:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p2,
|
||||
Dependencies: []*storage.Dependency{{ID: p1}},
|
||||
Path: p2Zip,
|
||||
}, nil
|
||||
case p3:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p3,
|
||||
Dependencies: []*storage.Dependency{{ID: p2}},
|
||||
Path: p3Zip,
|
||||
}, nil
|
||||
@ -260,11 +327,13 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
switch id {
|
||||
case p1:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p1,
|
||||
Dependencies: []*storage.Dependency{{ID: p2}},
|
||||
Path: p1Zip,
|
||||
}, nil
|
||||
case p2:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: p2,
|
||||
Dependencies: []*storage.Dependency{{ID: p1}},
|
||||
Path: p2Zip,
|
||||
}, nil
|
||||
@ -309,6 +378,7 @@ func TestPluginManager_Add_Remove(t *testing.T) {
|
||||
switch id {
|
||||
case testPluginID:
|
||||
return &storage.ExtractedPluginArchive{
|
||||
ID: testPluginID,
|
||||
Dependencies: []*storage.Dependency{{ID: pluginDependencyID}},
|
||||
Path: "test-plugin.zip",
|
||||
}, nil
|
||||
@ -352,6 +422,6 @@ 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)
|
||||
func testCompatOpts() plugins.AddOpts {
|
||||
return plugins.NewAddOpts("10.0.0", runtime.GOOS, runtime.GOARCH, "")
|
||||
}
|
||||
|
Reference in New Issue
Block a user