knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/injection/clients/dynamicclient/fake/fake_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 fake
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  
    23  	"k8s.io/client-go/rest"
    24  
    25  	"knative.dev/pkg/controller"
    26  	"knative.dev/pkg/injection"
    27  )
    28  
    29  func TestGetPanic(t *testing.T) {
    30  	ctx := context.Background()
    31  
    32  	defer func() {
    33  		if r := recover(); r == nil {
    34  			t.Error("Get() should have panicked")
    35  		}
    36  	}()
    37  
    38  	// Get before registration
    39  	if empty := Get(ctx); empty != nil {
    40  		t.Error("Unexpected informer:", empty)
    41  	}
    42  }
    43  
    44  func TestRegistration(t *testing.T) {
    45  	ctx := context.Background()
    46  
    47  	// Check how many informers have registered.
    48  	inffs := injection.Fake.GetClients()
    49  	if want, got := 1, len(inffs); want != got {
    50  		t.Errorf("GetClients() = %d, wanted %d", want, got)
    51  	}
    52  
    53  	// Setup the informers.
    54  	var infs []controller.Informer
    55  	ctx, infs = injection.Fake.SetupInformers(ctx, &rest.Config{})
    56  
    57  	// We should see that a single informer was set up.
    58  	if want, got := 0, len(infs); want != got {
    59  		t.Errorf("SetupInformers() = %d, wanted %d", want, got)
    60  	}
    61  
    62  	// Get our informer from the context.
    63  	if inf := Get(ctx); inf == nil {
    64  		t.Error("Get() = nil, wanted non-nil")
    65  	}
    66  }