|
| 1 | +/* |
| 2 | +Copyright 2024 The HAMi 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 scheduler |
| 18 | + |
| 19 | +import ( |
| 20 | + "reflect" |
| 21 | + "sync" |
| 22 | + "testing" |
| 23 | + |
| 24 | + "github.com/Project-HAMi/HAMi/pkg/util" |
| 25 | + |
| 26 | + "github.com/stretchr/testify/assert" |
| 27 | + k8stypes "k8s.io/apimachinery/pkg/types" |
| 28 | +) |
| 29 | + |
| 30 | +func TestPodInfo(t *testing.T) { |
| 31 | + tests := []struct { |
| 32 | + name string |
| 33 | + podInfo podInfo |
| 34 | + expected podInfo |
| 35 | + }{ |
| 36 | + { |
| 37 | + name: "Empty podInfo", |
| 38 | + podInfo: podInfo{}, |
| 39 | + expected: podInfo{}, |
| 40 | + }, |
| 41 | + { |
| 42 | + name: "Filled podInfo", |
| 43 | + podInfo: podInfo{ |
| 44 | + Namespace: "default", |
| 45 | + Name: "my-pod", |
| 46 | + UID: k8stypes.UID("12345678"), |
| 47 | + NodeID: "node1", |
| 48 | + Devices: util.PodDevices{ |
| 49 | + "device1": { |
| 50 | + { |
| 51 | + { |
| 52 | + Idx: 1, |
| 53 | + }, |
| 54 | + }, |
| 55 | + }, |
| 56 | + }, |
| 57 | + CtrIDs: []string{"ctr1", "ctr2"}, |
| 58 | + }, |
| 59 | + expected: podInfo{ |
| 60 | + Namespace: "default", |
| 61 | + Name: "my-pod", |
| 62 | + UID: k8stypes.UID("12345678"), |
| 63 | + NodeID: "node1", |
| 64 | + Devices: util.PodDevices{ |
| 65 | + "device1": { |
| 66 | + { |
| 67 | + { |
| 68 | + Idx: 1, |
| 69 | + }, |
| 70 | + }, |
| 71 | + }, |
| 72 | + }, |
| 73 | + CtrIDs: []string{"ctr1", "ctr2"}, |
| 74 | + }, |
| 75 | + }, |
| 76 | + } |
| 77 | + |
| 78 | + for _, tt := range tests { |
| 79 | + t.Run(tt.name, func(t *testing.T) { |
| 80 | + if !reflect.DeepEqual(tt.podInfo, tt.expected) { |
| 81 | + t.Errorf("Expected %v, got %v", tt.expected, tt.podInfo) |
| 82 | + } |
| 83 | + }) |
| 84 | + } |
| 85 | +} |
| 86 | + |
| 87 | +func TestPodUseDeviceStat(t *testing.T) { |
| 88 | + tests := []struct { |
| 89 | + name string |
| 90 | + stat PodUseDeviceStat |
| 91 | + expected PodUseDeviceStat |
| 92 | + }{ |
| 93 | + { |
| 94 | + name: "Empty PodUseDeviceStat", |
| 95 | + stat: PodUseDeviceStat{}, |
| 96 | + expected: PodUseDeviceStat{}, |
| 97 | + }, |
| 98 | + { |
| 99 | + name: "Filled PodUseDeviceStat", |
| 100 | + stat: PodUseDeviceStat{ |
| 101 | + TotalPod: 10, |
| 102 | + UseDevicePod: 5, |
| 103 | + }, |
| 104 | + expected: PodUseDeviceStat{ |
| 105 | + TotalPod: 10, |
| 106 | + UseDevicePod: 5, |
| 107 | + }, |
| 108 | + }, |
| 109 | + } |
| 110 | + |
| 111 | + for _, tt := range tests { |
| 112 | + t.Run(tt.name, func(t *testing.T) { |
| 113 | + if !reflect.DeepEqual(tt.stat, tt.expected) { |
| 114 | + t.Errorf("Expected %v, got %v", tt.expected, tt.stat) |
| 115 | + } |
| 116 | + }) |
| 117 | + } |
| 118 | +} |
| 119 | +func TestGetScheduledPods(t *testing.T) { |
| 120 | + podManager := &podManager{ |
| 121 | + pods: make(map[k8stypes.UID]*podInfo), |
| 122 | + mutex: sync.RWMutex{}, |
| 123 | + } |
| 124 | + |
| 125 | + pod1 := &podInfo{ |
| 126 | + Namespace: "default", |
| 127 | + Name: "pod1", |
| 128 | + UID: k8stypes.UID("uid1"), |
| 129 | + NodeID: "node1", |
| 130 | + Devices: util.PodDevices{"device1": {{}}}, |
| 131 | + CtrIDs: []string{"ctr1"}, |
| 132 | + } |
| 133 | + pod2 := &podInfo{ |
| 134 | + Namespace: "default", |
| 135 | + Name: "pod2", |
| 136 | + UID: k8stypes.UID("uid2"), |
| 137 | + NodeID: "node2", |
| 138 | + Devices: util.PodDevices{"device2": {{}}}, |
| 139 | + CtrIDs: []string{"ctr2"}, |
| 140 | + } |
| 141 | + podManager.pods[pod1.UID] = pod1 |
| 142 | + podManager.pods[pod2.UID] = pod2 |
| 143 | + |
| 144 | + scheduledPods, err := podManager.GetScheduledPods() |
| 145 | + |
| 146 | + assert.NoError(t, err, "GetScheduledPods should not return an error") |
| 147 | + assert.NotNil(t, scheduledPods, "The result should not be nil") |
| 148 | + assert.Equal(t, 2, len(scheduledPods), "The number of scheduled pods should be 2") |
| 149 | + |
| 150 | + expectedPods := map[k8stypes.UID]*podInfo{ |
| 151 | + pod1.UID: pod1, |
| 152 | + pod2.UID: pod2, |
| 153 | + } |
| 154 | + for uid, pod := range scheduledPods { |
| 155 | + expectedPod := expectedPods[uid] |
| 156 | + assert.NotNil(t, expectedPod, "Pod with UID %s should exist in the expected pods", uid) |
| 157 | + assert.Equal(t, expectedPod.Namespace, pod.Namespace, "Namespace should match") |
| 158 | + assert.Equal(t, expectedPod.Name, pod.Name, "Name should match") |
| 159 | + assert.Equal(t, expectedPod.UID, pod.UID, "UID should match") |
| 160 | + assert.Equal(t, expectedPod.NodeID, pod.NodeID, "NodeID should match") |
| 161 | + assert.Equal(t, expectedPod.Devices, pod.Devices, "Devices should match") |
| 162 | + assert.Equal(t, expectedPod.CtrIDs, pod.CtrIDs, "CtrIDs should match") |
| 163 | + } |
| 164 | +} |
0 commit comments