|
| 1 | +// |
| 2 | +// DISCLAIMER |
| 3 | +// |
| 4 | +// Copyright 2024 ArangoDB GmbH, Cologne, Germany |
| 5 | +// |
| 6 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 7 | +// you may not use this file except in compliance with the License. |
| 8 | +// You may obtain a copy of the License at |
| 9 | +// |
| 10 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 11 | +// |
| 12 | +// Unless required by applicable law or agreed to in writing, software |
| 13 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 14 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 15 | +// See the License for the specific language governing permissions and |
| 16 | +// limitations under the License. |
| 17 | +// |
| 18 | +// Copyright holder is ArangoDB GmbH, Cologne, Germany |
| 19 | +// |
| 20 | + |
| 21 | +package cmd |
| 22 | + |
| 23 | +import ( |
| 24 | + "testing" |
| 25 | + "time" |
| 26 | + |
| 27 | + "github.com/stretchr/testify/require" |
| 28 | + core "k8s.io/api/core/v1" |
| 29 | + meta "k8s.io/apimachinery/pkg/apis/meta/v1" |
| 30 | + |
| 31 | + "github.com/arangodb/kube-arangodb/pkg/util" |
| 32 | + "github.com/arangodb/kube-arangodb/pkg/util/kclient" |
| 33 | + "github.com/arangodb/kube-arangodb/pkg/util/tests" |
| 34 | +) |
| 35 | + |
| 36 | +func Test_PodDiscovery(t *testing.T) { |
| 37 | + operatorImageDiscovery.timeout = time.Millisecond |
| 38 | + |
| 39 | + type testCase struct { |
| 40 | + Name string |
| 41 | + |
| 42 | + Pod core.Pod |
| 43 | + |
| 44 | + Image, ServiceAccount string |
| 45 | + |
| 46 | + Valid bool |
| 47 | + |
| 48 | + DefaultStatusDiscovery *bool |
| 49 | + } |
| 50 | + |
| 51 | + var testCases = []testCase{ |
| 52 | + { |
| 53 | + Name: "Empty pod", |
| 54 | + Valid: false, |
| 55 | + }, |
| 56 | + { |
| 57 | + Name: "Not allowed containers", |
| 58 | + Valid: false, |
| 59 | + Pod: core.Pod{ |
| 60 | + ObjectMeta: meta.ObjectMeta{ |
| 61 | + Name: "operator", |
| 62 | + Namespace: tests.FakeNamespace, |
| 63 | + }, |
| 64 | + Spec: core.PodSpec{ |
| 65 | + Containers: []core.Container{ |
| 66 | + { |
| 67 | + Name: "unknown", |
| 68 | + Image: "image1", |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + Status: core.PodStatus{ |
| 73 | + ContainerStatuses: []core.ContainerStatus{ |
| 74 | + { |
| 75 | + Name: "unknown", |
| 76 | + Image: "image1", |
| 77 | + ImageID: "image1", |
| 78 | + }, |
| 79 | + }, |
| 80 | + }, |
| 81 | + }, |
| 82 | + }, |
| 83 | + { |
| 84 | + Name: "Allowed Status & Spec", |
| 85 | + Valid: true, |
| 86 | + Image: "image1", |
| 87 | + ServiceAccount: "sa", |
| 88 | + Pod: core.Pod{ |
| 89 | + ObjectMeta: meta.ObjectMeta{ |
| 90 | + Name: "operator", |
| 91 | + Namespace: tests.FakeNamespace, |
| 92 | + }, |
| 93 | + Spec: core.PodSpec{ |
| 94 | + ServiceAccountName: "sa", |
| 95 | + Containers: []core.Container{ |
| 96 | + { |
| 97 | + Name: "operator", |
| 98 | + Image: "image1", |
| 99 | + }, |
| 100 | + }, |
| 101 | + }, |
| 102 | + Status: core.PodStatus{ |
| 103 | + ContainerStatuses: []core.ContainerStatus{ |
| 104 | + { |
| 105 | + Name: "operator", |
| 106 | + Image: "image1", |
| 107 | + ImageID: "image1", |
| 108 | + }, |
| 109 | + }, |
| 110 | + }, |
| 111 | + }, |
| 112 | + }, |
| 113 | + { |
| 114 | + Name: "Allowed Status & Spec", |
| 115 | + Valid: true, |
| 116 | + Image: "imageStatusID1", |
| 117 | + ServiceAccount: "sa", |
| 118 | + Pod: core.Pod{ |
| 119 | + ObjectMeta: meta.ObjectMeta{ |
| 120 | + Name: "operator", |
| 121 | + Namespace: tests.FakeNamespace, |
| 122 | + }, |
| 123 | + Spec: core.PodSpec{ |
| 124 | + ServiceAccountName: "sa", |
| 125 | + Containers: []core.Container{ |
| 126 | + { |
| 127 | + Name: "operator", |
| 128 | + Image: "imageSpec1", |
| 129 | + }, |
| 130 | + }, |
| 131 | + }, |
| 132 | + Status: core.PodStatus{ |
| 133 | + ContainerStatuses: []core.ContainerStatus{ |
| 134 | + { |
| 135 | + Name: "operator", |
| 136 | + Image: "imageStatus1", |
| 137 | + ImageID: "imageStatusID1", |
| 138 | + }, |
| 139 | + }, |
| 140 | + }, |
| 141 | + }, |
| 142 | + }, |
| 143 | + { |
| 144 | + Name: "Allowed Status & Spec - From Spec", |
| 145 | + Valid: true, |
| 146 | + Image: "imageSpec1", |
| 147 | + ServiceAccount: "sa", |
| 148 | + DefaultStatusDiscovery: util.NewType(false), |
| 149 | + Pod: core.Pod{ |
| 150 | + ObjectMeta: meta.ObjectMeta{ |
| 151 | + Name: "operator", |
| 152 | + Namespace: tests.FakeNamespace, |
| 153 | + }, |
| 154 | + Spec: core.PodSpec{ |
| 155 | + ServiceAccountName: "sa", |
| 156 | + Containers: []core.Container{ |
| 157 | + { |
| 158 | + Name: "operator", |
| 159 | + Image: "imageSpec1", |
| 160 | + }, |
| 161 | + }, |
| 162 | + }, |
| 163 | + Status: core.PodStatus{ |
| 164 | + ContainerStatuses: []core.ContainerStatus{ |
| 165 | + { |
| 166 | + Name: "operator", |
| 167 | + Image: "imageStatus1", |
| 168 | + ImageID: "imageStatusID1", |
| 169 | + }, |
| 170 | + }, |
| 171 | + }, |
| 172 | + }, |
| 173 | + }, |
| 174 | + { |
| 175 | + Name: "Allowed Spec", |
| 176 | + Valid: true, |
| 177 | + Image: "imageSpec1", |
| 178 | + ServiceAccount: "sa", |
| 179 | + Pod: core.Pod{ |
| 180 | + ObjectMeta: meta.ObjectMeta{ |
| 181 | + Name: "operator", |
| 182 | + Namespace: tests.FakeNamespace, |
| 183 | + }, |
| 184 | + Spec: core.PodSpec{ |
| 185 | + ServiceAccountName: "sa", |
| 186 | + Containers: []core.Container{ |
| 187 | + { |
| 188 | + Name: "operator", |
| 189 | + Image: "imageSpec1", |
| 190 | + }, |
| 191 | + }, |
| 192 | + }, |
| 193 | + }, |
| 194 | + }, |
| 195 | + { |
| 196 | + Name: "Allowed Status & Spec - From Second Pod", |
| 197 | + Valid: true, |
| 198 | + Image: "imageStatusID2", |
| 199 | + ServiceAccount: "sa", |
| 200 | + Pod: core.Pod{ |
| 201 | + ObjectMeta: meta.ObjectMeta{ |
| 202 | + Name: "operator", |
| 203 | + Namespace: tests.FakeNamespace, |
| 204 | + }, |
| 205 | + Spec: core.PodSpec{ |
| 206 | + ServiceAccountName: "sa", |
| 207 | + Containers: []core.Container{ |
| 208 | + { |
| 209 | + Name: "test", |
| 210 | + Image: "imageSpec1", |
| 211 | + }, |
| 212 | + { |
| 213 | + Name: "operator", |
| 214 | + Image: "imageSpec2", |
| 215 | + }, |
| 216 | + }, |
| 217 | + }, |
| 218 | + Status: core.PodStatus{ |
| 219 | + ContainerStatuses: []core.ContainerStatus{ |
| 220 | + { |
| 221 | + Name: "test", |
| 222 | + Image: "imageStatus1", |
| 223 | + ImageID: "imageStatusID1", |
| 224 | + }, |
| 225 | + { |
| 226 | + Name: "operator", |
| 227 | + Image: "imageStatus2", |
| 228 | + ImageID: "imageStatusID2", |
| 229 | + }, |
| 230 | + }, |
| 231 | + }, |
| 232 | + }, |
| 233 | + }, |
| 234 | + { |
| 235 | + Name: "Allowed Status & Spec - From Second Pod Spec", |
| 236 | + Valid: true, |
| 237 | + Image: "imageSpec2", |
| 238 | + ServiceAccount: "sa", |
| 239 | + DefaultStatusDiscovery: util.NewType(false), |
| 240 | + Pod: core.Pod{ |
| 241 | + ObjectMeta: meta.ObjectMeta{ |
| 242 | + Name: "operator", |
| 243 | + Namespace: tests.FakeNamespace, |
| 244 | + }, |
| 245 | + Spec: core.PodSpec{ |
| 246 | + ServiceAccountName: "sa", |
| 247 | + Containers: []core.Container{ |
| 248 | + { |
| 249 | + Name: "test", |
| 250 | + Image: "imageSpec1", |
| 251 | + }, |
| 252 | + { |
| 253 | + Name: "operator", |
| 254 | + Image: "imageSpec2", |
| 255 | + }, |
| 256 | + }, |
| 257 | + }, |
| 258 | + Status: core.PodStatus{ |
| 259 | + ContainerStatuses: []core.ContainerStatus{ |
| 260 | + { |
| 261 | + Name: "test", |
| 262 | + Image: "imageStatus1", |
| 263 | + ImageID: "imageStatusID1", |
| 264 | + }, |
| 265 | + { |
| 266 | + Name: "operator", |
| 267 | + Image: "imageStatus2", |
| 268 | + ImageID: "imageStatusID2", |
| 269 | + }, |
| 270 | + }, |
| 271 | + }, |
| 272 | + }, |
| 273 | + }, |
| 274 | + } |
| 275 | + |
| 276 | + for _, testCase := range testCases { |
| 277 | + t.Run(testCase.Name, func(t *testing.T) { |
| 278 | + operatorImageDiscovery.defaultStatusDiscovery = util.TypeOrDefault(testCase.DefaultStatusDiscovery, true) |
| 279 | + |
| 280 | + c := kclient.NewFakeClientBuilder().Add(&testCase.Pod).Client() |
| 281 | + image, sa, err := getMyPodInfo(c.Kubernetes(), tests.FakeNamespace, "operator") |
| 282 | + |
| 283 | + if !testCase.Valid { |
| 284 | + require.Error(t, err) |
| 285 | + require.Empty(t, image) |
| 286 | + require.Empty(t, sa) |
| 287 | + } else { |
| 288 | + require.NoError(t, err) |
| 289 | + require.Equal(t, testCase.Image, image) |
| 290 | + require.Equal(t, testCase.ServiceAccount, sa) |
| 291 | + } |
| 292 | + }) |
| 293 | + } |
| 294 | +} |
0 commit comments