diff --git a/pkg/setup/collection.go b/pkg/setup/collection.go index 8d719137f4ab1ee9c4fa06d88880389fd8360efe..679ef2624fcdfe6cc24eaa44a89e384c7040c1eb 100644 --- a/pkg/setup/collection.go +++ b/pkg/setup/collection.go @@ -23,6 +23,7 @@ type DeleteCollectionFn func(s *Setup, col *collections.Collection) bool type CollectionConfig struct { collection *collections.Collection + metadata map[string]string UpdateFn UpdateCollectionFn DeleteFn DeleteCollectionFn } @@ -80,6 +81,12 @@ func UpdateExistingCollection() CollectionsOption { } } +func SetSchemaMetadata(md map[string]string) CollectionsOption { + return func(c *CollectionConfig) { + c.metadata = md + } +} + func (s *Setup) InstallCollections(ctx context.Context) (err error) { if len(s.Collections) == 0 { return nil @@ -164,9 +171,11 @@ func (s *Setup) InstallCollection(ctx context.Context, c CollectionConfig) (setS } func (s *Setup) checkSchemaMetadata(collection *collections.Collection, exist *collections.Collection) error { - if _, ok := collection.Schema.Metadata["extension"]; ok { - if _, ok := exist.Schema.Metadata["extension"]; !ok && !s.IsForce() { - return errors.WithHint(errors.New("fail to update collection"), "collection has the same id as extension's collection, change yours collection id") + if collection.Schema.Metadata != nil { + if _, ok := collection.Schema.Metadata["extension"]; ok { + if _, ok := exist.Schema.Metadata["extension"]; !ok && !s.IsForce() { + return errors.WithHint(errors.New("fail to update collection"), "collection has the same id as extension's collection, change yours collection id") + } } } return nil diff --git a/pkg/setup/collection_test.go b/pkg/setup/collection_test.go index bd90346ff0c8bf6cbc5db52e291702b134f10b9d..2b9f8e4cd5f0586be91bdd8d6d329d244d53ed23 100644 --- a/pkg/setup/collection_test.go +++ b/pkg/setup/collection_test.go @@ -22,6 +22,7 @@ func TestSetup_InstallCollections(t *testing.T) { collections []*collections.Collection collectionsCall func(svc *mockscollections.Collections) envsCall func(svc *envmocks.Environments) + co CollectionsOption wantErr func(t *testing.T, err error) }{ { @@ -105,6 +106,7 @@ func TestSetup_InstallCollections(t *testing.T) { wantErr: func(t *testing.T, err error) { assert.NoError(t, err) }, + co: SetSchemaMetadata(map[string]string{"extension": "test-extension"}), }, { name: "Fail to update collection with the same id", @@ -115,6 +117,7 @@ func TestSetup_InstallCollections(t *testing.T) { wantErr: func(t *testing.T, err error) { assert.Error(t, err) }, + co: SetSchemaMetadata(map[string]string{"extension": "test-extension"}), }, } @@ -130,7 +133,11 @@ func TestSetup_InstallCollections(t *testing.T) { } s := NewSetup(&content.Content{Collections: c, Environments: e}, "sp", "env", nil) - s.AddCollections(tt.collections) + if tt.co != nil { + s.AddCollections(tt.collections, tt.co) + } else { + s.AddCollections(tt.collections) + } tt.wantErr(t, s.InstallCollections(context.Background())) }) }