mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Add check for empty priority list and refactor tests.
This commit is contained in:
parent
18fa043b1b
commit
4bee622865
@ -49,10 +49,13 @@ func (g *genericScheduler) Schedule(pod api.Pod, minionLister MinionLister) (str
|
|||||||
if len(priorityList) == 0 {
|
if len(priorityList) == 0 {
|
||||||
return "", fmt.Errorf("failed to find a fit for pod: %v", pod)
|
return "", fmt.Errorf("failed to find a fit for pod: %v", pod)
|
||||||
}
|
}
|
||||||
return g.selectHost(priorityList), nil
|
return g.selectHost(priorityList)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (g *genericScheduler) selectHost(priorityList HostPriorityList) string {
|
func (g *genericScheduler) selectHost(priorityList HostPriorityList) (string, error) {
|
||||||
|
if len(priorityList) == 0 {
|
||||||
|
return "", fmt.Errorf("empty priorityList")
|
||||||
|
}
|
||||||
sort.Sort(priorityList)
|
sort.Sort(priorityList)
|
||||||
|
|
||||||
hosts := getMinHosts(priorityList)
|
hosts := getMinHosts(priorityList)
|
||||||
@ -60,7 +63,7 @@ func (g *genericScheduler) selectHost(priorityList HostPriorityList) string {
|
|||||||
defer g.randomLock.Unlock()
|
defer g.randomLock.Unlock()
|
||||||
|
|
||||||
ix := g.random.Int() % len(hosts)
|
ix := g.random.Int() % len(hosts)
|
||||||
return hosts[ix]
|
return hosts[ix], nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func findNodesThatFit(pod api.Pod, podLister PodLister, predicates []FitPredicate, nodes api.MinionList) (api.MinionList, error) {
|
func findNodesThatFit(pod api.Pod, podLister PodLister, predicates []FitPredicate, nodes api.MinionList) (api.MinionList, error) {
|
||||||
|
@ -23,6 +23,7 @@ import (
|
|||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/api"
|
||||||
|
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
|
||||||
)
|
)
|
||||||
|
|
||||||
func falsePredicate(pod api.Pod, existingPods []api.Pod, node string) (bool, error) {
|
func falsePredicate(pod api.Pod, existingPods []api.Pod, node string) (bool, error) {
|
||||||
@ -72,14 +73,16 @@ func TestSelectHost(t *testing.T) {
|
|||||||
scheduler := genericScheduler{random: rand.New(rand.NewSource(0))}
|
scheduler := genericScheduler{random: rand.New(rand.NewSource(0))}
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
list HostPriorityList
|
list HostPriorityList
|
||||||
possibleHosts map[string]bool
|
possibleHosts util.StringSet
|
||||||
|
expectsErr bool
|
||||||
}{
|
}{
|
||||||
{
|
{
|
||||||
list: []HostPriority{
|
list: []HostPriority{
|
||||||
{host: "machine1.1", score: 1},
|
{host: "machine1.1", score: 1},
|
||||||
{host: "machine2.1", score: 2},
|
{host: "machine2.1", score: 2},
|
||||||
},
|
},
|
||||||
possibleHosts: map[string]bool{"machine1.1": true},
|
possibleHosts: util.NewStringSet("machine1.1"),
|
||||||
|
expectsErr: false,
|
||||||
},
|
},
|
||||||
// equal scores
|
// equal scores
|
||||||
{
|
{
|
||||||
@ -89,7 +92,8 @@ func TestSelectHost(t *testing.T) {
|
|||||||
{host: "machine1.3", score: 1},
|
{host: "machine1.3", score: 1},
|
||||||
{host: "machine2.1", score: 2},
|
{host: "machine2.1", score: 2},
|
||||||
},
|
},
|
||||||
possibleHosts: map[string]bool{"machine1.1": true, "machine1.2": true, "machine1.3": true},
|
possibleHosts: util.NewStringSet("machine1.1", "machine1.2", "machine1.3"),
|
||||||
|
expectsErr: false,
|
||||||
},
|
},
|
||||||
// out of order scores
|
// out of order scores
|
||||||
{
|
{
|
||||||
@ -100,16 +104,32 @@ func TestSelectHost(t *testing.T) {
|
|||||||
{host: "machine3.1", score: 3},
|
{host: "machine3.1", score: 3},
|
||||||
{host: "machine1.3", score: 1},
|
{host: "machine1.3", score: 1},
|
||||||
},
|
},
|
||||||
possibleHosts: map[string]bool{"machine1.1": true, "machine1.2": true, "machine1.3": true},
|
possibleHosts: util.NewStringSet("machine1.1", "machine1.2", "machine1.3"),
|
||||||
|
expectsErr: false,
|
||||||
|
},
|
||||||
|
// empty priorityList
|
||||||
|
{
|
||||||
|
list: []HostPriority{},
|
||||||
|
possibleHosts: util.NewStringSet(),
|
||||||
|
expectsErr: true,
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range tests {
|
for _, test := range tests {
|
||||||
// increase the randomness
|
// increase the randomness
|
||||||
for i := 0; i < 10; i++ {
|
for i := 0; i < 10; i++ {
|
||||||
got := scheduler.selectHost(test.list)
|
got, err := scheduler.selectHost(test.list)
|
||||||
if !test.possibleHosts[got] {
|
if test.expectsErr {
|
||||||
t.Errorf("got %s is not in the possible map %v", got, test.possibleHosts)
|
if err == nil {
|
||||||
|
t.Error("Unexpected non-error")
|
||||||
|
}
|
||||||
|
} else {
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("Unexpected error: %v", err)
|
||||||
|
}
|
||||||
|
if !test.possibleHosts.Has(got) {
|
||||||
|
t.Errorf("got %s is not in the possible map %v", got, test.possibleHosts)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user