github.com/spotmaxtech/k8s-apimachinery-v0260@v0.0.1/pkg/apis/meta/v1/controller_ref_test.go (about) 1 /* 2 Copyright 2017 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 v1 18 19 import ( 20 "testing" 21 22 "github.com/spotmaxtech/k8s-apimachinery-v0260/pkg/runtime/schema" 23 ) 24 25 type metaObj struct { 26 ObjectMeta 27 TypeMeta 28 } 29 30 func TestNewControllerRef(t *testing.T) { 31 gvk := schema.GroupVersionKind{ 32 Group: "group", 33 Version: "v1", 34 Kind: "Kind", 35 } 36 obj1 := &metaObj{ 37 ObjectMeta: ObjectMeta{ 38 Name: "name", 39 UID: "uid1", 40 }, 41 } 42 controllerRef := NewControllerRef(obj1, gvk) 43 if controllerRef.UID != obj1.UID { 44 t.Errorf("Incorrect UID: %s", controllerRef.UID) 45 } 46 if controllerRef.Controller == nil || *controllerRef.Controller != true { 47 t.Error("Controller must be set to true") 48 } 49 if controllerRef.BlockOwnerDeletion == nil || *controllerRef.BlockOwnerDeletion != true { 50 t.Error("BlockOwnerDeletion must be set to true") 51 } 52 if controllerRef.APIVersion == "" || 53 controllerRef.Kind == "" || 54 controllerRef.Name == "" { 55 t.Errorf("All controllerRef fields must be set: %v", controllerRef) 56 } 57 } 58 59 func TestGetControllerOf(t *testing.T) { 60 gvk := schema.GroupVersionKind{ 61 Group: "group", 62 Version: "v1", 63 Kind: "Kind", 64 } 65 obj1 := &metaObj{ 66 ObjectMeta: ObjectMeta{ 67 UID: "uid1", 68 Name: "name1", 69 }, 70 } 71 controllerRef := NewControllerRef(obj1, gvk) 72 var falseRef = false 73 obj2 := &metaObj{ 74 ObjectMeta: ObjectMeta{ 75 UID: "uid2", 76 Name: "name1", 77 OwnerReferences: []OwnerReference{ 78 { 79 Name: "owner1", 80 Controller: &falseRef, 81 }, 82 *controllerRef, 83 { 84 Name: "owner2", 85 Controller: &falseRef, 86 }, 87 }, 88 }, 89 } 90 91 if GetControllerOf(obj1) != nil { 92 t.Error("GetControllerOf must return null") 93 } 94 c := GetControllerOf(obj2) 95 if c.Name != controllerRef.Name || c.UID != controllerRef.UID { 96 t.Errorf("Incorrect result of GetControllerOf: %v", c) 97 } 98 } 99 100 func BenchmarkGetControllerOf(b *testing.B) { 101 gvk := schema.GroupVersionKind{ 102 Group: "group", 103 Version: "v1", 104 Kind: "Kind", 105 } 106 obj1 := &metaObj{ 107 ObjectMeta: ObjectMeta{ 108 UID: "9d0cdf8a-dedc-11e9-bf91-42010a800167", 109 Name: "my-object", 110 }, 111 } 112 controllerRef := NewControllerRef(obj1, gvk) 113 controllerRef2 := *controllerRef 114 controllerRef2.Controller = nil 115 obj2 := &metaObj{ 116 ObjectMeta: ObjectMeta{ 117 UID: "uid2", 118 Name: "name1", 119 OwnerReferences: []OwnerReference{controllerRef2, controllerRef2, *controllerRef}, 120 }, 121 } 122 123 b.ReportAllocs() 124 b.ResetTimer() 125 for n := 0; n < b.N; n++ { 126 c := GetControllerOf(obj2) 127 if c.Name != controllerRef.Name || c.UID != controllerRef.UID { 128 b.Errorf("Incorrect result of GetControllerOf: %v", c) 129 } 130 } 131 } 132 133 func TestIsControlledBy(t *testing.T) { 134 gvk := schema.GroupVersionKind{ 135 Group: "group", 136 Version: "v1", 137 Kind: "Kind", 138 } 139 obj1 := &metaObj{ 140 ObjectMeta: ObjectMeta{ 141 UID: "uid1", 142 }, 143 } 144 obj2 := &metaObj{ 145 ObjectMeta: ObjectMeta{ 146 UID: "uid2", 147 OwnerReferences: []OwnerReference{ 148 *NewControllerRef(obj1, gvk), 149 }, 150 }, 151 } 152 obj3 := &metaObj{ 153 ObjectMeta: ObjectMeta{ 154 UID: "uid3", 155 OwnerReferences: []OwnerReference{ 156 *NewControllerRef(obj2, gvk), 157 }, 158 }, 159 } 160 if !IsControlledBy(obj2, obj1) || !IsControlledBy(obj3, obj2) { 161 t.Error("Incorrect IsControlledBy result: false") 162 } 163 if IsControlledBy(obj3, obj1) { 164 t.Error("Incorrect IsControlledBy result: true") 165 } 166 }