@@ -100,6 +100,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
100
100
knownPod2 client.Object
101
101
knownPod3 client.Object
102
102
knownPod4 client.Object
103
+ knownPod5 client.Object
104
+ knownPod6 client.Object
103
105
)
104
106
105
107
BeforeEach (func () {
@@ -122,14 +124,20 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
122
124
knownPod2 = createPod ("test-pod-2" , testNamespaceTwo , kcorev1 .RestartPolicyAlways )
123
125
knownPod3 = createPodWithLabels ("test-pod-3" , testNamespaceTwo , kcorev1 .RestartPolicyOnFailure , map [string ]string {"common-label" : "common" })
124
126
knownPod4 = createPodWithLabels ("test-pod-4" , testNamespaceThree , kcorev1 .RestartPolicyNever , map [string ]string {"common-label" : "common" })
127
+ knownPod5 = createPod ("test-pod-5" , testNamespaceOne , kcorev1 .RestartPolicyNever )
128
+ knownPod6 = createPod ("test-pod-6" , testNamespaceTwo , kcorev1 .RestartPolicyAlways )
129
+
125
130
podGVK := schema.GroupVersionKind {
126
131
Kind : "Pod" ,
127
132
Version : "v1" ,
128
133
}
134
+
129
135
knownPod1 .GetObjectKind ().SetGroupVersionKind (podGVK )
130
136
knownPod2 .GetObjectKind ().SetGroupVersionKind (podGVK )
131
137
knownPod3 .GetObjectKind ().SetGroupVersionKind (podGVK )
132
138
knownPod4 .GetObjectKind ().SetGroupVersionKind (podGVK )
139
+ knownPod5 .GetObjectKind ().SetGroupVersionKind (podGVK )
140
+ knownPod6 .GetObjectKind ().SetGroupVersionKind (podGVK )
133
141
134
142
By ("creating the informer cache" )
135
143
informerCache , err = createCacheFunc (cfg , cache.Options {})
@@ -149,6 +157,8 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
149
157
deletePod (knownPod2 )
150
158
deletePod (knownPod3 )
151
159
deletePod (knownPod4 )
160
+ deletePod (knownPod5 )
161
+ deletePod (knownPod6 )
152
162
153
163
informerCacheCancel ()
154
164
})
@@ -226,7 +236,7 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
226
236
227
237
By ("verifying that the returned pods have GVK populated" )
228
238
Expect (out .Items ).NotTo (BeEmpty ())
229
- Expect (out .Items ).Should (SatisfyAny (HaveLen (3 ), HaveLen (4 )))
239
+ Expect (out .Items ).Should (SatisfyAny (HaveLen (5 ), HaveLen (6 )))
230
240
for _ , p := range out .Items {
231
241
Expect (p .GroupVersionKind ()).To (Equal (kcorev1 .SchemeGroupVersion .WithKind ("Pod" )))
232
242
}
@@ -240,9 +250,10 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
240
250
241
251
By ("verifying that the returned pods are in test-namespace-1" )
242
252
Expect (listObj .Items ).NotTo (BeEmpty ())
243
- Expect (listObj .Items ).Should (HaveLen (1 ))
244
- actual := listObj .Items [0 ]
245
- Expect (actual .Namespace ).To (Equal (testNamespaceOne ))
253
+ Expect (listObj .Items ).Should (HaveLen (2 ))
254
+ for _ , item := range listObj .Items {
255
+ Expect (item .Namespace ).To (Equal (testNamespaceOne ))
256
+ }
246
257
})
247
258
248
259
It ("should deep copy the object unless told otherwise" , func () {
@@ -295,7 +306,15 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
295
306
Expect (errors .IsTimeout (err )).To (BeTrue ())
296
307
})
297
308
309
+ It ("should set the Limit option and limit number of objects to Limit when List is called" , func () {
310
+ opts := & client.ListOptions {Limit : int64 (3 )}
311
+ By ("verifying that only Limit (3) number of objects are retrieved from the cache" )
312
+ listObj := & kcorev1.PodList {}
313
+ Expect (informerCache .List (context .Background (), listObj , opts )).To (Succeed ())
314
+ Expect (listObj .Items ).Should (HaveLen (3 ))
315
+ })
298
316
})
317
+
299
318
Context ("with unstructured objects" , func () {
300
319
It ("should be able to list objects that haven't been watched previously" , func () {
301
320
By ("listing all services in the cluster" )
@@ -396,9 +415,10 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
396
415
397
416
By ("verifying that the returned pods are in test-namespace-1" )
398
417
Expect (listObj .Items ).NotTo (BeEmpty ())
399
- Expect (listObj .Items ).Should (HaveLen (1 ))
400
- actual := listObj .Items [0 ]
401
- Expect (actual .GetNamespace ()).To (Equal (testNamespaceOne ))
418
+ Expect (listObj .Items ).Should (HaveLen (2 ))
419
+ for _ , item := range listObj .Items {
420
+ Expect (item .GetNamespace ()).To (Equal (testNamespaceOne ))
421
+ }
402
422
})
403
423
404
424
It ("should be able to restrict cache to a namespace" , func () {
@@ -424,9 +444,10 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
424
444
425
445
By ("verifying the returned pod is from the watched namespace" )
426
446
Expect (out .Items ).NotTo (BeEmpty ())
427
- Expect (out .Items ).Should (HaveLen (1 ))
428
- Expect (out .Items [0 ].GetNamespace ()).To (Equal (testNamespaceOne ))
429
-
447
+ Expect (out .Items ).Should (HaveLen (2 ))
448
+ for _ , item := range out .Items {
449
+ Expect (item .GetNamespace ()).To (Equal (testNamespaceOne ))
450
+ }
430
451
By ("listing all nodes - should still be able to list a cluster-scoped resource" )
431
452
nodeList := & unstructured.UnstructuredList {}
432
453
nodeList .SetGroupVersionKind (schema.GroupVersionKind {
@@ -639,9 +660,10 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
639
660
640
661
By ("verifying that the returned pods are in test-namespace-1" )
641
662
Expect (listObj .Items ).NotTo (BeEmpty ())
642
- Expect (listObj .Items ).Should (HaveLen (1 ))
643
- actual := listObj .Items [0 ]
644
- Expect (actual .GetNamespace ()).To (Equal (testNamespaceOne ))
663
+ Expect (listObj .Items ).Should (HaveLen (2 ))
664
+ for _ , item := range listObj .Items {
665
+ Expect (item .Namespace ).To (Equal (testNamespaceOne ))
666
+ }
645
667
})
646
668
647
669
It ("should be able to restrict cache to a namespace" , func () {
@@ -667,9 +689,10 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
667
689
668
690
By ("verifying the returned pod is from the watched namespace" )
669
691
Expect (out .Items ).NotTo (BeEmpty ())
670
- Expect (out .Items ).Should (HaveLen (1 ))
671
- Expect (out .Items [0 ].GetNamespace ()).To (Equal (testNamespaceOne ))
672
-
692
+ Expect (out .Items ).Should (HaveLen (2 ))
693
+ for _ , item := range out .Items {
694
+ Expect (item .Namespace ).To (Equal (testNamespaceOne ))
695
+ }
673
696
By ("listing all nodes - should still be able to list a cluster-scoped resource" )
674
697
nodeList := & kmetav1.PartialObjectMetadataList {}
675
698
nodeList .SetGroupVersionKind (schema.GroupVersionKind {
@@ -828,25 +851,25 @@ func CacheTest(createCacheFunc func(config *rest.Config, opts cache.Options) (ca
828
851
Entry ("when selectors are empty it has to inform about all the pods" , selectorsTestCase {
829
852
fieldSelectors : map [string ]string {},
830
853
labelSelectors : map [string ]string {},
831
- expectedPods : []string {"test-pod-1" , "test-pod-2" , "test-pod-3" , "test-pod-4" },
854
+ expectedPods : []string {"test-pod-1" , "test-pod-2" , "test-pod-3" , "test-pod-4" , "test-pod-5" , "test-pod-6" },
832
855
}),
833
856
Entry ("when field matches one pod it has to inform about it" , selectorsTestCase {
834
857
fieldSelectors : map [string ]string {"metadata.name" : "test-pod-2" },
835
858
expectedPods : []string {"test-pod-2" },
836
859
}),
837
- Entry ("when field matches multiple pods it has to infor about all of them" , selectorsTestCase {
860
+ Entry ("when field matches multiple pods it has to inform about all of them" , selectorsTestCase {
838
861
fieldSelectors : map [string ]string {"metadata.namespace" : testNamespaceTwo },
839
- expectedPods : []string {"test-pod-2" , "test-pod-3" },
862
+ expectedPods : []string {"test-pod-2" , "test-pod-3" , "test-pod-6" },
840
863
}),
841
864
Entry ("when label matches one pod it has to inform about it" , selectorsTestCase {
842
865
labelSelectors : map [string ]string {"test-label" : "test-pod-4" },
843
866
expectedPods : []string {"test-pod-4" },
844
867
}),
845
- Entry ("when label matches multiple pods it has to infor about all of them" , selectorsTestCase {
868
+ Entry ("when label matches multiple pods it has to inform about all of them" , selectorsTestCase {
846
869
labelSelectors : map [string ]string {"common-label" : "common" },
847
870
expectedPods : []string {"test-pod-3" , "test-pod-4" },
848
871
}),
849
- Entry ("when label and field matches one pod it has to infor about about it" , selectorsTestCase {
872
+ Entry ("when label and field matches one pod it has to inform about about it" , selectorsTestCase {
850
873
labelSelectors : map [string ]string {"common-label" : "common" },
851
874
fieldSelectors : map [string ]string {"metadata.namespace" : testNamespaceTwo },
852
875
expectedPods : []string {"test-pod-3" },
0 commit comments