@@ -445,10 +445,12 @@ type allocatableTestData struct {
445445 podsResources []v1.ResourceList
446446 podResourcesToAllocate v1.ResourceList
447447 podAnnotations map [string ]string
448+ podOverhead v1.ResourceList
448449 expected bool
449450 expectedMessageContains []string
451+ expectOverheadMessage bool
450452}
451- type allocatableTestFunction func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , error )
453+ type allocatableTestFunction func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , * common_info. FitError )
452454
453455func TestIsTaskAllocatable (t * testing.T ) {
454456 nodeCapacityDifferent := common_info .BuildNode ("n2" , common_info .BuildResourceList ("1000m" , "1G" ))
@@ -519,13 +521,49 @@ func TestIsTaskAllocatable(t *testing.T) {
519521 expected : true ,
520522 expectedMessageContains : []string {},
521523 },
524+ "pod with overhead that fits without overhead but not with overhead" : {
525+ node : common_info .BuildNode ("n1" , common_info .BuildResourceList ("2000m" , "2G" )),
526+ podsResources : []v1.ResourceList {common_info .BuildResourceList ("1000m" , "1G" )},
527+ podResourcesToAllocate : common_info .BuildResourceList ("500m" , "500M" ),
528+ podOverhead : common_info .BuildResourceList ("600m" , "600M" ),
529+ expected : false ,
530+ expectedMessageContains : []string {"CPU cores" , "memory" },
531+ expectOverheadMessage : true ,
532+ },
533+ "pod with overhead that doesn't fit even without overhead" : {
534+ node : common_info .BuildNode ("n1" , common_info .BuildResourceList ("2000m" , "2G" )),
535+ podsResources : []v1.ResourceList {common_info .BuildResourceList ("1000m" , "1G" )},
536+ podResourcesToAllocate : common_info .BuildResourceList ("1500m" , "1500M" ),
537+ podOverhead : common_info .BuildResourceList ("100m" , "100M" ),
538+ expected : false ,
539+ expectedMessageContains : []string {"CPU cores" , "memory" },
540+ expectOverheadMessage : false ,
541+ },
542+ "pod without overhead that doesn't fit" : {
543+ node : common_info .BuildNode ("n1" , common_info .BuildResourceList ("2000m" , "2G" )),
544+ podsResources : []v1.ResourceList {common_info .BuildResourceList ("1000m" , "1G" )},
545+ podResourcesToAllocate : common_info .BuildResourceList ("1500m" , "1500M" ),
546+ podOverhead : v1.ResourceList {},
547+ expected : false ,
548+ expectedMessageContains : []string {"CPU cores" , "memory" },
549+ expectOverheadMessage : false ,
550+ },
551+ "pod with overhead that fits with overhead" : {
552+ node : common_info .BuildNode ("n1" , common_info .BuildResourceList ("2000m" , "2G" )),
553+ podsResources : []v1.ResourceList {common_info .BuildResourceList ("1000m" , "1G" )},
554+ podResourcesToAllocate : common_info .BuildResourceList ("500m" , "500M" ),
555+ podOverhead : common_info .BuildResourceList ("100m" , "100M" ),
556+ expected : true ,
557+ expectedMessageContains : []string {},
558+ expectOverheadMessage : false ,
559+ },
522560 }
523561
524562 for testName , testData := range tests {
525563 t .Run (testName , func (t * testing.T ) {
526564 runAllocatableTest (
527565 t , testData , testName ,
528- func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , error ) {
566+ func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , * common_info. FitError ) {
529567 return ni .IsTaskAllocatable (task ), ni .FittingError (task , false )
530568 },
531569 )
@@ -561,7 +599,7 @@ func TestIsTaskAllocatableOnReleasingOrIdle(t *testing.T) {
561599 t .Run (testName , func (t * testing.T ) {
562600 runAllocatableTest (
563601 t , testData , testName ,
564- func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , error ) {
602+ func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , * common_info. FitError ) {
565603 return ni .IsTaskAllocatableOnReleasingOrIdle (task ), nil
566604 },
567605 )
@@ -592,6 +630,11 @@ func runAllocatableTest(
592630 "podToAllocate" , "p1" , "n1" , v1 .PodRunning , testData .podResourcesToAllocate ,
593631 []metav1.OwnerReference {}, make (map [string ]string ), testData .podAnnotations )
594632 addJobAnnotation (pod )
633+
634+ if len (testData .podOverhead ) > 0 {
635+ pod .Spec .Overhead = testData .podOverhead
636+ }
637+
595638 task := pod_info .NewTaskInfo (pod )
596639 allocatable , fitErr := testedFunction (ni , task )
597640 if allocatable != testData .expected {
@@ -603,6 +646,20 @@ func runAllocatableTest(
603646 t .Errorf ("%s: expected error message to contain %s, got %s" , testName , expectedMessage , fitErr .Error ())
604647 }
605648 }
649+
650+ if testData .expectOverheadMessage {
651+ if ! strings .Contains (fitErr .Error (), "Not enough resources due to pod overhead resources" ) {
652+ t .Errorf ("%s: expected overhead message, got %s" , testName , fitErr .Error ())
653+ }
654+ } else {
655+ fitErrMessage := fitErr .Error ()
656+ if strings .Contains (fitErrMessage , "Not enough resources due to pod overhead resources" ) {
657+ t .Errorf ("%s: unexpected overhead message, got %s" , testName , fitErrMessage )
658+ }
659+ }
660+ } else if len (testData .expectedMessageContains ) > 0 {
661+ // If we expected an error but got none, that's a test failure
662+ t .Errorf ("%s: expected error but got none" , testName )
606663 }
607664}
608665
0 commit comments