@@ -208,6 +208,147 @@ func TestVisitContainers(t *testing.T) {
208
208
}
209
209
}
210
210
211
+ func TestContainerIter (t * testing.T ) {
212
+ testCases := []struct {
213
+ desc string
214
+ spec * api.PodSpec
215
+ wantContainers []string
216
+ mask ContainerType
217
+ }{
218
+ {
219
+ desc : "empty podspec" ,
220
+ spec : & api.PodSpec {},
221
+ wantContainers : []string {},
222
+ mask : AllContainers ,
223
+ },
224
+ {
225
+ desc : "regular containers" ,
226
+ spec : & api.PodSpec {
227
+ Containers : []api.Container {
228
+ {Name : "c1" },
229
+ {Name : "c2" },
230
+ },
231
+ InitContainers : []api.Container {
232
+ {Name : "i1" },
233
+ {Name : "i2" },
234
+ },
235
+ EphemeralContainers : []api.EphemeralContainer {
236
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e1" }},
237
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e2" }},
238
+ },
239
+ },
240
+ wantContainers : []string {"c1" , "c2" },
241
+ mask : Containers ,
242
+ },
243
+ {
244
+ desc : "init containers" ,
245
+ spec : & api.PodSpec {
246
+ Containers : []api.Container {
247
+ {Name : "c1" },
248
+ {Name : "c2" },
249
+ },
250
+ InitContainers : []api.Container {
251
+ {Name : "i1" },
252
+ {Name : "i2" },
253
+ },
254
+ EphemeralContainers : []api.EphemeralContainer {
255
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e1" }},
256
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e2" }},
257
+ },
258
+ },
259
+ wantContainers : []string {"i1" , "i2" },
260
+ mask : InitContainers ,
261
+ },
262
+ {
263
+ desc : "init + main containers" ,
264
+ spec : & api.PodSpec {
265
+ Containers : []api.Container {
266
+ {Name : "c1" },
267
+ {Name : "c2" },
268
+ },
269
+ InitContainers : []api.Container {
270
+ {Name : "i1" },
271
+ {Name : "i2" },
272
+ },
273
+ EphemeralContainers : []api.EphemeralContainer {
274
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e1" }},
275
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e2" }},
276
+ },
277
+ },
278
+ wantContainers : []string {"i1" , "i2" , "c1" , "c2" },
279
+ mask : InitContainers | Containers ,
280
+ },
281
+ {
282
+ desc : "ephemeral containers" ,
283
+ spec : & api.PodSpec {
284
+ Containers : []api.Container {
285
+ {Name : "c1" },
286
+ {Name : "c2" },
287
+ },
288
+ InitContainers : []api.Container {
289
+ {Name : "i1" },
290
+ {Name : "i2" },
291
+ },
292
+ EphemeralContainers : []api.EphemeralContainer {
293
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e1" }},
294
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e2" }},
295
+ },
296
+ },
297
+ wantContainers : []string {"e1" , "e2" },
298
+ mask : EphemeralContainers ,
299
+ },
300
+ {
301
+ desc : "all container types" ,
302
+ spec : & api.PodSpec {
303
+ Containers : []api.Container {
304
+ {Name : "c1" },
305
+ {Name : "c2" },
306
+ },
307
+ InitContainers : []api.Container {
308
+ {Name : "i1" },
309
+ {Name : "i2" },
310
+ },
311
+ EphemeralContainers : []api.EphemeralContainer {
312
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e1" }},
313
+ {EphemeralContainerCommon : api.EphemeralContainerCommon {Name : "e2" }},
314
+ },
315
+ },
316
+ wantContainers : []string {"i1" , "i2" , "c1" , "c2" , "e1" , "e2" },
317
+ mask : AllContainers ,
318
+ },
319
+ }
320
+
321
+ for _ , tc := range testCases {
322
+ t .Run (tc .desc , func (t * testing.T ) {
323
+ gotContainers := []string {}
324
+ for c , containerType := range ContainerIter (tc .spec , tc .mask ) {
325
+ gotContainers = append (gotContainers , c .Name )
326
+
327
+ switch containerType {
328
+ case InitContainers :
329
+ if c .Name [0 ] != 'i' {
330
+ t .Errorf ("ContainerIter() yielded container type InitContainers for container %q" , c .Name )
331
+ }
332
+ case Containers :
333
+ if c .Name [0 ] != 'c' {
334
+ t .Errorf ("ContainerIter() yielded container type Containers for container %q" , c .Name )
335
+ }
336
+ case EphemeralContainers :
337
+ if c .Name [0 ] != 'e' {
338
+ t .Errorf ("ContainerIter() yielded container type EphemeralContainers for container %q" , c .Name )
339
+ }
340
+ default :
341
+ t .Errorf ("ContainerIter() yielded unknown container type %d" , containerType )
342
+ }
343
+ }
344
+
345
+ if ! cmp .Equal (gotContainers , tc .wantContainers ) {
346
+ t .Errorf ("ContainerIter() = %+v, want %+v" , gotContainers , tc .wantContainers )
347
+ }
348
+ })
349
+ }
350
+ }
351
+
211
352
func TestPodSecrets (t * testing.T ) {
212
353
// Stub containing all possible secret references in a pod.
213
354
// The names of the referenced secrets match struct paths detected by reflection.
0 commit comments