volcano.sh/volcano@v1.9.0/pkg/scheduler/api/devices/nvidia/gpushare/device_info_test.go (about) 1 /* 2 Copyright 2023 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 gpushare 18 19 import ( 20 "testing" 21 22 v1 "k8s.io/api/core/v1" 23 "k8s.io/apimachinery/pkg/api/resource" 24 ) 25 26 func TestGetGPUMemoryOfPod(t *testing.T) { 27 testCases := []struct { 28 name string 29 pod *v1.Pod 30 want uint 31 }{ 32 { 33 name: "GPUs required only in Containers", 34 pod: &v1.Pod{ 35 Spec: v1.PodSpec{ 36 Containers: []v1.Container{ 37 { 38 Resources: v1.ResourceRequirements{ 39 Limits: v1.ResourceList{ 40 VolcanoGPUResource: resource.MustParse("1"), 41 }, 42 }, 43 }, 44 { 45 Resources: v1.ResourceRequirements{ 46 Limits: v1.ResourceList{ 47 VolcanoGPUResource: resource.MustParse("3"), 48 }, 49 }, 50 }, 51 }, 52 }, 53 }, 54 want: 4, 55 }, 56 { 57 name: "GPUs required both in initContainers and Containers", 58 pod: &v1.Pod{ 59 Spec: v1.PodSpec{ 60 InitContainers: []v1.Container{ 61 { 62 Resources: v1.ResourceRequirements{ 63 Limits: v1.ResourceList{ 64 VolcanoGPUResource: resource.MustParse("1"), 65 }, 66 }, 67 }, 68 { 69 Resources: v1.ResourceRequirements{ 70 Limits: v1.ResourceList{ 71 VolcanoGPUResource: resource.MustParse("3"), 72 }, 73 }, 74 }, 75 }, 76 Containers: []v1.Container{ 77 { 78 Resources: v1.ResourceRequirements{ 79 Limits: v1.ResourceList{ 80 VolcanoGPUResource: resource.MustParse("2"), 81 }, 82 }, 83 }, 84 }, 85 }, 86 }, 87 want: 3, 88 }, 89 } 90 91 for _, tc := range testCases { 92 t.Run(tc.name, func(t *testing.T) { 93 got := getGPUMemoryOfPod(tc.pod) 94 if tc.want != got { 95 t.Errorf("unexpected result, want: %v, got: %v", tc.want, got) 96 } 97 }) 98 } 99 } 100 101 func TestGetGPUNumberOfPod(t *testing.T) { 102 testCases := []struct { 103 name string 104 pod *v1.Pod 105 want int 106 }{ 107 { 108 name: "GPUs required only in Containers", 109 pod: &v1.Pod{ 110 Spec: v1.PodSpec{ 111 Containers: []v1.Container{ 112 { 113 Resources: v1.ResourceRequirements{ 114 Limits: v1.ResourceList{ 115 VolcanoGPUNumber: resource.MustParse("1"), 116 }, 117 }, 118 }, 119 { 120 Resources: v1.ResourceRequirements{ 121 Limits: v1.ResourceList{ 122 VolcanoGPUNumber: resource.MustParse("3"), 123 }, 124 }, 125 }, 126 }, 127 }, 128 }, 129 want: 4, 130 }, 131 { 132 name: "GPUs required both in initContainers and Containers", 133 pod: &v1.Pod{ 134 Spec: v1.PodSpec{ 135 InitContainers: []v1.Container{ 136 { 137 Resources: v1.ResourceRequirements{ 138 Limits: v1.ResourceList{ 139 VolcanoGPUNumber: resource.MustParse("1"), 140 }, 141 }, 142 }, 143 { 144 Resources: v1.ResourceRequirements{ 145 Limits: v1.ResourceList{ 146 VolcanoGPUNumber: resource.MustParse("3"), 147 }, 148 }, 149 }, 150 }, 151 Containers: []v1.Container{ 152 { 153 Resources: v1.ResourceRequirements{ 154 Limits: v1.ResourceList{ 155 VolcanoGPUNumber: resource.MustParse("2"), 156 }, 157 }, 158 }, 159 }, 160 }, 161 }, 162 want: 3, 163 }, 164 } 165 166 for _, tc := range testCases { 167 t.Run(tc.name, func(t *testing.T) { 168 got := getGPUNumberOfPod(tc.pod) 169 if tc.want != got { 170 t.Errorf("unexpected result, want: %v, got: %v", tc.want, got) 171 } 172 }) 173 } 174 }