@@ -24,6 +24,7 @@ import (
24
24
"time"
25
25
26
26
"k8s.io/api/core/v1"
27
+ "k8s.io/apimachinery/pkg/api/resource"
27
28
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
28
29
"k8s.io/apimachinery/pkg/runtime"
29
30
"k8s.io/apimachinery/pkg/util/sets"
@@ -653,3 +654,146 @@ func testGetBinderFunc(expectedBinderType, podName string, extenders []algorithm
653
654
t .Errorf ("Expected binder %q but got %q" , expectedBinderType , binderType )
654
655
}
655
656
}
657
+
658
+ func TestNodeAllocatableChanged (t * testing.T ) {
659
+ newQuantity := func (value int64 ) resource.Quantity {
660
+ return * resource .NewQuantity (value , resource .BinarySI )
661
+ }
662
+ for _ , c := range []struct {
663
+ Name string
664
+ Changed bool
665
+ OldAllocatable v1.ResourceList
666
+ NewAllocatable v1.ResourceList
667
+ }{
668
+ {
669
+ Name : "no allocatable resources changed" ,
670
+ Changed : false ,
671
+ OldAllocatable : v1.ResourceList {v1 .ResourceMemory : newQuantity (1024 )},
672
+ NewAllocatable : v1.ResourceList {v1 .ResourceMemory : newQuantity (1024 )},
673
+ },
674
+ {
675
+ Name : "new node has more allocatable resources" ,
676
+ Changed : true ,
677
+ OldAllocatable : v1.ResourceList {v1 .ResourceMemory : newQuantity (1024 )},
678
+ NewAllocatable : v1.ResourceList {v1 .ResourceMemory : newQuantity (1024 ), v1 .ResourceStorage : newQuantity (1024 )},
679
+ },
680
+ } {
681
+ oldNode := & v1.Node {Status : v1.NodeStatus {Allocatable : c .OldAllocatable }}
682
+ newNode := & v1.Node {Status : v1.NodeStatus {Allocatable : c .NewAllocatable }}
683
+ changed := nodeAllocatableChanged (newNode , oldNode )
684
+ if changed != c .Changed {
685
+ t .Errorf ("nodeAllocatableChanged should be %t, got %t" , c .Changed , changed )
686
+ }
687
+ }
688
+ }
689
+
690
+ func TestNodeLabelsChanged (t * testing.T ) {
691
+ for _ , c := range []struct {
692
+ Name string
693
+ Changed bool
694
+ OldLabels map [string ]string
695
+ NewLabels map [string ]string
696
+ }{
697
+ {
698
+ Name : "no labels changed" ,
699
+ Changed : false ,
700
+ OldLabels : map [string ]string {"foo" : "bar" },
701
+ NewLabels : map [string ]string {"foo" : "bar" },
702
+ },
703
+ // Labels changed.
704
+ {
705
+ Name : "new node has more labels" ,
706
+ Changed : true ,
707
+ OldLabels : map [string ]string {"foo" : "bar" },
708
+ NewLabels : map [string ]string {"foo" : "bar" , "test" : "value" },
709
+ },
710
+ } {
711
+ oldNode := & v1.Node {ObjectMeta : metav1.ObjectMeta {Labels : c .OldLabels }}
712
+ newNode := & v1.Node {ObjectMeta : metav1.ObjectMeta {Labels : c .NewLabels }}
713
+ changed := nodeLabelsChanged (newNode , oldNode )
714
+ if changed != c .Changed {
715
+ t .Errorf ("Test case %q failed: should be %t, got %t" , c .Name , c .Changed , changed )
716
+ }
717
+ }
718
+ }
719
+
720
+ func TestNodeTaintsChanged (t * testing.T ) {
721
+ for _ , c := range []struct {
722
+ Name string
723
+ Changed bool
724
+ OldTaints []v1.Taint
725
+ NewTaints []v1.Taint
726
+ }{
727
+ {
728
+ Name : "no taint changed" ,
729
+ Changed : false ,
730
+ OldTaints : []v1.Taint {{Key : "key" , Value : "value" }},
731
+ NewTaints : []v1.Taint {{Key : "key" , Value : "value" }},
732
+ },
733
+ {
734
+ Name : "taint value changed" ,
735
+ Changed : true ,
736
+ OldTaints : []v1.Taint {{Key : "key" , Value : "value1" }},
737
+ NewTaints : []v1.Taint {{Key : "key" , Value : "value2" }},
738
+ },
739
+ } {
740
+ oldNode := & v1.Node {Spec : v1.NodeSpec {Taints : c .OldTaints }}
741
+ newNode := & v1.Node {Spec : v1.NodeSpec {Taints : c .NewTaints }}
742
+ changed := nodeTaintsChanged (newNode , oldNode )
743
+ if changed != c .Changed {
744
+ t .Errorf ("Test case %q failed: should be %t, not %t" , c .Name , c .Changed , changed )
745
+ }
746
+ }
747
+ }
748
+
749
+ func TestNodeConditionsChanged (t * testing.T ) {
750
+ nodeConditionType := reflect .TypeOf (v1.NodeCondition {})
751
+ if nodeConditionType .NumField () != 6 {
752
+ t .Errorf ("NodeCondition type has changed. The nodeConditionsChanged() function must be reevaluated." )
753
+ }
754
+
755
+ for _ , c := range []struct {
756
+ Name string
757
+ Changed bool
758
+ OldConditions []v1.NodeCondition
759
+ NewConditions []v1.NodeCondition
760
+ }{
761
+ {
762
+ Name : "no condition changed" ,
763
+ Changed : false ,
764
+ OldConditions : []v1.NodeCondition {{Type : v1 .NodeOutOfDisk , Status : v1 .ConditionTrue }},
765
+ NewConditions : []v1.NodeCondition {{Type : v1 .NodeOutOfDisk , Status : v1 .ConditionTrue }},
766
+ },
767
+ {
768
+ Name : "only LastHeartbeatTime changed" ,
769
+ Changed : false ,
770
+ OldConditions : []v1.NodeCondition {{Type : v1 .NodeOutOfDisk , Status : v1 .ConditionTrue , LastHeartbeatTime : metav1 .Unix (1 , 0 )}},
771
+ NewConditions : []v1.NodeCondition {{Type : v1 .NodeOutOfDisk , Status : v1 .ConditionTrue , LastHeartbeatTime : metav1 .Unix (2 , 0 )}},
772
+ },
773
+ {
774
+ Name : "new node has more healthy conditions" ,
775
+ Changed : true ,
776
+ OldConditions : []v1.NodeCondition {},
777
+ NewConditions : []v1.NodeCondition {{Type : v1 .NodeReady , Status : v1 .ConditionTrue }},
778
+ },
779
+ {
780
+ Name : "new node has less unhealthy conditions" ,
781
+ Changed : true ,
782
+ OldConditions : []v1.NodeCondition {{Type : v1 .NodeOutOfDisk , Status : v1 .ConditionTrue }},
783
+ NewConditions : []v1.NodeCondition {},
784
+ },
785
+ {
786
+ Name : "condition status changed" ,
787
+ Changed : true ,
788
+ OldConditions : []v1.NodeCondition {{Type : v1 .NodeReady , Status : v1 .ConditionFalse }},
789
+ NewConditions : []v1.NodeCondition {{Type : v1 .NodeReady , Status : v1 .ConditionTrue }},
790
+ },
791
+ } {
792
+ oldNode := & v1.Node {Status : v1.NodeStatus {Conditions : c .OldConditions }}
793
+ newNode := & v1.Node {Status : v1.NodeStatus {Conditions : c .NewConditions }}
794
+ changed := nodeConditionsChanged (newNode , oldNode )
795
+ if changed != c .Changed {
796
+ t .Errorf ("Test case %q failed: should be %t, got %t" , c .Name , c .Changed , changed )
797
+ }
798
+ }
799
+ }
0 commit comments