Merge pull request #2197 from claire921/scheduler
Refactor selectHost in Sched
This commit is contained in:
@@ -53,6 +53,9 @@ func (g *genericScheduler) Schedule(pod api.Pod, minionLister MinionLister) (str
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (g *genericScheduler) selectHost(priorityList HostPriorityList) (string, error) {
|
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)
|
||||||
|
@@ -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) {
|
||||||
@@ -68,6 +69,72 @@ func makeMinionList(nodeNames []string) api.MinionList {
|
|||||||
return result
|
return result
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestSelectHost(t *testing.T) {
|
||||||
|
scheduler := genericScheduler{random: rand.New(rand.NewSource(0))}
|
||||||
|
tests := []struct {
|
||||||
|
list HostPriorityList
|
||||||
|
possibleHosts util.StringSet
|
||||||
|
expectsErr bool
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
list: []HostPriority{
|
||||||
|
{host: "machine1.1", score: 1},
|
||||||
|
{host: "machine2.1", score: 2},
|
||||||
|
},
|
||||||
|
possibleHosts: util.NewStringSet("machine1.1"),
|
||||||
|
expectsErr: false,
|
||||||
|
},
|
||||||
|
// equal scores
|
||||||
|
{
|
||||||
|
list: []HostPriority{
|
||||||
|
{host: "machine1.1", score: 1},
|
||||||
|
{host: "machine1.2", score: 1},
|
||||||
|
{host: "machine1.3", score: 1},
|
||||||
|
{host: "machine2.1", score: 2},
|
||||||
|
},
|
||||||
|
possibleHosts: util.NewStringSet("machine1.1", "machine1.2", "machine1.3"),
|
||||||
|
expectsErr: false,
|
||||||
|
},
|
||||||
|
// out of order scores
|
||||||
|
{
|
||||||
|
list: []HostPriority{
|
||||||
|
{host: "machine1.1", score: 1},
|
||||||
|
{host: "machine1.2", score: 1},
|
||||||
|
{host: "machine2.1", score: 2},
|
||||||
|
{host: "machine3.1", score: 3},
|
||||||
|
{host: "machine1.3", score: 1},
|
||||||
|
},
|
||||||
|
possibleHosts: util.NewStringSet("machine1.1", "machine1.2", "machine1.3"),
|
||||||
|
expectsErr: false,
|
||||||
|
},
|
||||||
|
// empty priorityList
|
||||||
|
{
|
||||||
|
list: []HostPriority{},
|
||||||
|
possibleHosts: util.NewStringSet(),
|
||||||
|
expectsErr: true,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, test := range tests {
|
||||||
|
// increase the randomness
|
||||||
|
for i := 0; i < 10; i++ {
|
||||||
|
got, err := scheduler.selectHost(test.list)
|
||||||
|
if test.expectsErr {
|
||||||
|
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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestGenericScheduler(t *testing.T) {
|
func TestGenericScheduler(t *testing.T) {
|
||||||
tests := []struct {
|
tests := []struct {
|
||||||
predicates []FitPredicate
|
predicates []FitPredicate
|
||||||
|
Reference in New Issue
Block a user