sigs.k8s.io/controller-runtime@v0.18.2/pkg/cache/informer_cache_unit_test.go (about) 1 /* 2 Copyright 2021 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 cache 18 19 import ( 20 . "github.com/onsi/ginkgo/v2" 21 . "github.com/onsi/gomega" 22 23 corev1 "k8s.io/api/core/v1" 24 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 25 "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" 26 "k8s.io/apimachinery/pkg/runtime" 27 "k8s.io/apimachinery/pkg/runtime/schema" 28 "k8s.io/client-go/kubernetes/scheme" 29 30 "sigs.k8s.io/controller-runtime/pkg/cache/internal" 31 "sigs.k8s.io/controller-runtime/pkg/controller/controllertest" 32 crscheme "sigs.k8s.io/controller-runtime/pkg/scheme" 33 ) 34 35 const ( 36 itemPointerSliceTypeGroupName = "jakob.fabian" 37 itemPointerSliceTypeVersion = "v1" 38 ) 39 40 var _ = Describe("ip.objectTypeForListObject", func() { 41 ip := &informerCache{ 42 scheme: scheme.Scheme, 43 Informers: &internal.Informers{}, 44 } 45 46 It("should find the object type for unstructured lists", func() { 47 unstructuredList := &unstructured.UnstructuredList{} 48 unstructuredList.SetAPIVersion("v1") 49 unstructuredList.SetKind("PodList") 50 51 gvk, obj, err := ip.objectTypeForListObject(unstructuredList) 52 Expect(err).ToNot(HaveOccurred()) 53 Expect(gvk.Group).To(Equal("")) 54 Expect(gvk.Version).To(Equal("v1")) 55 Expect(gvk.Kind).To(Equal("Pod")) 56 referenceUnstructured := &unstructured.Unstructured{} 57 referenceUnstructured.SetGroupVersionKind(*gvk) 58 Expect(obj).To(Equal(referenceUnstructured)) 59 }) 60 61 It("should find the object type for partial object metadata lists", func() { 62 partialList := &metav1.PartialObjectMetadataList{} 63 partialList.APIVersion = ("v1") 64 partialList.Kind = "PodList" 65 66 gvk, obj, err := ip.objectTypeForListObject(partialList) 67 Expect(err).ToNot(HaveOccurred()) 68 Expect(gvk.Group).To(Equal("")) 69 Expect(gvk.Version).To(Equal("v1")) 70 Expect(gvk.Kind).To(Equal("Pod")) 71 referencePartial := &metav1.PartialObjectMetadata{} 72 referencePartial.SetGroupVersionKind(*gvk) 73 Expect(obj).To(Equal(referencePartial)) 74 }) 75 76 It("should find the object type of a list with a slice of literals items field", func() { 77 gvk, obj, err := ip.objectTypeForListObject(&corev1.PodList{}) 78 Expect(err).ToNot(HaveOccurred()) 79 Expect(gvk.Group).To(Equal("")) 80 Expect(gvk.Version).To(Equal("v1")) 81 Expect(gvk.Kind).To(Equal("Pod")) 82 referencePod := &corev1.Pod{} 83 Expect(obj).To(Equal(referencePod)) 84 }) 85 86 It("should find the object type of a list with a slice of pointers items field", func() { 87 By("registering the type", func() { 88 ip.scheme = runtime.NewScheme() 89 err := (&crscheme.Builder{ 90 GroupVersion: schema.GroupVersion{Group: itemPointerSliceTypeGroupName, Version: itemPointerSliceTypeVersion}, 91 }). 92 Register( 93 &controllertest.UnconventionalListType{}, 94 &controllertest.UnconventionalListTypeList{}, 95 ).AddToScheme(ip.scheme) 96 Expect(err).ToNot(HaveOccurred()) 97 }) 98 99 By("calling objectTypeForListObject", func() { 100 gvk, obj, err := ip.objectTypeForListObject(&controllertest.UnconventionalListTypeList{}) 101 Expect(err).ToNot(HaveOccurred()) 102 Expect(gvk.Group).To(Equal(itemPointerSliceTypeGroupName)) 103 Expect(gvk.Version).To(Equal(itemPointerSliceTypeVersion)) 104 Expect(gvk.Kind).To(Equal("UnconventionalListType")) 105 referenceObject := &controllertest.UnconventionalListType{} 106 Expect(obj).To(Equal(referenceObject)) 107 }) 108 }) 109 })