mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-07-25 04:33:26 +00:00
Merge pull request #82368 from m3ngyang/sch-util-ut
complete pkg/scheduler/util unit test
This commit is contained in:
commit
974f201363
@ -16,7 +16,9 @@ go_test(
|
|||||||
embed = [":go_default_library"],
|
embed = [":go_default_library"],
|
||||||
deps = [
|
deps = [
|
||||||
"//pkg/api/v1/pod:go_default_library",
|
"//pkg/api/v1/pod:go_default_library",
|
||||||
|
"//pkg/scheduler/api:go_default_library",
|
||||||
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
"//staging/src/k8s.io/api/core/v1:go_default_library",
|
||||||
|
"//staging/src/k8s.io/apimachinery/pkg/apis/meta/v1:go_default_library",
|
||||||
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
|
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
@ -17,12 +17,16 @@ limitations under the License.
|
|||||||
package util
|
package util
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"reflect"
|
"reflect"
|
||||||
"testing"
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
"k8s.io/apimachinery/pkg/util/diff"
|
"k8s.io/apimachinery/pkg/util/diff"
|
||||||
"k8s.io/kubernetes/pkg/api/v1/pod"
|
"k8s.io/kubernetes/pkg/api/v1/pod"
|
||||||
|
"k8s.io/kubernetes/pkg/scheduler/api"
|
||||||
)
|
)
|
||||||
|
|
||||||
// TestSortableList tests SortableList by storing pods in the list and sorting
|
// TestSortableList tests SortableList by storing pods in the list and sorting
|
||||||
@ -171,3 +175,92 @@ func TestGetContainerPorts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestGetPodFullName(t *testing.T) {
|
||||||
|
pod := &v1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Namespace: "test",
|
||||||
|
Name: "pod",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
got := GetPodFullName(pod)
|
||||||
|
expected := fmt.Sprintf("%s_%s", pod.Name, pod.Namespace)
|
||||||
|
if got != expected {
|
||||||
|
t.Errorf("Got wrong full name, got: %s, expected: %s", got, expected)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func newPriorityPodWithStartTime(name string, priority int32, startTime time.Time) *v1.Pod {
|
||||||
|
return &v1.Pod{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: name,
|
||||||
|
},
|
||||||
|
Spec: v1.PodSpec{
|
||||||
|
Priority: &priority,
|
||||||
|
},
|
||||||
|
Status: v1.PodStatus{
|
||||||
|
StartTime: &metav1.Time{Time: startTime},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetEarliestPodStartTime(t *testing.T) {
|
||||||
|
currentTime := time.Now()
|
||||||
|
pod1 := newPriorityPodWithStartTime("pod1", 1, currentTime.Add(time.Second))
|
||||||
|
pod2 := newPriorityPodWithStartTime("pod2", 2, currentTime.Add(time.Second))
|
||||||
|
pod3 := newPriorityPodWithStartTime("pod3", 2, currentTime)
|
||||||
|
victims := &api.Victims{
|
||||||
|
Pods: []*v1.Pod{pod1, pod2, pod3},
|
||||||
|
}
|
||||||
|
startTime := GetEarliestPodStartTime(victims)
|
||||||
|
if !startTime.Equal(pod3.Status.StartTime) {
|
||||||
|
t.Errorf("Got wrong earliest pod start time")
|
||||||
|
}
|
||||||
|
|
||||||
|
pod1 = newPriorityPodWithStartTime("pod1", 2, currentTime)
|
||||||
|
pod2 = newPriorityPodWithStartTime("pod2", 2, currentTime.Add(time.Second))
|
||||||
|
pod3 = newPriorityPodWithStartTime("pod3", 2, currentTime.Add(2*time.Second))
|
||||||
|
victims = &api.Victims{
|
||||||
|
Pods: []*v1.Pod{pod1, pod2, pod3},
|
||||||
|
}
|
||||||
|
startTime = GetEarliestPodStartTime(victims)
|
||||||
|
if !startTime.Equal(pod1.Status.StartTime) {
|
||||||
|
t.Errorf("Got wrong earliest pod start time, got %v, expected %v", startTime, pod1.Status.StartTime)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestMoreImportantPod(t *testing.T) {
|
||||||
|
currentTime := time.Now()
|
||||||
|
pod1 := newPriorityPodWithStartTime("pod1", 1, currentTime)
|
||||||
|
pod2 := newPriorityPodWithStartTime("pod2", 2, currentTime.Add(time.Second))
|
||||||
|
pod3 := newPriorityPodWithStartTime("pod3", 2, currentTime)
|
||||||
|
|
||||||
|
tests := map[string]struct {
|
||||||
|
p1 *v1.Pod
|
||||||
|
p2 *v1.Pod
|
||||||
|
expected bool
|
||||||
|
}{
|
||||||
|
"Pod with higher priority": {
|
||||||
|
p1: pod1,
|
||||||
|
p2: pod2,
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
"Pod with older created time": {
|
||||||
|
p1: pod2,
|
||||||
|
p2: pod3,
|
||||||
|
expected: false,
|
||||||
|
},
|
||||||
|
"Pods with same start time": {
|
||||||
|
p1: pod3,
|
||||||
|
p2: pod1,
|
||||||
|
expected: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range tests {
|
||||||
|
got := MoreImportantPod(v.p1, v.p2)
|
||||||
|
if got != v.expected {
|
||||||
|
t.Errorf("%s failed, expected %t but got %t", k, v.expected, got)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user