Skip to content

Commit

Permalink
Merge pull request volcano-sh#3345 from lowang-bh/refactAllocateUT
Browse files Browse the repository at this point in the history
Refactor allocate action's UTs
  • Loading branch information
volcano-sh-bot authored Apr 11, 2024
2 parents c3b7ee8 + e32243b commit 607747e
Showing 1 changed file with 66 additions and 111 deletions.
177 changes: 66 additions & 111 deletions pkg/scheduler/actions/allocate/allocate_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,89 +24,68 @@ import (

"github.com/agiledragon/gomonkey/v2"
v1 "k8s.io/api/core/v1"
storagev1 "k8s.io/api/storage/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/client-go/kubernetes/fake"
"k8s.io/client-go/tools/record"

"volcano.sh/volcano/pkg/scheduler/plugins/gang"
"volcano.sh/volcano/pkg/scheduler/plugins/priority"

storagev1 "k8s.io/api/storage/v1"

schedulingv1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
"volcano.sh/volcano/cmd/scheduler/app/options"
"volcano.sh/volcano/pkg/scheduler/api"
"volcano.sh/volcano/pkg/scheduler/cache"
"volcano.sh/volcano/pkg/scheduler/conf"
"volcano.sh/volcano/pkg/scheduler/framework"
"volcano.sh/volcano/pkg/scheduler/plugins/drf"
"volcano.sh/volcano/pkg/scheduler/plugins/gang"
"volcano.sh/volcano/pkg/scheduler/plugins/nodeorder"
"volcano.sh/volcano/pkg/scheduler/plugins/predicates"
"volcano.sh/volcano/pkg/scheduler/plugins/priority"
"volcano.sh/volcano/pkg/scheduler/plugins/proportion"
"volcano.sh/volcano/pkg/scheduler/uthelper"
"volcano.sh/volcano/pkg/scheduler/util"
)

func TestAllocate(t *testing.T) {
var tmp *cache.SchedulerCache
patches := gomonkey.ApplyMethod(reflect.TypeOf(tmp), "AddBindTask", func(scCache *cache.SchedulerCache, task *api.TaskInfo) error {
scCache.Binder.Bind(nil, []*api.TaskInfo{task})
return nil
})
defer patches.Reset()

patchUpdateQueueStatus := gomonkey.ApplyMethod(reflect.TypeOf(tmp), "UpdateQueueStatus", func(scCache *cache.SchedulerCache, queue *api.QueueInfo) error {
return nil
})
defer patchUpdateQueueStatus.Reset()

framework.RegisterPluginBuilder("drf", drf.New)
framework.RegisterPluginBuilder("proportion", proportion.New)

options.ServerOpts = &options.ServerOption{
MinNodesToFind: 100,
MinPercentageOfNodesToFind: 5,
PercentageOfNodesToFind: 100,
plugins := map[string]framework.PluginBuilder{
drf.PluginName: drf.New,
proportion.PluginName: proportion.New,
predicates.PluginName: predicates.New,
nodeorder.PluginName: nodeorder.New,
}

defer framework.CleanupPluginBuilders()

tests := []struct {
name string
podGroups []*schedulingv1.PodGroup
pods []*v1.Pod
nodes []*v1.Node
queues []*schedulingv1.Queue
expected map[string]string
}{
options.Default()
tests := []uthelper.TestCommonStruct{
{
name: "one Job with two Pods on one node",
podGroups: []*schedulingv1.PodGroup{
Name: "one Job with two Pods on one node",
PodGroups: []*schedulingv1.PodGroup{
util.BuildPodGroup("pg1", "c1", "c1", 0, nil, schedulingv1.PodGroupInqueue),
},
pods: []*v1.Pod{
Pods: []*v1.Pod{
util.BuildPod("c1", "p1", "", v1.PodPending, api.BuildResourceList("1", "1G"), "pg1", make(map[string]string), make(map[string]string)),
util.BuildPod("c1", "p2", "", v1.PodPending, api.BuildResourceList("1", "1G"), "pg1", make(map[string]string), make(map[string]string)),
},
nodes: []*v1.Node{
Nodes: []*v1.Node{
util.BuildNode("n1", api.BuildResourceList("2", "4Gi", []api.ScalarResource{{Name: "pods", Value: "10"}}...), make(map[string]string)),
},
queues: []*schedulingv1.Queue{
Queues: []*schedulingv1.Queue{
util.BuildQueue("c1", 1, nil),
},
expected: map[string]string{
Bind: map[string]string{
"c1/p1": "n1",
"c1/p2": "n1",
},
BindsNum: 2,
},
{
name: "two Jobs on one node",
podGroups: []*schedulingv1.PodGroup{
Name: "two Jobs on one node",
PodGroups: []*schedulingv1.PodGroup{
util.BuildPodGroup("pg1", "c1", "c1", 0, nil, schedulingv1.PodGroupInqueue),
util.BuildPodGroup("pg2", "c2", "c2", 0, nil, schedulingv1.PodGroupInqueue),
},

// pod name should be like "*-*-{index}",
// due to change of TaskOrderFn
pods: []*v1.Pod{
Pods: []*v1.Pod{
// pending pod with owner1, under c1
util.BuildPod("c1", "pg1-p-1", "", v1.PodPending, api.BuildResourceList("1", "1G"), "pg1", make(map[string]string), make(map[string]string)),
// pending pod with owner1, under c1
Expand All @@ -116,105 +95,81 @@ func TestAllocate(t *testing.T) {
// pending pod with owner2, under c2
util.BuildPod("c2", "pg2-p-2", "", v1.PodPending, api.BuildResourceList("1", "1G"), "pg2", make(map[string]string), make(map[string]string)),
},
nodes: []*v1.Node{
Nodes: []*v1.Node{
util.BuildNode("n1", api.BuildResourceList("2", "4G", []api.ScalarResource{{Name: "pods", Value: "10"}}...), make(map[string]string)),
},
queues: []*schedulingv1.Queue{
Queues: []*schedulingv1.Queue{
util.BuildQueue("c1", 1, nil),
util.BuildQueue("c2", 1, nil),
},
expected: map[string]string{
Bind: map[string]string{
"c2/pg2-p-1": "n1",
"c1/pg1-p-1": "n1",
},
BindsNum: 2,
},
{
name: "high priority queue should not block others",
podGroups: []*schedulingv1.PodGroup{
Name: "high priority queue should not block others",
PodGroups: []*schedulingv1.PodGroup{
util.BuildPodGroup("pg1", "c1", "c1", 0, nil, schedulingv1.PodGroupInqueue),
util.BuildPodGroup("pg2", "c1", "c2", 0, nil, schedulingv1.PodGroupInqueue),
},

pods: []*v1.Pod{
Pods: []*v1.Pod{
// pending pod with owner1, under ns:c1/q:c1
util.BuildPod("c1", "p1", "", v1.PodPending, api.BuildResourceList("3", "1G"), "pg1", make(map[string]string), make(map[string]string)),
// pending pod with owner2, under ns:c1/q:c2
util.BuildPod("c1", "p2", "", v1.PodPending, api.BuildResourceList("1", "1G"), "pg2", make(map[string]string), make(map[string]string)),
},
nodes: []*v1.Node{
Nodes: []*v1.Node{
util.BuildNode("n1", api.BuildResourceList("2", "4G", []api.ScalarResource{{Name: "pods", Value: "10"}}...), make(map[string]string)),
},
queues: []*schedulingv1.Queue{
Queues: []*schedulingv1.Queue{
util.BuildQueue("c1", 1, nil),
util.BuildQueue("c2", 1, nil),
},
expected: map[string]string{
Bind: map[string]string{
"c1/p2": "n1",
},
BindsNum: 1,
},
}

allocate := New()

for _, test := range tests {
if test.name == "two Jobs on one node" {
// TODO(wangyang0616): First make sure that ut can run, and then fix the failed ut later
// See issue for details: https://github.com/volcano-sh/volcano/issues/2810
t.Skip("Test cases are not as expected, fixed later. see issue: #2810")
}
t.Run(test.name, func(t *testing.T) {
binder := &util.FakeBinder{
Binds: map[string]string{},
Channel: make(chan string, 10),
}
schedulerCache := &cache.SchedulerCache{
Nodes: make(map[string]*api.NodeInfo),
Jobs: make(map[api.JobID]*api.JobInfo),
Queues: make(map[api.QueueID]*api.QueueInfo),
Binder: binder,
StatusUpdater: &util.FakeStatusUpdater{},
VolumeBinder: &util.FakeVolumeBinder{},
Recorder: record.NewFakeRecorder(100),
}

for _, node := range test.nodes {
schedulerCache.AddOrUpdateNode(node)
}
for _, pod := range test.pods {
schedulerCache.AddPod(pod)
}

for _, ss := range test.podGroups {
schedulerCache.AddPodGroupV1beta1(ss)
}

for _, q := range test.queues {
schedulerCache.AddQueueV1beta1(q)
}

trueValue := true
ssn := framework.OpenSession(schedulerCache, []conf.Tier{
trueValue := true
tiers := []conf.Tier{
{
Plugins: []conf.PluginOption{
{
Plugins: []conf.PluginOption{
{
Name: "drf",
EnabledPreemptable: &trueValue,
EnabledJobOrder: &trueValue,
},
{
Name: "proportion",
EnabledQueueOrder: &trueValue,
EnabledReclaimable: &trueValue,
},
},
Name: "drf",
EnabledPreemptable: &trueValue,
EnabledJobOrder: &trueValue,
},
}, nil)
defer framework.CloseSession(ssn)

allocate.Execute(ssn)
{
Name: "proportion",
EnabledQueueOrder: &trueValue,
EnabledReclaimable: &trueValue,
EnabledAllocatable: &trueValue,
},
{
Name: predicates.PluginName,
EnabledPredicate: &trueValue,
},
{
Name: nodeorder.PluginName,
EnabledNodeOrder: &trueValue,
},
},
},
}

if !reflect.DeepEqual(test.expected, binder.Binds) {
t.Errorf("expected: %v, got %v ", test.expected, binder.Binds)
for i, test := range tests {
t.Run(test.Name, func(t *testing.T) {
test.Plugins = plugins
test.RegistSession(tiers, nil)
defer test.Close()
test.Run([]framework.Action{New()})
if err := test.CheckAll(i); err != nil {
t.Fatal(err)
}
})
}
Expand Down

0 comments on commit 607747e

Please sign in to comment.