Validating against negative weights for priority functions

This commit is contained in:
Abhishek Gupta 2015-04-02 10:58:58 -07:00
parent 25724f7e31
commit 8deedf8e1f
2 changed files with 7 additions and 8 deletions

View File

@ -29,8 +29,8 @@ func ValidatePolicy(policy schedulerapi.Policy) error {
validationErrors := make([]error, 0) validationErrors := make([]error, 0)
for _, priority := range policy.Priorities { for _, priority := range policy.Priorities {
if priority.Weight == 0 { if priority.Weight <= 0 {
validationErrors = append(validationErrors, fmt.Errorf("Priority %s should have a non-zero weight applied to it", priority.Name)) validationErrors = append(validationErrors, fmt.Errorf("Priority %s should have a positive weight applied to it", priority.Name))
} }
} }

View File

@ -25,29 +25,28 @@ import (
func TestValidatePriorityWithNoWeight(t *testing.T) { func TestValidatePriorityWithNoWeight(t *testing.T) {
policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority"}}} policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority"}}}
if ValidatePolicy(policy) == nil { if ValidatePolicy(policy) == nil {
t.Errorf("Expected error about priority weight being zero") t.Errorf("Expected error about priority weight not being positive")
} }
} }
func TestValidatePriorityWithZeroWeight(t *testing.T) { func TestValidatePriorityWithZeroWeight(t *testing.T) {
policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority", Weight: 0}}} policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority", Weight: 0}}}
if ValidatePolicy(policy) == nil { if ValidatePolicy(policy) == nil {
t.Errorf("Expected error about priority weight being zero") t.Errorf("Expected error about priority weight not being positive")
} }
} }
func TestValidatePriorityWithNonZeroWeight(t *testing.T) { func TestValidatePriorityWithNonZeroWeight(t *testing.T) {
policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: 2}}} policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: 2}}}
errs := ValidatePolicy(policy) errs := ValidatePolicy(policy)
if ValidatePolicy(policy) != nil { if errs != nil {
t.Errorf("Unexpected errors %v", errs) t.Errorf("Unexpected errors %v", errs)
} }
} }
func TestValidatePriorityWithNegativeWeight(t *testing.T) { func TestValidatePriorityWithNegativeWeight(t *testing.T) {
policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: -2}}} policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: -2}}}
errs := ValidatePolicy(policy) if ValidatePolicy(policy) == nil {
if ValidatePolicy(policy) != nil { t.Errorf("Expected error about priority weight not being positive")
t.Errorf("Unexpected errors %v", errs)
} }
} }