fix validation for HorizontalPodAutoscalerSpec
This commit is contained in:
		| @@ -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 | ||||||
| } | } | ||||||
|   | |||||||
| @@ -63,55 +63,67 @@ func TestValidateHorizontalPodAutoscaler(t *testing.T) { | |||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
|  |  | ||||||
| 	errorCases := map[string]extensions.HorizontalPodAutoscaler{ | 	errorCases := []struct { | ||||||
| 		"must be bigger or equal to 1": { | 		horizontalPodAutoscaler extensions.HorizontalPodAutoscaler | ||||||
| 			ObjectMeta: api.ObjectMeta{ | 		msg                     string | ||||||
| 				Name:      "myautoscaler", | 	}{ | ||||||
| 				Namespace: api.NamespaceDefault, | 		{ | ||||||
| 			}, | 			horizontalPodAutoscaler: extensions.HorizontalPodAutoscaler{ | ||||||
| 			Spec: extensions.HorizontalPodAutoscalerSpec{ | 				ObjectMeta: api.ObjectMeta{ | ||||||
| 				ScaleRef: extensions.SubresourceReference{ | 					Name:      "myautoscaler", | ||||||
| 					Subresource: "scale", | 					Namespace: api.NamespaceDefault, | ||||||
|  | 				}, | ||||||
|  | 				Spec: extensions.HorizontalPodAutoscalerSpec{ | ||||||
|  | 					ScaleRef: extensions.SubresourceReference{ | ||||||
|  | 						Subresource: "scale", | ||||||
|  | 					}, | ||||||
|  | 					MinReplicas: newInt(-1), | ||||||
|  | 					MaxReplicas: 5, | ||||||
| 				}, | 				}, | ||||||
| 				MinReplicas: newInt(-1), |  | ||||||
| 				MaxReplicas: 5, |  | ||||||
| 			}, | 			}, | ||||||
|  | 			msg: "must be bigger or equal to 1", | ||||||
| 		}, | 		}, | ||||||
| 		"must be bigger or equal to minReplicas": { | 		{ | ||||||
| 			ObjectMeta: api.ObjectMeta{ | 			horizontalPodAutoscaler: extensions.HorizontalPodAutoscaler{ | ||||||
| 				Name:      "myautoscaler", | 				ObjectMeta: api.ObjectMeta{ | ||||||
| 				Namespace: api.NamespaceDefault, | 					Name:      "myautoscaler", | ||||||
| 			}, | 					Namespace: api.NamespaceDefault, | ||||||
| 			Spec: extensions.HorizontalPodAutoscalerSpec{ | 				}, | ||||||
| 				ScaleRef: extensions.SubresourceReference{ | 				Spec: extensions.HorizontalPodAutoscalerSpec{ | ||||||
| 					Subresource: "scale", | 					ScaleRef: extensions.SubresourceReference{ | ||||||
|  | 						Subresource: "scale", | ||||||
|  | 					}, | ||||||
|  | 					MinReplicas: newInt(7), | ||||||
|  | 					MaxReplicas: 5, | ||||||
| 				}, | 				}, | ||||||
| 				MinReplicas: newInt(7), |  | ||||||
| 				MaxReplicas: 5, |  | ||||||
| 			}, | 			}, | ||||||
|  | 			msg: "must be bigger or equal to minReplicas", | ||||||
| 		}, | 		}, | ||||||
| 		"must be non-negative": { | 		{ | ||||||
| 			ObjectMeta: api.ObjectMeta{ | 			horizontalPodAutoscaler: extensions.HorizontalPodAutoscaler{ | ||||||
| 				Name:      "myautoscaler", | 				ObjectMeta: api.ObjectMeta{ | ||||||
| 				Namespace: api.NamespaceDefault, | 					Name:      "myautoscaler", | ||||||
| 			}, | 					Namespace: api.NamespaceDefault, | ||||||
| 			Spec: extensions.HorizontalPodAutoscalerSpec{ | 				}, | ||||||
| 				ScaleRef: extensions.SubresourceReference{ | 				Spec: extensions.HorizontalPodAutoscalerSpec{ | ||||||
| 					Subresource: "scale", | 					ScaleRef: extensions.SubresourceReference{ | ||||||
|  | 						Subresource: "scale", | ||||||
|  | 					}, | ||||||
|  | 					MinReplicas:    newInt(1), | ||||||
|  | 					MaxReplicas:    5, | ||||||
|  | 					CPUUtilization: &extensions.CPUTargetUtilization{TargetPercentage: -70}, | ||||||
| 				}, | 				}, | ||||||
| 				MinReplicas:    newInt(1), |  | ||||||
| 				MaxReplicas:    5, |  | ||||||
| 				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) | ||||||
| 		} | 		} | ||||||
| 	} | 	} | ||||||
| } | } | ||||||
|   | |||||||
		Reference in New Issue
	
	Block a user
	 mqliang
					mqliang