Merge pull request #48337 from sakeven/fix/validation_test

Automatic merge from submit-queue

scheduler: fix validation test

Signed-off-by: sakeven <jc5930@sina.cn>



**What this PR does / why we need it**:

Without setting `Weight`, `ValidatePolicy` will report
```
Priority for extender http://127.0.0.1:8081/extender should have a positive weight applied to it
```

**Besides**, it seems it's not a good way to test ValidatePolicy by```if ValidatePolicy(extenderPolicy) == nil```, because we can't determine specific reason which causes error.

**Special notes for your reviewer**:

**Release note**:

```
NONE
```
pull/6/head
Kubernetes Submit Queue 2017-07-07 22:38:28 -07:00 committed by GitHub
commit 093dd52db2
1 changed files with 48 additions and 58 deletions

View File

@ -17,70 +17,60 @@ limitations under the License.
package validation package validation
import ( import (
"errors"
"fmt"
"testing" "testing"
"k8s.io/kubernetes/plugin/pkg/scheduler/api" "k8s.io/kubernetes/plugin/pkg/scheduler/api"
) )
func TestValidatePriorityWithNoWeight(t *testing.T) { func TestValidatePolicy(t *testing.T) {
policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority"}}} tests := []struct {
if ValidatePolicy(policy) == nil { policy api.Policy
t.Errorf("Expected error about priority weight not being positive") expected error
} }{
} {
policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "NoWeightPriority"}}},
func TestValidatePriorityWithZeroWeight(t *testing.T) { 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}}} },
if ValidatePolicy(policy) == nil { {
t.Errorf("Expected error about priority weight not being positive") 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"),
} },
{
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}}} expected: nil,
errs := ValidatePolicy(policy) },
if errs != nil { {
t.Errorf("Unexpected errors %v", errs) 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"),
} },
{
func TestValidatePriorityWithNegativeWeight(t *testing.T) { policy: api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: api.MaxWeight}}},
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"),
if ValidatePolicy(policy) == nil { },
t.Errorf("Expected error about priority weight not being positive") {
} policy: api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", FilterVerb: "filter", Weight: 2}}},
} expected: nil,
},
func TestValidatePriorityWithOverFlowWeight(t *testing.T) { {
policy := api.Policy{Priorities: []api.PriorityPolicy{{Name: "WeightPriority", Weight: api.MaxWeight}}} policy: api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", FilterVerb: "filter", Weight: -2}}},
if ValidatePolicy(policy) == nil { expected: errors.New("Priority for extender http://127.0.0.1:8081/extender should have a positive weight applied to it"),
t.Errorf("Expected error about priority weight not being overflown.") },
} {
} policy: api.Policy{
ExtenderConfigs: []api.ExtenderConfig{
func TestValidateExtenderWithNonNegativeWeight(t *testing.T) { {URLPrefix: "http://127.0.0.1:8081/extender", BindVerb: "bind", Weight: 2},
extenderPolicy := api.Policy{ExtenderConfigs: []api.ExtenderConfig{{URLPrefix: "http://127.0.0.1:8081/extender", FilterVerb: "filter", Weight: 2}}} {URLPrefix: "http://127.0.0.1:8082/extender", BindVerb: "bind", Weight: 2},
errs := ValidatePolicy(extenderPolicy) }},
if errs != nil { expected: errors.New("Only one extender can implement bind, found 2"),
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"},
}, },
} }
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)
}
} }
} }