fix validation for HorizontalPodAutoscalerSpec

This commit is contained in:
mqliang 2015-10-16 18:24:34 +08:00
parent f3e0bb0d41
commit b6f19b140e
2 changed files with 50 additions and 38 deletions

View File

@ -63,7 +63,7 @@ func validateHorizontalPodAutoscalerSpec(autoscaler extensions.HorizontalPodAuto
allErrs = append(allErrs, errs.NewFieldInvalid("maxReplicas", autoscaler.MaxReplicas, `must be bigger or equal to minReplicas`)) allErrs = append(allErrs, errs.NewFieldInvalid("maxReplicas", autoscaler.MaxReplicas, `must be bigger or equal to minReplicas`))
} }
if autoscaler.CPUUtilization != nil && autoscaler.CPUUtilization.TargetPercentage < 1 { if autoscaler.CPUUtilization != nil && autoscaler.CPUUtilization.TargetPercentage < 1 {
allErrs = append(allErrs, errs.NewFieldInvalid("cpuUtilization.targetPercentage", autoscaler.CPUUtilization.TargetPercentage, isNegativeErrorMsg)) allErrs = append(allErrs, errs.NewFieldInvalid("cpuUtilization.targetPercentage", autoscaler.CPUUtilization.TargetPercentage, `must be bigger or equal to 1`))
} }
return allErrs return allErrs
} }

View File

@ -63,8 +63,12 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
} }
} }
errorCases := map[string]extensions.HorizontalPodAutoscaler{ errorCases := []struct {
"must be bigger or equal to 1": { horizontalPodAutoscaler extensions.HorizontalPodAutoscaler
msg string
}{
{
horizontalPodAutoscaler: extensions.HorizontalPodAutoscaler{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "myautoscaler", Name: "myautoscaler",
Namespace: api.NamespaceDefault, Namespace: api.NamespaceDefault,
@ -77,7 +81,10 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
MaxReplicas: 5, MaxReplicas: 5,
}, },
}, },
"must be bigger or equal to minReplicas": { msg: "must be bigger or equal to 1",
},
{
horizontalPodAutoscaler: extensions.HorizontalPodAutoscaler{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "myautoscaler", Name: "myautoscaler",
Namespace: api.NamespaceDefault, Namespace: api.NamespaceDefault,
@ -90,7 +97,10 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
MaxReplicas: 5, MaxReplicas: 5,
}, },
}, },
"must be non-negative": { msg: "must be bigger or equal to minReplicas",
},
{
horizontalPodAutoscaler: extensions.HorizontalPodAutoscaler{
ObjectMeta: api.ObjectMeta{ ObjectMeta: api.ObjectMeta{
Name: "myautoscaler", Name: "myautoscaler",
Namespace: api.NamespaceDefault, Namespace: api.NamespaceDefault,
@ -104,14 +114,16 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) {
CPUUtilization: &extensions.CPUTargetUtilization{TargetPercentage: -70}, CPUUtilization: &extensions.CPUTargetUtilization{TargetPercentage: -70},
}, },
}, },
msg: "must be bigger or equal to 1",
},
} }
for k, v := range errorCases { for _, c := range errorCases {
errs := ValidateHorizontalPodAutoscaler(&v) errs := ValidateHorizontalPodAutoscaler(&c.horizontalPodAutoscaler)
if len(errs) == 0 { if len(errs) == 0 {
t.Errorf("expected failure for %s", k) t.Errorf("expected failure for %s", c.msg)
} else if !strings.Contains(errs[0].Error(), k) { } else if !strings.Contains(errs[0].Error(), c.msg) {
t.Errorf("unexpected error: %v, expected: %s", errs[0], k) t.Errorf("unexpected error: %v, expected: %s", errs[0], c.msg)
} }
} }
} }