@@ -18,6 +18,7 @@ package cache_test
18
18
19
19
import (
20
20
"context"
21
+ "errors"
21
22
"fmt"
22
23
"reflect"
23
24
"sort"
@@ -117,6 +118,11 @@ func deletePod(pod client.Object) {
117
118
var _ = Describe ("Informer Cache" , func () {
118
119
CacheTest (cache .New , cache.Options {})
119
120
})
121
+
122
+ var _ = Describe ("Informer Cache with ReaderFailOnMissingInformer" , func () {
123
+ CacheTestReaderFailOnMissingInformer (cache .New , cache.Options {ReaderFailOnMissingInformer : true })
124
+ })
125
+
120
126
var _ = Describe ("Multi-Namespace Informer Cache" , func () {
121
127
CacheTest (cache .New , cache.Options {
122
128
DefaultNamespaces : map [string ]cache.Config {
@@ -422,6 +428,85 @@ var _ = Describe("Cache with selectors", func() {
422
428
})
423
429
})
424
430
431
+ func CacheTestReaderFailOnMissingInformer (createCacheFunc func (config * rest.Config , opts cache.Options ) (cache.Cache , error ), opts cache.Options ) {
432
+ Describe ("Cache test with ReaderFailOnMissingInformer = true" , func () {
433
+ var (
434
+ informerCache cache.Cache
435
+ informerCacheCtx context.Context
436
+ informerCacheCancel context.CancelFunc
437
+ errNotCached * cache.ErrResourceNotCached
438
+ )
439
+
440
+ BeforeEach (func () {
441
+ informerCacheCtx , informerCacheCancel = context .WithCancel (context .Background ())
442
+ Expect (cfg ).NotTo (BeNil ())
443
+
444
+ By ("creating the informer cache" )
445
+ var err error
446
+ informerCache , err = createCacheFunc (cfg , opts )
447
+ Expect (err ).NotTo (HaveOccurred ())
448
+ By ("running the cache and waiting for it to sync" )
449
+ // pass as an arg so that we don't race between close and re-assign
450
+ go func (ctx context.Context ) {
451
+ defer GinkgoRecover ()
452
+ Expect (informerCache .Start (ctx )).To (Succeed ())
453
+ }(informerCacheCtx )
454
+ Expect (informerCache .WaitForCacheSync (informerCacheCtx )).To (BeTrue ())
455
+ })
456
+
457
+ AfterEach (func () {
458
+ informerCacheCancel ()
459
+ })
460
+
461
+ Describe ("as a Reader" , func () {
462
+ Context ("with structured objects" , func () {
463
+ It ("should not be able to list objects that haven't been watched previously" , func () {
464
+ By ("listing all services in the cluster" )
465
+ listObj := & corev1.ServiceList {}
466
+ Expect (errors .As (informerCache .List (context .Background (), listObj ), & errNotCached )).To (BeTrue ())
467
+ })
468
+
469
+ It ("should not be able to get objects that haven't been watched previously" , func () {
470
+ By ("getting the Kubernetes service" )
471
+ svc := & corev1.Service {}
472
+ svcKey := client.ObjectKey {Namespace : "default" , Name : "kubernetes" }
473
+ Expect (errors .As (informerCache .Get (context .Background (), svcKey , svc ), & errNotCached )).To (BeTrue ())
474
+ })
475
+
476
+ It ("should be able to list objects that are configured to be watched" , func () {
477
+ By ("indicating that we need to watch services" )
478
+ _ , err := informerCache .GetInformer (context .Background (), & corev1.Service {})
479
+ Expect (err ).ToNot (HaveOccurred ())
480
+
481
+ By ("listing all services in the cluster" )
482
+ svcList := & corev1.ServiceList {}
483
+ Expect (informerCache .List (context .Background (), svcList )).To (Succeed ())
484
+
485
+ By ("verifying that the returned service looks reasonable" )
486
+ Expect (svcList .Items ).To (HaveLen (1 ))
487
+ Expect (svcList .Items [0 ].Name ).To (Equal ("kubernetes" ))
488
+ Expect (svcList .Items [0 ].Namespace ).To (Equal ("default" ))
489
+ })
490
+
491
+ It ("should be able to get objects that are configured to be watched" , func () {
492
+ By ("indicating that we need to watch services" )
493
+ _ , err := informerCache .GetInformer (context .Background (), & corev1.Service {})
494
+ Expect (err ).ToNot (HaveOccurred ())
495
+
496
+ By ("getting the Kubernetes service" )
497
+ svc := & corev1.Service {}
498
+ svcKey := client.ObjectKey {Namespace : "default" , Name : "kubernetes" }
499
+ Expect (informerCache .Get (context .Background (), svcKey , svc )).To (Succeed ())
500
+
501
+ By ("verifying that the returned service looks reasonable" )
502
+ Expect (svc .Name ).To (Equal ("kubernetes" ))
503
+ Expect (svc .Namespace ).To (Equal ("default" ))
504
+ })
505
+ })
506
+ })
507
+ })
508
+ }
509
+
425
510
func CacheTest (createCacheFunc func (config * rest.Config , opts cache.Options ) (cache.Cache , error ), opts cache.Options ) {
426
511
Describe ("Cache test" , func () {
427
512
var (
0 commit comments