-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
APIGOV-22850 - test apisi migration module
- Loading branch information
1 parent
1bb284a
commit c763fdb
Showing
2 changed files
with
172 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters