volcano.sh/volcano@v1.9.0/pkg/scheduler/plugins/capacity/capacity_test.go (about)

     1  /*
     2  Copyright 2024 The Volcano Authors.
     3  
     4  Licensed under the Apache License, Version 2.0 (the "License");
     5  you may not use this file except in compliance with the License.
     6  You may obtain a copy of the License at
     7  
     8      http://www.apache.org/licenses/LICENSE-2.0
     9  
    10  Unless required by applicable law or agreed to in writing, software
    11  distributed under the License is distributed on an "AS IS" BASIS,
    12  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  See the License for the specific language governing permissions and
    14  limitations under the License.
    15  */
    16  
    17  package capacity
    18  
    19  import (
    20  	"testing"
    21  
    22  	corev1 "k8s.io/api/core/v1"
    23  	schedulingv1beta1 "volcano.sh/apis/pkg/apis/scheduling/v1beta1"
    24  
    25  	"volcano.sh/volcano/cmd/scheduler/app/options"
    26  	"volcano.sh/volcano/pkg/scheduler/actions/allocate"
    27  	"volcano.sh/volcano/pkg/scheduler/actions/reclaim"
    28  	"volcano.sh/volcano/pkg/scheduler/api"
    29  	"volcano.sh/volcano/pkg/scheduler/conf"
    30  	"volcano.sh/volcano/pkg/scheduler/framework"
    31  	"volcano.sh/volcano/pkg/scheduler/plugins/predicates"
    32  	"volcano.sh/volcano/pkg/scheduler/uthelper"
    33  	"volcano.sh/volcano/pkg/scheduler/util"
    34  )
    35  
    36  func Test_capacityPlugin_OnSessionOpen(t *testing.T) {
    37  	plugins := map[string]framework.PluginBuilder{PluginName: New, predicates.PluginName: predicates.New}
    38  	trueValue := true
    39  	actions := []framework.Action{allocate.New(), reclaim.New()}
    40  	options.Default()
    41  
    42  	// nodes
    43  	n1 := util.BuildNode("n1", api.BuildResourceList("2", "4Gi", []api.ScalarResource{{Name: "pods", Value: "10"}}...), map[string]string{"selector": "worker"})
    44  	n2 := util.BuildNode("n2", api.BuildResourceList("2", "4Gi", []api.ScalarResource{{Name: "pods", Value: "10"}}...), map[string]string{})
    45  
    46  	// resources for test case 0
    47  	// pod
    48  	p1 := util.BuildPod("ns1", "p1", "n1", corev1.PodRunning, api.BuildResourceList("1", "1Gi"), "pg1", make(map[string]string), make(map[string]string))
    49  	p2 := util.BuildPod("ns1", "p2", "", corev1.PodPending, api.BuildResourceList("1", "1Gi"), "pg2", make(map[string]string), map[string]string{"selector": "worker"})
    50  	// podgroup
    51  	pg1 := util.BuildPodGroup("pg1", "ns1", "q1", 1, nil, schedulingv1beta1.PodGroupRunning)
    52  	pg2 := util.BuildPodGroup("pg2", "ns1", "q1", 1, nil, schedulingv1beta1.PodGroupInqueue)
    53  	// queue
    54  	queue1 := util.BuildQueueWithResourcesQuantity("q1", nil, api.BuildResourceList("2", "2Gi"))
    55  
    56  	// resources for test case 1
    57  	// pod
    58  	p3 := util.BuildPod("ns1", "p3", "n1", corev1.PodRunning, api.BuildResourceList("1", "1Gi"), "pg3", make(map[string]string), make(map[string]string))
    59  	p4 := util.BuildPod("ns1", "p4", "", corev1.PodPending, api.BuildResourceList("1", "1Gi"), "pg4", make(map[string]string), make(map[string]string))
    60  	// podgroup
    61  	pg3 := util.BuildPodGroup("pg3", "ns1", "q2", 1, nil, schedulingv1beta1.PodGroupRunning)
    62  	pg4 := util.BuildPodGroup("pg4", "ns1", "q2", 1, nil, schedulingv1beta1.PodGroupInqueue)
    63  	// queue
    64  	queue2 := util.BuildQueueWithResourcesQuantity("q2", nil, api.BuildResourceList("1.5", "1.5Gi"))
    65  
    66  	// resources for test case 2
    67  	// pod
    68  	p5 := util.BuildPod("ns1", "p5", "n1", corev1.PodRunning, api.BuildResourceList("2", "4Gi"), "pg5", map[string]string{schedulingv1beta1.PodPreemptable: "false"}, make(map[string]string))
    69  	p6 := util.BuildPod("ns1", "p6", "n2", corev1.PodRunning, api.BuildResourceList("2", "4Gi"), "pg5", make(map[string]string), make(map[string]string))
    70  	p7 := util.BuildPod("ns1", "p7", "", corev1.PodPending, api.BuildResourceList("2", "4Gi"), "pg6", make(map[string]string), make(map[string]string))
    71  	// podgroup
    72  	pg5 := util.BuildPodGroup("pg5", "ns1", "q3", 1, nil, schedulingv1beta1.PodGroupRunning)
    73  	pg6 := util.BuildPodGroup("pg6", "ns1", "q4", 1, nil, schedulingv1beta1.PodGroupInqueue)
    74  	// queue
    75  	queue3 := util.BuildQueueWithResourcesQuantity("q3", api.BuildResourceList("2", "4Gi"), nil)
    76  	queue4 := util.BuildQueueWithResourcesQuantity("q4", api.BuildResourceList("2", "4Gi"), nil)
    77  
    78  	tests := []uthelper.TestCommonStruct{
    79  		{
    80  			Name:      "case0: Pod allocatable when queue has not exceed capability",
    81  			Plugins:   plugins,
    82  			Pods:      []*corev1.Pod{p1, p2},
    83  			Nodes:     []*corev1.Node{n1, n2},
    84  			PodGroups: []*schedulingv1beta1.PodGroup{pg1, pg2},
    85  			Queues:    []*schedulingv1beta1.Queue{queue1},
    86  			Bind: map[string]string{
    87  				"ns1/p2": "n1",
    88  			},
    89  			BindsNum: 1,
    90  		},
    91  		{
    92  			Name:      "case1: Pod not allocatable when queue exceed queue capability",
    93  			Plugins:   plugins,
    94  			Pods:      []*corev1.Pod{p3, p4},
    95  			Nodes:     []*corev1.Node{n1, n2},
    96  			PodGroups: []*schedulingv1beta1.PodGroup{pg3, pg4},
    97  			Queues:    []*schedulingv1beta1.Queue{queue2},
    98  			BindsNum:  0,
    99  		},
   100  		{
   101  			Name:      "case2: Can reclaim from other queues when allocated < deserved",
   102  			Plugins:   plugins,
   103  			Pods:      []*corev1.Pod{p5, p6, p7},
   104  			Nodes:     []*corev1.Node{n1, n2},
   105  			PodGroups: []*schedulingv1beta1.PodGroup{pg5, pg6},
   106  			Queues:    []*schedulingv1beta1.Queue{queue3, queue4},
   107  			PipeLined: map[string][]string{
   108  				"ns1/pg6": {"n2"},
   109  			},
   110  			Evicted:  []string{"ns1/p6"},
   111  			EvictNum: 1,
   112  		},
   113  	}
   114  
   115  	tiers := []conf.Tier{
   116  		{
   117  			Plugins: []conf.PluginOption{
   118  				{
   119  					Name:               PluginName,
   120  					EnabledAllocatable: &trueValue,
   121  					EnablePreemptive:   &trueValue,
   122  					EnabledReclaimable: &trueValue,
   123  				},
   124  				{
   125  					Name:             predicates.PluginName,
   126  					EnabledPredicate: &trueValue,
   127  				},
   128  			},
   129  		},
   130  	}
   131  	for i, test := range tests {
   132  		t.Run(test.Name, func(t *testing.T) {
   133  			test.RegistSession(tiers, nil)
   134  			defer test.Close()
   135  			test.Run(actions)
   136  			if err := test.CheckAll(i); err != nil {
   137  				t.Fatal(err)
   138  			}
   139  		})
   140  	}
   141  }