Preinstall: Allow to set a download URL (#96535)

This commit is contained in:
Andres Martinez Gotor
2024-11-29 16:02:33 +01:00
committed by GitHub
parent b544b8afff
commit e0935246a3
11 changed files with 222 additions and 76 deletions
@@ -135,8 +135,6 @@ func (s *Service) shouldUpdate(ctx context.Context, pluginID, currentVersion str
}
func (s *Service) installPlugins(ctx context.Context) error {
compatOpts := plugins.NewCompatOpts(s.cfg.BuildVersion, runtime.GOOS, runtime.GOARCH)
for _, installPlugin := range s.cfg.PreinstallPlugins {
// Check if the plugin is already installed
p, exists := s.pluginStore.Plugin(ctx, installPlugin.ID)
@@ -162,6 +160,7 @@ func (s *Service) installPlugins(ctx context.Context) error {
s.log.Info("Installing plugin", "pluginId", installPlugin.ID, "version", installPlugin.Version)
start := time.Now()
ctx = repo.WithRequestOrigin(ctx, "preinstall")
compatOpts := plugins.NewAddOpts(s.cfg.BuildVersion, runtime.GOOS, runtime.GOARCH, installPlugin.URL)
err := s.pluginInstaller.Add(ctx, installPlugin.ID, installPlugin.Version, compatOpts)
if err != nil {
var dupeErr plugins.DuplicateError
@@ -115,6 +115,11 @@ func TestService_Run(t *testing.T) {
existingPlugins: []*plugins.Plugin{{JSONData: plugins.JSONData{ID: "myplugin", Info: plugins.Info{Version: "1.0.0"}}}},
latestPlugin: &repo.PluginArchiveInfo{Version: "2.0.0"},
},
{
name: "Should install a plugin with a URL",
shouldInstall: true,
pluginsToInstall: []setting.InstallPlugin{{ID: "myplugin", URL: "https://example.com/myplugin.tar.gz"}},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
@@ -124,6 +129,7 @@ func TestService_Run(t *testing.T) {
require.NoError(t, err)
}
installed := 0
installedFromURL := 0
s, err := ProvideService(
&setting.Cfg{
PreinstallPlugins: tt.pluginsToInstall,
@@ -131,7 +137,7 @@ func TestService_Run(t *testing.T) {
},
pluginstore.New(preg, &fakes.FakeLoader{}),
&fakes.FakePluginInstaller{
AddFunc: func(ctx context.Context, pluginID string, version string, opts plugins.CompatOpts) error {
AddFunc: func(ctx context.Context, pluginID string, version string, opts plugins.AddOpts) error {
for _, plugin := range tt.pluginsToFail {
if plugin == pluginID {
return errors.New("Failed to install plugin")
@@ -143,7 +149,11 @@ func TestService_Run(t *testing.T) {
}
for _, plugin := range tt.pluginsToInstall {
if plugin.ID == pluginID && plugin.Version == version {
installed++
if opts.URL() != "" {
installedFromURL++
} else {
installed++
}
}
}
return nil
@@ -168,7 +178,27 @@ func TestService_Run(t *testing.T) {
require.NoError(t, err)
}
if tt.shouldInstall {
require.Equal(t, len(tt.pluginsToInstall)-len(tt.pluginsToFail), installed)
expectedInstalled := 0
expectedInstalledFromURL := 0
for _, plugin := range tt.pluginsToInstall {
expectedFailed := false
for _, pluginFail := range tt.pluginsToFail {
if plugin.ID == pluginFail {
expectedFailed = true
break
}
}
if expectedFailed {
continue
}
if plugin.URL != "" {
expectedInstalledFromURL++
} else {
expectedInstalled++
}
}
require.Equal(t, expectedInstalled, installed)
require.Equal(t, expectedInstalledFromURL, installedFromURL)
}
})
}