Skip to content

Commit

Permalink
add enqueueable or allocatable unit test case
Browse files Browse the repository at this point in the history
Signed-off-by: lowang-bh <[email protected]>
  • Loading branch information
lowang-bh committed Jul 6, 2024
1 parent 3ee5e8e commit 7d793e9
Show file tree
Hide file tree
Showing 2 changed files with 263 additions and 82 deletions.
103 changes: 102 additions & 1 deletion pkg/scheduler/plugins/capacity/capacity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,15 @@ limitations under the License.
package capacity

import (
"os"
"testing"

corev1 "k8s.io/api/core/v1"
schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"

"volcano.sh/volcano/cmd/scheduler/app/options"
"volcano.sh/volcano/pkg/scheduler/actions/allocate"
"volcano.sh/volcano/pkg/scheduler/actions/enqueue"
"volcano.sh/volcano/pkg/scheduler/actions/reclaim"
"volcano.sh/volcano/pkg/scheduler/api"
"volcano.sh/volcano/pkg/scheduler/conf"
Expand All @@ -33,11 +35,15 @@ import (
"volcano.sh/volcano/pkg/scheduler/util"
)

func TestMain(m *testing.M) {
options.Default()
os.Exit(m.Run())
}

func Test_capacityPlugin_OnSessionOpen(t *testing.T) {
plugins := map[string]framework.PluginBuilder{PluginName: New, predicates.PluginName: predicates.New}
trueValue := true
actions := []framework.Action{allocate.New(), reclaim.New()}
options.Default()

// nodes
n1 := util.BuildNode("n1", api.BuildResourceList("2", "4Gi", []api.ScalarResource{{Name: "pods", Value: "10"}}...), map[string]string{"selector": "worker"})
Expand Down Expand Up @@ -139,3 +145,98 @@ func Test_capacityPlugin_OnSessionOpen(t *testing.T) {
})
}
}

func TestEnqueueAndAllocable(t *testing.T) {
// nodes
n1 := util.BuildNode("n1", api.BuildResourceList("3", "3G", []api.ScalarResource{{Name: "pods", Value: "10"}}...), nil)
n2 := util.BuildNode("n2", api.BuildResourceList("3", "3G", []api.ScalarResource{{Name: "pods", Value: "10"}}...), nil)

// resources
res1c3g := api.BuildResourceList("1", "3G")
res3c1g := api.BuildResourceList("3", "1G")
res1c0g := api.BuildResourceList("1", "0G")
res0c1g := api.BuildResourceList("0", "1G")
res1c1g := api.BuildResourceList("1", "1G")
// pod
p1 := util.BuildPod("ns1", "pod1", "n1", corev1.PodRunning, res1c3g, "pg1", nil, nil)
p2 := util.BuildPod("ns1", "pod2", "n2", corev1.PodRunning, res3c1g, "pg2", nil, nil)
p3 := util.BuildPod("ns1", "pod3", "", corev1.PodPending, res1c0g, "pg3", nil, nil)
p4 := util.BuildPod("ns1", "pod4", "", corev1.PodPending, res0c1g, "pg4", nil, nil)
p5 := util.BuildPod("ns1", "pod5", "", corev1.PodPending, res1c1g, "pg5", nil, nil)

// podgroup
pg1 := util.BuildPodGroup("pg1", "ns1", "q1", 1, nil, schedulingv1beta1.PodGroupRunning)
pg2 := util.BuildPodGroup("pg2", "ns1", "q2", 1, nil, schedulingv1beta1.PodGroupRunning)
pg3 := util.BuildPodGroup("pg3", "ns1", "q1", 1, nil, schedulingv1beta1.PodGroupPending)
pg4 := util.BuildPodGroup("pg4", "ns1", "q2", 1, nil, schedulingv1beta1.PodGroupPending)
pg5 := util.BuildPodGroup("pg5", "ns1", "q1", 1, nil, schedulingv1beta1.PodGroupPending)
pg1.Spec.MinResources = &res1c3g
pg2.Spec.MinResources = &res3c1g
pg3.Spec.MinResources = &res1c0g
pg4.Spec.MinResources = &res0c1g
pg5.Spec.MinResources = &res1c1g

queue1 := util.BuildQueueWithResourcesQuantity("q1", api.BuildResourceList("2", "2G"), api.BuildResourceList("2", "2G"))
queue2 := util.BuildQueueWithResourcesQuantity("q2", api.BuildResourceList("2", "2G"), api.BuildResourceList("3", "3G"))

plugins := map[string]framework.PluginBuilder{PluginName: New}
trueValue := true
tiers := []conf.Tier{
{
Plugins: []conf.PluginOption{
{
Name: PluginName,
EnabledAllocatable: &trueValue,
EnablePreemptive: &trueValue,
EnabledOverused: &trueValue,
EnabledJobEnqueued: &trueValue,
},
},
},
}
tests := []uthelper.TestCommonStruct{
{
Name: "case0: memory exceed derserved, job only request cpu can be enqueued and allocated",
Plugins: plugins,
Pods: []*corev1.Pod{p1, p2, p3},
Nodes: []*corev1.Node{n1, n2},
PodGroups: []*schedulingv1beta1.PodGroup{pg1, pg2, pg3},
Queues: []*schedulingv1beta1.Queue{queue1, queue2},
ExpectBindsNum: 1,
ExpectBindMap: map[string]string{"ns1/pod3": "n1"},
},
{
Name: "case1: cpu exceed derserved, job only request memory can be enqueued and allocated",
Plugins: plugins,
Pods: []*corev1.Pod{p1, p2, p4},
Nodes: []*corev1.Node{n1, n2},
PodGroups: []*schedulingv1beta1.PodGroup{pg1, pg2, pg4},
Queues: []*schedulingv1beta1.Queue{queue1, queue2},
ExpectBindsNum: 1,
ExpectBindMap: map[string]string{"ns1/pod4": "n2"},
},
{
Name: "case2: exceed capacity, can not enqueue",
Plugins: plugins,
Pods: []*corev1.Pod{p1, p2, p5},
Nodes: []*corev1.Node{n1, n2},
PodGroups: []*schedulingv1beta1.PodGroup{pg1, pg2, pg5},
Queues: []*schedulingv1beta1.Queue{queue1, queue2},
ExpectBindsNum: 0,
ExpectBindMap: map[string]string{},
},
}
actions := []framework.Action{enqueue.New(), allocate.New()}

for i, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test.RegisterSession(tiers, nil)
defer test.Close()
test.Run(actions)

if err := test.CheckAll(i); err != nil {
t.Fatal(err)
}
})
}
}
Loading

0 comments on commit 7d793e9

Please sign in to comment.