volcano.sh/volcano@v1.9.0/pkg/scheduler/plugins/predicates/predicates_test.go (about) 1 package predicates 2 3 import ( 4 "testing" 5 6 apiv1 "k8s.io/api/core/v1" 7 schedulingv1 "k8s.io/api/scheduling/v1" 8 metav1 "k8s.io/apimachinery/pkg/apis/meta/v1" 9 10 schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1" 11 "volcano.sh/volcano/pkg/scheduler/actions/allocate" 12 "volcano.sh/volcano/pkg/scheduler/api" 13 "volcano.sh/volcano/pkg/scheduler/conf" 14 "volcano.sh/volcano/pkg/scheduler/framework" 15 "volcano.sh/volcano/pkg/scheduler/plugins/gang" 16 "volcano.sh/volcano/pkg/scheduler/plugins/priority" 17 "volcano.sh/volcano/pkg/scheduler/uthelper" 18 "volcano.sh/volcano/pkg/scheduler/util" 19 ) 20 21 func getWorkerAffinity() *apiv1.Affinity { 22 return &apiv1.Affinity{ 23 PodAntiAffinity: &apiv1.PodAntiAffinity{ 24 RequiredDuringSchedulingIgnoredDuringExecution: []apiv1.PodAffinityTerm{ 25 { 26 LabelSelector: &metav1.LabelSelector{ 27 MatchExpressions: []metav1.LabelSelectorRequirement{ 28 { 29 Key: "role", 30 Operator: "In", 31 Values: []string{"worker"}, 32 }, 33 }, 34 }, 35 TopologyKey: "kubernetes.io/hostname", 36 }, 37 }, 38 }, 39 } 40 } 41 42 func TestEventHandler(t *testing.T) { 43 plugins := map[string]framework.PluginBuilder{ 44 PluginName: New, 45 gang.PluginName: gang.New, 46 priority.PluginName: priority.New, 47 } 48 49 // pending pods 50 w1 := util.BuildPod("ns1", "worker-1", "", apiv1.PodPending, api.BuildResourceList("3", "3k"), "pg1", map[string]string{"role": "worker"}, map[string]string{"selector": "worker"}) 51 w2 := util.BuildPod("ns1", "worker-2", "", apiv1.PodPending, api.BuildResourceList("5", "5k"), "pg1", map[string]string{"role": "worker"}, map[string]string{}) 52 w3 := util.BuildPod("ns1", "worker-3", "", apiv1.PodPending, api.BuildResourceList("4", "4k"), "pg2", map[string]string{"role": "worker"}, map[string]string{}) 53 w1.Spec.Affinity = getWorkerAffinity() 54 w2.Spec.Affinity = getWorkerAffinity() 55 w3.Spec.Affinity = getWorkerAffinity() 56 57 // nodes 58 n1 := util.BuildNode("node1", api.BuildResourceList("4", "4k", []api.ScalarResource{{Name: "pods", Value: "10"}}...), map[string]string{"selector": "worker"}) 59 n2 := util.BuildNode("node2", api.BuildResourceList("3", "3k", []api.ScalarResource{{Name: "pods", Value: "10"}}...), map[string]string{}) 60 n1.Labels["kubernetes.io/hostname"] = "node1" 61 n2.Labels["kubernetes.io/hostname"] = "node2" 62 63 // priority 64 p1 := &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "p1"}, Value: 1} 65 p2 := &schedulingv1.PriorityClass{ObjectMeta: metav1.ObjectMeta{Name: "p2"}, Value: 2} 66 // podgroup 67 pg1 := util.BuildPodGroupWithPrio("pg1", "ns1", "q1", 2, nil, schedulingv1beta1.PodGroupInqueue, p2.Name) 68 pg2 := util.BuildPodGroupWithPrio("pg2", "ns1", "q1", 1, nil, schedulingv1beta1.PodGroupInqueue, p1.Name) 69 70 // queue 71 queue1 := util.BuildQueue("q1", 0, nil) 72 73 // tests 74 tests := []uthelper.TestCommonStruct{ 75 { 76 Name: "pod-deallocate", 77 Plugins: plugins, 78 Pods: []*apiv1.Pod{w1, w2, w3}, 79 Nodes: []*apiv1.Node{n1, n2}, 80 PriClass: []*schedulingv1.PriorityClass{p1, p2}, 81 PodGroups: []*schedulingv1beta1.PodGroup{pg1, pg2}, 82 Queues: []*schedulingv1beta1.Queue{queue1}, 83 Bind: map[string]string{ // podKey -> node 84 "ns1/worker-3": "node1", 85 }, 86 BindsNum: 1, 87 }, 88 } 89 90 for i, test := range tests { 91 // allocate 92 actions := []framework.Action{allocate.New()} 93 trueValue := true 94 tiers := []conf.Tier{ 95 { 96 Plugins: []conf.PluginOption{ 97 { 98 Name: PluginName, 99 EnabledPredicate: &trueValue, 100 }, 101 { 102 Name: gang.PluginName, 103 EnabledJobReady: &trueValue, 104 EnabledJobPipelined: &trueValue, 105 }, 106 { 107 Name: priority.PluginName, 108 EnabledJobOrder: &trueValue, 109 }, 110 }, 111 }, 112 } 113 t.Run(test.Name, func(t *testing.T) { 114 test.RegistSession(tiers, nil) 115 defer test.Close() 116 test.Run(actions) 117 if err := test.CheckAll(i); err != nil { 118 t.Fatal(err) 119 } 120 }) 121 } 122 }