Skip to content

Commit

Permalink
Added test cases
Browse files Browse the repository at this point in the history
  • Loading branch information
Abhishek Gupta committed Mar 2, 2015
1 parent e5d319d commit a04e600
Show file tree
Hide file tree
Showing 3 changed files with 81 additions and 1 deletion.
7 changes: 7 additions & 0 deletions pkg/scheduler/generic_scheduler.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,6 +135,13 @@ func findNodesThatFit(pod api.Pod, podLister PodLister, predicates map[string]Fi
// All scores are finally combined (added) to get the total weighted scores of all minions
func prioritizeNodes(pod api.Pod, podLister PodLister, priorityConfigs []PriorityConfig, minionLister MinionLister) (HostPriorityList, error) {
result := HostPriorityList{}

// If no priority configs are provided, then the EqualPriority function is applied
// This is required to generate the priority list in the required format
if len(priorityConfigs) == 0 {
return EqualPriority(pod, podLister, minionLister)
}

combinedScores := map[string]int{}
for _, priorityConfig := range priorityConfigs {
weight := priorityConfig.Weight
Expand Down
2 changes: 1 addition & 1 deletion plugin/pkg/scheduler/api/latest/latest.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,6 @@ const OldestVersion = "v1"
var Versions = []string{"v1"}

// Codec is the default codec for serializing input that should use
// the latest supported version. Use this Codec when reading from the file.
// the latest supported version.
// This codec can decode any object that Kubernetes is aware of.
var Codec = v1.Codec
73 changes: 73 additions & 0 deletions plugin/pkg/scheduler/factory/factory_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,10 @@ import (
"github.com/GoogleCloudPlatform/kubernetes/pkg/client"
"github.com/GoogleCloudPlatform/kubernetes/pkg/client/cache"
"github.com/GoogleCloudPlatform/kubernetes/pkg/runtime"
algorithm "github.com/GoogleCloudPlatform/kubernetes/pkg/scheduler"
"github.com/GoogleCloudPlatform/kubernetes/pkg/util"
schedulerapi "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler/api"
latestschedulerapi "github.com/GoogleCloudPlatform/kubernetes/plugin/pkg/scheduler/api/latest"
)

func TestCreate(t *testing.T) {
Expand All @@ -46,6 +49,76 @@ func TestCreate(t *testing.T) {
factory.Create()
}

func TestCreateFromConfig(t *testing.T) {
var configData []byte
var policy schedulerapi.Policy

handler := util.FakeHandler{
StatusCode: 500,
ResponseBody: "",
T: t,
}
server := httptest.NewServer(&handler)
defer server.Close()
client := client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})
factory := NewConfigFactory(client)

// Register the predicate and priority functions
// These would be registered by the DefaultProvider in regular operation
RegisterFitPredicate("PodFitsPorts", algorithm.PodFitsPorts)
RegisterFitPredicate("PodFitsResources", algorithm.NewResourceFitPredicate(MinionLister))
RegisterFitPredicate("NoDiskConflict", algorithm.NoDiskConflict)
RegisterFitPredicate("MatchNodeSelector", algorithm.NewSelectorMatchPredicate(MinionLister))
RegisterFitPredicate("HostName", algorithm.PodFitsHost)
RegisterPriorityFunction("LeastRequestedPriority", algorithm.LeastRequestedPriority, 1)
RegisterPriorityFunction("ServiceSpreadingPriority", algorithm.NewServiceSpreadPriority(ServiceLister), 1)
RegisterPriorityFunction("EqualPriority", algorithm.EqualPriority, 0)

configData = []byte(`{
"kind" : "Policy",
"apiVersion" : "v1",
"predicates" : [
{"name" : "TestZoneAffinity", "argument" : {"serviceAffinity" : {"labels" : ["zone"]}}},
{"name" : "TestRequireZone", "argument" : {"labelsPresence" : {"labels" : ["zone"], "presence" : true}}},
{"name" : "PodFitsPorts"},
{"name" : "MatchNodeSelector"}
],
"priorities" : [
{"name" : "RackSpread", "weight" : 2, "argument" : {"serviceAntiAffinity" : {"label" : "rack"}}},
{"name" : "ServiceSpreadingPriority", "weight" : 1}
]
}`)
err := latestschedulerapi.Codec.DecodeInto(configData, &policy)
if err != nil {
t.Errorf("Invalid configuration: %v", err)
}

factory.CreateFromConfig(policy)
}

func TestCreateFromEmptyConfig(t *testing.T) {
var configData []byte
var policy schedulerapi.Policy

handler := util.FakeHandler{
StatusCode: 500,
ResponseBody: "",
T: t,
}
server := httptest.NewServer(&handler)
defer server.Close()
client := client.NewOrDie(&client.Config{Host: server.URL, Version: testapi.Version()})
factory := NewConfigFactory(client)

configData = []byte(`{}`)
err := latestschedulerapi.Codec.DecodeInto(configData, &policy)
if err != nil {
t.Errorf("Invalid configuration: %v", err)
}

factory.CreateFromConfig(policy)
}

func TestPollMinions(t *testing.T) {
table := []struct {
minions []api.Node
Expand Down

0 comments on commit a04e600

Please sign in to comment.