knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/injection/informers_test.go (about)

     1  /*
     2  Copyright 2019 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 injection
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"k8s.io/client-go/rest"
    24  
    25  	"knative.dev/pkg/controller"
    26  )
    27  
    28  type fakeInformer struct{}
    29  
    30  // HasSynced implements controller.Informer
    31  func (*fakeInformer) HasSynced() bool {
    32  	return false
    33  }
    34  
    35  // Run implements controller.Informer
    36  func (*fakeInformer) Run(<-chan struct{}) {}
    37  
    38  var _ controller.Informer = (*fakeInformer)(nil)
    39  
    40  func injectFooInformer(ctx context.Context) (context.Context, controller.Informer) {
    41  	return ctx, nil
    42  }
    43  
    44  func injectBarInformer(ctx context.Context) (context.Context, controller.Informer) {
    45  	return ctx, nil
    46  }
    47  
    48  func injectFooFilteredInformers(ctx context.Context) (context.Context, []controller.Informer) {
    49  	return ctx, []controller.Informer{nil, nil}
    50  }
    51  
    52  func TestRegisterInformersAndSetup(t *testing.T) {
    53  	i := &impl{}
    54  
    55  	if want, got := 0, len(i.GetInformers()); got != want {
    56  		t.Errorf("GetInformerFactories() = %d, wanted %d", want, got)
    57  	}
    58  
    59  	i.RegisterClient(injectFoo)
    60  	i.RegisterClient(injectBar)
    61  
    62  	i.RegisterInformerFactory(injectFooFactory)
    63  	i.RegisterInformerFactory(injectBarFactory)
    64  
    65  	i.RegisterInformer(injectFooInformer)
    66  	i.RegisterInformer(injectBarInformer)
    67  
    68  	i.RegisterFilteredInformers(injectFooFilteredInformers)
    69  
    70  	_, infs := i.SetupInformers(context.Background(), &rest.Config{})
    71  	// 2 nil informers injectFooInformer and injectBarInformer are registered by RegisterInformer,
    72  	// and another 2 nil informers are registered by injectFooFilteredInformers
    73  	if want, got := 4, len(infs); got != want {
    74  		t.Errorf("SetupInformers() = %d, wanted %d", want, got)
    75  	}
    76  }