Skip to content

Commit

Permalink
add ut for device/iluvatar
Browse files Browse the repository at this point in the history
Signed-off-by: jinye <jinye.shi@daocloud.io>
  • Loading branch information
shijinye committed Jan 9, 2025
1 parent 594ef64 commit 711b5ae
Showing 1 changed file with 143 additions and 0 deletions.
143 changes: 143 additions & 0 deletions pkg/device/iluvatar/device_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,13 +17,16 @@ limitations under the License.
package iluvatar

import (
"flag"
"strconv"
"testing"
"time"

"gotest.tools/v3/assert"
corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/klog/v2"

"github.com/Project-HAMi/HAMi/pkg/util"
)
Expand Down Expand Up @@ -168,3 +171,143 @@ func TestPatchAnnotations(t *testing.T) {
})
}
}

func Test_MutateAdmission(t *testing.T) {
tests := []struct {
name string
args struct {
ctr *corev1.Container
p *corev1.Pod
}
want bool
err error
}{
{
name: "IluvatarResourceCount and IluvatarResourceCores set to limits",
args: struct {
ctr *corev1.Container
p *corev1.Pod
}{
ctr: &corev1.Container{
Resources: corev1.ResourceRequirements{
Limits: corev1.ResourceList{
"iluvatar.ai/vgpu": *resource.NewQuantity(2, resource.DecimalSI),
"iluvatar.ai/vcuda-core": *resource.NewQuantity(1, resource.DecimalSI),
},
},
},
p: &corev1.Pod{},
},
want: true,
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
config := IluvatarConfig{
ResourceCountName: "iluvatar.ai/vgpu",
ResourceCoreName: "iluvatar.ai/vcuda-core",
ResourceMemoryName: "iluvatar.ai/vcuda-memory",
}
InitIluvatarDevice(config)
dev := IluvatarDevices{}
result, _ := dev.MutateAdmission(test.args.ctr, test.args.p)
assert.Equal(t, result, test.want)
})
}
}

func Test_GetNodeDevices(t *testing.T) {
tests := []struct {
name string
args corev1.Node
want []*util.DeviceInfo
}{
{
name: "get node devices",
args: corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: corev1.NodeStatus{
Capacity: corev1.ResourceList{
"iluvatar.ai/vcuda-core": *resource.NewQuantity(1, resource.DecimalSI),
"iluvatar.ai/vcuda-memory": *resource.NewQuantity(1000, resource.DecimalSI),
},
},
},
want: []*util.DeviceInfo{
{
Index: uint(0),
ID: "test-iluvatar-0",
Count: int32(100),
Devmem: int32(25600000),
Devcore: int32(100),
Type: IluvatarGPUDevice,
Numa: 0,
Health: true,
},
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dev := IluvatarDevices{}
fs := flag.FlagSet{}
ParseConfig(&fs)
result, err := dev.GetNodeDevices(test.args)
if err != nil {
klog.InfoS("failed to get node devices info")
}
assert.DeepEqual(t, result, test.want)
})
}
}

func Test_PatchAnnotations(t *testing.T) {
tests := []struct {
name string
args struct {
annoinput *map[string]string
pd util.PodDevices
}
want map[string]string
}{
{
name: "exist device",
args: struct {
annoinput *map[string]string
pd util.PodDevices
}{
annoinput: &map[string]string{},
pd: util.PodDevices{
IluvatarGPUDevice: util.PodSingleDevice{
util.ContainerDevices{
{
Idx: 0,
UUID: "test123",
Type: IluvatarGPUDevice,
Usedcores: int32(1),
Usedmem: int32(1000),
},
},
},
},
},
want: map[string]string{
util.InRequestDevices[IluvatarGPUDevice]: "test123,Iluvatar,1000,1:;",
util.SupportDevices[IluvatarGPUDevice]: "test123,Iluvatar,1000,1:;",
"iluvatar.ai/gpu-assigned": "false",
"iluvatar.ai/predicate-gpu-idx-0": "0",
},
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
dev := IluvatarDevices{}
result := dev.PatchAnnotations(test.args.annoinput, test.args.pd)
assert.Equal(t, result[IluvatarGPUDevice], test.want[IluvatarGPUDevice])
assert.Equal(t, result["iluvatar.ai/gpu-assigned"], test.want["iluvatar.ai/gpu-assigned"])
assert.Equal(t, result["iluvatar.ai/predicate-gpu-idx-0"], test.want["iluvatar.ai/predicate-gpu-idx-0"])
})
}
}

0 comments on commit 711b5ae

Please sign in to comment.