knative.dev/pkg@v0.0.0-20260602142205-ac97e43f6622/controller/helper_test.go (about) 1 /* 2 Copyright 2018 The Knative 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 https://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 controller 18 19 import ( 20 "testing" 21 22 "github.com/google/go-cmp/cmp" 23 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 24 "k8s.io/apimachinery/pkg/runtime" 25 "k8s.io/apimachinery/pkg/runtime/schema" 26 "k8s.io/client-go/tools/cache" 27 28 . "knative.dev/pkg/testing" 29 ) 30 31 func TestEnsureTypeMeta(t *testing.T) { 32 gvk := schema.GroupVersionKind{ 33 Group: "foo.bar.com", 34 Version: "v23", 35 Kind: "Magic", 36 } 37 apiVersion, kind := gvk.ToAPIVersionAndKind() 38 39 tests := []struct { 40 name string 41 obj interface{} 42 want runtime.Object 43 }{{ 44 name: "not a runtime.Object", 45 obj: struct{}{}, 46 }, { 47 name: "called without type meta", 48 obj: &Resource{ 49 ObjectMeta: metav1.ObjectMeta{ 50 Namespace: "default", 51 Name: "thing", 52 }, 53 }, 54 want: &Resource{ 55 TypeMeta: metav1.TypeMeta{ 56 APIVersion: apiVersion, 57 Kind: kind, 58 }, 59 ObjectMeta: metav1.ObjectMeta{ 60 Namespace: "default", 61 Name: "thing", 62 }, 63 }, 64 }, { 65 name: "called with correct type meta", 66 obj: &Resource{ 67 TypeMeta: metav1.TypeMeta{ 68 APIVersion: apiVersion, 69 Kind: kind, 70 }, 71 ObjectMeta: metav1.ObjectMeta{ 72 Namespace: "default", 73 Name: "thing", 74 }, 75 }, 76 want: &Resource{ 77 TypeMeta: metav1.TypeMeta{ 78 APIVersion: apiVersion, 79 Kind: kind, 80 }, 81 ObjectMeta: metav1.ObjectMeta{ 82 Namespace: "default", 83 Name: "thing", 84 }, 85 }, 86 }, { 87 name: "called with wrong type meta", 88 obj: &Resource{ 89 TypeMeta: metav1.TypeMeta{ 90 APIVersion: "foo", 91 Kind: "bar", 92 }, 93 ObjectMeta: metav1.ObjectMeta{ 94 Namespace: "default", 95 Name: "thing", 96 }, 97 }, 98 want: &Resource{ 99 TypeMeta: metav1.TypeMeta{ 100 APIVersion: apiVersion, 101 Kind: kind, 102 }, 103 ObjectMeta: metav1.ObjectMeta{ 104 Namespace: "default", 105 Name: "thing", 106 }, 107 }, 108 }, { 109 name: "called with deleted obj", 110 obj: cache.DeletedFinalStateUnknown{ 111 Key: "default/thing", 112 Obj: &Resource{ 113 ObjectMeta: metav1.ObjectMeta{ 114 Namespace: "default", 115 Name: "thing", 116 }, 117 }, 118 }, 119 want: &Resource{ 120 TypeMeta: metav1.TypeMeta{ 121 APIVersion: apiVersion, 122 Kind: kind, 123 }, 124 ObjectMeta: metav1.ObjectMeta{ 125 Namespace: "default", 126 Name: "thing", 127 }, 128 }, 129 }} 130 131 for _, test := range tests { 132 t.Run(test.name, func(t *testing.T) { 133 var cb Callback 134 if test.want != nil { 135 cb = func(got interface{}) { 136 if diff := cmp.Diff(got, test.want); diff != "" { 137 t.Error("EnsureTypeMeta =", diff) 138 } 139 } 140 } else { 141 cb = func(got interface{}) { 142 t.Errorf("Wanted no call, got %#v", got) 143 } 144 } 145 146 ncb := EnsureTypeMeta(cb, gvk) 147 148 // This should either invoke the callback or not, the 149 // rest of the test is in the configured callback. 150 ncb(test.obj) 151 }) 152 } 153 }