123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338 |
- /*
- Copyright 2016 The Kubernetes Authors.
- Licensed under the Apache License, Version 2.0 (the "License");
- you may not use this file except in compliance with the License.
- You may obtain a copy of the License at
- http://www.apache.org/licenses/LICENSE-2.0
- Unless required by applicable law or agreed to in writing, software
- distributed under the License is distributed on an "AS IS" BASIS,
- WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
- See the License for the specific language governing permissions and
- limitations under the License.
- */
- package validation
- import (
- "strings"
- "testing"
- "k8s.io/kubernetes/pkg/api"
- "k8s.io/kubernetes/pkg/apis/autoscaling"
- "k8s.io/kubernetes/pkg/controller/podautoscaler"
- )
- func TestValidateScale(t *testing.T) {
- successCases := []autoscaling.Scale{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "frontend",
- Namespace: api.NamespaceDefault,
- },
- Spec: autoscaling.ScaleSpec{
- Replicas: 1,
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "frontend",
- Namespace: api.NamespaceDefault,
- },
- Spec: autoscaling.ScaleSpec{
- Replicas: 10,
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "frontend",
- Namespace: api.NamespaceDefault,
- },
- Spec: autoscaling.ScaleSpec{
- Replicas: 0,
- },
- },
- }
- for _, successCase := range successCases {
- if errs := ValidateScale(&successCase); len(errs) != 0 {
- t.Errorf("expected success: %v", errs)
- }
- }
- errorCases := []struct {
- scale autoscaling.Scale
- msg string
- }{
- {
- scale: autoscaling.Scale{
- ObjectMeta: api.ObjectMeta{
- Name: "frontend",
- Namespace: api.NamespaceDefault,
- },
- Spec: autoscaling.ScaleSpec{
- Replicas: -1,
- },
- },
- msg: "must be greater than or equal to 0",
- },
- }
- for _, c := range errorCases {
- if errs := ValidateScale(&c.scale); len(errs) == 0 {
- t.Errorf("expected failure for %s", c.msg)
- } else if !strings.Contains(errs[0].Error(), c.msg) {
- t.Errorf("unexpected error: %v, expected: %s", errs[0], c.msg)
- }
- }
- }
- func TestValidateHorizontalPodAutoscaler(t *testing.T) {
- successCases := []autoscaling.HorizontalPodAutoscaler{
- {
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Kind: "ReplicationController",
- Name: "myrc",
- },
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- TargetCPUUtilizationPercentage: newInt32(70),
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Kind: "ReplicationController",
- Name: "myrc",
- },
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- },
- },
- {
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- Annotations: map[string]string{
- podautoscaler.HpaCustomMetricsTargetAnnotationName: "{\"items\":[{\"name\":\"qps\",\"value\":\"20\"}]}",
- },
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Kind: "ReplicationController",
- Name: "myrc",
- },
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- },
- },
- }
- for _, successCase := range successCases {
- if errs := ValidateHorizontalPodAutoscaler(&successCase); len(errs) != 0 {
- t.Errorf("expected success: %v", errs)
- }
- }
- errorCases := []struct {
- horizontalPodAutoscaler autoscaling.HorizontalPodAutoscaler
- msg string
- }{
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{Name: "myautoscaler", Namespace: api.NamespaceDefault},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{Name: "myrc"},
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- TargetCPUUtilizationPercentage: newInt32(70),
- },
- },
- msg: "scaleTargetRef.kind: Required",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{Name: "myautoscaler", Namespace: api.NamespaceDefault},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{Kind: "..", Name: "myrc"},
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- TargetCPUUtilizationPercentage: newInt32(70),
- },
- },
- msg: "scaleTargetRef.kind: Invalid",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{Name: "myautoscaler", Namespace: api.NamespaceDefault},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{Kind: "ReplicationController"},
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- TargetCPUUtilizationPercentage: newInt32(70),
- },
- },
- msg: "scaleTargetRef.name: Required",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{Name: "myautoscaler", Namespace: api.NamespaceDefault},
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{Kind: "ReplicationController", Name: ".."},
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- TargetCPUUtilizationPercentage: newInt32(70),
- },
- },
- msg: "scaleTargetRef.name: Invalid",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{},
- MinReplicas: newInt32(-1),
- MaxReplicas: 5,
- },
- },
- msg: "must be greater than 0",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{},
- MinReplicas: newInt32(7),
- MaxReplicas: 5,
- },
- },
- msg: "must be greater than or equal to `minReplicas`",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{},
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- TargetCPUUtilizationPercentage: newInt32(-70),
- },
- },
- msg: "must be greater than 0",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- Annotations: map[string]string{
- podautoscaler.HpaCustomMetricsTargetAnnotationName: "broken",
- },
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Kind: "ReplicationController",
- Name: "myrc",
- },
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- },
- },
- msg: "failed to parse custom metrics target annotation",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- Annotations: map[string]string{
- podautoscaler.HpaCustomMetricsTargetAnnotationName: "{}",
- },
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Kind: "ReplicationController",
- Name: "myrc",
- },
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- },
- },
- msg: "custom metrics target must not be empty",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- Annotations: map[string]string{
- podautoscaler.HpaCustomMetricsTargetAnnotationName: "{\"items\":[{\"value\":\"20\"}]}",
- },
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Kind: "ReplicationController",
- Name: "myrc",
- },
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- },
- },
- msg: "missing custom metric target name",
- },
- {
- horizontalPodAutoscaler: autoscaling.HorizontalPodAutoscaler{
- ObjectMeta: api.ObjectMeta{
- Name: "myautoscaler",
- Namespace: api.NamespaceDefault,
- Annotations: map[string]string{
- podautoscaler.HpaCustomMetricsTargetAnnotationName: "{\"items\":[{\"name\":\"qps\",\"value\":\"0\"}]}",
- },
- },
- Spec: autoscaling.HorizontalPodAutoscalerSpec{
- ScaleTargetRef: autoscaling.CrossVersionObjectReference{
- Kind: "ReplicationController",
- Name: "myrc",
- },
- MinReplicas: newInt32(1),
- MaxReplicas: 5,
- },
- },
- msg: "custom metric target value must be greater than 0",
- },
- }
- for _, c := range errorCases {
- errs := ValidateHorizontalPodAutoscaler(&c.horizontalPodAutoscaler)
- if len(errs) == 0 {
- t.Errorf("expected failure for %q", c.msg)
- } else if !strings.Contains(errs[0].Error(), c.msg) {
- t.Errorf("unexpected error: %q, expected: %q", errs[0], c.msg)
- }
- }
- }
- func newInt32(val int32) *int32 {
- p := new(int32)
- *p = val
- return p
- }
|