From 86c453a192d6ad5b8dfaf3960b480a919776610d Mon Sep 17 00:00:00 2001 From: sakeven Date: Fri, 30 Jun 2017 19:19:21 +0800 Subject: [PATCH] schduler: fix validation test Signed-off-by: sakeven --- .../api/validation/validation_test.go | 106 ++++++++---------- 1 file changed, 48 insertions(+), 58 deletions(-) diff --git a/plugin/pkg/scheduler/api/validation/validation_test.go b/plugin/pkg/scheduler/api/validation/validation_test.go index 1e5f44ec91bd..b7a6cd2ca9a8 100644 --- a/plugin/pkg/scheduler/api/validation/validation_test.go +++ b/plugin/pkg/scheduler/api/validation/validation_test.go @@ -17,70 +17,60 @@ limitations under the License. package validation import ( + "errors" + "fmt" "testing" "k8s.io/kubernetes/plugin/pkg/scheduler/api" ) -func TestValidatePriorityWithNoWeight(t *testing.T) { - policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority"}}} - if ValidatePolicy(policy) == nil { - t.Errorf("Expected error about priority weight not being positive") - } -} - -func TestValidatePriorityWithZeroWeight(t *testing.T) { - policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority", Weight: 0}}} - if ValidatePolicy(policy) == nil { - t.Errorf("Expected error about priority weight not being positive") - } -} - -func TestValidatePriorityWithNonZeroWeight(t *testing.T) { - policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: 2}}} - errs := ValidatePolicy(policy) - if errs != nil { - t.Errorf("Unexpected errors %v", errs) - } -} - -func TestValidatePriorityWithNegativeWeight(t *testing.T) { - policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: -2}}} - if ValidatePolicy(policy) == nil { - t.Errorf("Expected error about priority weight not being positive") - } -} - -func TestValidatePriorityWithOverFlowWeight(t *testing.T) { - policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: api.MaxWeight}}} - if ValidatePolicy(policy) == nil { - t.Errorf("Expected error about priority weight not being overflown.") - } -} - -func TestValidateExtenderWithNonNegativeWeight(t *testing.T) { - extenderPolicy := api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", FilterVerb: "filter", Weight: 2}}} - errs := ValidatePolicy(extenderPolicy) - if errs != nil { - t.Errorf("Unexpected errors %v", errs) - } -} - -func TestValidateExtenderWithNegativeWeight(t *testing.T) { - extenderPolicy := api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", FilterVerb: "filter", Weight: -2}}} - if ValidatePolicy(extenderPolicy) == nil { - t.Errorf("Expected error about priority weight for extender not being positive") - } -} - -func TestValidateMultipleExtendersWithBind(t *testing.T) { - extenderPolicy := api.Policy{ - ExtenderConfigs: []api.ExtenderConfig{ - {URLPrefix: "http://127.0.0.1:8081/extender", BindVerb: "bind"}, - {URLPrefix: "http://127.0.0.1:8082/extender", BindVerb: "bind"}, +func TestValidatePolicy(t *testing.T) { + tests := []struct { + policy api.Policy + expected error + }{ + { + policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority"}}}, + expected: errors.New("Priority NoWeightPriority should have a positive weight applied to it or it has overflown"), + }, + { + policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority", Weight: 0}}}, + expected: errors.New("Priority NoWeightPriority should have a positive weight applied to it or it has overflown"), + }, + { + policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: 2}}}, + expected: nil, + }, + { + policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: -2}}}, + expected: errors.New("Priority WeightPriority should have a positive weight applied to it or it has overflown"), + }, + { + policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: api.MaxWeight}}}, + expected: errors.New("Priority WeightPriority should have a positive weight applied to it or it has overflown"), + }, + { + policy: api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", FilterVerb: "filter", Weight: 2}}}, + expected: nil, + }, + { + policy: api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", FilterVerb: "filter", Weight: -2}}}, + expected: errors.New("Priority for extender http://127.0.0.1:8081/extender should have a positive weight applied to it"), + }, + { + policy: api.Policy{ + ExtenderConfigs: []api.ExtenderConfig{ + {URLPrefix: "http://127.0.0.1:8081/extender", BindVerb: "bind", Weight: 2}, + {URLPrefix: "http://127.0.0.1:8082/extender", BindVerb: "bind", Weight: 2}, + }}, + expected: errors.New("Only one extender can implement bind, found 2"), }, } - if ValidatePolicy(extenderPolicy) == nil { - t.Errorf("Expected failure when multiple extenders with bind") + + for _, test := range tests { + actual := ValidatePolicy(test.policy) + if fmt.Sprint(test.expected) != fmt.Sprint(actual) { + t.Errorf("expected: %s, actual: %s", test.expected, actual) + } } }