Debian Codegraph @

pkg/apis/meta/v1/controller_ref_test.go

raw
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 "k8s.io/apimachinery/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 controllerRef.BlockOwnerDeletion = nil
73 var falseRef = false
74 obj2 := &metaObj{
75 ObjectMeta: ObjectMeta{
76 UID: "uid2",
77 Name: "name1",
78 OwnerReferences: []OwnerReference{
79 {
80 Name: "owner1",
81 Controller: &falseRef,
82 },
83 *controllerRef,
84 {
85 Name: "owner2",
86 Controller: &falseRef,
87 },
88 },
89 },
90 }
91
92 if GetControllerOf(obj1) != nil {
93 t.Error("GetControllerOf must return null")
94 }
95 c := GetControllerOf(obj2)
96 if c.Name != controllerRef.Name || c.UID != controllerRef.UID {
97 t.Errorf("Incorrect result of GetControllerOf: %v", c)
98 }
99
100 // test that all pointers are also deep copied
101 if (c.Controller == controllerRef.Controller) ||
102 (c.BlockOwnerDeletion != nil && c.BlockOwnerDeletion == controllerRef.BlockOwnerDeletion) {
103 t.Errorf("GetControllerOf did not return deep copy: %v", c)
104 }
105 }
106
107 func BenchmarkGetControllerOf(b *testing.B) {
108 gvk := schema.GroupVersionKind{
109 Group: "group",
110 Version: "v1",
111 Kind: "Kind",
112 }
113 obj1 := &metaObj{
114 ObjectMeta: ObjectMeta{
115 UID: "9d0cdf8a-dedc-11e9-bf91-42010a800167",
116 Name: "my-object",
117 },
118 }
119 controllerRef := NewControllerRef(obj1, gvk)
120 controllerRef2 := *controllerRef
121 controllerRef2.Controller = nil
122 obj2 := &metaObj{
123 ObjectMeta: ObjectMeta{
124 UID: "uid2",
125 Name: "name1",
126 OwnerReferences: []OwnerReference{controllerRef2, controllerRef2, *controllerRef},
127 },
128 }
129
130 b.ReportAllocs()
131 b.ResetTimer()
132 for n := 0; n < b.N; n++ {
133 c := GetControllerOf(obj2)
134 if c.Name != controllerRef.Name || c.UID != controllerRef.UID {
135 b.Errorf("Incorrect result of GetControllerOf: %v", c)
136 }
137 }
138 }
139
140 func TestIsControlledBy(t *testing.T) {
141 gvk := schema.GroupVersionKind{
142 Group: "group",
143 Version: "v1",
144 Kind: "Kind",
145 }
146 obj1 := &metaObj{
147 ObjectMeta: ObjectMeta{
148 UID: "uid1",
149 },
150 }
151 obj2 := &metaObj{
152 ObjectMeta: ObjectMeta{
153 UID: "uid2",
154 OwnerReferences: []OwnerReference{
155 *NewControllerRef(obj1, gvk),
156 },
157 },
158 }
159 obj3 := &metaObj{
160 ObjectMeta: ObjectMeta{
161 UID: "uid3",
162 OwnerReferences: []OwnerReference{
163 *NewControllerRef(obj2, gvk),
164 },
165 },
166 }
167 if !IsControlledBy(obj2, obj1) || !IsControlledBy(obj3, obj2) {
168 t.Error("Incorrect IsControlledBy result: false")
169 }
170 if IsControlledBy(obj3, obj1) {
171 t.Error("Incorrect IsControlledBy result: true")
172 }
173 }