diff --git a/pkg/migrate/apisimigration_test.go b/pkg/migrate/apisimigration_test.go new file mode 100644 index 000000000..93202e035 --- /dev/null +++ b/pkg/migrate/apisimigration_test.go @@ -0,0 +1,169 @@ +package migrate + +import ( + "fmt" + "math/rand" + "strings" + "testing" + + apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" + v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" + mv1a "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" + "github.com/Axway/agent-sdk/pkg/apic/definitions" + "github.com/Axway/agent-sdk/pkg/config" + "github.com/Axway/agent-sdk/pkg/util" + "github.com/stretchr/testify/assert" +) + +var defEnvName = config.NewTestCentralConfig(config.DiscoveryAgent).GetEnvironmentName() + +func createRevisionsResponse(serviceName string, numRevs int) []*v1.ResourceInstance { + revs := []*v1.ResourceInstance{} + for i := 1; i <= numRevs; i++ { + rev := mv1a.NewAPIServiceRevision(fmt.Sprintf("%v.%v", serviceName, i), defEnvName) + rev.Spec.ApiService = serviceName + + ri, _ := rev.AsInstance() + revs = append(revs, ri) + } + return revs +} + +func createInstanceResponse(serviceName string, numRevs int) []*v1.ResourceInstance { + insts := []*v1.ResourceInstance{} + for i := 1; i <= numRevs; i++ { + inst := mv1a.NewAPIServiceInstance(fmt.Sprintf("%v.%v", serviceName, i), defEnvName) + inst.Spec.ApiServiceRevision = fmt.Sprintf("%v.%v", serviceName, i) + + ri, _ := inst.AsInstance() + insts = append(insts, ri) + } + + rand.Shuffle(len(insts), func(i, j int) { + insts[i], insts[j] = insts[j], insts[i] + }) + + return insts +} + +func TestAPISIMigration(t *testing.T) { + tests := []struct { + name string + resource v1.Interface + expectErr bool + turnOff bool + migrationComplete bool + setMigCompelete bool + revisions []*v1.ResourceInstance + instances []*v1.ResourceInstance + expectedDeletes int + }{ + { + name: "called with non-apiservice returns without error", + resource: mv1a.NewAccessRequestDefinition("asdf", defEnvName), + }, + { + name: "called with apiservice and config off returns without error", + resource: mv1a.NewAPIService("asdf", defEnvName), + turnOff: true, + }, + { + name: "called with apiservice that previously was migrated", + resource: mv1a.NewAPIService("asdf", defEnvName), + setMigCompelete: true, + migrationComplete: true, + }, + { + name: "called with apiservice and with no revisions returns without error", + resource: mv1a.NewAPIService("asdf", defEnvName), + migrationComplete: true, + }, + { + name: "called with apiservice, revisions, and instances of same stage returns without error", + resource: mv1a.NewAPIService("apisi", defEnvName), + revisions: createRevisionsResponse("apisi", 10), + instances: createInstanceResponse("apisi", 10), + expectedDeletes: 9, + migrationComplete: true, + }, + { + name: "called with apiservice, revisions, and instances of diff stages returns without error", + resource: mv1a.NewAPIService("apisi", defEnvName), + revisions: append(createRevisionsResponse("apisi-stage1", 5), createRevisionsResponse("apisi-sage2", 5)...), + instances: append(createInstanceResponse("apisi-stage1", 5), createInstanceResponse("apisi-sage2", 5)...), + expectedDeletes: 8, + migrationComplete: true, + }, + } + + for _, tt := range tests { + t.Run(tt.name, func(t *testing.T) { + c := &mockAPISIMigClient{ + revisions: tt.revisions, + instances: tt.instances, + } + + cfg := config.NewTestCentralConfig(config.DiscoveryAgent) + cfg.(*config.CentralConfiguration).MigrationSettings.(*config.MigrationSettings).CleanInstances = !tt.turnOff + mig := NewAPISIMigration(c, cfg) + + resInst, _ := tt.resource.AsInstance() + if tt.setMigCompelete { + util.SetAgentDetailsKey(resInst, definitions.InstanceMigration, definitions.MigrationCompleted) + } + ri, err := mig.Migrate(resInst) + if tt.expectErr { + assert.NotNil(t, err) + return + } + + assert.Nil(t, err) + migVal, _ := util.GetAgentDetailsValue(ri, definitions.InstanceMigration) + if tt.migrationComplete { + assert.Equal(t, definitions.MigrationCompleted, migVal) + } else { + assert.Equal(t, "", migVal) + } + assert.Equal(t, tt.expectedDeletes, c.deleteCalls) + }) + } +} + +type mockAPISIMigClient struct { + deleteCalls int + revisions []*v1.ResourceInstance + instances []*v1.ResourceInstance + instanceReturned bool +} + +func (m *mockAPISIMigClient) ExecuteAPI(method, url string, queryParam map[string]string, buffer []byte) ([]byte, error) { + return nil, nil +} + +func (m *mockAPISIMigClient) GetAPIV1ResourceInstancesWithPageSize(query map[string]string, url string, pageSize int) ([]*apiv1.ResourceInstance, error) { + if m.instanceReturned { + return nil, nil + } else if strings.Contains(url, "instances") { + m.instanceReturned = true + return m.instances, nil + } + return m.revisions, nil +} + +func (m *mockAPISIMigClient) UpdateResourceInstance(ri apiv1.Interface) (*apiv1.ResourceInstance, error) { + r, err := ri.AsInstance() + return r, err +} + +func (m *mockAPISIMigClient) CreateOrUpdateResource(data apiv1.Interface) (*apiv1.ResourceInstance, error) { + return nil, nil +} + +func (m *mockAPISIMigClient) CreateSubResource(rm apiv1.ResourceMeta, subs map[string]interface{}) error { + return nil +} + +func (m *mockAPISIMigClient) DeleteResourceInstance(ri apiv1.Interface) error { + m.deleteCalls++ + return nil +} diff --git a/pkg/migrate/marketplacemigration_test.go b/pkg/migrate/marketplacemigration_test.go index bacbbc2b9..f57723b04 100644 --- a/pkg/migrate/marketplacemigration_test.go +++ b/pkg/migrate/marketplacemigration_test.go @@ -5,7 +5,7 @@ import ( "sync" "testing" - cache2 "github.com/Axway/agent-sdk/pkg/agent/cache" + "github.com/Axway/agent-sdk/pkg/agent/cache" apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" mv1a "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" @@ -34,7 +34,7 @@ func TestMarketplaceMigrationForAPIKeyRevision(t *testing.T) { Environment: envName, APIServerVersion: "v1alpha1", } - cm := cache2.NewAgentCacheManager(cfg, false) + cm := cache.NewAgentCacheManager(cfg, false) ard := mv1a.NewAccessRequestDefinition("api-key", envName) ard.Metadata.ID = "123" @@ -67,7 +67,7 @@ func TestMarketplaceMigrationForOAuthRevision(t *testing.T) { Environment: envName, APIServerVersion: "v1alpha1", } - cm := cache2.NewAgentCacheManager(cfg, false) + cm := cache.NewAgentCacheManager(cfg, false) ard := mv1a.NewAccessRequestDefinition("oauth", envName) ard.Metadata.ID = "0012"