|
| 1 | +package cache |
| 2 | + |
| 3 | +import ( |
| 4 | + . "github.com/onsi/ginkgo" |
| 5 | + . "github.com/onsi/gomega" |
| 6 | + |
| 7 | + corev1 "k8s.io/api/core/v1" |
| 8 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 9 | + "k8s.io/apimachinery/pkg/runtime/schema" |
| 10 | + "k8s.io/client-go/kubernetes/scheme" |
| 11 | + |
| 12 | + "sigs.k8s.io/controller-runtime/pkg/cache/internal" |
| 13 | + "sigs.k8s.io/controller-runtime/pkg/controller/controllertest" |
| 14 | + crscheme "sigs.k8s.io/controller-runtime/pkg/scheme" |
| 15 | +) |
| 16 | + |
| 17 | +const ( |
| 18 | + itemPointerSliceTypeGroupName = "jakob.fabian" |
| 19 | + itemPointerSliceTypeVersion = "v1" |
| 20 | +) |
| 21 | + |
| 22 | +var _ = Describe("ip.objectTypeForListObject", func() { |
| 23 | + ip := &informerCache{ |
| 24 | + InformersMap: &internal.InformersMap{Scheme: scheme.Scheme}, |
| 25 | + } |
| 26 | + |
| 27 | + It("should error on non-list types", func() { |
| 28 | + _, _, err := ip.objectTypeForListObject(&corev1.Pod{}) |
| 29 | + Expect(err).To(HaveOccurred()) |
| 30 | + Expect(err.Error()).To(Equal(`non-list type *v1.Pod (kind "/v1, Kind=Pod") passed as output`)) |
| 31 | + }) |
| 32 | + |
| 33 | + It("should find the object type for unstructured lists", func() { |
| 34 | + unstructuredList := &unstructured.UnstructuredList{} |
| 35 | + unstructuredList.SetAPIVersion("v1") |
| 36 | + unstructuredList.SetKind("PodList") |
| 37 | + |
| 38 | + gvk, obj, err := ip.objectTypeForListObject(unstructuredList) |
| 39 | + Expect(err).ToNot(HaveOccurred()) |
| 40 | + Expect(gvk.Group).To(Equal("")) |
| 41 | + Expect(gvk.Version).To(Equal("v1")) |
| 42 | + Expect(gvk.Kind).To(Equal("Pod")) |
| 43 | + referenceUnstructured := &unstructured.Unstructured{} |
| 44 | + referenceUnstructured.SetGroupVersionKind(*gvk) |
| 45 | + Expect(obj).To(Equal(referenceUnstructured)) |
| 46 | + |
| 47 | + }) |
| 48 | + |
| 49 | + It("should find the object type of a list with a slice of literals items field", func() { |
| 50 | + gvk, obj, err := ip.objectTypeForListObject(&corev1.PodList{}) |
| 51 | + Expect(err).ToNot(HaveOccurred()) |
| 52 | + Expect(gvk.Group).To(Equal("")) |
| 53 | + Expect(gvk.Version).To(Equal("v1")) |
| 54 | + Expect(gvk.Kind).To(Equal("Pod")) |
| 55 | + var referencePod *corev1.Pod |
| 56 | + Expect(obj).To(Equal(referencePod)) |
| 57 | + |
| 58 | + }) |
| 59 | + |
| 60 | + It("should find the object type of a list with a slice of pointers items field", func() { |
| 61 | + By("registering the type", func() { |
| 62 | + err := (&crscheme.Builder{ |
| 63 | + GroupVersion: schema.GroupVersion{Group: itemPointerSliceTypeGroupName, Version: itemPointerSliceTypeVersion}, |
| 64 | + }). |
| 65 | + Register( |
| 66 | + &controllertest.UnconventionalListType{}, |
| 67 | + &controllertest.UnconventionalListTypeList{}, |
| 68 | + ).AddToScheme(scheme.Scheme) |
| 69 | + Expect(err).To(BeNil()) |
| 70 | + }) |
| 71 | + |
| 72 | + By("calling objectTypeForListObject", func() { |
| 73 | + gvk, obj, err := ip.objectTypeForListObject(&controllertest.UnconventionalListTypeList{}) |
| 74 | + Expect(err).ToNot(HaveOccurred()) |
| 75 | + Expect(gvk.Group).To(Equal(itemPointerSliceTypeGroupName)) |
| 76 | + Expect(gvk.Version).To(Equal(itemPointerSliceTypeVersion)) |
| 77 | + Expect(gvk.Kind).To(Equal("UnconventionalListType")) |
| 78 | + var referenceObject *controllertest.UnconventionalListType |
| 79 | + Expect(obj).To(Equal(referenceObject)) |
| 80 | + }) |
| 81 | + }) |
| 82 | +}) |
0 commit comments