kubernetes/pkg/scheduler/util/utils_test.go
Wei Huang cbdb4e3fdb
refactor scheduler extender related API
- move extender related API from pkg/scheduler/api to pkg/scheduler/apis/extender/v1

- alias extenderv1 to pkg/scheduler/apis/extender/v1

- use NodeScore and NodeScoreList in non-extender logic
2019-10-01 09:51:24 -07:00

267 lines
6.4 KiB
Go

/*
Copyright 2017 The Kubernetes 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 util
import (
"fmt"
"reflect"
"testing"
"time"
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"
extenderv1 "k8s.io/kubernetes/pkg/scheduler/apis/extender/v1"
)
// TestSortableList tests SortableList by storing pods in the list and sorting
// them by their priority.
func TestSortableList(t *testing.T) {
higherPriority := func(pod1, pod2 interface{}) bool {
return pod.GetPodPriority(pod1.(*v1.Pod)) > pod.GetPodPriority(pod2.(*v1.Pod))
}
podList := SortableList{CompFunc: higherPriority}
// Add a few Pods with different priorities from lowest to highest priority.
for i := 0; i < 10; i++ {
var p = int32(i)
pod := &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Name: "container",
Image: "image",
},
},
Priority: &p,
},
}
podList.Items = append(podList.Items, pod)
}
podList.Sort()
if len(podList.Items) != 10 {
t.Errorf("expected length of list was 10, got: %v", len(podList.Items))
}
var prevPriority = int32(10)
for _, p := range podList.Items {
if *p.(*v1.Pod).Spec.Priority >= prevPriority {
t.Errorf("Pods are not soreted. Current pod pririty is %v, while previous one was %v.", *p.(*v1.Pod).Spec.Priority, prevPriority)
}
}
}
func TestGetContainerPorts(t *testing.T) {
tests := []struct {
pod1 *v1.Pod
pod2 *v1.Pod
expected []*v1.ContainerPort
}{
{
pod1: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Ports: []v1.ContainerPort{
{
ContainerPort: 8001,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8002,
Protocol: v1.ProtocolTCP,
},
},
},
{
Ports: []v1.ContainerPort{
{
ContainerPort: 8003,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8004,
Protocol: v1.ProtocolTCP,
},
},
},
},
},
},
pod2: &v1.Pod{
Spec: v1.PodSpec{
Containers: []v1.Container{
{
Ports: []v1.ContainerPort{
{
ContainerPort: 8011,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8012,
Protocol: v1.ProtocolTCP,
},
},
},
{
Ports: []v1.ContainerPort{
{
ContainerPort: 8013,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8014,
Protocol: v1.ProtocolTCP,
},
},
},
},
},
},
expected: []*v1.ContainerPort{
{
ContainerPort: 8001,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8002,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8003,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8004,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8011,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8012,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8013,
Protocol: v1.ProtocolTCP,
},
{
ContainerPort: 8014,
Protocol: v1.ProtocolTCP,
},
},
},
}
for _, test := range tests {
result := GetContainerPorts(test.pod1, test.pod2)
if !reflect.DeepEqual(test.expected, result) {
t.Errorf("Got different result than expected.\nDifference detected on:\n%s", diff.ObjectGoPrintSideBySide(test.expected, result))
}
}
}
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 := &extenderv1.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 = &extenderv1.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)
}
}
}