ceph-csi/vendor/k8s.io/kubernetes/pkg/kubectl/autoscale_test.go

133 lines
3.6 KiB
Go
Raw Normal View History

2018-01-09 18:57:14 +00:00
/*
Copyright 2017 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 kubectl
import (
"reflect"
"testing"
autoscalingv1 "k8s.io/api/autoscaling/v1"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
)
func TestHPAGenerate(t *testing.T) {
tests := []struct {
2018-03-06 22:33:18 +00:00
name string
HPAName string
scaleRefKind string
scaleRefName string
scaleRefApiVersion string
minReplicas int32
maxReplicas int32
CPUPercent int32
expected *autoscalingv1.HorizontalPodAutoscaler
expectErr bool
2018-01-09 18:57:14 +00:00
}{
{
2018-03-06 22:33:18 +00:00
name: "valid case",
HPAName: "foo",
minReplicas: 1,
maxReplicas: 10,
CPUPercent: 80,
scaleRefKind: "kind",
scaleRefName: "name",
scaleRefApiVersion: "apiVersion",
2018-01-09 18:57:14 +00:00
expected: &autoscalingv1.HorizontalPodAutoscaler{
ObjectMeta: metav1.ObjectMeta{
Name: "foo",
},
Spec: autoscalingv1.HorizontalPodAutoscalerSpec{
TargetCPUUtilizationPercentage: newInt32(80),
ScaleTargetRef: autoscalingv1.CrossVersionObjectReference{
Kind: "kind",
Name: "name",
APIVersion: "apiVersion",
},
MaxReplicas: int32(10),
MinReplicas: newInt32(1),
},
},
expectErr: false,
},
{
2018-03-06 22:33:18 +00:00
name: "'name' is a required parameter",
scaleRefKind: "kind",
scaleRefName: "name",
scaleRefApiVersion: "apiVersion",
expectErr: true,
2018-01-09 18:57:14 +00:00
},
{
2018-03-06 22:33:18 +00:00
name: "'max' is a required parameter",
HPAName: "foo",
scaleRefKind: "kind",
scaleRefName: "name",
scaleRefApiVersion: "apiVersion",
expectErr: true,
2018-01-09 18:57:14 +00:00
},
{
2018-03-06 22:33:18 +00:00
name: "'max' must be greater than or equal to 'min'",
HPAName: "foo",
minReplicas: 10,
maxReplicas: 1,
scaleRefKind: "kind",
scaleRefName: "name",
scaleRefApiVersion: "apiVersion",
expectErr: true,
2018-01-09 18:57:14 +00:00
},
{
2018-03-06 22:33:18 +00:00
name: "'max' must be at least 1",
HPAName: "foo",
minReplicas: 1,
maxReplicas: -10,
scaleRefKind: "kind",
scaleRefName: "name",
scaleRefApiVersion: "apiVersion",
expectErr: true,
2018-01-09 18:57:14 +00:00
},
}
2018-03-06 22:33:18 +00:00
2018-01-09 18:57:14 +00:00
for _, test := range tests {
2018-03-06 22:33:18 +00:00
generator := HorizontalPodAutoscalerGeneratorV1{
Name: test.HPAName,
ScaleRefKind: test.scaleRefKind,
ScaleRefName: test.scaleRefName,
ScaleRefApiVersion: test.scaleRefApiVersion,
MinReplicas: test.minReplicas,
MaxReplicas: test.maxReplicas,
CPUPercent: test.CPUPercent,
}
obj, err := generator.StructuredGenerate()
2018-01-09 18:57:14 +00:00
if test.expectErr && err != nil {
continue
}
if !test.expectErr && err != nil {
t.Errorf("[%s] unexpected error: %v", test.name, err)
}
if test.expectErr && err == nil {
t.Errorf("[%s] expect error, got nil", test.name)
}
if !reflect.DeepEqual(obj.(*autoscalingv1.HorizontalPodAutoscaler), test.expected) {
t.Errorf("[%s] want:\n%#v\ngot:\n%#v", test.name, test.expected, obj.(*autoscalingv1.HorizontalPodAutoscaler))
}
}
}
func newInt32(value int) *int32 {
v := int32(value)
return &v
}