...

Source file src/k8s.io/client-go/examples/fake-client/main_test.go

Documentation: k8s.io/client-go/examples/fake-client

     1  /*
     2  Copyright 2018 The Kubernetes 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 fakeclient
    18  
    19  import (
    20  	"context"
    21  	"testing"
    22  	"time"
    23  
    24  	v1 "k8s.io/api/core/v1"
    25  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    26  	"k8s.io/apimachinery/pkg/util/wait"
    27  	"k8s.io/apimachinery/pkg/watch"
    28  	"k8s.io/client-go/informers"
    29  	"k8s.io/client-go/kubernetes/fake"
    30  	clienttesting "k8s.io/client-go/testing"
    31  	"k8s.io/client-go/tools/cache"
    32  )
    33  
    34  // TestFakeClient demonstrates how to use a fake client with SharedInformerFactory in tests.
    35  func TestFakeClient(t *testing.T) {
    36  	ctx, cancel := context.WithCancel(context.Background())
    37  	defer cancel()
    38  
    39  	watcherStarted := make(chan struct{})
    40  	// Create the fake client.
    41  	client := fake.NewSimpleClientset()
    42  	// A catch-all watch reactor that allows us to inject the watcherStarted channel.
    43  	client.PrependWatchReactor("*", func(action clienttesting.Action) (handled bool, ret watch.Interface, err error) {
    44  		gvr := action.GetResource()
    45  		ns := action.GetNamespace()
    46  		watch, err := client.Tracker().Watch(gvr, ns)
    47  		if err != nil {
    48  			return false, nil, err
    49  		}
    50  		close(watcherStarted)
    51  		return true, watch, nil
    52  	})
    53  
    54  	// We will create an informer that writes added pods to a channel.
    55  	pods := make(chan *v1.Pod, 1)
    56  	informers := informers.NewSharedInformerFactory(client, 0)
    57  	podInformer := informers.Core().V1().Pods().Informer()
    58  	podInformer.AddEventHandler(&cache.ResourceEventHandlerFuncs{
    59  		AddFunc: func(obj interface{}) {
    60  			pod := obj.(*v1.Pod)
    61  			t.Logf("pod added: %s/%s", pod.Namespace, pod.Name)
    62  			pods <- pod
    63  		},
    64  	})
    65  
    66  	// Make sure informers are running.
    67  	informers.Start(ctx.Done())
    68  
    69  	// This is not required in tests, but it serves as a proof-of-concept by
    70  	// ensuring that the informer goroutine have warmed up and called List before
    71  	// we send any events to it.
    72  	cache.WaitForCacheSync(ctx.Done(), podInformer.HasSynced)
    73  
    74  	// The fake client doesn't support resource version. Any writes to the client
    75  	// after the informer's initial LIST and before the informer establishing the
    76  	// watcher will be missed by the informer. Therefore we wait until the watcher
    77  	// starts.
    78  	// Note that the fake client isn't designed to work with informer. It
    79  	// doesn't support resource version. It's encouraged to use a real client
    80  	// in an integration/E2E test if you need to test complex behavior with
    81  	// informer/controllers.
    82  	<-watcherStarted
    83  	// Inject an event into the fake client.
    84  	p := &v1.Pod{ObjectMeta: metav1.ObjectMeta{Name: "my-pod"}}
    85  	_, err := client.CoreV1().Pods("test-ns").Create(context.TODO(), p, metav1.CreateOptions{})
    86  	if err != nil {
    87  		t.Fatalf("error injecting pod add: %v", err)
    88  	}
    89  
    90  	select {
    91  	case pod := <-pods:
    92  		t.Logf("Got pod from channel: %s/%s", pod.Namespace, pod.Name)
    93  	case <-time.After(wait.ForeverTestTimeout):
    94  		t.Error("Informer did not get the added pod")
    95  	}
    96  }
    97  

View as plain text