volcano.sh/volcano@v1.9.0/pkg/scheduler/plugins/deviceshare/deviceshare_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 deviceshare
    18  
    19  import (
    20  	"testing"
    21  
    22  	v1 "k8s.io/api/core/v1"
    23  	"k8s.io/apimachinery/pkg/api/resource"
    24  
    25  	"volcano.sh/volcano/pkg/scheduler/api"
    26  	"volcano.sh/volcano/pkg/scheduler/api/devices/nvidia/vgpu"
    27  	"volcano.sh/volcano/pkg/scheduler/framework"
    28  	"volcano.sh/volcano/pkg/scheduler/util"
    29  )
    30  
    31  func TestArguments(t *testing.T) {
    32  	framework.RegisterPluginBuilder(PluginName, New)
    33  	defer framework.CleanupPluginBuilders()
    34  
    35  	arguments := framework.Arguments{
    36  		"deviceshare.VGPUEnable":     true,
    37  		"deviceshare.SchedulePolicy": "binpack",
    38  		"deviceshare.ScheduleWeight": 10,
    39  	}
    40  
    41  	builder, ok := framework.GetPluginBuilder(PluginName)
    42  
    43  	if !ok {
    44  		t.Fatalf("should have plugin named %s", PluginName)
    45  	}
    46  
    47  	plugin := builder(arguments)
    48  	deviceshare, ok := plugin.(*deviceSharePlugin)
    49  
    50  	if !ok {
    51  		t.Fatalf("plugin should be %T, but not %T", deviceshare, plugin)
    52  	}
    53  
    54  	weight := deviceshare.scheduleWeight
    55  
    56  	if weight != 10 {
    57  		t.Errorf("weight should be 10, but not %v", weight)
    58  	}
    59  
    60  	if deviceshare.schedulePolicy != "binpack" {
    61  		t.Errorf("policy should be binpack, but not %s", deviceshare.schedulePolicy)
    62  	}
    63  }
    64  
    65  func addResource(resourceList v1.ResourceList, name v1.ResourceName, need string) {
    66  	resourceList[name] = resource.MustParse(need)
    67  }
    68  
    69  func TestVgpuScore(t *testing.T) {
    70  	gpuNode1 := vgpu.GPUDevices{
    71  		Name:   "node1",
    72  		Score:  float64(0),
    73  		Device: make(map[int]*vgpu.GPUDevice),
    74  	}
    75  	gpuNode1.Device[0] = vgpu.NewGPUDevice(0, 30000)
    76  	gpuNode1.Device[0].Type = "NVIDIA"
    77  	gpuNode1.Device[0].Number = 10
    78  	gpuNode1.Device[0].UsedNum = 1
    79  	gpuNode1.Device[0].UsedMem = 3000
    80  
    81  	gpunumber := v1.ResourceName("volcano.sh/vgpu-number")
    82  	gpumemory := v1.ResourceName("volcano.sh/vgpu-memory")
    83  
    84  	vgpu.VGPUEnable = true
    85  
    86  	p1 := util.BuildPod("c1", "p3", "", v1.PodPending, api.BuildResourceList("2", "10Gi"), "pg1", make(map[string]string), make(map[string]string))
    87  	addResource(p1.Spec.Containers[0].Resources.Requests, gpunumber, "1")
    88  	addResource(p1.Spec.Containers[0].Resources.Requests, gpumemory, "1000")
    89  	p1.Spec.Containers[0].Resources.Limits = make(v1.ResourceList)
    90  	addResource(p1.Spec.Containers[0].Resources.Limits, gpunumber, "1")
    91  	addResource(p1.Spec.Containers[0].Resources.Limits, gpumemory, "1000")
    92  
    93  	canAccess, _, err := gpuNode1.FilterNode(p1, "binpack")
    94  	if err != nil || canAccess != 0 {
    95  		t.Errorf("binpack filter failed %s", err.Error())
    96  	}
    97  
    98  	score := gpuNode1.ScoreNode(p1, "binpack")
    99  	if score-float64(4000*100)/float64(30000) > 0.05 {
   100  		t.Errorf("score failed expected %f, get %f", float64(4000*100)/float64(30000), score)
   101  	}
   102  
   103  	gpuNode2 := vgpu.GPUDevices{
   104  		Name:   "node2",
   105  		Score:  float64(0),
   106  		Device: make(map[int]*vgpu.GPUDevice),
   107  	}
   108  	gpuNode2.Device[0] = vgpu.NewGPUDevice(0, 30000)
   109  	gpuNode2.Device[0].Type = "NVIDIA"
   110  	gpuNode2.Device[0].Number = 10
   111  	gpuNode2.Device[0].UsedNum = 0
   112  	gpuNode2.Device[0].UsedMem = 0
   113  	p2 := util.BuildPod("c2", "p4", "", v1.PodPending, api.BuildResourceList("2", "10Gi"), "pg1", make(map[string]string), make(map[string]string))
   114  	addResource(p2.Spec.Containers[0].Resources.Requests, gpunumber, "1")
   115  	addResource(p2.Spec.Containers[0].Resources.Requests, gpumemory, "1000")
   116  	p2.Spec.Containers[0].Resources.Limits = make(v1.ResourceList)
   117  	addResource(p2.Spec.Containers[0].Resources.Limits, gpunumber, "1")
   118  	addResource(p2.Spec.Containers[0].Resources.Limits, gpumemory, "1000")
   119  
   120  	canAccess, _, err = gpuNode2.FilterNode(p2, "spread")
   121  	if err != nil || canAccess != 0 {
   122  		t.Errorf("binpack filter failed %s", err.Error())
   123  	}
   124  
   125  	score = gpuNode2.ScoreNode(p1, "spread")
   126  	if score-float64(100) > 0.05 {
   127  		t.Errorf("score failed expected %f, get %f", float64(4000*100)/float64(30000), score)
   128  	}
   129  
   130  }