Plugins API: Merge meta and installs (#112962)
This commit is contained in:
@@ -1,20 +1,15 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
"k8s.io/apiserver/pkg/authorization/authorizer"
|
||||
"k8s.io/apiserver/pkg/registry/generic"
|
||||
"k8s.io/apiserver/pkg/registry/rest"
|
||||
restclient "k8s.io/client-go/rest"
|
||||
|
||||
"github.com/grafana/grafana-app-sdk/app"
|
||||
appsdkapiserver "github.com/grafana/grafana-app-sdk/k8s/apiserver"
|
||||
"github.com/grafana/grafana-app-sdk/simple"
|
||||
pluginsappapis "github.com/grafana/grafana/apps/plugins/pkg/apis"
|
||||
pluginsv0alpha1 "github.com/grafana/grafana/apps/plugins/pkg/apis/plugins/v0alpha1"
|
||||
pluginsapp "github.com/grafana/grafana/apps/plugins/pkg/app"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/appinstaller"
|
||||
"github.com/grafana/grafana/pkg/services/apiserver/endpoints/request"
|
||||
"github.com/grafana/grafana/pkg/services/featuremgmt"
|
||||
"github.com/grafana/grafana/pkg/setting"
|
||||
)
|
||||
@@ -26,16 +21,13 @@ var (
|
||||
|
||||
type PluginsAppInstaller struct {
|
||||
appsdkapiserver.AppInstaller
|
||||
cfg *setting.Cfg
|
||||
}
|
||||
|
||||
func RegisterAppInstaller(
|
||||
cfg *setting.Cfg,
|
||||
features featuremgmt.FeatureToggles,
|
||||
) (*PluginsAppInstaller, error) {
|
||||
installer := &PluginsAppInstaller{
|
||||
cfg: cfg,
|
||||
}
|
||||
installer := &PluginsAppInstaller{}
|
||||
specificConfig := any(nil)
|
||||
provider := simple.NewAppProvider(pluginsappapis.LocalManifest(), specificConfig, pluginsapp.New)
|
||||
appConfig := app.Config{
|
||||
@@ -51,21 +43,6 @@ func RegisterAppInstaller(
|
||||
return installer, nil
|
||||
}
|
||||
|
||||
func (p *PluginsAppInstaller) InstallAPIs(
|
||||
server appsdkapiserver.GenericAPIServer,
|
||||
restOptsGetter generic.RESTOptionsGetter,
|
||||
) error {
|
||||
pluginMetaGVR := pluginsv0alpha1.PluginMetaKind().GroupVersionResource()
|
||||
replacedStorage := map[schema.GroupVersionResource]rest.Storage{
|
||||
pluginMetaGVR: pluginsapp.NewPluginMetaStorage(request.GetNamespaceMapper(p.cfg)),
|
||||
}
|
||||
wrappedServer := &customStorageWrapper{
|
||||
wrapped: server,
|
||||
replace: replacedStorage,
|
||||
}
|
||||
return p.AppInstaller.InstallAPIs(wrappedServer, restOptsGetter)
|
||||
}
|
||||
|
||||
// GetAuthorizer returns the authorizer for the plugins app.
|
||||
func (p *PluginsAppInstaller) GetAuthorizer() authorizer.Authorizer {
|
||||
return pluginsapp.GetAuthorizer()
|
||||
|
||||
@@ -4,6 +4,7 @@ import (
|
||||
"context"
|
||||
"time"
|
||||
|
||||
"github.com/grafana/grafana-app-sdk/logging"
|
||||
"github.com/grafana/grafana-app-sdk/resource"
|
||||
|
||||
"github.com/grafana/grafana/apps/plugins/pkg/app/install"
|
||||
@@ -90,6 +91,11 @@ func (s *syncer) Sync(ctx context.Context, source install.Source, installedPlugi
|
||||
return nil
|
||||
}
|
||||
|
||||
if !s.featureToggles.IsEnabled(ctx, featuremgmt.FlagPluginStoreServiceLoading) {
|
||||
logging.DefaultLogger.Warn("pluginInstallAPISync is enabled, but pluginStoreServiceLoading is disabled. skipping plugin sync.")
|
||||
return nil
|
||||
}
|
||||
|
||||
if len(installedPlugins) == 0 {
|
||||
return nil
|
||||
}
|
||||
|
||||
@@ -20,234 +20,68 @@ import (
|
||||
"github.com/grafana/grafana/pkg/services/org/orgtest"
|
||||
)
|
||||
|
||||
// Test helpers to avoid import cycles
|
||||
type fakeServerLock struct {
|
||||
lockFunc func(ctx context.Context, actionName string, maxInterval time.Duration, fn func(ctx context.Context)) error
|
||||
}
|
||||
|
||||
func (f *fakeServerLock) LockExecuteAndRelease(ctx context.Context, actionName string, maxInterval time.Duration, fn func(ctx context.Context)) error {
|
||||
if f.lockFunc != nil {
|
||||
return f.lockFunc(ctx, actionName, maxInterval, fn)
|
||||
}
|
||||
fn(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakePluginInstallClient struct {
|
||||
listAllFunc func(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginInstallList, error)
|
||||
getFunc func(ctx context.Context, identifier resource.Identifier) (*pluginsv0alpha1.PluginInstall, error)
|
||||
createFunc func(ctx context.Context, obj *pluginsv0alpha1.PluginInstall, opts resource.CreateOptions) (*pluginsv0alpha1.PluginInstall, error)
|
||||
updateFunc func(ctx context.Context, obj *pluginsv0alpha1.PluginInstall, opts resource.UpdateOptions) (*pluginsv0alpha1.PluginInstall, error)
|
||||
deleteFunc func(ctx context.Context, identifier resource.Identifier, opts resource.DeleteOptions) error
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Get(ctx context.Context, identifier resource.Identifier) (*pluginsv0alpha1.PluginInstall, error) {
|
||||
if f.getFunc != nil {
|
||||
return f.getFunc(ctx, identifier)
|
||||
}
|
||||
// Return a proper k8s NotFound error
|
||||
return nil, errorsK8s.NewNotFound(schema.GroupResource{
|
||||
Group: pluginsv0alpha1.APIGroup,
|
||||
Resource: "plugininstalls",
|
||||
}, identifier.Name)
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) ListAll(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginInstallList, error) {
|
||||
if f.listAllFunc != nil {
|
||||
return f.listAllFunc(ctx, namespace, opts)
|
||||
}
|
||||
return &pluginsv0alpha1.PluginInstallList{}, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) List(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginInstallList, error) {
|
||||
return f.ListAll(ctx, namespace, opts)
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Create(ctx context.Context, obj *pluginsv0alpha1.PluginInstall, opts resource.CreateOptions) (*pluginsv0alpha1.PluginInstall, error) {
|
||||
if f.createFunc != nil {
|
||||
return f.createFunc(ctx, obj, opts)
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Update(ctx context.Context, obj *pluginsv0alpha1.PluginInstall, opts resource.UpdateOptions) (*pluginsv0alpha1.PluginInstall, error) {
|
||||
if f.updateFunc != nil {
|
||||
return f.updateFunc(ctx, obj, opts)
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) UpdateStatus(ctx context.Context, identifier resource.Identifier, newStatus pluginsv0alpha1.PluginInstallStatus, opts resource.UpdateOptions) (*pluginsv0alpha1.PluginInstall, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Patch(ctx context.Context, identifier resource.Identifier, req resource.PatchRequest, opts resource.PatchOptions) (*pluginsv0alpha1.PluginInstall, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Delete(ctx context.Context, identifier resource.Identifier, opts resource.DeleteOptions) error {
|
||||
if f.deleteFunc != nil {
|
||||
return f.deleteFunc(ctx, identifier, opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeClientGenerator struct {
|
||||
client *fakePluginInstallClient
|
||||
}
|
||||
|
||||
func (f *fakeClientGenerator) ClientFor(kind resource.Kind) (resource.Client, error) {
|
||||
return &fakeResourceClient{client: f.client}, nil
|
||||
}
|
||||
|
||||
type fakeResourceClient struct {
|
||||
client *fakePluginInstallClient
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Get(ctx context.Context, identifier resource.Identifier) (resource.Object, error) {
|
||||
return f.client.Get(ctx, identifier)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) GetInto(ctx context.Context, identifier resource.Identifier, into resource.Object) error {
|
||||
obj, err := f.client.Get(ctx, identifier)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the object data into the provided 'into' object
|
||||
if target, ok := into.(*pluginsv0alpha1.PluginInstall); ok {
|
||||
*target = *obj
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) List(ctx context.Context, namespace string, options resource.ListOptions) (resource.ListObject, error) {
|
||||
return f.client.ListAll(ctx, namespace, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) ListInto(ctx context.Context, namespace string, options resource.ListOptions, into resource.ListObject) error {
|
||||
list, err := f.client.ListAll(ctx, namespace, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the list data into the provided 'into' object
|
||||
if target, ok := into.(*pluginsv0alpha1.PluginInstallList); ok {
|
||||
*target = *list
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Create(ctx context.Context, identifier resource.Identifier, obj resource.Object, options resource.CreateOptions) (resource.Object, error) {
|
||||
plugin := obj.(*pluginsv0alpha1.PluginInstall)
|
||||
return f.client.Create(ctx, plugin, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) CreateInto(ctx context.Context, identifier resource.Identifier, obj resource.Object, options resource.CreateOptions, into resource.Object) error {
|
||||
created, err := f.Create(ctx, identifier, obj, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the created object data into the provided 'into' object
|
||||
if plugin, ok := created.(*pluginsv0alpha1.PluginInstall); ok {
|
||||
if target, ok := into.(*pluginsv0alpha1.PluginInstall); ok {
|
||||
*target = *plugin
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Update(ctx context.Context, identifier resource.Identifier, obj resource.Object, options resource.UpdateOptions) (resource.Object, error) {
|
||||
plugin := obj.(*pluginsv0alpha1.PluginInstall)
|
||||
return f.client.Update(ctx, plugin, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) UpdateInto(ctx context.Context, identifier resource.Identifier, obj resource.Object, options resource.UpdateOptions, into resource.Object) error {
|
||||
updated, err := f.Update(ctx, identifier, obj, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the updated object data into the provided 'into' object
|
||||
if plugin, ok := updated.(*pluginsv0alpha1.PluginInstall); ok {
|
||||
if target, ok := into.(*pluginsv0alpha1.PluginInstall); ok {
|
||||
*target = *plugin
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Patch(ctx context.Context, identifier resource.Identifier, patch resource.PatchRequest, options resource.PatchOptions) (resource.Object, error) {
|
||||
return f.client.Patch(ctx, identifier, patch, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) PatchInto(ctx context.Context, identifier resource.Identifier, patch resource.PatchRequest, options resource.PatchOptions, into resource.Object) error {
|
||||
patched, err := f.Patch(ctx, identifier, patch, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the patched object data into the provided 'into' object
|
||||
if plugin, ok := patched.(*pluginsv0alpha1.PluginInstall); ok {
|
||||
if target, ok := into.(*pluginsv0alpha1.PluginInstall); ok {
|
||||
*target = *plugin
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Delete(ctx context.Context, identifier resource.Identifier, options resource.DeleteOptions) error {
|
||||
return f.client.Delete(ctx, identifier, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) SubresourceRequest(ctx context.Context, identifier resource.Identifier, req resource.CustomRouteRequestOptions) ([]byte, error) {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Watch(ctx context.Context, namespace string, options resource.WatchOptions) (resource.WatchResponse, error) {
|
||||
return &fakeWatchResponse{}, nil
|
||||
}
|
||||
|
||||
type fakeWatchResponse struct{}
|
||||
|
||||
func (f *fakeWatchResponse) Stop() {}
|
||||
|
||||
func (f *fakeWatchResponse) WatchEvents() <-chan resource.WatchEvent {
|
||||
ch := make(chan resource.WatchEvent)
|
||||
close(ch)
|
||||
return ch
|
||||
}
|
||||
|
||||
func TestSyncer_Sync(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
featureToggleEnabled bool
|
||||
orgs []*org.OrgDTO
|
||||
orgServiceError error
|
||||
serverLockError error
|
||||
expectedError error
|
||||
expectSyncCalls int
|
||||
name string
|
||||
pluginInstallAPISyncEnabled bool
|
||||
pluginStoreServiceEnabled bool
|
||||
installedPlugins []*plugins.Plugin
|
||||
orgs []*org.OrgDTO
|
||||
orgServiceError error
|
||||
serverLockError error
|
||||
expectedError error
|
||||
expectSyncCalls int
|
||||
}{
|
||||
{
|
||||
name: "feature toggle disabled",
|
||||
featureToggleEnabled: false,
|
||||
orgs: []*org.OrgDTO{{ID: 1, Name: "Org 1"}},
|
||||
expectedError: nil,
|
||||
expectSyncCalls: 0,
|
||||
name: "plugin install API sync feature toggle disabled",
|
||||
pluginInstallAPISyncEnabled: false,
|
||||
pluginStoreServiceEnabled: true,
|
||||
installedPlugins: []*plugins.Plugin{{JSONData: plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}, Class: plugins.ClassCore}},
|
||||
orgs: []*org.OrgDTO{{ID: 1, Name: "Org 1"}},
|
||||
expectedError: nil,
|
||||
expectSyncCalls: 0,
|
||||
},
|
||||
{
|
||||
name: "feature toggle enabled, no orgs",
|
||||
featureToggleEnabled: true,
|
||||
orgs: []*org.OrgDTO{},
|
||||
expectedError: nil,
|
||||
expectSyncCalls: 0,
|
||||
name: "plugin store service feature toggle disabled",
|
||||
pluginInstallAPISyncEnabled: true,
|
||||
pluginStoreServiceEnabled: false,
|
||||
installedPlugins: []*plugins.Plugin{{JSONData: plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}, Class: plugins.ClassCore}},
|
||||
orgs: []*org.OrgDTO{{ID: 1, Name: "Org 1"}},
|
||||
expectedError: nil,
|
||||
expectSyncCalls: 0,
|
||||
},
|
||||
{
|
||||
name: "feature toggle enabled, single org",
|
||||
featureToggleEnabled: true,
|
||||
orgs: []*org.OrgDTO{{ID: 1, Name: "Org 1"}},
|
||||
expectedError: nil,
|
||||
expectSyncCalls: 1,
|
||||
name: "both feature toggles enabled, no orgs",
|
||||
pluginInstallAPISyncEnabled: true,
|
||||
pluginStoreServiceEnabled: true,
|
||||
installedPlugins: []*plugins.Plugin{{JSONData: plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}, Class: plugins.ClassCore}},
|
||||
orgs: []*org.OrgDTO{},
|
||||
expectedError: nil,
|
||||
expectSyncCalls: 0,
|
||||
},
|
||||
{
|
||||
name: "feature toggle enabled, multiple orgs",
|
||||
featureToggleEnabled: true,
|
||||
name: "both feature toggles enabled, empty installed plugins",
|
||||
pluginInstallAPISyncEnabled: true,
|
||||
pluginStoreServiceEnabled: true,
|
||||
installedPlugins: []*plugins.Plugin{},
|
||||
orgs: []*org.OrgDTO{{ID: 1, Name: "Org 1"}},
|
||||
expectedError: nil,
|
||||
expectSyncCalls: 0,
|
||||
},
|
||||
{
|
||||
name: "both feature toggles enabled, single org",
|
||||
pluginInstallAPISyncEnabled: true,
|
||||
pluginStoreServiceEnabled: true,
|
||||
installedPlugins: []*plugins.Plugin{{JSONData: plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}, Class: plugins.ClassCore}},
|
||||
orgs: []*org.OrgDTO{{ID: 1, Name: "Org 1"}},
|
||||
expectedError: nil,
|
||||
expectSyncCalls: 1,
|
||||
},
|
||||
{
|
||||
name: "both feature toggles enabled, multiple orgs",
|
||||
pluginInstallAPISyncEnabled: true,
|
||||
pluginStoreServiceEnabled: true,
|
||||
installedPlugins: []*plugins.Plugin{{JSONData: plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}, Class: plugins.ClassCore}},
|
||||
orgs: []*org.OrgDTO{
|
||||
{ID: 1, Name: "Org 1"},
|
||||
{ID: 2, Name: "Org 2"},
|
||||
@@ -257,20 +91,24 @@ func TestSyncer_Sync(t *testing.T) {
|
||||
expectSyncCalls: 3,
|
||||
},
|
||||
{
|
||||
name: "org service error",
|
||||
featureToggleEnabled: true,
|
||||
orgs: nil,
|
||||
orgServiceError: errors.New("org service error"),
|
||||
expectedError: errors.New("org service error"),
|
||||
expectSyncCalls: 0,
|
||||
name: "org service error",
|
||||
pluginInstallAPISyncEnabled: true,
|
||||
pluginStoreServiceEnabled: true,
|
||||
installedPlugins: []*plugins.Plugin{{JSONData: plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}, Class: plugins.ClassCore}},
|
||||
orgs: nil,
|
||||
orgServiceError: errors.New("org service error"),
|
||||
expectedError: errors.New("org service error"),
|
||||
expectSyncCalls: 0,
|
||||
},
|
||||
{
|
||||
name: "server lock error",
|
||||
featureToggleEnabled: true,
|
||||
orgs: []*org.OrgDTO{{ID: 1, Name: "Org 1"}},
|
||||
serverLockError: errors.New("lock error"),
|
||||
expectedError: errors.New("lock error"),
|
||||
expectSyncCalls: 0,
|
||||
name: "server lock error",
|
||||
pluginInstallAPISyncEnabled: true,
|
||||
pluginStoreServiceEnabled: true,
|
||||
installedPlugins: []*plugins.Plugin{{JSONData: plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}, Class: plugins.ClassCore}},
|
||||
orgs: []*org.OrgDTO{{ID: 1, Name: "Org 1"}},
|
||||
serverLockError: errors.New("lock error"),
|
||||
expectedError: errors.New("lock error"),
|
||||
expectSyncCalls: 0,
|
||||
},
|
||||
}
|
||||
|
||||
@@ -280,7 +118,8 @@ func TestSyncer_Sync(t *testing.T) {
|
||||
|
||||
// Setup feature toggles
|
||||
ft := featuremgmt.NewMockFeatureToggles(t)
|
||||
ft.EXPECT().IsEnabled(ctx, featuremgmt.FlagPluginInstallAPISync).Return(tt.featureToggleEnabled).Maybe()
|
||||
ft.EXPECT().IsEnabled(ctx, featuremgmt.FlagPluginInstallAPISync).Return(tt.pluginInstallAPISyncEnabled).Maybe()
|
||||
ft.EXPECT().IsEnabled(ctx, featuremgmt.FlagPluginStoreServiceLoading).Return(tt.pluginStoreServiceEnabled).Maybe()
|
||||
|
||||
// Setup org service
|
||||
orgService := orgtest.NewOrgServiceFake()
|
||||
@@ -298,12 +137,12 @@ func TestSyncer_Sync(t *testing.T) {
|
||||
// Setup fake client and registrar
|
||||
syncCalls := 0
|
||||
fakeClient := &fakePluginInstallClient{
|
||||
createFunc: func(ctx context.Context, obj *pluginsv0alpha1.PluginInstall, opts resource.CreateOptions) (*pluginsv0alpha1.PluginInstall, error) {
|
||||
createFunc: func(ctx context.Context, obj *pluginsv0alpha1.Plugin, opts resource.CreateOptions) (*pluginsv0alpha1.Plugin, error) {
|
||||
syncCalls++
|
||||
return obj, nil
|
||||
},
|
||||
listAllFunc: func(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginInstallList, error) {
|
||||
return &pluginsv0alpha1.PluginInstallList{}, nil
|
||||
listAllFunc: func(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginList, error) {
|
||||
return &pluginsv0alpha1.PluginList{}, nil
|
||||
},
|
||||
}
|
||||
clientGen := &fakeClientGenerator{client: fakeClient}
|
||||
@@ -320,10 +159,7 @@ func TestSyncer_Sync(t *testing.T) {
|
||||
)
|
||||
|
||||
// Execute
|
||||
installedPlugins := []*plugins.Plugin{
|
||||
{JSONData: plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}},
|
||||
}
|
||||
err := s.Sync(ctx, install.SourcePluginStore, installedPlugins)
|
||||
err := s.Sync(ctx, install.SourcePluginStore, tt.installedPlugins)
|
||||
|
||||
// Verify
|
||||
if tt.expectedError != nil {
|
||||
@@ -342,7 +178,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
installedPlugins []*plugins.Plugin
|
||||
apiPlugins []pluginsv0alpha1.PluginInstall
|
||||
apiPlugins []pluginsv0alpha1.Plugin
|
||||
clientListError error
|
||||
expectedError error
|
||||
expectedRegCalls int
|
||||
@@ -353,7 +189,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
{
|
||||
name: "no installed plugins, no API plugins",
|
||||
installedPlugins: []*plugins.Plugin{},
|
||||
apiPlugins: []pluginsv0alpha1.PluginInstall{},
|
||||
apiPlugins: []pluginsv0alpha1.Plugin{},
|
||||
expectedError: nil,
|
||||
expectedRegCalls: 0,
|
||||
expectedUnregCalls: 0,
|
||||
@@ -364,7 +200,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
{JSONData: plugins.JSONData{ID: "plugin-1", Info: plugins.Info{Version: "1.0.0"}}, Class: plugins.ClassCore},
|
||||
{JSONData: plugins.JSONData{ID: "plugin-2", Info: plugins.Info{Version: "2.0.0"}}, Class: plugins.ClassExternal},
|
||||
},
|
||||
apiPlugins: []pluginsv0alpha1.PluginInstall{},
|
||||
apiPlugins: []pluginsv0alpha1.Plugin{},
|
||||
expectedError: nil,
|
||||
expectedRegCalls: 2,
|
||||
expectedUnregCalls: 0,
|
||||
@@ -373,7 +209,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
{
|
||||
name: "API plugins only",
|
||||
installedPlugins: []*plugins.Plugin{},
|
||||
apiPlugins: []pluginsv0alpha1.PluginInstall{
|
||||
apiPlugins: []pluginsv0alpha1.Plugin{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "plugin-1",
|
||||
@@ -381,7 +217,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
install.PluginInstallSourceAnnotation: install.SourcePluginStore,
|
||||
},
|
||||
},
|
||||
Spec: pluginsv0alpha1.PluginInstallSpec{Id: "plugin-1"},
|
||||
Spec: pluginsv0alpha1.PluginSpec{Id: "plugin-1"},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
@@ -390,7 +226,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
install.PluginInstallSourceAnnotation: install.SourcePluginStore,
|
||||
},
|
||||
},
|
||||
Spec: pluginsv0alpha1.PluginInstallSpec{Id: "plugin-2"},
|
||||
Spec: pluginsv0alpha1.PluginSpec{Id: "plugin-2"},
|
||||
},
|
||||
},
|
||||
expectedError: nil,
|
||||
@@ -405,7 +241,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
{JSONData: plugins.JSONData{ID: "plugin-2", Info: plugins.Info{Version: "2.0.0"}}, Class: plugins.ClassExternal},
|
||||
{JSONData: plugins.JSONData{ID: "plugin-3", Info: plugins.Info{Version: "3.0.0"}}, Class: plugins.ClassExternal},
|
||||
},
|
||||
apiPlugins: []pluginsv0alpha1.PluginInstall{
|
||||
apiPlugins: []pluginsv0alpha1.Plugin{
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
Name: "plugin-2",
|
||||
@@ -413,7 +249,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
install.PluginInstallSourceAnnotation: install.SourcePluginStore,
|
||||
},
|
||||
},
|
||||
Spec: pluginsv0alpha1.PluginInstallSpec{Id: "plugin-2", Version: "2.0.0"},
|
||||
Spec: pluginsv0alpha1.PluginSpec{Id: "plugin-2", Version: "2.0.0"},
|
||||
},
|
||||
{
|
||||
ObjectMeta: metav1.ObjectMeta{
|
||||
@@ -422,7 +258,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
install.PluginInstallSourceAnnotation: install.SourcePluginStore,
|
||||
},
|
||||
},
|
||||
Spec: pluginsv0alpha1.PluginInstallSpec{Id: "plugin-4"},
|
||||
Spec: pluginsv0alpha1.PluginSpec{Id: "plugin-4"},
|
||||
},
|
||||
},
|
||||
expectedError: nil,
|
||||
@@ -434,7 +270,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
{
|
||||
name: "list error",
|
||||
installedPlugins: []*plugins.Plugin{},
|
||||
apiPlugins: []pluginsv0alpha1.PluginInstall{},
|
||||
apiPlugins: []pluginsv0alpha1.Plugin{},
|
||||
clientListError: errors.New("list error"),
|
||||
expectedError: errors.New("list error"),
|
||||
},
|
||||
@@ -450,15 +286,15 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
|
||||
// Setup fake client
|
||||
fakeClient := &fakePluginInstallClient{
|
||||
listAllFunc: func(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginInstallList, error) {
|
||||
listAllFunc: func(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginList, error) {
|
||||
if tt.clientListError != nil {
|
||||
return nil, tt.clientListError
|
||||
}
|
||||
return &pluginsv0alpha1.PluginInstallList{
|
||||
return &pluginsv0alpha1.PluginList{
|
||||
Items: tt.apiPlugins,
|
||||
}, nil
|
||||
},
|
||||
createFunc: func(ctx context.Context, obj *pluginsv0alpha1.PluginInstall, opts resource.CreateOptions) (*pluginsv0alpha1.PluginInstall, error) {
|
||||
createFunc: func(ctx context.Context, obj *pluginsv0alpha1.Plugin, opts resource.CreateOptions) (*pluginsv0alpha1.Plugin, error) {
|
||||
registeredIDs = append(registeredIDs, obj.Spec.Id)
|
||||
return obj, nil
|
||||
},
|
||||
@@ -466,7 +302,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
unregisteredIDs = append(unregisteredIDs, identifier.Name)
|
||||
return nil
|
||||
},
|
||||
getFunc: func(ctx context.Context, identifier resource.Identifier) (*pluginsv0alpha1.PluginInstall, error) {
|
||||
getFunc: func(ctx context.Context, identifier resource.Identifier) (*pluginsv0alpha1.Plugin, error) {
|
||||
// Check if plugin exists in apiPlugins
|
||||
for i := range tt.apiPlugins {
|
||||
if tt.apiPlugins[i].Name == identifier.Name {
|
||||
@@ -521,7 +357,7 @@ func TestSyncer_syncNamespace(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncer_getClient(t *testing.T) {
|
||||
func TestInstallRegistrar_GetClient(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
}{
|
||||
@@ -559,92 +395,196 @@ func TestSyncer_getClient(t *testing.T) {
|
||||
}
|
||||
}
|
||||
|
||||
func TestSyncer_syncAllNamespaces(t *testing.T) {
|
||||
tests := []struct {
|
||||
name string
|
||||
orgs []*org.OrgDTO
|
||||
orgServiceError error
|
||||
expectedError error
|
||||
expectedCalls int
|
||||
}{
|
||||
{
|
||||
name: "no orgs",
|
||||
orgs: []*org.OrgDTO{},
|
||||
expectedError: nil,
|
||||
expectedCalls: 0,
|
||||
},
|
||||
{
|
||||
name: "single org",
|
||||
orgs: []*org.OrgDTO{
|
||||
{ID: 1, Name: "Org 1"},
|
||||
},
|
||||
expectedError: nil,
|
||||
expectedCalls: 1,
|
||||
},
|
||||
{
|
||||
name: "multiple orgs",
|
||||
orgs: []*org.OrgDTO{
|
||||
{ID: 1, Name: "Org 1"},
|
||||
{ID: 2, Name: "Org 2"},
|
||||
{ID: 3, Name: "Org 3"},
|
||||
},
|
||||
expectedError: nil,
|
||||
expectedCalls: 3,
|
||||
},
|
||||
{
|
||||
name: "org service error",
|
||||
orgs: nil,
|
||||
orgServiceError: errors.New("org service error"),
|
||||
expectedError: errors.New("org service error"),
|
||||
expectedCalls: 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tt := range tests {
|
||||
t.Run(tt.name, func(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
|
||||
orgService := orgtest.NewOrgServiceFake()
|
||||
orgService.ExpectedOrgs = tt.orgs
|
||||
orgService.ExpectedError = tt.orgServiceError
|
||||
|
||||
// Track namespace sync calls
|
||||
syncCalls := 0
|
||||
fakeClient := &fakePluginInstallClient{
|
||||
createFunc: func(ctx context.Context, obj *pluginsv0alpha1.PluginInstall, opts resource.CreateOptions) (*pluginsv0alpha1.PluginInstall, error) {
|
||||
syncCalls++
|
||||
return obj, nil
|
||||
},
|
||||
listAllFunc: func(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginInstallList, error) {
|
||||
return &pluginsv0alpha1.PluginInstallList{}, nil
|
||||
},
|
||||
}
|
||||
|
||||
clientGen := &fakeClientGenerator{client: fakeClient}
|
||||
|
||||
s := newSyncer(
|
||||
featuremgmt.NewMockFeatureToggles(t),
|
||||
clientGen,
|
||||
install.NewInstallRegistrar(clientGen),
|
||||
orgService,
|
||||
func(orgID int64) string { return "org-1" },
|
||||
&fakeServerLock{},
|
||||
)
|
||||
|
||||
installedPlugins := []*plugins.Plugin{
|
||||
{JSONData: plugins.JSONData{ID: "test-plugin", Info: plugins.Info{Version: "1.0.0"}}, Class: plugins.ClassCore},
|
||||
}
|
||||
|
||||
err := s.syncAllNamespaces(ctx, install.SourcePluginStore, installedPlugins)
|
||||
|
||||
if tt.expectedError != nil {
|
||||
require.Error(t, err)
|
||||
require.Equal(t, tt.expectedError.Error(), err.Error())
|
||||
} else {
|
||||
require.NoError(t, err)
|
||||
}
|
||||
|
||||
require.Equal(t, tt.expectedCalls, syncCalls)
|
||||
})
|
||||
// Test helpers to avoid import cycles
|
||||
type fakeServerLock struct {
|
||||
lockFunc func(ctx context.Context, actionName string, maxInterval time.Duration, fn func(ctx context.Context)) error
|
||||
}
|
||||
|
||||
func (f *fakeServerLock) LockExecuteAndRelease(ctx context.Context, actionName string, maxInterval time.Duration, fn func(ctx context.Context)) error {
|
||||
if f.lockFunc != nil {
|
||||
return f.lockFunc(ctx, actionName, maxInterval, fn)
|
||||
}
|
||||
fn(ctx)
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakePluginInstallClient struct {
|
||||
listAllFunc func(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginList, error)
|
||||
getFunc func(ctx context.Context, identifier resource.Identifier) (*pluginsv0alpha1.Plugin, error)
|
||||
createFunc func(ctx context.Context, obj *pluginsv0alpha1.Plugin, opts resource.CreateOptions) (*pluginsv0alpha1.Plugin, error)
|
||||
updateFunc func(ctx context.Context, obj *pluginsv0alpha1.Plugin, opts resource.UpdateOptions) (*pluginsv0alpha1.Plugin, error)
|
||||
deleteFunc func(ctx context.Context, identifier resource.Identifier, opts resource.DeleteOptions) error
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Get(ctx context.Context, identifier resource.Identifier) (*pluginsv0alpha1.Plugin, error) {
|
||||
if f.getFunc != nil {
|
||||
return f.getFunc(ctx, identifier)
|
||||
}
|
||||
// Return a proper k8s NotFound error
|
||||
return nil, errorsK8s.NewNotFound(schema.GroupResource{
|
||||
Group: pluginsv0alpha1.APIGroup,
|
||||
Resource: "plugininstalls",
|
||||
}, identifier.Name)
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) ListAll(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginList, error) {
|
||||
if f.listAllFunc != nil {
|
||||
return f.listAllFunc(ctx, namespace, opts)
|
||||
}
|
||||
return &pluginsv0alpha1.PluginList{}, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) List(ctx context.Context, namespace string, opts resource.ListOptions) (*pluginsv0alpha1.PluginList, error) {
|
||||
return f.ListAll(ctx, namespace, opts)
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Create(ctx context.Context, obj *pluginsv0alpha1.Plugin, opts resource.CreateOptions) (*pluginsv0alpha1.Plugin, error) {
|
||||
if f.createFunc != nil {
|
||||
return f.createFunc(ctx, obj, opts)
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Update(ctx context.Context, obj *pluginsv0alpha1.Plugin, opts resource.UpdateOptions) (*pluginsv0alpha1.Plugin, error) {
|
||||
if f.updateFunc != nil {
|
||||
return f.updateFunc(ctx, obj, opts)
|
||||
}
|
||||
return obj, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) UpdateStatus(ctx context.Context, identifier resource.Identifier, newStatus pluginsv0alpha1.PluginStatus, opts resource.UpdateOptions) (*pluginsv0alpha1.Plugin, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Patch(ctx context.Context, identifier resource.Identifier, req resource.PatchRequest, opts resource.PatchOptions) (*pluginsv0alpha1.Plugin, error) {
|
||||
return nil, nil
|
||||
}
|
||||
|
||||
func (f *fakePluginInstallClient) Delete(ctx context.Context, identifier resource.Identifier, opts resource.DeleteOptions) error {
|
||||
if f.deleteFunc != nil {
|
||||
return f.deleteFunc(ctx, identifier, opts)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
type fakeClientGenerator struct {
|
||||
client *fakePluginInstallClient
|
||||
}
|
||||
|
||||
func (f *fakeClientGenerator) ClientFor(kind resource.Kind) (resource.Client, error) {
|
||||
return &fakeResourceClient{client: f.client}, nil
|
||||
}
|
||||
|
||||
type fakeResourceClient struct {
|
||||
client *fakePluginInstallClient
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Get(ctx context.Context, identifier resource.Identifier) (resource.Object, error) {
|
||||
return f.client.Get(ctx, identifier)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) GetInto(ctx context.Context, identifier resource.Identifier, into resource.Object) error {
|
||||
obj, err := f.client.Get(ctx, identifier)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the object data into the provided 'into' object
|
||||
if target, ok := into.(*pluginsv0alpha1.Plugin); ok {
|
||||
*target = *obj
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) List(ctx context.Context, namespace string, options resource.ListOptions) (resource.ListObject, error) {
|
||||
return f.client.ListAll(ctx, namespace, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) ListInto(ctx context.Context, namespace string, options resource.ListOptions, into resource.ListObject) error {
|
||||
list, err := f.client.ListAll(ctx, namespace, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the list data into the provided 'into' object
|
||||
if target, ok := into.(*pluginsv0alpha1.PluginList); ok {
|
||||
*target = *list
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Create(ctx context.Context, identifier resource.Identifier, obj resource.Object, options resource.CreateOptions) (resource.Object, error) {
|
||||
plugin := obj.(*pluginsv0alpha1.Plugin)
|
||||
return f.client.Create(ctx, plugin, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) CreateInto(ctx context.Context, identifier resource.Identifier, obj resource.Object, options resource.CreateOptions, into resource.Object) error {
|
||||
created, err := f.Create(ctx, identifier, obj, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the created object data into the provided 'into' object
|
||||
if plugin, ok := created.(*pluginsv0alpha1.Plugin); ok {
|
||||
if target, ok := into.(*pluginsv0alpha1.Plugin); ok {
|
||||
*target = *plugin
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Update(ctx context.Context, identifier resource.Identifier, obj resource.Object, options resource.UpdateOptions) (resource.Object, error) {
|
||||
plugin := obj.(*pluginsv0alpha1.Plugin)
|
||||
return f.client.Update(ctx, plugin, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) UpdateInto(ctx context.Context, identifier resource.Identifier, obj resource.Object, options resource.UpdateOptions, into resource.Object) error {
|
||||
updated, err := f.Update(ctx, identifier, obj, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the updated object data into the provided 'into' object
|
||||
if plugin, ok := updated.(*pluginsv0alpha1.Plugin); ok {
|
||||
if target, ok := into.(*pluginsv0alpha1.Plugin); ok {
|
||||
*target = *plugin
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Patch(ctx context.Context, identifier resource.Identifier, patch resource.PatchRequest, options resource.PatchOptions) (resource.Object, error) {
|
||||
return f.client.Patch(ctx, identifier, patch, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) PatchInto(ctx context.Context, identifier resource.Identifier, patch resource.PatchRequest, options resource.PatchOptions, into resource.Object) error {
|
||||
patched, err := f.Patch(ctx, identifier, patch, options)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
// Copy the patched object data into the provided 'into' object
|
||||
if plugin, ok := patched.(*pluginsv0alpha1.Plugin); ok {
|
||||
if target, ok := into.(*pluginsv0alpha1.Plugin); ok {
|
||||
*target = *plugin
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Delete(ctx context.Context, identifier resource.Identifier, options resource.DeleteOptions) error {
|
||||
return f.client.Delete(ctx, identifier, options)
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) SubresourceRequest(ctx context.Context, identifier resource.Identifier, req resource.CustomRouteRequestOptions) ([]byte, error) {
|
||||
return []byte{}, nil
|
||||
}
|
||||
|
||||
func (f *fakeResourceClient) Watch(ctx context.Context, namespace string, options resource.WatchOptions) (resource.WatchResponse, error) {
|
||||
return &fakeWatchResponse{}, nil
|
||||
}
|
||||
|
||||
type fakeWatchResponse struct{}
|
||||
|
||||
func (f *fakeWatchResponse) Stop() {}
|
||||
|
||||
func (f *fakeWatchResponse) WatchEvents() <-chan resource.WatchEvent {
|
||||
ch := make(chan resource.WatchEvent)
|
||||
close(ch)
|
||||
return ch
|
||||
}
|
||||
|
||||
@@ -20,19 +20,30 @@ func TestIntegrationPluginsIntegrationDiscovery(t *testing.T) {
|
||||
"freshness": "Current",
|
||||
"resources": [
|
||||
{
|
||||
"resource": "plugininstalls",
|
||||
"resource": "plugins",
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "PluginInstall",
|
||||
"kind": "Plugin",
|
||||
"version": ""
|
||||
},
|
||||
"scope": "Namespaced",
|
||||
"singularResource": "plugininstalls",
|
||||
"singularResource": "plugins",
|
||||
"subresources": [
|
||||
{
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "PluginInstall",
|
||||
"kind": "ResourceCallOptions",
|
||||
"version": ""
|
||||
},
|
||||
"subresource": "meta",
|
||||
"verbs": [
|
||||
"get"
|
||||
]
|
||||
},
|
||||
{
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "Plugin",
|
||||
"version": ""
|
||||
},
|
||||
"subresource": "status",
|
||||
@@ -53,35 +64,6 @@ func TestIntegrationPluginsIntegrationDiscovery(t *testing.T) {
|
||||
"update",
|
||||
"watch"
|
||||
]
|
||||
},
|
||||
{
|
||||
"resource": "pluginmetas",
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "PluginMeta",
|
||||
"version": ""
|
||||
},
|
||||
"scope": "Namespaced",
|
||||
"singularResource": "pluginmeta",
|
||||
"subresources": [
|
||||
{
|
||||
"responseKind": {
|
||||
"group": "",
|
||||
"kind": "PluginMeta",
|
||||
"version": ""
|
||||
},
|
||||
"subresource": "status",
|
||||
"verbs": [
|
||||
"get",
|
||||
"patch",
|
||||
"update"
|
||||
]
|
||||
}
|
||||
],
|
||||
"verbs": [
|
||||
"get",
|
||||
"list"
|
||||
]
|
||||
}
|
||||
]
|
||||
}
|
||||
|
||||
@@ -16,88 +16,54 @@ import (
|
||||
"github.com/grafana/grafana/pkg/util/testutil"
|
||||
)
|
||||
|
||||
var gvrPluginInstalls = schema.GroupVersionResource{
|
||||
var gvrPlugins = schema.GroupVersionResource{
|
||||
Group: "plugins.grafana.app",
|
||||
Version: "v0alpha1",
|
||||
Resource: "plugininstalls",
|
||||
Resource: "plugins",
|
||||
}
|
||||
|
||||
func TestMain(m *testing.M) {
|
||||
testsuite.Run(m)
|
||||
}
|
||||
|
||||
func TestIntegrationPluginInstalls(t *testing.T) {
|
||||
func TestIntegrationPlugins(t *testing.T) {
|
||||
testutil.SkipIntegrationTestInShortMode(t)
|
||||
|
||||
t.Run("create plugin install", func(t *testing.T) {
|
||||
t.Run("create plugin", func(t *testing.T) {
|
||||
helper := setupHelper(t)
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginInstalls,
|
||||
GVR: gvrPlugins,
|
||||
})
|
||||
pluginName := "test-plugin-create"
|
||||
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
"apiVersion": "plugins.grafana.app/v0alpha1",
|
||||
"kind": "PluginInstall",
|
||||
"kind": "Plugin",
|
||||
"metadata": {"name": "%s"},
|
||||
"spec": {"version": "1.0.0"}
|
||||
}`, pluginName))
|
||||
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
|
||||
created, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, created)
|
||||
require.Equal(t, pluginName, created.GetName())
|
||||
})
|
||||
|
||||
t.Run("create plugin install with status is ignored", func(t *testing.T) {
|
||||
t.Skip("status is not ignored on create. this might require a change in the SDK. skipping for now")
|
||||
helper := setupHelper(t)
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginInstalls,
|
||||
})
|
||||
pluginName := "test-plugin-create-with-status"
|
||||
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
"apiVersion": "plugins.grafana.app/v0alpha1",
|
||||
"kind": "PluginInstall",
|
||||
"metadata": {"name": "%s"},
|
||||
"spec": {"version": "1.0.0"},
|
||||
"status": {
|
||||
"operatorStates": {
|
||||
"test-operator": {
|
||||
"lastEvaluation": "1",
|
||||
"state": "success"
|
||||
}
|
||||
}
|
||||
}
|
||||
}`, pluginName))
|
||||
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, created)
|
||||
require.Equal(t, pluginName, created.GetName())
|
||||
// Status should be empty as it's ignored on create
|
||||
status, found, err := unstructured.NestedMap(created.Object, "status")
|
||||
require.NoError(t, err)
|
||||
require.True(t, found) // status field should exist
|
||||
require.Empty(t, status) // but it should be empty
|
||||
})
|
||||
|
||||
t.Run("get plugin install", func(t *testing.T) {
|
||||
helper := setupHelper(t)
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginInstalls,
|
||||
GVR: gvrPlugins,
|
||||
})
|
||||
pluginName := "test-plugin-get"
|
||||
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
"apiVersion": "plugins.grafana.app/v0alpha1",
|
||||
"kind": "PluginInstall",
|
||||
"kind": "Plugin",
|
||||
"metadata": {"name": "%s"},
|
||||
"spec": {"version": "1.0.0"}
|
||||
}`, pluginName))
|
||||
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
|
||||
created, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
fetched, err := client.Resource.Get(ctx, pluginName, metav1.GetOptions{})
|
||||
require.NoError(t, err)
|
||||
@@ -111,16 +77,16 @@ func TestIntegrationPluginInstalls(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginInstalls,
|
||||
GVR: gvrPlugins,
|
||||
})
|
||||
pluginName := "test-plugin-update"
|
||||
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
"apiVersion": "plugins.grafana.app/v0alpha1",
|
||||
"kind": "PluginInstall",
|
||||
"kind": "Plugin",
|
||||
"metadata": {"name": "%s"},
|
||||
"spec": {"version": "1.0.0"}
|
||||
}`, pluginName))
|
||||
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
|
||||
created, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
updatedSpec := created.DeepCopy()
|
||||
updatedSpec.Object["spec"] = map[string]interface{}{
|
||||
@@ -132,116 +98,21 @@ func TestIntegrationPluginInstalls(t *testing.T) {
|
||||
require.Equal(t, "2.0.0", updated.Object["spec"].(map[string]interface{})["version"])
|
||||
})
|
||||
|
||||
t.Run("update plugin install with status is ignored", func(t *testing.T) {
|
||||
helper := setupHelper(t)
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginInstalls,
|
||||
})
|
||||
pluginName := "test-plugin-update-with-status"
|
||||
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
"apiVersion": "plugins.grafana.app/v0alpha1",
|
||||
"kind": "PluginInstall",
|
||||
"metadata": {"name": "%s"},
|
||||
"spec": {"version": "1.0.0"}
|
||||
}`, pluginName))
|
||||
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Try to update the status via a normal update
|
||||
withStatus := created.DeepCopy()
|
||||
withStatus.Object["status"] = map[string]interface{}{
|
||||
"operatorStates": map[string]interface{}{
|
||||
"test-operator": map[string]interface{}{
|
||||
"lastEvaluation": "1",
|
||||
"state": "success",
|
||||
},
|
||||
},
|
||||
}
|
||||
updated, err := client.Resource.Update(ctx, withStatus, metav1.UpdateOptions{})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, updated)
|
||||
|
||||
// The status should not have been updated
|
||||
status, found, err := unstructured.NestedMap(updated.Object, "status")
|
||||
require.NoError(t, err)
|
||||
require.True(t, found)
|
||||
require.Empty(t, status)
|
||||
|
||||
// also check with get
|
||||
fetched, err := client.Resource.Get(ctx, pluginName, metav1.GetOptions{})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, fetched)
|
||||
status, found, err = unstructured.NestedMap(fetched.Object, "status")
|
||||
require.NoError(t, err)
|
||||
require.True(t, found)
|
||||
require.Empty(t, status)
|
||||
})
|
||||
|
||||
t.Run("update plugin install status", func(t *testing.T) {
|
||||
helper := setupHelper(t)
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginInstalls,
|
||||
})
|
||||
pluginName := "test-plugin-status"
|
||||
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
"apiVersion": "plugins.grafana.app/v0alpha1",
|
||||
"kind": "PluginInstall",
|
||||
"metadata": {"name": "%s"},
|
||||
"spec": {"version": "1.0.0"}
|
||||
}`, pluginName))
|
||||
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
|
||||
// Update the status
|
||||
status := created.DeepCopy()
|
||||
statusPayload := map[string]interface{}{
|
||||
"operatorStates": map[string]interface{}{
|
||||
"test-operator": map[string]interface{}{
|
||||
"lastEvaluation": "1",
|
||||
"state": "success",
|
||||
},
|
||||
},
|
||||
}
|
||||
status.Object["status"] = statusPayload
|
||||
updated, err := client.Resource.UpdateStatus(ctx, status, metav1.UpdateOptions{})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, updated)
|
||||
|
||||
// Check the status on the returned object
|
||||
actualStatus, found, err := unstructured.NestedMap(updated.Object, "status")
|
||||
require.NoError(t, err)
|
||||
require.True(t, found)
|
||||
require.Equal(t, statusPayload, actualStatus)
|
||||
|
||||
// Get the status to ensure it persisted
|
||||
fetched, err := client.Resource.Get(ctx, pluginName, metav1.GetOptions{})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, fetched)
|
||||
actualStatus, found, err = unstructured.NestedMap(fetched.Object, "status")
|
||||
require.NoError(t, err)
|
||||
require.True(t, found)
|
||||
require.Equal(t, statusPayload, actualStatus)
|
||||
})
|
||||
|
||||
t.Run("list plugin installs", func(t *testing.T) {
|
||||
helper := setupHelper(t)
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginInstalls,
|
||||
GVR: gvrPlugins,
|
||||
})
|
||||
pluginName := "test-plugin-list"
|
||||
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
"apiVersion": "plugins.grafana.app/v0alpha1",
|
||||
"kind": "PluginInstall",
|
||||
"kind": "Plugin",
|
||||
"metadata": {"name": "%s"},
|
||||
"spec": {"version": "1.0.0"}
|
||||
}`, pluginName))
|
||||
created, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
|
||||
created, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
list, err := client.Resource.List(ctx, metav1.ListOptions{})
|
||||
require.NoError(t, err)
|
||||
@@ -254,16 +125,16 @@ func TestIntegrationPluginInstalls(t *testing.T) {
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginInstalls,
|
||||
GVR: gvrPlugins,
|
||||
})
|
||||
pluginName := "test-plugin-delete"
|
||||
pluginInstall := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
plugin := helper.LoadYAMLOrJSON(fmt.Sprintf(`{
|
||||
"apiVersion": "plugins.grafana.app/v0alpha1",
|
||||
"kind": "PluginInstall",
|
||||
"kind": "Plugin",
|
||||
"metadata": {"name": "%s"},
|
||||
"spec": {"version": "1.0.0"}
|
||||
}`, pluginName))
|
||||
_, err := client.Resource.Create(ctx, pluginInstall, metav1.CreateOptions{})
|
||||
_, err := client.Resource.Create(ctx, plugin, metav1.CreateOptions{})
|
||||
require.NoError(t, err)
|
||||
err = client.Resource.Delete(ctx, pluginName, metav1.DeleteOptions{})
|
||||
require.NoError(t, err)
|
||||
@@ -281,15 +152,15 @@ func TestIntegrationPluginInstalls(t *testing.T) {
|
||||
t.Run(fmt.Sprintf("with basic role: %s", user.Identity.GetOrgRole()), func(t *testing.T) {
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: user,
|
||||
GVR: gvrPluginInstalls,
|
||||
GVR: gvrPlugins,
|
||||
})
|
||||
pluginInstall := helper.LoadYAMLOrJSON(`{
|
||||
plugin := helper.LoadYAMLOrJSON(`{
|
||||
"apiVersion": "plugins.grafana.app/v0alpha1",
|
||||
"kind": "PluginInstall",
|
||||
"kind": "Plugin",
|
||||
"metadata": {"name": "test-plugin"},
|
||||
"spec": {"version": "1.0.0"}
|
||||
}`)
|
||||
_, err := client.Resource.Create(context.Background(), pluginInstall, metav1.CreateOptions{})
|
||||
_, err := client.Resource.Create(context.Background(), plugin, metav1.CreateOptions{})
|
||||
statusError := helper.AsStatusError(err)
|
||||
require.Equal(t, metav1.StatusReasonForbidden, statusError.Status().Reason)
|
||||
err = client.Resource.Delete(context.Background(), "test-plugin", metav1.DeleteOptions{})
|
||||
|
||||
@@ -1,47 +0,0 @@
|
||||
package plugins
|
||||
|
||||
import (
|
||||
"context"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/require"
|
||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||
"k8s.io/apimachinery/pkg/runtime/schema"
|
||||
|
||||
"github.com/grafana/grafana/pkg/tests/apis"
|
||||
"github.com/grafana/grafana/pkg/util/testutil"
|
||||
)
|
||||
|
||||
var gvrPluginMeta = schema.GroupVersionResource{
|
||||
Group: "plugins.grafana.app",
|
||||
Version: "v0alpha1",
|
||||
Resource: "pluginmetas",
|
||||
}
|
||||
|
||||
func TestIntegrationPluginMeta(t *testing.T) {
|
||||
testutil.SkipIntegrationTestInShortMode(t)
|
||||
|
||||
t.Run("list plugin metas", func(t *testing.T) {
|
||||
helper := setupHelper(t)
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginMeta,
|
||||
})
|
||||
list, err := client.Resource.List(ctx, metav1.ListOptions{})
|
||||
require.NoError(t, err)
|
||||
require.NotNil(t, list)
|
||||
require.Empty(t, list.Items)
|
||||
})
|
||||
|
||||
t.Run("get plugin meta", func(t *testing.T) {
|
||||
helper := setupHelper(t)
|
||||
ctx := context.Background()
|
||||
client := helper.GetResourceClient(apis.ResourceClientArgs{
|
||||
User: helper.Org1.Admin,
|
||||
GVR: gvrPluginMeta,
|
||||
})
|
||||
_, err := client.Resource.Get(ctx, "example", metav1.GetOptions{})
|
||||
require.Error(t, err)
|
||||
})
|
||||
}
|
||||
Reference in New Issue
Block a user