Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: error happen when allocate iluvatar device #522

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion pkg/device/iluvatar/device.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,6 @@ func (dev *IluvatarDevices) GetNodeDevices(n corev1.Node) ([]*api.DeviceInfo, er
cards, _ := n.Status.Capacity.Name(corev1.ResourceName(IluvatarResourceCores), resource.DecimalSI).AsInt64()
memoryTotal, _ := n.Status.Capacity.Name(corev1.ResourceName(IluvatarResourceMemory), resource.DecimalSI).AsInt64()
for int64(i)*100 < cards {
i++
nodedevices = append(nodedevices, &api.DeviceInfo{
Index: i,
ID: n.Name + "-iluvatar-" + fmt.Sprint(i),
Expand All @@ -91,6 +90,7 @@ func (dev *IluvatarDevices) GetNodeDevices(n corev1.Node) ([]*api.DeviceInfo, er
Numa: 0,
Health: true,
})
i++
}
return nodedevices, nil
}
Expand Down
98 changes: 98 additions & 0 deletions pkg/device/iluvatar/device_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,98 @@
/*
Copyright 2024 The HAMi Authors.

Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at

http://www.apache.org/licenses/LICENSE-2.0

Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/

package iluvatar

import (
"testing"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"

"github.com/Project-HAMi/HAMi/pkg/api"
)

func TestGetNodeDevices(t *testing.T) {
IluvatarResourceCores = "iluvatar.ai/MR-V100.vCore"
IluvatarResourceMemory = "iluvatar.ai/MR-V100.vMem"

tests := []struct {
name string
node corev1.Node
expected []*api.DeviceInfo
err error
}{
{
name: "Test with valid node",
node: corev1.Node{
ObjectMeta: metav1.ObjectMeta{
Name: "test",
},
Status: corev1.NodeStatus{
Capacity: corev1.ResourceList{
corev1.ResourceName(IluvatarResourceCores): *resource.NewQuantity(100, resource.DecimalSI),
corev1.ResourceName(IluvatarResourceMemory): *resource.NewQuantity(128, resource.DecimalSI),
},
},
},
expected: []*api.DeviceInfo{
{
Index: 0,
ID: "test-iluvatar-0",
Count: 100,
Devmem: 32768,
Devcore: 100,
Type: IluvatarGPUDevice,
Numa: 0,
Health: true,
},
},
err: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
dev := &IluvatarDevices{}
got, err := dev.GetNodeDevices(tt.node)
if (err != nil) != (tt.err != nil) {
t.Errorf("GetNodeDevices() error = %v, expected %v", err, tt.err)
return
}

if len(got) != len(tt.expected) {
t.Errorf("GetNodeDevices() got %d devices, expected %d", len(got), len(tt.expected))
return
}

for i, device := range got {
if device.Index != tt.expected[i].Index {
t.Errorf("Expected index %d, got %d", tt.expected[i].Index, device.Index)
}
if device.ID != tt.expected[i].ID {
t.Errorf("Expected id %s, got %s", tt.expected[i].ID, device.ID)
}
if device.Devcore != tt.expected[i].Devcore {
t.Errorf("Expected devcore %d, got %d", tt.expected[i].Devcore, device.Devcore)
}
if device.Devmem != tt.expected[i].Devmem {
t.Errorf("Expected cevmem %d, got %d", tt.expected[i].Devmem, device.Devmem)
}
}
})
}
}