github.com/inspektor-gadget/inspektor-gadget@v0.28.1/pkg/container-collection/options_test.go (about) 1 // Copyright 2019-2021 The Inspektor Gadget 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 containercollection 16 17 import ( 18 "testing" 19 20 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 21 ) 22 23 func TestGetExpectedOwnerReference(t *testing.T) { 24 cTrue := true 25 cFalse := false 26 table := []struct { 27 description string 28 ownerReferences []metav1.OwnerReference 29 expectedResult *metav1.OwnerReference 30 }{ 31 { 32 description: "From empty list", 33 ownerReferences: []metav1.OwnerReference{}, 34 }, 35 { 36 description: "Neither controller reference nor expected kind", 37 ownerReferences: []metav1.OwnerReference{ 38 { 39 UID: "abcde1", 40 Kind: "non-expected-kind", 41 Controller: &cFalse, 42 }, 43 { 44 UID: "abcde2", 45 Kind: "non-expected-kind", 46 Controller: &cFalse, 47 }, 48 { 49 UID: "abcde3", 50 Kind: "non-expected-kind", 51 Controller: &cFalse, 52 }, 53 }, 54 }, 55 { 56 description: "One element with expected kind", 57 ownerReferences: []metav1.OwnerReference{ 58 { 59 UID: "abcde", 60 Kind: "Deployment", 61 Controller: &cFalse, 62 }, 63 }, 64 expectedResult: &metav1.OwnerReference{ 65 UID: "abcde", 66 }, 67 }, 68 { 69 description: "Selecting first expected kind reference (First element)", 70 ownerReferences: []metav1.OwnerReference{ 71 { 72 UID: "abcde1", 73 Kind: "ReplicaSet", 74 Controller: &cFalse, 75 }, 76 { 77 UID: "abcde2", 78 Kind: "StatefulSet", 79 Controller: &cFalse, 80 }, 81 { 82 UID: "abcde3", 83 Kind: "DaemonSet", 84 Controller: &cFalse, 85 }, 86 }, 87 expectedResult: &metav1.OwnerReference{ 88 UID: "abcde1", 89 }, 90 }, 91 { 92 description: "Selecting first expected kind reference (Intermediate element)", 93 ownerReferences: []metav1.OwnerReference{ 94 { 95 UID: "abcde1", 96 Kind: "non-expected-kind", 97 Controller: &cFalse, 98 }, 99 { 100 UID: "abcde2", 101 Kind: "Job", 102 Controller: &cFalse, 103 }, 104 { 105 UID: "abcde3", 106 Kind: "CronJob", 107 Controller: &cFalse, 108 }, 109 }, 110 expectedResult: &metav1.OwnerReference{ 111 UID: "abcde2", 112 }, 113 }, 114 { 115 description: "Selecting first expected kind reference (Last element)", 116 ownerReferences: []metav1.OwnerReference{ 117 { 118 UID: "abcde1", 119 Kind: "non-expected-kind", 120 Controller: &cFalse, 121 }, 122 { 123 UID: "abcde2", 124 Kind: "non-expected-kind", 125 Controller: &cFalse, 126 }, 127 { 128 UID: "abcde3", 129 Kind: "ReplicationController", 130 Controller: &cFalse, 131 }, 132 }, 133 expectedResult: &metav1.OwnerReference{ 134 UID: "abcde3", 135 }, 136 }, 137 { 138 description: "Nil controller flag", 139 ownerReferences: []metav1.OwnerReference{ 140 { 141 UID: "abcde", 142 Kind: "CronJob", 143 }, 144 }, 145 expectedResult: &metav1.OwnerReference{ 146 UID: "abcde", 147 }, 148 }, 149 { 150 description: "Controller reference without expected kind and no fallback", 151 ownerReferences: []metav1.OwnerReference{ 152 { 153 UID: "abcde1", 154 Kind: "non-expected-kind", 155 Controller: &cTrue, 156 }, 157 { 158 UID: "abcde2", 159 Kind: "non-expected-kind", 160 Controller: &cFalse, 161 }, 162 { 163 UID: "abcde3", 164 Kind: "non-expected-kind", 165 Controller: &cFalse, 166 }, 167 }, 168 }, 169 { 170 description: "Fallback references for controller reference without expected kind", 171 ownerReferences: []metav1.OwnerReference{ 172 { 173 UID: "abcde1", 174 Kind: "non-expected-kind", 175 Controller: &cTrue, 176 }, 177 { 178 UID: "abcde2", 179 Kind: "ReplicaSet", 180 Controller: &cFalse, 181 }, 182 { 183 UID: "abcde3", 184 Kind: "StatefulSet", 185 Controller: &cFalse, 186 }, 187 }, 188 expectedResult: &metav1.OwnerReference{ 189 UID: "abcde2", 190 }, 191 }, 192 { 193 description: "Selecting controller reference (First element)", 194 ownerReferences: []metav1.OwnerReference{ 195 { 196 UID: "abcde1", 197 Kind: "Deployment", 198 Controller: &cTrue, 199 }, 200 { 201 UID: "abcde2", 202 Kind: "Job", 203 Controller: &cFalse, 204 }, 205 { 206 UID: "abcde3", 207 Kind: "ReplicaSet", 208 Controller: &cFalse, 209 }, 210 }, 211 expectedResult: &metav1.OwnerReference{ 212 UID: "abcde1", 213 }, 214 }, 215 { 216 description: "Selecting controller reference (Intermediate element)", 217 ownerReferences: []metav1.OwnerReference{ 218 { 219 UID: "abcde1", 220 Kind: "DaemonSet", 221 Controller: &cFalse, 222 }, 223 { 224 UID: "abcde2", 225 Kind: "ReplicationController", 226 Controller: &cTrue, 227 }, 228 { 229 UID: "abcde3", 230 Kind: "StatefulSet", 231 Controller: &cFalse, 232 }, 233 }, 234 expectedResult: &metav1.OwnerReference{ 235 UID: "abcde2", 236 }, 237 }, 238 { 239 description: "Selecting controller reference (Last element)", 240 ownerReferences: []metav1.OwnerReference{ 241 { 242 UID: "abcde1", 243 Kind: "ReplicaSet", 244 Controller: &cFalse, 245 }, 246 { 247 UID: "abcde2", 248 Kind: "Deployment", 249 Controller: &cFalse, 250 }, 251 { 252 UID: "abcde3", 253 Kind: "CronJob", 254 Controller: &cTrue, 255 }, 256 }, 257 expectedResult: &metav1.OwnerReference{ 258 UID: "abcde3", 259 }, 260 }, 261 } 262 263 for i, entry := range table { 264 result := getExpectedOwnerReference(entry.ownerReferences) 265 if (entry.expectedResult == nil && result != nil) || (entry.expectedResult != nil && entry.expectedResult.UID != result.UID) { 266 t.Fatalf("Failed test %q (index %d): result %+v expected %+v", 267 entry.description, i, result, entry.expectedResult) 268 } 269 } 270 }