@@ -17,6 +17,7 @@ limitations under the License.
17
17
package frameworkext
18
18
19
19
import (
20
+ "encoding/json"
20
21
"sort"
21
22
"testing"
22
23
"time"
@@ -733,6 +734,7 @@ func TestReservationInfoRefreshAvailable(t *testing.T) {
733
734
},
734
735
},
735
736
},
737
+
736
738
Owners : []schedulingv1alpha1.ReservationOwner {
737
739
{
738
740
LabelSelector : & metav1.LabelSelector {
@@ -743,6 +745,7 @@ func TestReservationInfoRefreshAvailable(t *testing.T) {
743
745
},
744
746
},
745
747
},
748
+
746
749
Status : schedulingv1alpha1.ReservationStatus {
747
750
NodeName : "test-node" ,
748
751
Phase : schedulingv1alpha1 .ReasonReservationAvailable ,
@@ -792,3 +795,126 @@ func TestReservationInfoRefreshAvailable(t *testing.T) {
792
795
assert .Equal (t , expectedRInfo , rInfo )
793
796
})
794
797
}
798
+
799
+ func TestReservationInfoMatchReservationAffinity (t * testing.T ) {
800
+ tests := []struct {
801
+ name string
802
+ reservation * schedulingv1alpha1.Reservation
803
+ reservationAffinity * apiext.ReservationAffinity
804
+ want bool
805
+ }{
806
+ {
807
+ name : "nothing to match" ,
808
+ reservation : & schedulingv1alpha1.Reservation {
809
+ Spec : schedulingv1alpha1.ReservationSpec {
810
+ Owners : []schedulingv1alpha1.ReservationOwner {
811
+ {
812
+ LabelSelector : & metav1.LabelSelector {
813
+ MatchLabels : map [string ]string {
814
+ "app" : "test" ,
815
+ },
816
+ },
817
+ },
818
+ },
819
+ },
820
+ },
821
+ want : true ,
822
+ },
823
+ {
824
+ name : "match reservation affinity" ,
825
+ reservation : & schedulingv1alpha1.Reservation {
826
+ ObjectMeta : metav1.ObjectMeta {
827
+ Labels : map [string ]string {
828
+ "reservation-type" : "reservation-test" ,
829
+ },
830
+ },
831
+ Spec : schedulingv1alpha1.ReservationSpec {
832
+ Owners : []schedulingv1alpha1.ReservationOwner {
833
+ {
834
+ LabelSelector : & metav1.LabelSelector {
835
+ MatchLabels : map [string ]string {
836
+ "app" : "test" ,
837
+ },
838
+ },
839
+ },
840
+ },
841
+ },
842
+ },
843
+ reservationAffinity : & apiext.ReservationAffinity {
844
+ RequiredDuringSchedulingIgnoredDuringExecution : & apiext.ReservationAffinitySelector {
845
+ ReservationSelectorTerms : []corev1.NodeSelectorTerm {
846
+ {
847
+ MatchExpressions : []corev1.NodeSelectorRequirement {
848
+ {
849
+ Key : "reservation-type" ,
850
+ Operator : corev1 .NodeSelectorOpIn ,
851
+ Values : []string {"reservation-test" },
852
+ },
853
+ },
854
+ },
855
+ },
856
+ },
857
+ },
858
+ want : true ,
859
+ },
860
+ {
861
+ name : "not match reservation affinity" ,
862
+ reservation : & schedulingv1alpha1.Reservation {
863
+ ObjectMeta : metav1.ObjectMeta {
864
+ Labels : map [string ]string {
865
+ "reservation-type" : "reservation-test-not-match" ,
866
+ },
867
+ },
868
+ Spec : schedulingv1alpha1.ReservationSpec {
869
+ Owners : []schedulingv1alpha1.ReservationOwner {
870
+ {
871
+ LabelSelector : & metav1.LabelSelector {
872
+ MatchLabels : map [string ]string {
873
+ "app" : "test" ,
874
+ },
875
+ },
876
+ },
877
+ },
878
+ },
879
+ },
880
+ reservationAffinity : & apiext.ReservationAffinity {
881
+ RequiredDuringSchedulingIgnoredDuringExecution : & apiext.ReservationAffinitySelector {
882
+ ReservationSelectorTerms : []corev1.NodeSelectorTerm {
883
+ {
884
+ MatchExpressions : []corev1.NodeSelectorRequirement {
885
+ {
886
+ Key : "reservation-type" ,
887
+ Operator : corev1 .NodeSelectorOpIn ,
888
+ Values : []string {"reservation-test" },
889
+ },
890
+ },
891
+ },
892
+ },
893
+ },
894
+ },
895
+ want : false ,
896
+ },
897
+ }
898
+ for _ , tt := range tests {
899
+ t .Run (tt .name , func (t * testing.T ) {
900
+ pod := & corev1.Pod {
901
+ ObjectMeta : metav1.ObjectMeta {
902
+ Name : "test-pod" ,
903
+ },
904
+ }
905
+ if tt .reservationAffinity != nil {
906
+ affinityData , err := json .Marshal (tt .reservationAffinity )
907
+ assert .NoError (t , err )
908
+ if pod .Annotations == nil {
909
+ pod .Annotations = map [string ]string {}
910
+ }
911
+ pod .Annotations [apiext .AnnotationReservationAffinity ] = string (affinityData )
912
+ }
913
+ reservationAffinity , err := reservationutil .GetRequiredReservationAffinity (pod )
914
+ assert .NoError (t , err )
915
+ rInfo := NewReservationInfo (tt .reservation )
916
+ got := rInfo .MatchReservationAffinity (reservationAffinity , & corev1.Node {})
917
+ assert .Equal (t , tt .want , got )
918
+ })
919
+ }
920
+ }
0 commit comments