@@ -30,6 +30,7 @@ import (
30
30
"k8s.io/apimachinery/pkg/api/equality"
31
31
"k8s.io/apimachinery/pkg/api/resource"
32
32
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
33
+ "k8s.io/apimachinery/pkg/runtime"
33
34
podlisterv1 "k8s.io/client-go/listers/core/v1"
34
35
"k8s.io/klog/v2"
35
36
@@ -434,17 +435,35 @@ func TestFlush(t *testing.T) {
434
435
435
436
lh := klog .Background ()
436
437
437
- nrtCache .FlushNodes (lh , expectedNodeTopology .DeepCopy ())
438
+ expectedGen := nrtCache .generation + 1
439
+ gen1 := nrtCache .FlushNodes (lh , expectedNodeTopology .DeepCopy ())
440
+ if gen1 != expectedGen {
441
+ t .Fatalf ("generation is expected to increase once after flushing a dirty node\n got %d expected %d" , gen1 , expectedGen )
442
+ }
438
443
439
444
dirtyNodes := nrtCache .GetDesyncedNodes (lh )
440
445
if dirtyNodes .Len () != 0 {
441
446
t .Errorf ("dirty nodes after flush: %v" , dirtyNodes )
442
447
}
443
448
444
- nrtObj , _ := nrtCache .GetCachedNRTCopy (context .Background (), "node1" , testPod )
449
+ nrtObj , nrtInfo := nrtCache .GetCachedNRTCopy (context .Background (), "node1" , testPod )
445
450
if ! reflect .DeepEqual (nrtObj , expectedNodeTopology ) {
446
451
t .Fatalf ("unexpected object from cache\n got: %s\n expected: %s\n " , dumpNRT (nrtObj ), dumpNRT (nodeTopologies [0 ]))
447
452
}
453
+
454
+ expectedNrtInfo := CachedNRTInfo {
455
+ Fresh : true ,
456
+ Generation : expectedGen ,
457
+ }
458
+ if ! reflect .DeepEqual (nrtInfo , expectedNrtInfo ) {
459
+ t .Fatalf ("unexpected NRT info from cache\n got: %+v\n expected: %+v\n " , nrtInfo , expectedNrtInfo )
460
+ }
461
+
462
+ // flush again without dirty nodes
463
+ gen2 := nrtCache .FlushNodes (lh )
464
+ if gen2 != expectedGen {
465
+ t .Fatalf ("generation shouldn't change with no dirty nodes\n got %d expected %d" , gen2 , expectedGen )
466
+ }
448
467
}
449
468
450
469
func TestResyncNoPodFingerprint (t * testing.T ) {
@@ -971,3 +990,49 @@ func TestMakeNodeToPodDataMap(t *testing.T) {
971
990
})
972
991
}
973
992
}
993
+
994
+ func TestOverresevedGetCachedNRTCopyWithForeignPods (t * testing.T ) {
995
+ testNodeName := "worker-node-G"
996
+ nrt := makeTestNRT (testNodeName )
997
+ pod := & corev1.Pod {} // API placeholder
998
+ fakePodLister := & fakePodLister {}
999
+
1000
+ objs := []runtime.Object {nrt }
1001
+ fakeClient , err := tu .NewFakeClient (objs ... )
1002
+ if err != nil {
1003
+ t .Fatal (err )
1004
+ }
1005
+
1006
+ ctx := context .Background ()
1007
+ lh := klog .Background ()
1008
+ nrtCache , err := NewOverReserve (ctx , lh , nil , fakeClient , fakePodLister , podprovider .IsPodRelevantAlways )
1009
+ if err != nil {
1010
+ t .Fatalf ("unexpected error creating cache: %v" , err )
1011
+ }
1012
+
1013
+ expectedNrtInfo := CachedNRTInfo {
1014
+ Generation : 0 ,
1015
+ Fresh : true ,
1016
+ }
1017
+ _ , gotInfo := nrtCache .GetCachedNRTCopy (ctx , testNodeName , pod )
1018
+ if ! reflect .DeepEqual (gotInfo , expectedNrtInfo ) {
1019
+ t .Errorf ("mismatched nrt info from cache, got %+v expected %+v" , gotInfo , expectedNrtInfo )
1020
+ }
1021
+
1022
+ // pointless, but will force a generation increase
1023
+ gen := nrtCache .FlushNodes (lh , nrt )
1024
+ if gen == 0 {
1025
+ t .Fatalf ("FlushNodes didn't increase the generation" )
1026
+ }
1027
+
1028
+ // expected to mark cached data as stale (!fresh)
1029
+ nrtCache .NodeHasForeignPods (testNodeName , pod )
1030
+
1031
+ _ , gotInfo = nrtCache .GetCachedNRTCopy (ctx , testNodeName , pod )
1032
+ if gotInfo .Generation != gen {
1033
+ t .Errorf ("mismatched generation, got %v expected %v" , gotInfo .Generation , gen )
1034
+ }
1035
+ if gotInfo .Fresh {
1036
+ t .Errorf ("cached data reported fresh when node has foreign pods" )
1037
+ }
1038
+ }
0 commit comments