Skip to content
Permalink
Browse files

Merge pull request #82368 from m3ngyang/sch-util-ut

complete pkg/scheduler/util unit test
  • Loading branch information
k8s-ci-robot committed Sep 30, 2019
2 parents 2b795b9 + a638431 commit 974f201363b1cb3c3e1943722f10fe79bce4f576
Showing with 96 additions and 1 deletion.
  1. +2 −0 pkg/scheduler/util/BUILD
  2. +94 −1 pkg/scheduler/util/utils_test.go
@@ -16,7 +16,9 @@ go_test(
embed = [":go_default_library"],
deps = [
"//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/apimachinery/pkg/apis/meta/v1:go_default_library",
"//staging/src/k8s.io/apimachinery/pkg/util/diff:go_default_library",
],
)
@@ -17,12 +17,16 @@ limitations under the License.
package util

import (
"fmt"
"reflect"
"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/kubernetes/pkg/api/v1/pod"
"k8s.io/kubernetes/pkg/scheduler/api"
)

// 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)
}
}
}

0 comments on commit 974f201

Please sign in to comment.
You can’t perform that action at this time.