knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/apis/duck/enqueue_test.go (about)

     1  /*
     2  Copyright 2018 The Knative Authors
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package duck
    18  
    19  import (
    20  	"context"
    21  	"errors"
    22  	"testing"
    23  	"time"
    24  
    25  	"k8s.io/apimachinery/pkg/runtime/schema"
    26  	"k8s.io/client-go/tools/cache"
    27  )
    28  
    29  func TestEnqueueInformerFactory(t *testing.T) {
    30  	called := false
    31  	want := cache.ResourceEventHandlerFuncs{
    32  		AddFunc: func(obj interface{}) {
    33  			called = true
    34  		},
    35  	}
    36  	fsii := &fakeSharedIndexInformer{t: t}
    37  	fif := &FixedInformerFactory{inf: fsii}
    38  	eif := &EnqueueInformerFactory{
    39  		Delegate:     fif,
    40  		EventHandler: want,
    41  	}
    42  
    43  	gvr := schema.GroupVersionResource{
    44  		Group:    "testing.knative.dev",
    45  		Version:  "v3",
    46  		Resource: "caches",
    47  	}
    48  	inf, _, err := eif.Get(context.Background(), gvr)
    49  	if err != nil {
    50  		t.Fatal("Get() =", err)
    51  	}
    52  	if inf != fsii {
    53  		t.Fatalf("Get() = %v, wanted %v", inf, fsii)
    54  	}
    55  
    56  	got, ok := fsii.eventHandler.(cache.ResourceEventHandlerFuncs)
    57  	if !ok {
    58  		t.Errorf("eventHandler = %T, wanted %T", fsii.eventHandler, want)
    59  	}
    60  	if called {
    61  		t.Error("Want not called, got called")
    62  	}
    63  
    64  	got.AddFunc(nil)
    65  
    66  	if !called {
    67  		t.Error("Want called, got not called")
    68  	}
    69  
    70  	if got.UpdateFunc != nil {
    71  		t.Error("UpdateFunc = non-nil, wanted nil")
    72  	}
    73  
    74  	if got.DeleteFunc != nil {
    75  		t.Error("DeleteFunc = non-nil, wanted nil")
    76  	}
    77  }
    78  
    79  func TestEnqueueInformerFactoryWithFailure(t *testing.T) {
    80  	want := errors.New("expected error")
    81  	fif := &FixedInformerFactory{err: want}
    82  	eif := &EnqueueInformerFactory{
    83  		Delegate: fif,
    84  		EventHandler: cache.ResourceEventHandlerFuncs{
    85  			AddFunc: func(obj interface{}) {
    86  				t.Error("Unexpected call to AddFunc.")
    87  			},
    88  			UpdateFunc: func(old, new interface{}) {
    89  				t.Error("Unexpected call to UpdateFunc.")
    90  			},
    91  		},
    92  	}
    93  
    94  	gvr := schema.GroupVersionResource{
    95  		Group:    "testing.knative.dev",
    96  		Version:  "v3",
    97  		Resource: "caches",
    98  	}
    99  	inf, _, got := eif.Get(context.Background(), gvr)
   100  	if !errors.Is(got, want) {
   101  		t.Fatalf("Get() = %v, wanted %v", got, want)
   102  	}
   103  
   104  	if inf != nil {
   105  		t.Fatal("Get() = non nil, wanted nil")
   106  	}
   107  }
   108  
   109  type FixedInformerFactory struct {
   110  	inf    cache.SharedIndexInformer
   111  	lister cache.GenericLister
   112  	err    error
   113  }
   114  
   115  var _ InformerFactory = (*FixedInformerFactory)(nil)
   116  
   117  func (fif *FixedInformerFactory) Get(ctx context.Context, gvr schema.GroupVersionResource) (cache.SharedIndexInformer, cache.GenericLister, error) {
   118  	return fif.inf, fif.lister, fif.err
   119  }
   120  
   121  type fakeSharedIndexInformer struct {
   122  	t            *testing.T
   123  	eventHandler cache.ResourceEventHandler
   124  }
   125  
   126  var _ cache.SharedIndexInformer = (*fakeSharedIndexInformer)(nil)
   127  
   128  func (fsii *fakeSharedIndexInformer) AddEventHandler(handler cache.ResourceEventHandler) (cache.ResourceEventHandlerRegistration, error) {
   129  	fsii.eventHandler = handler
   130  	return nil, nil
   131  }
   132  
   133  func (fsii *fakeSharedIndexInformer) AddEventHandlerWithResyncPeriod(handler cache.ResourceEventHandler, resyncPeriod time.Duration) (cache.ResourceEventHandlerRegistration, error) {
   134  	fsii.t.Fatalf("NYI: AddEventHandlerWithResyncPeriod")
   135  	return nil, nil
   136  }
   137  
   138  func (fsii *fakeSharedIndexInformer) AddEventHandlerWithOptions(handler cache.ResourceEventHandler, options cache.HandlerOptions) (cache.ResourceEventHandlerRegistration, error) {
   139  	fsii.t.Fatalf("NYI: AddEventHandlerWithOptions")
   140  	return nil, nil
   141  }
   142  
   143  func (fsii *fakeSharedIndexInformer) GetStore() cache.Store {
   144  	fsii.t.Fatalf("NYI: GetStore")
   145  	return nil
   146  }
   147  
   148  func (fsii *fakeSharedIndexInformer) GetController() cache.Controller {
   149  	fsii.t.Fatalf("NYI: GetController")
   150  	return nil
   151  }
   152  
   153  func (fsii *fakeSharedIndexInformer) Run(stopCh <-chan struct{}) {
   154  	fsii.t.Fatalf("NYI: Run")
   155  }
   156  
   157  func (fsii *fakeSharedIndexInformer) RunWithContext(ctx context.Context) {
   158  	fsii.t.Fatalf("NYI: RunWithContext")
   159  }
   160  
   161  func (fsii *fakeSharedIndexInformer) HasSynced() bool {
   162  	fsii.t.Fatalf("NYI: HadSynced")
   163  	return false
   164  }
   165  
   166  func (fsii *fakeSharedIndexInformer) LastSyncResourceVersion() string {
   167  	fsii.t.Fatalf("NYI: LastSyncResourceVersion")
   168  	return ""
   169  }
   170  
   171  func (fsii *fakeSharedIndexInformer) AddIndexers(indexers cache.Indexers) error {
   172  	fsii.t.Fatalf("NYI: AddIndexers")
   173  	return nil
   174  }
   175  
   176  func (fsii *fakeSharedIndexInformer) GetIndexer() cache.Indexer {
   177  	fsii.t.Fatalf("NYI: GetIndexer")
   178  	return nil
   179  }
   180  
   181  func (fsii *fakeSharedIndexInformer) SetWatchErrorHandler(handler cache.WatchErrorHandler) error {
   182  	fsii.t.Fatalf("NYI: SetWatchErrorHandler")
   183  	return nil
   184  }
   185  
   186  func (fsii *fakeSharedIndexInformer) SetWatchErrorHandlerWithContext(handler cache.WatchErrorHandlerWithContext) error {
   187  	fsii.t.Fatalf("NYI: SetWatchErrorHandler")
   188  	return nil
   189  }
   190  
   191  func (fsii *fakeSharedIndexInformer) SetTransform(handler cache.TransformFunc) error {
   192  	fsii.t.Fatalf("NYI: SetTransform")
   193  	return nil
   194  }
   195  
   196  func (fsii *fakeSharedIndexInformer) IsStopped() bool {
   197  	fsii.t.Fatalf("NYI: IsStopped")
   198  	return false
   199  }
   200  
   201  func (fsii *fakeSharedIndexInformer) RemoveEventHandler(handler cache.ResourceEventHandlerRegistration) error {
   202  	fsii.t.Fatalf("NYI: RemoveEventHandler")
   203  	return nil
   204  }