github.com/Axway/agent-sdk@v1.1.101/pkg/agent/cache/migratepersistedcache_test.go (about)

     1  package cache
     2  
     3  import (
     4  	"os"
     5  	"testing"
     6  
     7  	v1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
     8  	management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
     9  	"github.com/Axway/agent-sdk/pkg/cache"
    10  	"github.com/Axway/agent-sdk/pkg/config"
    11  	"github.com/stretchr/testify/assert"
    12  )
    13  
    14  func TestMigrateInstanceCount(t *testing.T) {
    15  	m := NewAgentCacheManager(&config.CentralConfiguration{AgentName: "test", GRPCCfg: config.GRPCConfig{Enabled: true}}, true)
    16  	assert.NotNil(t, m)
    17  
    18  	api1 := createAPIService("apiID", "apiID", "")
    19  	err := m.AddAPIService(api1)
    20  	assert.Nil(t, err)
    21  
    22  	instance1 := createAPIServiceInstance("id1", "apiID", "stage")
    23  	m.AddAPIServiceInstance(instance1)
    24  
    25  	instance2 := createAPIServiceInstance("id2", "apiID", "stage2")
    26  	m.AddAPIServiceInstance(instance2)
    27  
    28  	// remove the instanceCount map before saving
    29  	defer func() {
    30  		// Remove file if it exists
    31  		_, err := os.Stat("./data")
    32  		if !os.IsExist(err) {
    33  			os.RemoveAll("./data")
    34  		}
    35  	}()
    36  
    37  	count := m.GetAPIServiceInstanceCount(api1.Name)
    38  	assert.Equal(t, 2, count)
    39  
    40  	// instance count not updated properly check
    41  	m.(*cacheManager).instanceCountMap = cache.New()
    42  	count = m.GetAPIServiceInstanceCount(api1.Name)
    43  	assert.Equal(t, 2, count)
    44  
    45  	m.(*cacheManager).migratePersistentCache(instanceCountKey)
    46  
    47  	count = m.GetAPIServiceInstanceCount(api1.Name)
    48  	assert.Equal(t, 2, count)
    49  }
    50  
    51  func TestMigrateAccessRequest(t *testing.T) {
    52  	m := NewAgentCacheManager(&config.CentralConfiguration{AgentName: "test", GRPCCfg: config.GRPCConfig{Enabled: true}}, true)
    53  	assert.NotNil(t, m)
    54  
    55  	ar1 := createAccessRequest("id1", "apiID", "appName1", "instID", "instName")
    56  	ri, _ := ar1.AsInstance()
    57  	fakeAddAccessRequest(m.(*cacheManager).accessRequestMap, ri)
    58  
    59  	ar2 := createAccessRequest("id2", "apiID2", "appName1", "instID2", "instName2")
    60  	ri, _ = ar2.AsInstance()
    61  	fakeAddAccessRequest(m.(*cacheManager).accessRequestMap, ri)
    62  
    63  	// remove the instanceCount map before saving
    64  	defer func() {
    65  		// Remove file if it exists
    66  		_, err := os.Stat("./data")
    67  		if !os.IsExist(err) {
    68  			os.RemoveAll("./data")
    69  		}
    70  	}()
    71  
    72  	ars := m.GetAccessRequestsByApp(ar1.Spec.ManagedApplication)
    73  	assert.Len(t, ars, 0)
    74  
    75  	m.(*cacheManager).migratePersistentCache(accReqKey)
    76  
    77  	ars = m.GetAccessRequestsByApp(ar1.Spec.ManagedApplication)
    78  	assert.Len(t, ars, 2)
    79  }
    80  
    81  func fakeAddAccessRequest(instanceCache cache.Cache, ri *v1.ResourceInstance) {
    82  	if ri == nil {
    83  		return
    84  	}
    85  
    86  	ar := &management.AccessRequest{}
    87  	if ar.FromInstance(ri) != nil {
    88  		return
    89  	}
    90  
    91  	instanceCache.SetWithSecondaryKey(ar.Metadata.ID, "test-"+ar.Metadata.ID, ri)
    92  }