|
| 1 | +// Copyright 2021 OnMetal authors |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +package clientutils |
| 16 | + |
| 17 | +import ( |
| 18 | + "context" |
| 19 | + |
| 20 | + "github.com/golang/mock/gomock" |
| 21 | + mockclient "github.com/onmetal/controller-utils/mock/controller-runtime/client" |
| 22 | + . "github.com/onsi/ginkgo" |
| 23 | + . "github.com/onsi/gomega" |
| 24 | + corev1 "k8s.io/api/core/v1" |
| 25 | + metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 26 | + "k8s.io/apimachinery/pkg/apis/meta/v1/unstructured" |
| 27 | + "k8s.io/client-go/kubernetes/scheme" |
| 28 | + "sigs.k8s.io/controller-runtime/pkg/client" |
| 29 | +) |
| 30 | + |
| 31 | +var _ = Describe("FieldIndexer", func() { |
| 32 | + var ( |
| 33 | + ctx context.Context |
| 34 | + ctrl *gomock.Controller |
| 35 | + ) |
| 36 | + BeforeEach(func() { |
| 37 | + ctx = context.Background() |
| 38 | + ctrl = gomock.NewController(GinkgoT()) |
| 39 | + }) |
| 40 | + AfterEach(func() { |
| 41 | + ctrl.Finish() |
| 42 | + }) |
| 43 | + |
| 44 | + Context("SharedFieldIndexer", func() { |
| 45 | + var ( |
| 46 | + fieldIndexer *mockclient.MockFieldIndexer |
| 47 | + ) |
| 48 | + BeforeEach(func() { |
| 49 | + fieldIndexer = mockclient.NewMockFieldIndexer(ctrl) |
| 50 | + }) |
| 51 | + |
| 52 | + It("should register an indexer func and call it", func() { |
| 53 | + f := mockclient.NewMockIndexerFunc(ctrl) |
| 54 | + gomock.InOrder( |
| 55 | + fieldIndexer.EXPECT().IndexField(ctx, &corev1.Pod{}, ".spec", gomock.Any()).Do( |
| 56 | + func(ctx context.Context, obj client.Object, field string, f client.IndexerFunc) error { |
| 57 | + f(obj) |
| 58 | + return nil |
| 59 | + }), |
| 60 | + f.EXPECT().Call(&corev1.Pod{}).Times(1), |
| 61 | + ) |
| 62 | + |
| 63 | + idx := NewSharedFieldIndexer(fieldIndexer, scheme.Scheme) |
| 64 | + |
| 65 | + Expect(idx.Register(&corev1.Pod{}, ".spec", f.Call)).To(Succeed()) |
| 66 | + |
| 67 | + Expect(idx.IndexField(ctx, &corev1.Pod{}, ".spec")).To(Succeed()) |
| 68 | + }) |
| 69 | + |
| 70 | + It("should error if a field is indexed twice", func() { |
| 71 | + f := mockclient.NewMockIndexerFunc(ctrl) |
| 72 | + idx := NewSharedFieldIndexer(fieldIndexer, scheme.Scheme) |
| 73 | + |
| 74 | + Expect(idx.Register(&corev1.Pod{}, ".spec", f.Call)).To(Succeed()) |
| 75 | + Expect(idx.Register(&corev1.Pod{}, ".spec", f.Call)).To(MatchError("indexer for type *v1.Pod field .spec already registered")) |
| 76 | + }) |
| 77 | + |
| 78 | + It("should call the index function only once", func() { |
| 79 | + f := mockclient.NewMockIndexerFunc(ctrl) |
| 80 | + gomock.InOrder( |
| 81 | + fieldIndexer.EXPECT().IndexField(ctx, &corev1.Pod{}, ".spec", gomock.Any()).Do( |
| 82 | + func(ctx context.Context, obj client.Object, field string, f client.IndexerFunc) error { |
| 83 | + f(obj) |
| 84 | + return nil |
| 85 | + }), |
| 86 | + f.EXPECT().Call(&corev1.Pod{}).Times(1), |
| 87 | + ) |
| 88 | + |
| 89 | + idx := NewSharedFieldIndexer(fieldIndexer, scheme.Scheme) |
| 90 | + |
| 91 | + Expect(idx.Register(&corev1.Pod{}, ".spec", f.Call)).To(Succeed()) |
| 92 | + |
| 93 | + Expect(idx.IndexField(ctx, &corev1.Pod{}, ".spec")).To(Succeed()) |
| 94 | + Expect(idx.IndexField(ctx, &corev1.Pod{}, ".spec")).To(Succeed()) |
| 95 | + Expect(idx.IndexField(ctx, &corev1.Pod{}, ".spec")).To(Succeed()) |
| 96 | + }) |
| 97 | + |
| 98 | + It("should work with unstructured objects", func() { |
| 99 | + pod := &unstructured.Unstructured{ |
| 100 | + Object: map[string]interface{}{ |
| 101 | + "apiVersion": "v1", |
| 102 | + "kind": "Pod", |
| 103 | + }, |
| 104 | + } |
| 105 | + f := mockclient.NewMockIndexerFunc(ctrl) |
| 106 | + gomock.InOrder( |
| 107 | + fieldIndexer.EXPECT().IndexField(ctx, pod, ".spec", gomock.Any()).Do( |
| 108 | + func(ctx context.Context, obj client.Object, field string, f client.IndexerFunc) error { |
| 109 | + f(obj) |
| 110 | + return nil |
| 111 | + }), |
| 112 | + f.EXPECT().Call(pod).Times(1), |
| 113 | + ) |
| 114 | + |
| 115 | + idx := NewSharedFieldIndexer(fieldIndexer, scheme.Scheme) |
| 116 | + |
| 117 | + Expect(idx.Register(pod, ".spec", f.Call)).To(Succeed()) |
| 118 | + |
| 119 | + Expect(idx.IndexField(ctx, pod, ".spec")).To(Succeed()) |
| 120 | + }) |
| 121 | + |
| 122 | + It("should work with partial object metadata", func() { |
| 123 | + pod := &metav1.PartialObjectMetadata{ |
| 124 | + TypeMeta: metav1.TypeMeta{ |
| 125 | + APIVersion: "v1", |
| 126 | + Kind: "Pod", |
| 127 | + }, |
| 128 | + } |
| 129 | + f := mockclient.NewMockIndexerFunc(ctrl) |
| 130 | + gomock.InOrder( |
| 131 | + fieldIndexer.EXPECT().IndexField(ctx, pod, ".spec", gomock.Any()).Do( |
| 132 | + func(ctx context.Context, obj client.Object, field string, f client.IndexerFunc) error { |
| 133 | + f(obj) |
| 134 | + return nil |
| 135 | + }), |
| 136 | + f.EXPECT().Call(pod).Times(1), |
| 137 | + ) |
| 138 | + |
| 139 | + idx := NewSharedFieldIndexer(fieldIndexer, scheme.Scheme) |
| 140 | + |
| 141 | + Expect(idx.Register(pod, ".spec", f.Call)).To(Succeed()) |
| 142 | + |
| 143 | + Expect(idx.IndexField(ctx, pod, ".spec")).To(Succeed()) |
| 144 | + }) |
| 145 | + |
| 146 | + It("should error if the gvk could not be obtained", func() { |
| 147 | + f := mockclient.NewMockIndexerFunc(ctrl) |
| 148 | + idx := NewSharedFieldIndexer(fieldIndexer, scheme.Scheme) |
| 149 | + |
| 150 | + type CustomObject struct{ corev1.Pod } |
| 151 | + |
| 152 | + Expect(idx.Register(&CustomObject{}, ".spec", f.Call)).To(HaveOccurred()) |
| 153 | + }) |
| 154 | + |
| 155 | + It("should error if the indexed function is unknown", func() { |
| 156 | + idx := NewSharedFieldIndexer(fieldIndexer, scheme.Scheme) |
| 157 | + Expect(idx.IndexField(ctx, &corev1.Pod{}, "unknown")).To(HaveOccurred()) |
| 158 | + }) |
| 159 | + }) |
| 160 | +}) |
0 commit comments