@@ -19,11 +19,14 @@ package scheduler
19
19
import (
20
20
"reflect"
21
21
"testing"
22
+ "time"
22
23
23
24
"k8s.io/api/core/v1"
24
25
"k8s.io/apimachinery/pkg/api/resource"
25
26
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
27
+ "k8s.io/kubernetes/pkg/scheduler/internal/cache"
26
28
fakecache "k8s.io/kubernetes/pkg/scheduler/internal/cache/fake"
29
+ "k8s.io/kubernetes/pkg/scheduler/internal/queue"
27
30
)
28
31
29
32
func TestSkipPodUpdate (t * testing.T ) {
@@ -346,3 +349,51 @@ func TestNodeConditionsChanged(t *testing.T) {
346
349
})
347
350
}
348
351
}
352
+
353
+ func TestUpdatePodInCache (t * testing.T ) {
354
+ ttl := 10 * time .Second
355
+ nodeName := "node"
356
+
357
+ tests := []struct {
358
+ name string
359
+ oldObj interface {}
360
+ newObj interface {}
361
+ }{
362
+ {
363
+ name : "pod updated with the same UID" ,
364
+ oldObj : withPodName (podWithPort ("oldUID" , nodeName , 80 ), "pod" ),
365
+ newObj : withPodName (podWithPort ("oldUID" , nodeName , 8080 ), "pod" ),
366
+ },
367
+ {
368
+ name : "pod updated with different UIDs" ,
369
+ oldObj : withPodName (podWithPort ("oldUID" , nodeName , 80 ), "pod" ),
370
+ newObj : withPodName (podWithPort ("newUID" , nodeName , 8080 ), "pod" ),
371
+ },
372
+ }
373
+ for _ , tt := range tests {
374
+ t .Run (tt .name , func (t * testing.T ) {
375
+ stopCh := make (chan struct {})
376
+ defer close (stopCh )
377
+ schedulerCache := cache .New (ttl , stopCh )
378
+ schedulerQueue := queue .NewPriorityQueue (nil )
379
+ sched := & Scheduler {
380
+ SchedulerCache : schedulerCache ,
381
+ SchedulingQueue : schedulerQueue ,
382
+ }
383
+ sched .addPodToCache (tt .oldObj )
384
+ sched .updatePodInCache (tt .oldObj , tt .newObj )
385
+ pod , err := sched .SchedulerCache .GetPod (tt .newObj .(* v1.Pod ))
386
+ if err != nil {
387
+ t .Errorf ("Failed to get pod from scheduler: %v" , err )
388
+ }
389
+ if pod .UID != tt .newObj .(* v1.Pod ).UID {
390
+ t .Errorf ("Want pod UID %v, got %v" , tt .newObj .(* v1.Pod ).UID , pod .UID )
391
+ }
392
+ })
393
+ }
394
+ }
395
+
396
+ func withPodName (pod * v1.Pod , name string ) * v1.Pod {
397
+ pod .Name = name
398
+ return pod
399
+ }
0 commit comments