@@ -178,6 +178,169 @@ spec:
178
178
assert .Nil (t , GetDeploymentReplicas (& noDeployment ))
179
179
}
180
180
181
+ func TestGetResourceImages (t * testing.T ) {
182
+ testCases := []struct {
183
+ manifest []byte
184
+ expected []string
185
+ description string
186
+ }{
187
+ {
188
+ manifest : []byte (`
189
+ apiVersion: extensions/v1beta2
190
+ kind: Deployment
191
+ metadata:
192
+ name: nginx-deployment
193
+ labels:
194
+ foo: bar
195
+ spec:
196
+ template:
197
+ metadata:
198
+ labels:
199
+ app: nginx
200
+ spec:
201
+ containers:
202
+ - name: nginx
203
+ image: nginx:1.7.9
204
+ ports:
205
+ - containerPort: 80
206
+ - name: agent
207
+ image: agent:1.0.0` ),
208
+ expected : []string {"nginx:1.7.9" , "agent:1.0.0" },
209
+ description : "deployment with two containers" ,
210
+ },
211
+ {
212
+ manifest : []byte (`
213
+ apiVersion: v1
214
+ kind: Pod
215
+ metadata:
216
+ name: example-pod
217
+ labels:
218
+ app: my-app
219
+ spec:
220
+ containers:
221
+ - name: nginx-container
222
+ image: nginx:1.21
223
+ ports:
224
+ - containerPort: 80
225
+ - name: sidecar-container
226
+ image: busybox:1.35
227
+ command: ["sh", "-c", "echo Hello from the sidecar; sleep 3600"]
228
+ ` ),
229
+ expected : []string {"nginx:1.21" , "busybox:1.35" },
230
+ description : "pod with containers" ,
231
+ },
232
+ {
233
+ manifest : []byte (`
234
+ apiVersion: batch/v1
235
+ kind: CronJob
236
+ metadata:
237
+ name: hello
238
+ spec:
239
+ schedule: "* * * * *"
240
+ jobTemplate:
241
+ spec:
242
+ template:
243
+ spec:
244
+ containers:
245
+ - name: hello
246
+ image: busybox:1.28
247
+ ` ),
248
+ expected : []string {"busybox:1.28" },
249
+ description : "cronjob with containers" ,
250
+ },
251
+ {
252
+ manifest : []byte (`
253
+ apiVersion: v1
254
+ kind: ConfigMap
255
+ metadata:
256
+ name: example-config
257
+ namespace: default
258
+ labels:
259
+ app: my-app
260
+ data:
261
+ app.properties: |
262
+ key1=value1
263
+ key2=value2
264
+ key3=value3
265
+ log.level: debug
266
+ ` ),
267
+ expected : nil ,
268
+ description : "configmap without containers" ,
269
+ },
270
+ {
271
+ manifest : []byte (`
272
+ apiVersion: apps/v1
273
+ kind: Deployment
274
+ metadata:
275
+ name: deployment-no-containers
276
+ labels:
277
+ foo: bar
278
+ spec:
279
+ replicas: 1
280
+ selector:
281
+ matchLabels:
282
+ app: agent
283
+ template:
284
+ metadata:
285
+ labels:
286
+ app: agent
287
+ spec:
288
+ volumes:
289
+ - name: config-volume
290
+ configMap:
291
+ name: config
292
+ ` ),
293
+ expected : nil ,
294
+ description : "deployment without containers" ,
295
+ },
296
+ {
297
+ manifest : []byte (`
298
+ apiVersion: apps/v1
299
+ kind: Deployment
300
+ metadata:
301
+ name: deployment-without-image
302
+ spec:
303
+ template:
304
+ metadata:
305
+ labels:
306
+ app: nginx
307
+ spec:
308
+ containers:
309
+ - name: text-service
310
+ command: ["echo", "hello"]
311
+ ` ),
312
+ expected : nil ,
313
+ description : "deployment with container without image" ,
314
+ },
315
+ {
316
+ manifest : []byte (`
317
+ apiVersion: v1
318
+ kind: Pod
319
+ metadata:
320
+ name: example-pod
321
+ labels:
322
+ app: my-app
323
+ spec:
324
+ containers:
325
+ - name: no-image-container
326
+ command: ["echo", "hello"]
327
+ ` ),
328
+ expected : nil ,
329
+ description : "pod with container without image" ,
330
+ },
331
+ }
332
+
333
+ for _ , tc := range testCases {
334
+ t .Run (tc .description , func (t * testing.T ) {
335
+ resource := unstructured.Unstructured {}
336
+ err := yaml .Unmarshal (tc .manifest , & resource )
337
+ require .NoError (t , err )
338
+ images := GetResourceImages (& resource )
339
+ require .Equal (t , tc .expected , images )
340
+ })
341
+ }
342
+ }
343
+
181
344
func TestSplitYAML_SingleObject (t * testing.T ) {
182
345
objs , err := SplitYAML ([]byte (depWithLabel ))
183
346
require .NoError (t , err )
0 commit comments