@@ -464,10 +464,12 @@ type allocatableTestData struct {
464464 podsResources []v1.ResourceList
465465 podResourcesToAllocate v1.ResourceList
466466 podAnnotations map [string ]string
467+ podOverhead v1.ResourceList
467468 expected bool
468469 expectedMessageContains []string
470+ expectOverheadMessage bool
469471}
470- type allocatableTestFunction func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , error )
472+ type allocatableTestFunction func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , * common_info. FitError )
471473
472474func TestIsTaskAllocatable (t * testing.T ) {
473475 nodeCapacityDifferent := common_info .BuildNode ("n2" , common_info .BuildResourceList ("1000m" , "1G" ))
@@ -538,13 +540,49 @@ func TestIsTaskAllocatable(t *testing.T) {
538540 expected : true ,
539541 expectedMessageContains : []string {},
540542 },
543+ "pod with overhead that fits without overhead but not with overhead" : {
544+ node : common_info .BuildNode ("n1" , common_info .BuildResourceList ("2000m" , "2G" )),
545+ podsResources : []v1.ResourceList {common_info .BuildResourceList ("1000m" , "1G" )},
546+ podResourcesToAllocate : common_info .BuildResourceList ("500m" , "500M" ),
547+ podOverhead : common_info .BuildResourceList ("600m" , "600M" ),
548+ expected : false ,
549+ expectedMessageContains : []string {"CPU cores" , "memory" },
550+ expectOverheadMessage : true ,
551+ },
552+ "pod with overhead that doesn't fit even without overhead" : {
553+ node : common_info .BuildNode ("n1" , common_info .BuildResourceList ("2000m" , "2G" )),
554+ podsResources : []v1.ResourceList {common_info .BuildResourceList ("1000m" , "1G" )},
555+ podResourcesToAllocate : common_info .BuildResourceList ("1500m" , "1500M" ),
556+ podOverhead : common_info .BuildResourceList ("100m" , "100M" ),
557+ expected : false ,
558+ expectedMessageContains : []string {"CPU cores" , "memory" },
559+ expectOverheadMessage : false ,
560+ },
561+ "pod without overhead that doesn't fit" : {
562+ node : common_info .BuildNode ("n1" , common_info .BuildResourceList ("2000m" , "2G" )),
563+ podsResources : []v1.ResourceList {common_info .BuildResourceList ("1000m" , "1G" )},
564+ podResourcesToAllocate : common_info .BuildResourceList ("1500m" , "1500M" ),
565+ podOverhead : v1.ResourceList {},
566+ expected : false ,
567+ expectedMessageContains : []string {"CPU cores" , "memory" },
568+ expectOverheadMessage : false ,
569+ },
570+ "pod with overhead that fits with overhead" : {
571+ node : common_info .BuildNode ("n1" , common_info .BuildResourceList ("2000m" , "2G" )),
572+ podsResources : []v1.ResourceList {common_info .BuildResourceList ("1000m" , "1G" )},
573+ podResourcesToAllocate : common_info .BuildResourceList ("500m" , "500M" ),
574+ podOverhead : common_info .BuildResourceList ("100m" , "100M" ),
575+ expected : true ,
576+ expectedMessageContains : []string {},
577+ expectOverheadMessage : false ,
578+ },
541579 }
542580
543581 for testName , testData := range tests {
544582 t .Run (testName , func (t * testing.T ) {
545583 runAllocatableTest (
546584 t , testData , testName ,
547- func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , error ) {
585+ func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , * common_info. FitError ) {
548586 return ni .IsTaskAllocatable (task ), ni .FittingError (task , false )
549587 },
550588 )
@@ -580,7 +618,7 @@ func TestIsTaskAllocatableOnReleasingOrIdle(t *testing.T) {
580618 t .Run (testName , func (t * testing.T ) {
581619 runAllocatableTest (
582620 t , testData , testName ,
583- func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , error ) {
621+ func (ni * NodeInfo , task * pod_info.PodInfo ) (bool , * common_info. FitError ) {
584622 return ni .IsTaskAllocatableOnReleasingOrIdle (task ), nil
585623 },
586624 )
@@ -611,6 +649,11 @@ func runAllocatableTest(
611649 "podToAllocate" , "p1" , "n1" , v1 .PodRunning , testData .podResourcesToAllocate ,
612650 []metav1.OwnerReference {}, make (map [string ]string ), testData .podAnnotations )
613651 addJobAnnotation (pod )
652+
653+ if len (testData .podOverhead ) > 0 {
654+ pod .Spec .Overhead = testData .podOverhead
655+ }
656+
614657 task := pod_info .NewTaskInfo (pod )
615658 allocatable , fitErr := testedFunction (ni , task )
616659 if allocatable != testData .expected {
@@ -622,6 +665,20 @@ func runAllocatableTest(
622665 t .Errorf ("%s: expected error message to contain %s, got %s" , testName , expectedMessage , fitErr .Error ())
623666 }
624667 }
668+
669+ if testData .expectOverheadMessage {
670+ if ! strings .Contains (fitErr .Error (), "Not enough resources due to pod overhead resources" ) {
671+ t .Errorf ("%s: expected overhead message, got %s" , testName , fitErr .Error ())
672+ }
673+ } else {
674+ fitErrMessage := fitErr .Error ()
675+ if strings .Contains (fitErrMessage , "Not enough resources due to pod overhead resources" ) {
676+ t .Errorf ("%s: unexpected overhead message, got %s" , testName , fitErrMessage )
677+ }
678+ }
679+ } else if len (testData .expectedMessageContains ) > 0 {
680+ // If we expected an error but got none, that's a test failure
681+ t .Errorf ("%s: expected error but got none" , testName )
625682 }
626683}
627684
0 commit comments