diff --git a/test/database/check.go b/test/database/check.go index 8daab7831..398a1d75c 100644 --- a/test/database/check.go +++ b/test/database/check.go @@ -29,23 +29,58 @@ func NewChecker() *Checker { } func (f *Checker) AddOpenShiftClusterDocuments(docs ...*api.OpenShiftClusterDocument) { - f.openshiftClusterDocuments = append(f.openshiftClusterDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.openshiftClusterDocuments = append(f.openshiftClusterDocuments, docCopy.(*api.OpenShiftClusterDocument)) + } } func (f *Checker) AddSubscriptionDocuments(docs ...*api.SubscriptionDocument) { - f.subscriptionDocuments = append(f.subscriptionDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.subscriptionDocuments = append(f.subscriptionDocuments, docCopy.(*api.SubscriptionDocument)) + } } func (f *Checker) AddBillingDocuments(docs ...*api.BillingDocument) { - f.billingDocuments = append(f.billingDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.billingDocuments = append(f.billingDocuments, docCopy.(*api.BillingDocument)) + } } func (f *Checker) AddAsyncOperationDocuments(docs ...*api.AsyncOperationDocument) { - f.asyncOperationDocuments = append(f.asyncOperationDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.asyncOperationDocuments = append(f.asyncOperationDocuments, docCopy.(*api.AsyncOperationDocument)) + } } func (f *Checker) AddPortalDocuments(docs ...*api.PortalDocument) { - f.portalDocuments = append(f.portalDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.portalDocuments = append(f.portalDocuments, docCopy.(*api.PortalDocument)) + } } func (f *Checker) CheckOpenShiftClusters(openShiftClusters *cosmosdb.FakeOpenShiftClusterDocumentClient) (errs []error) { diff --git a/test/database/deepcopy.go b/test/database/deepcopy.go new file mode 100644 index 000000000..4d0f51b39 --- /dev/null +++ b/test/database/deepcopy.go @@ -0,0 +1,28 @@ +package database + +// Copyright (c) Microsoft Corporation. +// Licensed under the Apache License 2.0. + +import ( + "reflect" + + "github.com/ugorji/go/codec" +) + +var h = &codec.JsonHandle{} + +func deepCopy(i interface{}) (interface{}, error) { + var b []byte + err := codec.NewEncoderBytes(&b, h).Encode(i) + if err != nil { + return nil, err + } + + i = reflect.New(reflect.ValueOf(i).Elem().Type()).Interface() + err = codec.NewDecoderBytes(b, h).Decode(&i) + if err != nil { + return nil, err + } + + return i, nil +} diff --git a/test/database/fixtures.go b/test/database/fixtures.go index 8831a6925..b42cb1476 100644 --- a/test/database/fixtures.go +++ b/test/database/fixtures.go @@ -56,23 +56,58 @@ func (f *Fixture) WithPortal(db database.Portal) *Fixture { } func (f *Fixture) AddOpenShiftClusterDocuments(docs ...*api.OpenShiftClusterDocument) { - f.openshiftClusterDocuments = append(f.openshiftClusterDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.openshiftClusterDocuments = append(f.openshiftClusterDocuments, docCopy.(*api.OpenShiftClusterDocument)) + } } func (f *Fixture) AddSubscriptionDocuments(docs ...*api.SubscriptionDocument) { - f.subscriptionDocuments = append(f.subscriptionDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.subscriptionDocuments = append(f.subscriptionDocuments, docCopy.(*api.SubscriptionDocument)) + } } func (f *Fixture) AddBillingDocuments(docs ...*api.BillingDocument) { - f.billingDocuments = append(f.billingDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.billingDocuments = append(f.billingDocuments, docCopy.(*api.BillingDocument)) + } } func (f *Fixture) AddAsyncOperationDocuments(docs ...*api.AsyncOperationDocument) { - f.asyncOperationDocuments = append(f.asyncOperationDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.asyncOperationDocuments = append(f.asyncOperationDocuments, docCopy.(*api.AsyncOperationDocument)) + } } func (f *Fixture) AddPortalDocuments(docs ...*api.PortalDocument) { - f.portalDocuments = append(f.portalDocuments, docs...) + for _, doc := range docs { + docCopy, err := deepCopy(doc) + if err != nil { + panic(err) + } + + f.portalDocuments = append(f.portalDocuments, docCopy.(*api.PortalDocument)) + } } func (f *Fixture) Create() error {