@@ -355,3 +355,94 @@ func TestMetrics(t *testing.T) {
355
355
})
356
356
}
357
357
}
358
+
359
+ func TestPods (t * testing.T ) {
360
+ updatedPod := & corev1.Pod {
361
+ ObjectMeta : metav1.ObjectMeta {
362
+ Name : "pod1" ,
363
+ },
364
+ Spec : corev1.PodSpec {
365
+ NodeName : "node-1" ,
366
+ },
367
+ }
368
+ tests := []struct {
369
+ name string
370
+ op func (ctx context.Context , ds Datastore )
371
+ existingPods []* corev1.Pod
372
+ wantPods []* corev1.Pod
373
+ }{
374
+ {
375
+ name : "Add new pod, no existing pods, should add" ,
376
+ existingPods : []* corev1.Pod {},
377
+ wantPods : []* corev1.Pod {pod1 },
378
+ op : func (ctx context.Context , ds Datastore ) {
379
+ ds .PodUpdateOrAddIfNotExist (pod1 )
380
+ },
381
+ },
382
+ {
383
+ name : "Add new pod, with existing pods, should add" ,
384
+ existingPods : []* corev1.Pod {pod1 },
385
+ wantPods : []* corev1.Pod {pod1 , pod2 },
386
+ op : func (ctx context.Context , ds Datastore ) {
387
+ ds .PodUpdateOrAddIfNotExist (pod2 )
388
+ },
389
+ },
390
+ {
391
+ name : "Update existing pod, new field, should update" ,
392
+ existingPods : []* corev1.Pod {pod1 },
393
+ wantPods : []* corev1.Pod {updatedPod },
394
+ op : func (ctx context.Context , ds Datastore ) {
395
+ ds .PodUpdateOrAddIfNotExist (updatedPod )
396
+ },
397
+ },
398
+ {
399
+ name : "Update existing pod, no new fields, should not update" ,
400
+ existingPods : []* corev1.Pod {pod1 },
401
+ wantPods : []* corev1.Pod {pod1 },
402
+ op : func (ctx context.Context , ds Datastore ) {
403
+ incoming := & corev1.Pod {
404
+ ObjectMeta : metav1.ObjectMeta {
405
+ Name : "pod1" ,
406
+ Namespace : "default" ,
407
+ },
408
+ }
409
+ ds .PodUpdateOrAddIfNotExist (incoming )
410
+ },
411
+ },
412
+ {
413
+ name : "Delete the pod" ,
414
+ wantPods : []* corev1.Pod {pod1 },
415
+ op : func (ctx context.Context , ds Datastore ) {
416
+ ds .PodDelete (pod2NamespacedName )
417
+ },
418
+ },
419
+ {
420
+ name : "Delete the pod that doesn't exist" ,
421
+ existingPods : []* corev1.Pod {pod1 },
422
+ wantPods : []* corev1.Pod {pod1 },
423
+ op : func (ctx context.Context , ds Datastore ) {
424
+ ds .PodDelete (pod2NamespacedName )
425
+ },
426
+ },
427
+ }
428
+ for _ , test := range tests {
429
+ t .Run (test .name , func (t * testing.T ) {
430
+ ctx := context .Background ()
431
+ pmf := backendmetrics .NewPodMetricsFactory (& backendmetrics.FakePodMetricsClient {}, time .Second )
432
+ ds := NewDatastore (t .Context (), pmf )
433
+ for _ , pod := range test .existingPods {
434
+ ds .PodUpdateOrAddIfNotExist (pod )
435
+ }
436
+
437
+ test .op (ctx , ds )
438
+ var gotPods []* corev1.Pod
439
+ for _ , pm := range ds .PodGetAll () {
440
+ pod := & corev1.Pod {ObjectMeta : metav1.ObjectMeta {Name : pm .GetPod ().NamespacedName .Name , Namespace : pm .GetPod ().NamespacedName .Namespace }, Status : corev1.PodStatus {PodIP : pm .GetPod ().Address }}
441
+ gotPods = append (gotPods , pod )
442
+ }
443
+ if ! cmp .Equal (gotPods , test .wantPods , cmpopts .SortSlices (func (a , b * corev1.Pod ) bool { return a .Name < b .Name })) {
444
+ t .Logf ("got (%v) != want (%v);" , gotPods , test .wantPods )
445
+ }
446
+ })
447
+ }
448
+ }
0 commit comments