Unverified Commit af9531b8 authored by Kubernetes Submit Queue's avatar Kubernetes Submit Queue Committed by GitHub

Merge pull request #63658 from xchapter7x/pkg-scheduler-algorithm-priorities

Automatic merge from submit-queue. If you want to cherry-pick this change to another branch, please follow the instructions <a href="https://github.com/kubernetes/community/blob/master/contributors/devel/cherry-picks.md">here</a>. use subtest for table units (pkg-scheduler-algorithm-priorities) **What this PR does / why we need it**: Update scheduler's unit table tests to use subtest **Which issue(s) this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close the issue(s) when PR gets merged)*: **Special notes for your reviewer**: breaks up PR: https://github.com/kubernetes/kubernetes/pull/63281 /ref #63267 **Release note**: ```release-note This PR will leverage subtests on the existing table tests for the scheduler units. Some refactoring of error/status messages and functions to align with new approach. ```
parents b71fe87a 3a9e9880
...@@ -216,7 +216,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -216,7 +216,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
pods []*v1.Pod pods []*v1.Pod
nodes []*v1.Node nodes []*v1.Node
expectedList schedulerapi.HostPriorityList expectedList schedulerapi.HostPriorityList
test string name string
}{ }{
{ {
/* /*
...@@ -233,7 +233,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -233,7 +233,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}},
test: "nothing scheduled, nothing requested", name: "nothing scheduled, nothing requested",
}, },
{ {
/* /*
...@@ -250,7 +250,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -250,7 +250,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 7}, {Host: "machine2", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 7}, {Host: "machine2", Score: schedulerapi.MaxPriority}},
test: "nothing scheduled, resources requested, differently sized machines", name: "nothing scheduled, resources requested, differently sized machines",
}, },
{ {
/* /*
...@@ -267,7 +267,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -267,7 +267,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}},
test: "no resources requested, pods scheduled", name: "no resources requested, pods scheduled",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels2}}, {Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels2}},
{Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels1}}, {Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
...@@ -290,7 +290,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -290,7 +290,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)}, nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 4}, {Host: "machine2", Score: 6}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 4}, {Host: "machine2", Score: 6}},
test: "no resources requested, pods scheduled with resources", name: "no resources requested, pods scheduled with resources",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels2}}, {Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels2}},
{Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels1}}, {Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
...@@ -313,7 +313,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -313,7 +313,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)}, nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 6}, {Host: "machine2", Score: 9}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 6}, {Host: "machine2", Score: 9}},
test: "resources requested, pods scheduled with resources", name: "resources requested, pods scheduled with resources",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly}, {Spec: cpuOnly},
{Spec: cpuAndMemory}, {Spec: cpuAndMemory},
...@@ -334,7 +334,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -334,7 +334,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 50000)}, nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 50000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 6}, {Host: "machine2", Score: 6}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 6}, {Host: "machine2", Score: 6}},
test: "resources requested, pods scheduled with resources, differently sized machines", name: "resources requested, pods scheduled with resources, differently sized machines",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly}, {Spec: cpuOnly},
{Spec: cpuAndMemory}, {Spec: cpuAndMemory},
...@@ -355,7 +355,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -355,7 +355,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
pod: &v1.Pod{Spec: cpuOnly}, pod: &v1.Pod{Spec: cpuOnly},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}},
test: "requested resources exceed node capacity", name: "requested resources exceed node capacity",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly}, {Spec: cpuOnly},
{Spec: cpuAndMemory}, {Spec: cpuAndMemory},
...@@ -365,7 +365,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -365,7 +365,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 0, 0), makeNode("machine2", 0, 0)}, nodes: []*v1.Node{makeNode("machine1", 0, 0), makeNode("machine2", 0, 0)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}},
test: "zero node resources, pods scheduled with resources", name: "zero node resources, pods scheduled with resources",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly}, {Spec: cpuOnly},
{Spec: cpuAndMemory}, {Spec: cpuAndMemory},
...@@ -389,7 +389,7 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -389,7 +389,7 @@ func TestBalancedResourceAllocation(t *testing.T) {
}, },
nodes: []*v1.Node{makeNode("machine3", 3500, 40000), makeNode("machine4", 4000, 10000)}, nodes: []*v1.Node{makeNode("machine3", 3500, 40000), makeNode("machine4", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine3", Score: 8}, {Host: "machine4", Score: 9}}, expectedList: []schedulerapi.HostPriority{{Host: "machine3", Score: 8}, {Host: "machine4", Score: 9}},
test: "Include volume count on a node for balanced resource allocation", name: "Include volume count on a node for balanced resource allocation",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuAndMemory3}, {Spec: cpuAndMemory3},
{Spec: podwithVol1}, {Spec: podwithVol1},
...@@ -400,20 +400,22 @@ func TestBalancedResourceAllocation(t *testing.T) { ...@@ -400,20 +400,22 @@ func TestBalancedResourceAllocation(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes) t.Run(test.name, func(t *testing.T) {
if len(test.pod.Spec.Volumes) > 0 { nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes)
maxVolumes := 5 if len(test.pod.Spec.Volumes) > 0 {
for _, info := range nodeNameToInfo { maxVolumes := 5
info.TransientInfo.TransNodeInfo.AllocatableVolumesCount = getExistingVolumeCountForNode(info.Pods(), maxVolumes) for _, info := range nodeNameToInfo {
info.TransientInfo.TransNodeInfo.RequestedVolumes = len(test.pod.Spec.Volumes) info.TransientInfo.TransNodeInfo.AllocatableVolumesCount = getExistingVolumeCountForNode(info.Pods(), maxVolumes)
info.TransientInfo.TransNodeInfo.RequestedVolumes = len(test.pod.Spec.Volumes)
}
} }
} list, err := priorityFunction(BalancedResourceAllocationMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes)
list, err := priorityFunction(BalancedResourceAllocationMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes) if err != nil {
if err != nil { t.Errorf("unexpected error: %v", err)
t.Errorf("unexpected error: %v", err) }
} if !reflect.DeepEqual(test.expectedList, list) {
if !reflect.DeepEqual(test.expectedList, list) { t.Errorf("expected %#v, got %#v", test.expectedList, list)
t.Errorf("%s: expected %#v, got %#v", test.test, test.expectedList, list) }
} })
} }
} }
...@@ -110,7 +110,7 @@ func TestImageLocalityPriority(t *testing.T) { ...@@ -110,7 +110,7 @@ func TestImageLocalityPriority(t *testing.T) {
pods []*v1.Pod pods []*v1.Pod
nodes []*v1.Node nodes []*v1.Node
expectedList schedulerapi.HostPriorityList expectedList schedulerapi.HostPriorityList
test string name string
}{ }{
{ {
// Pod: gcr.io/40 gcr.io/250 // Pod: gcr.io/40 gcr.io/250
...@@ -125,7 +125,7 @@ func TestImageLocalityPriority(t *testing.T) { ...@@ -125,7 +125,7 @@ func TestImageLocalityPriority(t *testing.T) {
pod: &v1.Pod{Spec: test40250}, pod: &v1.Pod{Spec: test40250},
nodes: []*v1.Node{makeImageNode("machine1", node401402000), makeImageNode("machine2", node25010)}, nodes: []*v1.Node{makeImageNode("machine1", node401402000), makeImageNode("machine2", node25010)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 3}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 3}},
test: "two images spread on two nodes, prefer the larger image one", name: "two images spread on two nodes, prefer the larger image one",
}, },
{ {
// Pod: gcr.io/40 gcr.io/140 // Pod: gcr.io/40 gcr.io/140
...@@ -140,7 +140,7 @@ func TestImageLocalityPriority(t *testing.T) { ...@@ -140,7 +140,7 @@ func TestImageLocalityPriority(t *testing.T) {
pod: &v1.Pod{Spec: test40140}, pod: &v1.Pod{Spec: test40140},
nodes: []*v1.Node{makeImageNode("machine1", node401402000), makeImageNode("machine2", node25010)}, nodes: []*v1.Node{makeImageNode("machine1", node401402000), makeImageNode("machine2", node25010)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 2}, {Host: "machine2", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 2}, {Host: "machine2", Score: 0}},
test: "two images on one node, prefer this node", name: "two images on one node, prefer this node",
}, },
{ {
// Pod: gcr.io/2000 gcr.io/10 // Pod: gcr.io/2000 gcr.io/10
...@@ -155,23 +155,25 @@ func TestImageLocalityPriority(t *testing.T) { ...@@ -155,23 +155,25 @@ func TestImageLocalityPriority(t *testing.T) {
pod: &v1.Pod{Spec: testMinMax}, pod: &v1.Pod{Spec: testMinMax},
nodes: []*v1.Node{makeImageNode("machine1", node401402000), makeImageNode("machine2", node25010)}, nodes: []*v1.Node{makeImageNode("machine1", node401402000), makeImageNode("machine2", node25010)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}},
test: "if exceed limit, use limit", name: "if exceed limit, use limit",
}, },
} }
for _, test := range tests { for _, test := range tests {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes) t.Run(test.name, func(t *testing.T) {
list, err := priorityFunction(ImageLocalityPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes) nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes)
if err != nil { list, err := priorityFunction(ImageLocalityPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
}
sort.Sort(test.expectedList)
sort.Sort(list) sort.Sort(test.expectedList)
sort.Sort(list)
if !reflect.DeepEqual(test.expectedList, list) {
t.Errorf("%s: expected %#v, got %#v", test.test, test.expectedList, list) if !reflect.DeepEqual(test.expectedList, list) {
} t.Errorf("expected %#v, got %#v", test.expectedList, list)
}
})
} }
} }
......
...@@ -94,7 +94,7 @@ func TestLeastRequested(t *testing.T) { ...@@ -94,7 +94,7 @@ func TestLeastRequested(t *testing.T) {
pods []*v1.Pod pods []*v1.Pod
nodes []*v1.Node nodes []*v1.Node
expectedList schedulerapi.HostPriorityList expectedList schedulerapi.HostPriorityList
test string name string
}{ }{
{ {
/* /*
...@@ -111,7 +111,7 @@ func TestLeastRequested(t *testing.T) { ...@@ -111,7 +111,7 @@ func TestLeastRequested(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}},
test: "nothing scheduled, nothing requested", name: "nothing scheduled, nothing requested",
}, },
{ {
/* /*
...@@ -128,7 +128,7 @@ func TestLeastRequested(t *testing.T) { ...@@ -128,7 +128,7 @@ func TestLeastRequested(t *testing.T) {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 3}, {Host: "machine2", Score: 5}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 3}, {Host: "machine2", Score: 5}},
test: "nothing scheduled, resources requested, differently sized machines", name: "nothing scheduled, resources requested, differently sized machines",
}, },
{ {
/* /*
...@@ -145,7 +145,7 @@ func TestLeastRequested(t *testing.T) { ...@@ -145,7 +145,7 @@ func TestLeastRequested(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}},
test: "no resources requested, pods scheduled", name: "no resources requested, pods scheduled",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels2}}, {Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels2}},
{Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels1}}, {Spec: machine1Spec, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
...@@ -168,7 +168,7 @@ func TestLeastRequested(t *testing.T) { ...@@ -168,7 +168,7 @@ func TestLeastRequested(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)}, nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 7}, {Host: "machine2", Score: 5}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 7}, {Host: "machine2", Score: 5}},
test: "no resources requested, pods scheduled with resources", name: "no resources requested, pods scheduled with resources",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels2}}, {Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels2}},
{Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels1}}, {Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
...@@ -191,7 +191,7 @@ func TestLeastRequested(t *testing.T) { ...@@ -191,7 +191,7 @@ func TestLeastRequested(t *testing.T) {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)}, nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 5}, {Host: "machine2", Score: 4}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 5}, {Host: "machine2", Score: 4}},
test: "resources requested, pods scheduled with resources", name: "resources requested, pods scheduled with resources",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly}, {Spec: cpuOnly},
{Spec: cpuAndMemory}, {Spec: cpuAndMemory},
...@@ -212,7 +212,7 @@ func TestLeastRequested(t *testing.T) { ...@@ -212,7 +212,7 @@ func TestLeastRequested(t *testing.T) {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 50000)}, nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 50000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 5}, {Host: "machine2", Score: 6}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 5}, {Host: "machine2", Score: 6}},
test: "resources requested, pods scheduled with resources, differently sized machines", name: "resources requested, pods scheduled with resources, differently sized machines",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly}, {Spec: cpuOnly},
{Spec: cpuAndMemory}, {Spec: cpuAndMemory},
...@@ -233,7 +233,7 @@ func TestLeastRequested(t *testing.T) { ...@@ -233,7 +233,7 @@ func TestLeastRequested(t *testing.T) {
pod: &v1.Pod{Spec: cpuOnly}, pod: &v1.Pod{Spec: cpuOnly},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 5}, {Host: "machine2", Score: 2}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 5}, {Host: "machine2", Score: 2}},
test: "requested resources exceed node capacity", name: "requested resources exceed node capacity",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly}, {Spec: cpuOnly},
{Spec: cpuAndMemory}, {Spec: cpuAndMemory},
...@@ -243,7 +243,7 @@ func TestLeastRequested(t *testing.T) { ...@@ -243,7 +243,7 @@ func TestLeastRequested(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 0, 0), makeNode("machine2", 0, 0)}, nodes: []*v1.Node{makeNode("machine1", 0, 0), makeNode("machine2", 0, 0)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}},
test: "zero node resources, pods scheduled with resources", name: "zero node resources, pods scheduled with resources",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly}, {Spec: cpuOnly},
{Spec: cpuAndMemory}, {Spec: cpuAndMemory},
...@@ -252,13 +252,15 @@ func TestLeastRequested(t *testing.T) { ...@@ -252,13 +252,15 @@ func TestLeastRequested(t *testing.T) {
} }
for _, test := range tests { for _, test := range tests {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes) t.Run(test.name, func(t *testing.T) {
list, err := priorityFunction(LeastRequestedPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes) nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes)
if err != nil { list, err := priorityFunction(LeastRequestedPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
if !reflect.DeepEqual(test.expectedList, list) { }
t.Errorf("%s: expected %#v, got %#v", test.test, test.expectedList, list) if !reflect.DeepEqual(test.expectedList, list) {
} t.Errorf("expected %#v, got %#v", test.expectedList, list)
}
})
} }
} }
...@@ -117,13 +117,13 @@ func TestPriorityMetadata(t *testing.T) { ...@@ -117,13 +117,13 @@ func TestPriorityMetadata(t *testing.T) {
} }
tests := []struct { tests := []struct {
pod *v1.Pod pod *v1.Pod
test string name string
expected interface{} expected interface{}
}{ }{
{ {
pod: nil, pod: nil,
expected: nil, expected: nil,
test: "pod is nil , priorityMetadata is nil", name: "pod is nil , priorityMetadata is nil",
}, },
{ {
pod: podWithTolerationsAndAffinity, pod: podWithTolerationsAndAffinity,
...@@ -132,7 +132,7 @@ func TestPriorityMetadata(t *testing.T) { ...@@ -132,7 +132,7 @@ func TestPriorityMetadata(t *testing.T) {
podTolerations: tolerations, podTolerations: tolerations,
affinity: podAffinity, affinity: podAffinity,
}, },
test: "Produce a priorityMetadata with default requests", name: "Produce a priorityMetadata with default requests",
}, },
{ {
pod: podWithTolerationsAndRequests, pod: podWithTolerationsAndRequests,
...@@ -141,7 +141,7 @@ func TestPriorityMetadata(t *testing.T) { ...@@ -141,7 +141,7 @@ func TestPriorityMetadata(t *testing.T) {
podTolerations: tolerations, podTolerations: tolerations,
affinity: nil, affinity: nil,
}, },
test: "Produce a priorityMetadata with specified requests", name: "Produce a priorityMetadata with specified requests",
}, },
{ {
pod: podWithAffinityAndRequests, pod: podWithAffinityAndRequests,
...@@ -150,7 +150,7 @@ func TestPriorityMetadata(t *testing.T) { ...@@ -150,7 +150,7 @@ func TestPriorityMetadata(t *testing.T) {
podTolerations: nil, podTolerations: nil,
affinity: podAffinity, affinity: podAffinity,
}, },
test: "Produce a priorityMetadata with specified requests", name: "Produce a priorityMetadata with specified requests",
}, },
} }
mataDataProducer := NewPriorityMetadataFactory( mataDataProducer := NewPriorityMetadataFactory(
...@@ -159,9 +159,11 @@ func TestPriorityMetadata(t *testing.T) { ...@@ -159,9 +159,11 @@ func TestPriorityMetadata(t *testing.T) {
schedulertesting.FakeReplicaSetLister([]*extensions.ReplicaSet{}), schedulertesting.FakeReplicaSetLister([]*extensions.ReplicaSet{}),
schedulertesting.FakeStatefulSetLister([]*apps.StatefulSet{})) schedulertesting.FakeStatefulSetLister([]*apps.StatefulSet{}))
for _, test := range tests { for _, test := range tests {
ptData := mataDataProducer(test.pod, nil) t.Run(test.name, func(t *testing.T) {
if !reflect.DeepEqual(test.expected, ptData) { ptData := mataDataProducer(test.pod, nil)
t.Errorf("%s: expected %#v, got %#v", test.test, test.expected, ptData) if !reflect.DeepEqual(test.expected, ptData) {
} t.Errorf("expected %#v, got %#v", test.expected, ptData)
}
})
} }
} }
...@@ -109,7 +109,7 @@ func TestMostRequested(t *testing.T) { ...@@ -109,7 +109,7 @@ func TestMostRequested(t *testing.T) {
pods []*v1.Pod pods []*v1.Pod
nodes []*v1.Node nodes []*v1.Node
expectedList schedulerapi.HostPriorityList expectedList schedulerapi.HostPriorityList
test string name string
}{ }{
{ {
/* /*
...@@ -126,7 +126,7 @@ func TestMostRequested(t *testing.T) { ...@@ -126,7 +126,7 @@ func TestMostRequested(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}},
test: "nothing scheduled, nothing requested", name: "nothing scheduled, nothing requested",
}, },
{ {
/* /*
...@@ -143,7 +143,7 @@ func TestMostRequested(t *testing.T) { ...@@ -143,7 +143,7 @@ func TestMostRequested(t *testing.T) {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 6000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 6}, {Host: "machine2", Score: 5}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 6}, {Host: "machine2", Score: 5}},
test: "nothing scheduled, resources requested, differently sized machines", name: "nothing scheduled, resources requested, differently sized machines",
}, },
{ {
/* /*
...@@ -160,7 +160,7 @@ func TestMostRequested(t *testing.T) { ...@@ -160,7 +160,7 @@ func TestMostRequested(t *testing.T) {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)}, nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 3}, {Host: "machine2", Score: 4}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 3}, {Host: "machine2", Score: 4}},
test: "no resources requested, pods scheduled with resources", name: "no resources requested, pods scheduled with resources",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels2}}, {Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels2}},
{Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels1}}, {Spec: cpuOnly, ObjectMeta: metav1.ObjectMeta{Labels: labels1}},
...@@ -183,7 +183,7 @@ func TestMostRequested(t *testing.T) { ...@@ -183,7 +183,7 @@ func TestMostRequested(t *testing.T) {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)}, nodes: []*v1.Node{makeNode("machine1", 10000, 20000), makeNode("machine2", 10000, 20000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 4}, {Host: "machine2", Score: 5}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 4}, {Host: "machine2", Score: 5}},
test: "resources requested, pods scheduled with resources", name: "resources requested, pods scheduled with resources",
pods: []*v1.Pod{ pods: []*v1.Pod{
{Spec: cpuOnly}, {Spec: cpuOnly},
{Spec: cpuAndMemory}, {Spec: cpuAndMemory},
...@@ -204,18 +204,20 @@ func TestMostRequested(t *testing.T) { ...@@ -204,18 +204,20 @@ func TestMostRequested(t *testing.T) {
pod: &v1.Pod{Spec: bigCPUAndMemory}, pod: &v1.Pod{Spec: bigCPUAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 10000, 8000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 10000, 8000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 4}, {Host: "machine2", Score: 2}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 4}, {Host: "machine2", Score: 2}},
test: "resources requested with more than the node, pods scheduled with resources", name: "resources requested with more than the node, pods scheduled with resources",
}, },
} }
for _, test := range tests { for _, test := range tests {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes) t.Run(test.name, func(t *testing.T) {
list, err := priorityFunction(MostRequestedPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes) nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(test.pods, test.nodes)
if err != nil { list, err := priorityFunction(MostRequestedPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
if !reflect.DeepEqual(test.expectedList, list) { }
t.Errorf("%s: expected %#v, got %#v", test.test, test.expectedList, list) if !reflect.DeepEqual(test.expectedList, list) {
} t.Errorf("expected %#v, got %#v", test.expectedList, list)
}
})
} }
} }
...@@ -105,7 +105,7 @@ func TestNodeAffinityPriority(t *testing.T) { ...@@ -105,7 +105,7 @@ func TestNodeAffinityPriority(t *testing.T) {
pod *v1.Pod pod *v1.Pod
nodes []*v1.Node nodes []*v1.Node
expectedList schedulerapi.HostPriorityList expectedList schedulerapi.HostPriorityList
test string name string
}{ }{
{ {
pod: &v1.Pod{ pod: &v1.Pod{
...@@ -119,7 +119,7 @@ func TestNodeAffinityPriority(t *testing.T) { ...@@ -119,7 +119,7 @@ func TestNodeAffinityPriority(t *testing.T) {
{ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}}, {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
}, },
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}},
test: "all machines are same priority as NodeAffinity is nil", name: "all machines are same priority as NodeAffinity is nil",
}, },
{ {
pod: &v1.Pod{ pod: &v1.Pod{
...@@ -133,7 +133,7 @@ func TestNodeAffinityPriority(t *testing.T) { ...@@ -133,7 +133,7 @@ func TestNodeAffinityPriority(t *testing.T) {
{ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}}, {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
}, },
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}},
test: "no machine macthes preferred scheduling requirements in NodeAffinity of pod so all machines' priority is zero", name: "no machine macthes preferred scheduling requirements in NodeAffinity of pod so all machines' priority is zero",
}, },
{ {
pod: &v1.Pod{ pod: &v1.Pod{
...@@ -147,7 +147,7 @@ func TestNodeAffinityPriority(t *testing.T) { ...@@ -147,7 +147,7 @@ func TestNodeAffinityPriority(t *testing.T) {
{ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}}, {ObjectMeta: metav1.ObjectMeta{Name: "machine3", Labels: label3}},
}, },
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}},
test: "only machine1 matches the preferred scheduling requirements of pod", name: "only machine1 matches the preferred scheduling requirements of pod",
}, },
{ {
pod: &v1.Pod{ pod: &v1.Pod{
...@@ -161,19 +161,21 @@ func TestNodeAffinityPriority(t *testing.T) { ...@@ -161,19 +161,21 @@ func TestNodeAffinityPriority(t *testing.T) {
{ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}}, {ObjectMeta: metav1.ObjectMeta{Name: "machine2", Labels: label2}},
}, },
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine5", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 3}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine5", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 3}},
test: "all machines matches the preferred scheduling requirements of pod but with different priorities ", name: "all machines matches the preferred scheduling requirements of pod but with different priorities ",
}, },
} }
for _, test := range tests { for _, test := range tests {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes) t.Run(test.name, func(t *testing.T) {
nap := priorityFunction(CalculateNodeAffinityPriorityMap, CalculateNodeAffinityPriorityReduce, nil) nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes)
list, err := nap(test.pod, nodeNameToInfo, test.nodes) nap := priorityFunction(CalculateNodeAffinityPriorityMap, CalculateNodeAffinityPriorityReduce, nil)
if err != nil { list, err := nap(test.pod, nodeNameToInfo, test.nodes)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
if !reflect.DeepEqual(test.expectedList, list) { }
t.Errorf("%s: \nexpected %#v, \ngot %#v", test.test, test.expectedList, list) if !reflect.DeepEqual(test.expectedList, list) {
} t.Errorf("expected %#v, \ngot %#v", test.expectedList, list)
}
})
} }
} }
...@@ -36,7 +36,7 @@ func TestNewNodeLabelPriority(t *testing.T) { ...@@ -36,7 +36,7 @@ func TestNewNodeLabelPriority(t *testing.T) {
label string label string
presence bool presence bool
expectedList schedulerapi.HostPriorityList expectedList schedulerapi.HostPriorityList
test string name string
}{ }{
{ {
nodes: []*v1.Node{ nodes: []*v1.Node{
...@@ -47,7 +47,7 @@ func TestNewNodeLabelPriority(t *testing.T) { ...@@ -47,7 +47,7 @@ func TestNewNodeLabelPriority(t *testing.T) {
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}},
label: "baz", label: "baz",
presence: true, presence: true,
test: "no match found, presence true", name: "no match found, presence true",
}, },
{ {
nodes: []*v1.Node{ nodes: []*v1.Node{
...@@ -58,7 +58,7 @@ func TestNewNodeLabelPriority(t *testing.T) { ...@@ -58,7 +58,7 @@ func TestNewNodeLabelPriority(t *testing.T) {
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
label: "baz", label: "baz",
presence: false, presence: false,
test: "no match found, presence false", name: "no match found, presence false",
}, },
{ {
nodes: []*v1.Node{ nodes: []*v1.Node{
...@@ -69,7 +69,7 @@ func TestNewNodeLabelPriority(t *testing.T) { ...@@ -69,7 +69,7 @@ func TestNewNodeLabelPriority(t *testing.T) {
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}},
label: "foo", label: "foo",
presence: true, presence: true,
test: "one match found, presence true", name: "one match found, presence true",
}, },
{ {
nodes: []*v1.Node{ nodes: []*v1.Node{
...@@ -80,7 +80,7 @@ func TestNewNodeLabelPriority(t *testing.T) { ...@@ -80,7 +80,7 @@ func TestNewNodeLabelPriority(t *testing.T) {
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
label: "foo", label: "foo",
presence: false, presence: false,
test: "one match found, presence false", name: "one match found, presence false",
}, },
{ {
nodes: []*v1.Node{ nodes: []*v1.Node{
...@@ -91,7 +91,7 @@ func TestNewNodeLabelPriority(t *testing.T) { ...@@ -91,7 +91,7 @@ func TestNewNodeLabelPriority(t *testing.T) {
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
label: "bar", label: "bar",
presence: true, presence: true,
test: "two matches found, presence true", name: "two matches found, presence true",
}, },
{ {
nodes: []*v1.Node{ nodes: []*v1.Node{
...@@ -102,25 +102,27 @@ func TestNewNodeLabelPriority(t *testing.T) { ...@@ -102,25 +102,27 @@ func TestNewNodeLabelPriority(t *testing.T) {
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}},
label: "bar", label: "bar",
presence: false, presence: false,
test: "two matches found, presence false", name: "two matches found, presence false",
}, },
} }
for _, test := range tests { for _, test := range tests {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes) t.Run(test.name, func(t *testing.T) {
labelPrioritizer := &NodeLabelPrioritizer{ nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes)
label: test.label, labelPrioritizer := &NodeLabelPrioritizer{
presence: test.presence, label: test.label,
} presence: test.presence,
list, err := priorityFunction(labelPrioritizer.CalculateNodeLabelPriorityMap, nil, nil)(nil, nodeNameToInfo, test.nodes) }
if err != nil { list, err := priorityFunction(labelPrioritizer.CalculateNodeLabelPriorityMap, nil, nil)(nil, nodeNameToInfo, test.nodes)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
// sort the two lists to avoid failures on account of different ordering }
sort.Sort(test.expectedList) // sort the two lists to avoid failures on account of different ordering
sort.Sort(list) sort.Sort(test.expectedList)
if !reflect.DeepEqual(test.expectedList, list) { sort.Sort(list)
t.Errorf("%s: expected %#v, got %#v", test.test, test.expectedList, list) if !reflect.DeepEqual(test.expectedList, list) {
} t.Errorf("expected %#v, got %#v", test.expectedList, list)
}
})
} }
} }
...@@ -84,7 +84,7 @@ func TestNodePreferAvoidPriority(t *testing.T) { ...@@ -84,7 +84,7 @@ func TestNodePreferAvoidPriority(t *testing.T) {
pod *v1.Pod pod *v1.Pod
nodes []*v1.Node nodes []*v1.Node
expectedList schedulerapi.HostPriorityList expectedList schedulerapi.HostPriorityList
test string name string
}{ }{
{ {
pod: &v1.Pod{ pod: &v1.Pod{
...@@ -97,7 +97,7 @@ func TestNodePreferAvoidPriority(t *testing.T) { ...@@ -97,7 +97,7 @@ func TestNodePreferAvoidPriority(t *testing.T) {
}, },
nodes: testNodes, nodes: testNodes,
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
test: "pod managed by ReplicationController should avoid a node, this node get lowest priority score", name: "pod managed by ReplicationController should avoid a node, this node get lowest priority score",
}, },
{ {
pod: &v1.Pod{ pod: &v1.Pod{
...@@ -110,7 +110,7 @@ func TestNodePreferAvoidPriority(t *testing.T) { ...@@ -110,7 +110,7 @@ func TestNodePreferAvoidPriority(t *testing.T) {
}, },
nodes: testNodes, nodes: testNodes,
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
test: "ownership by random controller should be ignored", name: "ownership by random controller should be ignored",
}, },
{ {
pod: &v1.Pod{ pod: &v1.Pod{
...@@ -123,7 +123,7 @@ func TestNodePreferAvoidPriority(t *testing.T) { ...@@ -123,7 +123,7 @@ func TestNodePreferAvoidPriority(t *testing.T) {
}, },
nodes: testNodes, nodes: testNodes,
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: schedulerapi.MaxPriority}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
test: "owner without Controller field set should be ignored", name: "owner without Controller field set should be ignored",
}, },
{ {
pod: &v1.Pod{ pod: &v1.Pod{
...@@ -136,21 +136,23 @@ func TestNodePreferAvoidPriority(t *testing.T) { ...@@ -136,21 +136,23 @@ func TestNodePreferAvoidPriority(t *testing.T) {
}, },
nodes: testNodes, nodes: testNodes,
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: schedulerapi.MaxPriority}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: schedulerapi.MaxPriority}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: schedulerapi.MaxPriority}},
test: "pod managed by ReplicaSet should avoid a node, this node get lowest priority score", name: "pod managed by ReplicaSet should avoid a node, this node get lowest priority score",
}, },
} }
for _, test := range tests { for _, test := range tests {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes) t.Run(test.name, func(t *testing.T) {
list, err := priorityFunction(CalculateNodePreferAvoidPodsPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes) nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes)
if err != nil { list, err := priorityFunction(CalculateNodePreferAvoidPodsPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
// sort the two lists to avoid failures on account of different ordering }
sort.Sort(test.expectedList) // sort the two lists to avoid failures on account of different ordering
sort.Sort(list) sort.Sort(test.expectedList)
if !reflect.DeepEqual(test.expectedList, list) { sort.Sort(list)
t.Errorf("%s: expected %#v, got %#v", test.test, test.expectedList, list) if !reflect.DeepEqual(test.expectedList, list) {
} t.Errorf("expected %#v, got %#v", test.expectedList, list)
}
})
} }
} }
...@@ -103,49 +103,50 @@ func TestResourceLimistPriority(t *testing.T) { ...@@ -103,49 +103,50 @@ func TestResourceLimistPriority(t *testing.T) {
pod *v1.Pod pod *v1.Pod
nodes []*v1.Node nodes []*v1.Node
expectedList schedulerapi.HostPriorityList expectedList schedulerapi.HostPriorityList
test string name string
}{ }{
{ {
pod: &v1.Pod{Spec: noResources}, pod: &v1.Pod{Spec: noResources},
nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 0), makeNode("machine3", 0, 10000), makeNode("machine4", 0, 0)}, nodes: []*v1.Node{makeNode("machine1", 4000, 10000), makeNode("machine2", 4000, 0), makeNode("machine3", 0, 10000), makeNode("machine4", 0, 0)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}, {Host: "machine4", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 0}, {Host: "machine3", Score: 0}, {Host: "machine4", Score: 0}},
test: "pod does not specify its resource limits", name: "pod does not specify its resource limits",
}, },
{ {
pod: &v1.Pod{Spec: cpuOnly}, pod: &v1.Pod{Spec: cpuOnly},
nodes: []*v1.Node{makeNode("machine1", 3000, 10000), makeNode("machine2", 2000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 3000, 10000), makeNode("machine2", 2000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 0}},
test: "pod only specifies cpu limits", name: "pod only specifies cpu limits",
}, },
{ {
pod: &v1.Pod{Spec: memOnly}, pod: &v1.Pod{Spec: memOnly},
nodes: []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 1}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}, {Host: "machine2", Score: 1}},
test: "pod only specifies mem limits", name: "pod only specifies mem limits",
}, },
{ {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)}, nodes: []*v1.Node{makeNode("machine1", 4000, 4000), makeNode("machine2", 5000, 10000)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 1}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 1}, {Host: "machine2", Score: 1}},
test: "pod specifies both cpu and mem limits", name: "pod specifies both cpu and mem limits",
}, },
{ {
pod: &v1.Pod{Spec: cpuAndMemory}, pod: &v1.Pod{Spec: cpuAndMemory},
nodes: []*v1.Node{makeNode("machine1", 0, 0)}, nodes: []*v1.Node{makeNode("machine1", 0, 0)},
expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}}, expectedList: []schedulerapi.HostPriority{{Host: "machine1", Score: 0}},
test: "node does not advertise its allocatables", name: "node does not advertise its allocatables",
}, },
} }
for _, test := range tests { for _, test := range tests {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes) t.Run(test.name, func(t *testing.T) {
list, err := priorityFunction(ResourceLimitsPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes) nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes)
if err != nil { list, err := priorityFunction(ResourceLimitsPriorityMap, nil, nil)(test.pod, nodeNameToInfo, test.nodes)
t.Errorf("unexpected error: %v", err) if err != nil {
} t.Errorf("unexpected error: %v", err)
if !reflect.DeepEqual(test.expectedList, list) { }
t.Errorf("%s: expected %#v, got %#v", test.test, test.expectedList, list) if !reflect.DeepEqual(test.expectedList, list) {
} t.Errorf("expected %#v, got %#v", test.expectedList, list)
}
})
} }
} }
...@@ -54,11 +54,11 @@ func TestTaintAndToleration(t *testing.T) { ...@@ -54,11 +54,11 @@ func TestTaintAndToleration(t *testing.T) {
pod *v1.Pod pod *v1.Pod
nodes []*v1.Node nodes []*v1.Node
expectedList schedulerapi.HostPriorityList expectedList schedulerapi.HostPriorityList
test string name string
}{ }{
// basic test case // basic test case
{ {
test: "node with taints tolerated by the pod, gets a higher score than those node with intolerable taints", name: "node with taints tolerated by the pod, gets a higher score than those node with intolerable taints",
pod: podWithTolerations([]v1.Toleration{{ pod: podWithTolerations([]v1.Toleration{{
Key: "foo", Key: "foo",
Operator: v1.TolerationOpEqual, Operator: v1.TolerationOpEqual,
...@@ -84,7 +84,7 @@ func TestTaintAndToleration(t *testing.T) { ...@@ -84,7 +84,7 @@ func TestTaintAndToleration(t *testing.T) {
}, },
// the count of taints that are tolerated by pod, does not matter. // the count of taints that are tolerated by pod, does not matter.
{ {
test: "the nodes that all of their taints are tolerated by the pod, get the same score, no matter how many tolerable taints a node has", name: "the nodes that all of their taints are tolerated by the pod, get the same score, no matter how many tolerable taints a node has",
pod: podWithTolerations([]v1.Toleration{ pod: podWithTolerations([]v1.Toleration{
{ {
Key: "cpu-type", Key: "cpu-type",
...@@ -127,7 +127,7 @@ func TestTaintAndToleration(t *testing.T) { ...@@ -127,7 +127,7 @@ func TestTaintAndToleration(t *testing.T) {
}, },
// the count of taints on a node that are not tolerated by pod, matters. // the count of taints on a node that are not tolerated by pod, matters.
{ {
test: "the more intolerable taints a node has, the lower score it gets.", name: "the more intolerable taints a node has, the lower score it gets.",
pod: podWithTolerations([]v1.Toleration{{ pod: podWithTolerations([]v1.Toleration{{
Key: "foo", Key: "foo",
Operator: v1.TolerationOpEqual, Operator: v1.TolerationOpEqual,
...@@ -163,7 +163,7 @@ func TestTaintAndToleration(t *testing.T) { ...@@ -163,7 +163,7 @@ func TestTaintAndToleration(t *testing.T) {
}, },
// taints-tolerations priority only takes care about the taints and tolerations that have effect PreferNoSchedule // taints-tolerations priority only takes care about the taints and tolerations that have effect PreferNoSchedule
{ {
test: "only taints and tolerations that have effect PreferNoSchedule are checked by taints-tolerations priority function", name: "only taints and tolerations that have effect PreferNoSchedule are checked by taints-tolerations priority function",
pod: podWithTolerations([]v1.Toleration{ pod: podWithTolerations([]v1.Toleration{
{ {
Key: "cpu-type", Key: "cpu-type",
...@@ -205,7 +205,7 @@ func TestTaintAndToleration(t *testing.T) { ...@@ -205,7 +205,7 @@ func TestTaintAndToleration(t *testing.T) {
}, },
}, },
{ {
test: "Default behaviour No taints and tolerations, lands on node with no taints", name: "Default behaviour No taints and tolerations, lands on node with no taints",
//pod without tolerations //pod without tolerations
pod: podWithTolerations([]v1.Toleration{}), pod: podWithTolerations([]v1.Toleration{}),
nodes: []*v1.Node{ nodes: []*v1.Node{
...@@ -226,16 +226,17 @@ func TestTaintAndToleration(t *testing.T) { ...@@ -226,16 +226,17 @@ func TestTaintAndToleration(t *testing.T) {
}, },
} }
for _, test := range tests { for _, test := range tests {
nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes) t.Run(test.name, func(t *testing.T) {
ttp := priorityFunction(ComputeTaintTolerationPriorityMap, ComputeTaintTolerationPriorityReduce, nil) nodeNameToInfo := schedulercache.CreateNodeNameToInfoMap(nil, test.nodes)
list, err := ttp(test.pod, nodeNameToInfo, test.nodes) ttp := priorityFunction(ComputeTaintTolerationPriorityMap, ComputeTaintTolerationPriorityReduce, nil)
if err != nil { list, err := ttp(test.pod, nodeNameToInfo, test.nodes)
t.Errorf("%s, unexpected error: %v", test.test, err) if err != nil {
} t.Errorf("unexpected error: %v", err)
}
if !reflect.DeepEqual(test.expectedList, list) { if !reflect.DeepEqual(test.expectedList, list) {
t.Errorf("%s,\nexpected:\n\t%+v,\ngot:\n\t%+v", test.test, test.expectedList, list) t.Errorf("expected:\n\t%+v,\ngot:\n\t%+v", test.expectedList, list)
} }
})
} }
} }
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment