...

Source file src/sigs.k8s.io/controller-runtime/pkg/cache/internal/informers_test.go

Documentation: sigs.k8s.io/controller-runtime/pkg/cache/internal

     1  /*
     2  Copyright 2022 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 internal
    18  
    19  import (
    20  	"fmt"
    21  
    22  	. "github.com/onsi/ginkgo/v2"
    23  	. "github.com/onsi/gomega"
    24  	metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
    25  	"k8s.io/apimachinery/pkg/runtime"
    26  	"k8s.io/apimachinery/pkg/runtime/schema"
    27  	"k8s.io/apimachinery/pkg/watch"
    28  )
    29  
    30  // Test that gvkFixupWatcher behaves like watch.FakeWatcher
    31  // and that it overrides the GVK.
    32  // These tests are adapted from the watch.FakeWatcher tests in:
    33  // https://github.com/kubernetes/kubernetes/blob/adbda068c1808fcc8a64a94269e0766b5c46ec41/staging/src/k8s.io/apimachinery/pkg/watch/watch_test.go#L33-L78
    34  var _ = Describe("gvkFixupWatcher", func() {
    35  	It("behaves like watch.FakeWatcher", func() {
    36  		newTestType := func(name string) runtime.Object {
    37  			return &metav1.PartialObjectMetadata{
    38  				ObjectMeta: metav1.ObjectMeta{
    39  					Name: name,
    40  				},
    41  			}
    42  		}
    43  
    44  		f := watch.NewFake()
    45  		// This is the GVK which we expect the wrapper to set on all the events
    46  		expectedGVK := schema.GroupVersionKind{
    47  			Group:   "testgroup",
    48  			Version: "v1test2",
    49  			Kind:    "TestKind",
    50  		}
    51  		gvkfw := newGVKFixupWatcher(expectedGVK, f)
    52  
    53  		table := []struct {
    54  			t watch.EventType
    55  			s runtime.Object
    56  		}{
    57  			{watch.Added, newTestType("foo")},
    58  			{watch.Modified, newTestType("qux")},
    59  			{watch.Modified, newTestType("bar")},
    60  			{watch.Deleted, newTestType("bar")},
    61  			{watch.Error, newTestType("error: blah")},
    62  		}
    63  
    64  		consumer := func(w watch.Interface) {
    65  			for _, expect := range table {
    66  				By(fmt.Sprintf("Fixing up watch.EventType: %v and passing it on", expect.t))
    67  				got, ok := <-w.ResultChan()
    68  				Expect(ok).To(BeTrue(), "closed early")
    69  				Expect(expect.t).To(Equal(got.Type), "unexpected Event.Type or out-of-order Event")
    70  				Expect(got.Object).To(BeAssignableToTypeOf(&metav1.PartialObjectMetadata{}), "unexpected Event.Object type")
    71  				a := got.Object.(*metav1.PartialObjectMetadata)
    72  				Expect(got.Object.GetObjectKind().GroupVersionKind()).To(Equal(expectedGVK), "GVK was not fixed up")
    73  				expected := expect.s.DeepCopyObject()
    74  				expected.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{})
    75  				actual := a.DeepCopyObject()
    76  				actual.GetObjectKind().SetGroupVersionKind(schema.GroupVersionKind{})
    77  				Expect(actual).To(Equal(expected), "unexpected change to the Object")
    78  			}
    79  			Eventually(w.ResultChan()).Should(BeClosed())
    80  		}
    81  
    82  		sender := func() {
    83  			f.Add(newTestType("foo"))
    84  			f.Action(watch.Modified, newTestType("qux"))
    85  			f.Modify(newTestType("bar"))
    86  			f.Delete(newTestType("bar"))
    87  			f.Error(newTestType("error: blah"))
    88  			f.Stop()
    89  		}
    90  
    91  		go sender()
    92  		consumer(gvkfw)
    93  	})
    94  })
    95  

View as plain text