@@ -17,6 +17,7 @@ limitations under the License.
17
17
package trimaran
18
18
19
19
import (
20
+ "math"
20
21
"reflect"
21
22
"strconv"
22
23
"testing"
@@ -375,6 +376,13 @@ func TestGetNodeRequestsAndLimits(t *testing.T) {
375
376
376
377
testNode := st .MakeNode ().Name ("test-node" ).Capacity (nr ).Obj ()
377
378
379
+ nr = map [v1.ResourceName ]string {
380
+ v1 .ResourceCPU : "1600m" ,
381
+ v1 .ResourceMemory : "6Ki" ,
382
+ }
383
+
384
+ testNodeWithLowNodeCapacity := st .MakeNode ().Name ("test-node" ).Capacity (nr ).Obj ()
385
+
378
386
var initCPUReq int64 = 100
379
387
var initMemReq int64 = 2048
380
388
contCPUReq := []int64 {1000 , 500 }
@@ -406,6 +414,20 @@ func TestGetNodeRequestsAndLimits(t *testing.T) {
406
414
podRequests := GetResourceRequested (pod )
407
415
podLimits := GetResourceLimits (pod )
408
416
417
+ allocatableResources := testNodeWithLowNodeCapacity .Status .Allocatable
418
+ amCpu := allocatableResources [v1 .ResourceCPU ]
419
+ capCpu := amCpu .MilliValue ()
420
+ amMem := allocatableResources [v1 .ResourceMemory ]
421
+ capMem := amMem .Value ()
422
+
423
+ contCPULimit = []int64 {1000 , 400 }
424
+
425
+ pod4 := getPodWithContainersAndOverhead (0 , initCPUReq , initMemReq , contCPUReq , contMemReq )
426
+ pod4 = getPodWithLimits (pod4 , initCPULimit , initMemLimit , contCPULimit , contMemLimit )
427
+
428
+ pod4Requests := GetResourceRequested (pod4 )
429
+ pod4Limits := GetResourceLimits (pod4 )
430
+
409
431
type args struct {
410
432
podsOnNode []* framework.PodInfo
411
433
node * v1.Node
@@ -485,12 +507,91 @@ func TestGetNodeRequestsAndLimits(t *testing.T) {
485
507
},
486
508
},
487
509
},
510
+ {
511
+ name : "test-2" ,
512
+ // Test case for Node with low capacity than the requested Pod limits
513
+ args : args {
514
+ podsOnNode : []* framework.PodInfo {},
515
+ node : testNodeWithLowNodeCapacity ,
516
+ pod : pod ,
517
+ podRequests : podRequests ,
518
+ podLimits : podLimits ,
519
+ },
520
+ want : & NodeRequestsAndLimits {
521
+ NodeRequest : & framework.Resource {
522
+ MilliCPU : int64 (math .Min (float64 (GetResourceRequested (pod ).MilliCPU ), float64 (capCpu ))),
523
+ Memory : int64 (math .Min (float64 (GetResourceRequested (pod ).Memory ), float64 (capMem ))),
524
+ },
525
+ NodeLimit : & framework.Resource {
526
+ MilliCPU : int64 (math .Max (float64 (GetResourceRequested (pod ).MilliCPU ), float64 (GetResourceLimits (pod ).MilliCPU ))),
527
+ Memory : int64 (math .Max (float64 (GetResourceRequested (pod ).Memory ), float64 (GetResourceLimits (pod ).Memory ))),
528
+ },
529
+ NodeRequestMinusPod : & framework.Resource {
530
+ MilliCPU : 0 ,
531
+ Memory : 0 ,
532
+ },
533
+ NodeLimitMinusPod : & framework.Resource {
534
+ MilliCPU : 0 ,
535
+ Memory : 0 ,
536
+ },
537
+ Nodecapacity : & framework.Resource {
538
+ MilliCPU : capCpu ,
539
+ Memory : capMem ,
540
+ },
541
+ },
542
+ },
543
+ {
544
+ name : "test-3" ,
545
+ // Test case for Pod with more Requests than Limits
546
+ args : args {
547
+ podsOnNode : []* framework.PodInfo {},
548
+ node : testNodeWithLowNodeCapacity ,
549
+ pod : pod4 ,
550
+ podRequests : pod4Requests ,
551
+ podLimits : pod4Limits ,
552
+ },
553
+ want : & NodeRequestsAndLimits {
554
+ NodeRequest : & framework.Resource {
555
+ MilliCPU : int64 (math .Min (float64 (GetResourceRequested (pod4 ).MilliCPU ), float64 (capCpu ))),
556
+ Memory : int64 (math .Min (float64 (GetResourceRequested (pod4 ).Memory ), float64 (capMem ))),
557
+ },
558
+ NodeLimit : & framework.Resource {
559
+ MilliCPU : int64 (math .Min (
560
+ math .Max (float64 (GetResourceRequested (pod4 ).MilliCPU ), float64 (GetResourceLimits (pod4 ).MilliCPU )),
561
+ float64 (capCpu ))),
562
+ Memory : int64 (math .Min (
563
+ math .Max (float64 (GetResourceRequested (pod4 ).Memory ), float64 (GetResourceLimits (pod4 ).Memory )),
564
+ float64 (capMem ))),
565
+ },
566
+ NodeRequestMinusPod : & framework.Resource {
567
+ MilliCPU : 0 ,
568
+ Memory : 0 ,
569
+ },
570
+ NodeLimitMinusPod : & framework.Resource {
571
+ MilliCPU : 0 ,
572
+ Memory : 0 ,
573
+ },
574
+ Nodecapacity : & framework.Resource {
575
+ MilliCPU : capCpu ,
576
+ Memory : capMem ,
577
+ },
578
+ },
579
+ },
488
580
}
489
581
for _ , tt := range tests {
490
582
t .Run (tt .name , func (t * testing.T ) {
491
- if got := GetNodeRequestsAndLimits (tt .args .podsOnNode , tt .args .node , tt .args .pod ,
583
+ var got * NodeRequestsAndLimits
584
+ SetMaxLimits (tt .args .podRequests , tt .args .podLimits )
585
+ if got = GetNodeRequestsAndLimits (tt .args .podsOnNode , tt .args .node , tt .args .pod ,
492
586
tt .args .podRequests , tt .args .podLimits ); ! reflect .DeepEqual (got , tt .want ) {
493
- t .Errorf ("GetNodeRequestsAndLimits() = %v, want %v" , got , tt .want )
587
+ t .Errorf ("GetNodeRequestsAndLimits(): got = {%+v, %+v, %+v, %+v, %+v}, want = {%+v, %+v, %+v, %+v, %+v}" ,
588
+ * got .NodeRequest , * got .NodeLimit , * got .NodeRequestMinusPod , * got .NodeLimitMinusPod , * got .Nodecapacity ,
589
+ * tt .want .NodeRequest , * tt .want .NodeLimit , * tt .want .NodeRequestMinusPod , * tt .want .NodeLimitMinusPod , * tt .want .Nodecapacity )
590
+ }
591
+ // The below asserts should hold good for all test cases, ideally; "test-3" exercises this by setting NodeLimit less than NodeRequest
592
+ if tt .name == "test-3" {
593
+ assert .LessOrEqual (t , (* got .NodeRequest ).MilliCPU , (* got .NodeLimit ).MilliCPU )
594
+ assert .LessOrEqual (t , (* got .NodeRequest ).Memory , (* got .NodeLimit ).Memory )
494
595
}
495
596
})
496
597
}
0 commit comments