@@ -10,6 +10,7 @@ import (
1010 apiv1 "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/api/v1"
1111 management "github.com/Axway/agent-sdk/pkg/apic/apiserver/models/management/v1alpha1"
1212 "github.com/stretchr/testify/assert"
13+ "github.com/stretchr/testify/require"
1314)
1415
1516// mockCacheGetter implements cacheGetter for tests
@@ -123,9 +124,10 @@ func TestCacheValidator_Execute(t *testing.T) {
123124 }
124125
125126 type testCase struct {
126- setup func (* mockResourceClient , * mockCacheGetter )
127- watchTopic * management.WatchTopic
128- expectErr error
127+ setup func (* mockResourceClient , * mockCacheGetter )
128+ watchTopic * management.WatchTopic
129+ expectErr error
130+ expectedFailedKinds []string
129131 }
130132
131133 tests := map [string ]testCase {
@@ -137,7 +139,8 @@ func TestCacheValidator_Execute(t *testing.T) {
137139 },
138140 },
139141 },
140- expectErr : nil ,
142+ expectErr : nil ,
143+ expectedFailedKinds : nil ,
141144 },
142145 "in sync" : {
143146 setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
@@ -146,8 +149,9 @@ func TestCacheValidator_Execute(t *testing.T) {
146149 agentcache .ResourceCacheKey (svcGVK .Kind , scopeName , "svc1" ): modTime ,
147150 })
148151 },
149- watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
150- expectErr : nil ,
152+ watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
153+ expectErr : nil ,
154+ expectedFailedKinds : nil ,
151155 },
152156 "count mismatch" : {
153157 setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
@@ -156,8 +160,9 @@ func TestCacheValidator_Execute(t *testing.T) {
156160 agentcache .ResourceCacheKey (svcGVK .Kind , scopeName , "svc1" ): modTime ,
157161 })
158162 },
159- watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
160- expectErr : errCacheOutOfSync ,
163+ watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
164+ expectErr : errCacheOutOfSync ,
165+ expectedFailedKinds : []string {svcGVK .Kind },
161166 },
162167 "resource missing from cache" : {
163168 setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
@@ -166,8 +171,9 @@ func TestCacheValidator_Execute(t *testing.T) {
166171 agentcache .ResourceCacheKey (svcGVK .Kind , scopeName , "svc-other" ): modTime ,
167172 })
168173 },
169- watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
170- expectErr : errCacheOutOfSync ,
174+ watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
175+ expectErr : errCacheOutOfSync ,
176+ expectedFailedKinds : []string {svcGVK .Kind },
171177 },
172178 "modify time mismatch" : {
173179 setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
@@ -177,8 +183,9 @@ func TestCacheValidator_Execute(t *testing.T) {
177183 agentcache .ResourceCacheKey (svcGVK .Kind , scopeName , "svc1" ): modTime ,
178184 })
179185 },
180- watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
181- expectErr : errCacheOutOfSync ,
186+ watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
187+ expectErr : errCacheOutOfSync ,
188+ expectedFailedKinds : []string {svcGVK .Kind },
182189 },
183190 "zero timestamps are ignored" : {
184191 setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
@@ -188,8 +195,9 @@ func TestCacheValidator_Execute(t *testing.T) {
188195 agentcache .ResourceCacheKey (svcGVK .Kind , scopeName , "svc1" ): time .Date (2026 , 1 , 1 , 0 , 0 , 0 , 0 , time .UTC ),
189196 })
190197 },
191- watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
192- expectErr : nil ,
198+ watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
199+ expectErr : nil ,
200+ expectedFailedKinds : nil ,
193201 },
194202 "extra resource in cache" : {
195203 setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
@@ -199,15 +207,17 @@ func TestCacheValidator_Execute(t *testing.T) {
199207 agentcache .ResourceCacheKey (svcGVK .Kind , scopeName , "svc3" ): modTime ,
200208 })
201209 },
202- watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
203- expectErr : errCacheOutOfSync ,
210+ watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
211+ expectErr : errCacheOutOfSync ,
212+ expectedFailedKinds : []string {svcGVK .Kind },
204213 },
205214 "fetch error" : {
206215 setup : func (client * mockResourceClient , _ * mockCacheGetter ) {
207216 client .err = fmt .Errorf ("network error" )
208217 },
209- watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
210- expectErr : errCacheOutOfSync ,
218+ watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
219+ expectErr : errCacheOutOfSync ,
220+ expectedFailedKinds : []string {svcGVK .Kind },
211221 },
212222 "APIServiceInstances from multiple scopes are each validated independently" : {
213223 setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
@@ -228,7 +238,8 @@ func TestCacheValidator_Execute(t *testing.T) {
228238 },
229239 },
230240 },
231- expectErr : nil ,
241+ expectErr : nil ,
242+ expectedFailedKinds : nil ,
232243 },
233244 "multiple kinds in sync" : {
234245 setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
@@ -249,7 +260,8 @@ func TestCacheValidator_Execute(t *testing.T) {
249260 },
250261 },
251262 },
252- expectErr : nil ,
263+ expectErr : nil ,
264+ expectedFailedKinds : nil ,
253265 },
254266 "second kind out of sync" : {
255267 setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
@@ -268,7 +280,25 @@ func TestCacheValidator_Execute(t *testing.T) {
268280 },
269281 },
270282 },
271- expectErr : errCacheOutOfSync ,
283+ expectErr : errCacheOutOfSync ,
284+ expectedFailedKinds : []string {instGVK .Kind },
285+ },
286+ "both kinds out of sync" : {
287+ setup : func (client * mockResourceClient , cacheMan * mockCacheGetter ) {
288+ client .resources [svc1 .GetKindLink ()] = []* apiv1.ResourceInstance {svc1 }
289+ client .resources [inst1 .GetKindLink ()] = []* apiv1.ResourceInstance {inst1 }
290+ // both empty in cache
291+ },
292+ watchTopic : & management.WatchTopic {
293+ Spec : management.WatchTopicSpec {
294+ Filters : []management.WatchTopicSpecFilters {
295+ {Group : svcGVK .Group , Kind : svcGVK .Kind , Name : "*" , Scope : & management.WatchTopicSpecScope {Kind : "Environment" , Name : scopeName }},
296+ {Group : instGVK .Group , Kind : instGVK .Kind , Name : "*" , Scope : & management.WatchTopicSpecScope {Kind : "Environment" , Name : scopeName }},
297+ },
298+ },
299+ },
300+ expectErr : errCacheOutOfSync ,
301+ expectedFailedKinds : []string {svcGVK .Kind , instGVK .Kind },
272302 },
273303 "empty server and cache" : {
274304 setup : func (client * mockResourceClient , _ * mockCacheGetter ) {
@@ -285,15 +315,17 @@ func TestCacheValidator_Execute(t *testing.T) {
285315 }
286316 client .resources [ri .GetKindLink ()] = []* apiv1.ResourceInstance {}
287317 },
288- watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
289- expectErr : nil ,
318+ watchTopic : singleScopeWatchTopic (svcGVK , scopeName ),
319+ expectErr : nil ,
320+ expectedFailedKinds : nil ,
290321 },
291322 "ComplianceRuntimeResult out of sync is detected" : {
292323 setup : func (client * mockResourceClient , _ * mockCacheGetter ) {
293324 client .resources [crr1 .GetKindLink ()] = []* apiv1.ResourceInstance {crr1 }
294325 },
295- watchTopic : singleScopeWatchTopic (crrGVK , scopeName ),
296- expectErr : errCacheOutOfSync ,
326+ watchTopic : singleScopeWatchTopic (crrGVK , scopeName ),
327+ expectErr : errCacheOutOfSync ,
328+ expectedFailedKinds : []string {crrGVK .Kind },
297329 },
298330 }
299331
@@ -305,8 +337,19 @@ func TestCacheValidator_Execute(t *testing.T) {
305337 tc .setup (client , cacheMan )
306338 }
307339 cv := newCacheValidator (client , tc .watchTopic , cacheMan )
308- err := cv .Execute ()
340+ failedFilters , err := cv .Execute ()
309341 assert .Equal (t , tc .expectErr , err )
342+
343+ if len (tc .expectedFailedKinds ) == 0 {
344+ assert .Empty (t , failedFilters )
345+ } else {
346+ require .Len (t , failedFilters , len (tc .expectedFailedKinds ))
347+ actualKinds := make ([]string , len (failedFilters ))
348+ for i , f := range failedFilters {
349+ actualKinds [i ] = f .Kind
350+ }
351+ assert .ElementsMatch (t , tc .expectedFailedKinds , actualKinds )
352+ }
310353 })
311354 }
312355}
0 commit comments