@@ -18,6 +18,7 @@ package cache_test
1818
1919import (
2020"context"
21+ "errors"
2122"fmt"
2223"reflect"
2324"sort"
@@ -117,6 +118,11 @@ func deletePod(pod client.Object) {
117118var _ = Describe ("Informer Cache" , func () {
118119CacheTest (cache .New , cache.Options {})
119120})
121+
122+ var _ = Describe ("Informer Cache with ReaderFailOnMissingInformer" , func () {
123+ CacheTestReaderFailOnMissingInformer (cache .New , cache.Options {ReaderFailOnMissingInformer : true })
124+ })
125+
120126var _ = Describe ("Multi-Namespace Informer Cache" , func () {
121127CacheTest (cache .New , cache.Options {
122128DefaultNamespaces : map [string ]cache.Config {
@@ -422,6 +428,85 @@ var _ = Describe("Cache with selectors", func() {
422428})
423429})
424430
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+
425510func CacheTest (createCacheFunc func (config * rest.Config , opts cache.Options ) (cache.Cache , error ), opts cache.Options ) {
426511Describe ("Cache test" , func () {
427512var (
0 commit comments