Skip to content

Commit

Permalink
APIGOV-22850 - test apisi migration module
Browse files Browse the repository at this point in the history
  • Loading branch information
jcollins-axway committed Jul 13, 2022
1 parent 1bb284a commit c763fdb
Show file tree
Hide file tree
Showing 2 changed files with 172 additions and 3 deletions.
169 changes: 169 additions & 0 deletions pkg/migrate/apisimigration_test.go
Original file line number Diff line number Diff line change
@@ -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
}
6 changes: 3 additions & 3 deletions pkg/migrate/marketplacemigration_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down Expand Up @@ -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"
Expand Down

0 comments on commit c763fdb

Please sign in to comment.