github.com/Axway/agent-sdk@v1.1.101/pkg/agent/discoverycache_test.go (about) 1 package agent 2 3 import ( 4 "context" 5 "fmt" 6 "strings" 7 "sync" 8 "testing" 9 10 agentcache "github.com/Axway/agent-sdk/pkg/agent/cache" 11 "github.com/Axway/agent-sdk/pkg/agent/handler" 12 apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1" 13 management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1" 14 "github.com/Axway/agent-sdk/pkg/config" 15 "github.com/Axway/agent-sdk/pkg/watchmanager/proto" 16 "github.com/stretchr/testify/assert" 17 ) 18 19 const envName = "mockEnv" 20 21 func TestDiscoveryCache_execute(t *testing.T) { 22 tests := []struct { 23 agentType config.AgentType 24 name string 25 svcCount int 26 managedAppCount int 27 accessReqCount int 28 credCount int 29 withMigration bool 30 wt *management.WatchTopic 31 }{ 32 { 33 name: "should fetch resources based on the watch topic", 34 agentType: config.DiscoveryAgent, 35 svcCount: 2, 36 managedAppCount: 2, 37 accessReqCount: 2, 38 credCount: 2, 39 wt: mpWatchTopic, 40 }, 41 { 42 name: "should fetch resources and perform a migration", 43 agentType: config.DiscoveryAgent, 44 svcCount: 2, 45 managedAppCount: 2, 46 accessReqCount: 2, 47 credCount: 2, 48 withMigration: true, 49 wt: mpWatchTopic, 50 }, 51 { 52 name: "should fetch resources based on the watch topic with marketplace disabled", 53 agentType: config.TraceabilityAgent, 54 svcCount: 2, 55 managedAppCount: 0, 56 accessReqCount: 0, 57 credCount: 0, 58 wt: watchTopicNoMP, 59 }, 60 } 61 62 for _, tc := range tests { 63 t.Run(tc.name, func(t *testing.T) { 64 cfg := createCentralCfg("apicentral.axway.com", envName) 65 cfg.AgentType = tc.agentType 66 agent.cacheManager = agentcache.NewAgentCacheManager(agent.cfg, false) 67 agent.cfg = cfg 68 Initialize(cfg) 69 scopeName := agent.cfg.GetEnvironmentName() 70 c := &mockRIClient{ 71 svcs: newAPIServices(scopeName), 72 managedApps: newManagedApps(scopeName), 73 accessReqs: newAccessReqs(scopeName), 74 creds: newCredentials(scopeName), 75 } 76 svcHandler := &mockHandler{ 77 kind: management.APIServiceGVK().Kind, 78 } 79 managedAppHandler := &mockHandler{ 80 kind: management.ManagedApplicationGVK().Kind, 81 } 82 accessReqHandler := &mockHandler{ 83 kind: management.AccessRequestGVK().Kind, 84 } 85 credHandler := &mockHandler{ 86 kind: management.CredentialGVK().Kind, 87 } 88 89 handlers := []handler.Handler{ 90 svcHandler, 91 managedAppHandler, 92 accessReqHandler, 93 credHandler, 94 } 95 96 opts := []discoveryOpt{ 97 withAdditionalDiscoverFuncs(func() error { 98 return nil 99 }), 100 } 101 102 migration := &mockMigrator{mutex: sync.Mutex{}} 103 if tc.withMigration { 104 opts = append(opts, withMigration(migration)) 105 } 106 107 dc := newDiscoveryCache( 108 cfg, 109 c, 110 handlers, 111 tc.wt, 112 opts..., 113 ) 114 115 err := dc.execute() 116 assert.Nil(t, err) 117 assert.Equal(t, tc.svcCount, svcHandler.count) 118 assert.Equal(t, tc.managedAppCount, managedAppHandler.count) 119 assert.Equal(t, tc.accessReqCount, accessReqHandler.count) 120 assert.Equal(t, tc.credCount, credHandler.count) 121 if tc.withMigration { 122 assert.True(t, migration.called) 123 } else { 124 assert.False(t, migration.called) 125 } 126 }) 127 } 128 129 } 130 131 type mockHandler struct { 132 count int 133 err error 134 kind string 135 } 136 137 func (m *mockHandler) Handle(_ context.Context, _ *proto.EventMeta, ri *apiv1.ResourceInstance) error { 138 if m.kind != "" && ri.Kind != m.kind { 139 return nil 140 } 141 m.count = m.count + 1 142 return m.err 143 } 144 145 func newAPIServices(scope string) []*apiv1.ResourceInstance { 146 svc1, _ := management.NewAPIService("svc1", scope).AsInstance() 147 svc2, _ := management.NewAPIService("svc2", scope).AsInstance() 148 return []*apiv1.ResourceInstance{ 149 svc1, svc2, 150 } 151 } 152 153 func newManagedApps(scope string) []*apiv1.ResourceInstance { 154 app1, _ := management.NewManagedApplication("app1", scope).AsInstance() 155 app2, _ := management.NewManagedApplication("app2", scope).AsInstance() 156 return []*apiv1.ResourceInstance{ 157 app1, app2, 158 } 159 } 160 161 func newAccessReqs(scope string) []*apiv1.ResourceInstance { 162 ar1, _ := management.NewAccessRequest("ar1", scope).AsInstance() 163 ar2, _ := management.NewAccessRequest("ar2", scope).AsInstance() 164 return []*apiv1.ResourceInstance{ 165 ar1, ar2, 166 } 167 } 168 169 func newCredentials(scope string) []*apiv1.ResourceInstance { 170 cred1, _ := management.NewCredential("cred1", scope).AsInstance() 171 cred2, _ := management.NewCredential("cred2", scope).AsInstance() 172 return []*apiv1.ResourceInstance{ 173 cred1, cred2, 174 } 175 } 176 177 type mockRIClient struct { 178 svcs []*apiv1.ResourceInstance 179 managedApps []*apiv1.ResourceInstance 180 accessReqs []*apiv1.ResourceInstance 181 creds []*apiv1.ResourceInstance 182 err error 183 } 184 185 func (m mockRIClient) GetAPIV1ResourceInstances(_ map[string]string, URL string) ([]*apiv1.ResourceInstance, error) { 186 fmt.Println(URL) 187 if strings.Contains(URL, "apiservices") { 188 return m.svcs, m.err 189 } else if strings.Contains(URL, "managedapplications") { 190 return m.managedApps, m.err 191 } else if strings.Contains(URL, "accessrequests") { 192 return m.accessReqs, m.err 193 } else if strings.Contains(URL, "credentials") { 194 return m.creds, m.err 195 } 196 return make([]*apiv1.ResourceInstance, 0), m.err 197 } 198 199 type mockMigrator struct { 200 called bool 201 mutex sync.Mutex 202 } 203 204 func (m *mockMigrator) Migrate(_ context.Context, ri *apiv1.ResourceInstance) (*apiv1.ResourceInstance, error) { 205 m.mutex.Lock() 206 defer m.mutex.Unlock() 207 m.called = true 208 return ri, nil 209 } 210 211 var mpWatchTopic = &management.WatchTopic{ 212 ResourceMeta: apiv1.ResourceMeta{}, 213 Owner: nil, 214 Spec: management.WatchTopicSpec{ 215 Description: "", 216 Filters: []management.WatchTopicSpecFilters{ 217 { 218 Group: "management", 219 Kind: management.APIServiceGVK().Kind, 220 Name: "*", 221 Scope: &management.WatchTopicSpecScope{ 222 Kind: "Environment", 223 Name: envName, 224 }, 225 }, 226 { 227 Group: "management", 228 Kind: management.ManagedApplicationGVK().Kind, 229 Name: "*", 230 Scope: &management.WatchTopicSpecScope{ 231 Kind: "Environment", 232 Name: envName, 233 }, 234 }, 235 { 236 Group: "management", 237 Kind: management.AccessRequestGVK().Kind, 238 Name: "*", 239 Scope: &management.WatchTopicSpecScope{ 240 Kind: "Environment", 241 Name: envName, 242 }, 243 }, 244 { 245 Group: "management", 246 Kind: management.CredentialGVK().Kind, 247 Name: "*", 248 Scope: &management.WatchTopicSpecScope{ 249 Kind: "Environment", 250 Name: envName, 251 }, 252 }, 253 }, 254 }, 255 } 256 257 var watchTopicNoMP = &management.WatchTopic{ 258 ResourceMeta: apiv1.ResourceMeta{}, 259 Owner: nil, 260 Spec: management.WatchTopicSpec{ 261 Description: "", 262 Filters: []management.WatchTopicSpecFilters{ 263 { 264 Group: "management", 265 Kind: management.APIServiceGVK().Kind, 266 Name: "*", 267 Scope: &management.WatchTopicSpecScope{ 268 Kind: "Environment", 269 Name: envName, 270 }, 271 }, 272 }, 273 }, 274 }