mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-03 17:30:00 +00:00
Merge pull request #51024 from fisherxu/UTautoscaler
Automatic merge from submit-queue (batch tested with PRs 51114, 51233, 51024, 51053, 51197) add UT for pkg/apis/autoscaling/v2alpha1/defaults.go **What this PR does / why we need it**: add UT for pkg/apis/autoscaling/v2alpha1/defaults.go **Which issue this PR fixes** *(optional, in `fixes #<issue number>(, fixes #<issue_number>, ...)` format, will close that issue when PR gets merged)*: fixes # **Special notes for your reviewer**: **Release note**: ```release-note None ```
This commit is contained in:
commit
4a4c194bbd
@ -107,7 +107,6 @@ pkg/apis/authorization/v1
|
|||||||
pkg/apis/authorization/v1beta1
|
pkg/apis/authorization/v1beta1
|
||||||
pkg/apis/authorization/validation
|
pkg/apis/authorization/validation
|
||||||
pkg/apis/autoscaling
|
pkg/apis/autoscaling
|
||||||
pkg/apis/autoscaling/v2alpha1
|
|
||||||
pkg/apis/autoscaling/validation
|
pkg/apis/autoscaling/validation
|
||||||
pkg/apis/batch
|
pkg/apis/batch
|
||||||
pkg/apis/batch/validation
|
pkg/apis/batch/validation
|
||||||
|
@ -3,6 +3,7 @@ package(default_visibility = ["//visibility:public"])
|
|||||||
load(
|
load(
|
||||||
"@io_bazel_rules_go//go:def.bzl",
|
"@io_bazel_rules_go//go:def.bzl",
|
||||||
"go_library",
|
"go_library",
|
||||||
|
"go_test",
|
||||||
)
|
)
|
||||||
|
|
||||||
go_library(
|
go_library(
|
||||||
@ -39,3 +40,19 @@ filegroup(
|
|||||||
srcs = [":package-srcs"],
|
srcs = [":package-srcs"],
|
||||||
tags = ["automanaged"],
|
tags = ["automanaged"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
go_test(
|
||||||
|
name = "go_default_xtest",
|
||||||
|
srcs = ["defaults_test.go"],
|
||||||
|
deps = [
|
||||||
|
":go_default_library",
|
||||||
|
"//pkg/api:go_default_library",
|
||||||
|
"//pkg/api/install:go_default_library",
|
||||||
|
"//pkg/apis/autoscaling:go_default_library",
|
||||||
|
"//pkg/apis/autoscaling/install:go_default_library",
|
||||||
|
"//vendor/k8s.io/api/autoscaling/v2alpha1:go_default_library",
|
||||||
|
"//vendor/k8s.io/api/core/v1:go_default_library",
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/api/equality:go_default_library",
|
||||||
|
"//vendor/k8s.io/apimachinery/pkg/runtime:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
131
pkg/apis/autoscaling/v2alpha1/defaults_test.go
Normal file
131
pkg/apis/autoscaling/v2alpha1/defaults_test.go
Normal file
@ -0,0 +1,131 @@
|
|||||||
|
/*
|
||||||
|
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 v2alpha1_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"reflect"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
autoscalingv2alpha1 "k8s.io/api/autoscaling/v2alpha1"
|
||||||
|
"k8s.io/api/core/v1"
|
||||||
|
apiequality "k8s.io/apimachinery/pkg/api/equality"
|
||||||
|
"k8s.io/apimachinery/pkg/runtime"
|
||||||
|
"k8s.io/kubernetes/pkg/api"
|
||||||
|
_ "k8s.io/kubernetes/pkg/api/install"
|
||||||
|
"k8s.io/kubernetes/pkg/apis/autoscaling"
|
||||||
|
_ "k8s.io/kubernetes/pkg/apis/autoscaling/install"
|
||||||
|
. "k8s.io/kubernetes/pkg/apis/autoscaling/v2alpha1"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestSetDefaultHPA(t *testing.T) {
|
||||||
|
utilizationDefaultVal := int32(autoscaling.DefaultCPUUtilization)
|
||||||
|
defaultReplicas := newInt32(1)
|
||||||
|
defaultTemplate := []autoscalingv2alpha1.MetricSpec{
|
||||||
|
{
|
||||||
|
Type: autoscalingv2alpha1.ResourceMetricSourceType,
|
||||||
|
Resource: &autoscalingv2alpha1.ResourceMetricSource{
|
||||||
|
Name: v1.ResourceCPU,
|
||||||
|
TargetAverageUtilization: &utilizationDefaultVal,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
tests := []struct {
|
||||||
|
original *autoscalingv2alpha1.HorizontalPodAutoscaler
|
||||||
|
expected *autoscalingv2alpha1.HorizontalPodAutoscaler
|
||||||
|
}{
|
||||||
|
{ // MinReplicas default value
|
||||||
|
original: &autoscalingv2alpha1.HorizontalPodAutoscaler{
|
||||||
|
Spec: autoscalingv2alpha1.HorizontalPodAutoscalerSpec{
|
||||||
|
Metrics: defaultTemplate,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: &autoscalingv2alpha1.HorizontalPodAutoscaler{
|
||||||
|
Spec: autoscalingv2alpha1.HorizontalPodAutoscalerSpec{
|
||||||
|
MinReplicas: defaultReplicas,
|
||||||
|
Metrics: defaultTemplate,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ // MinReplicas update
|
||||||
|
original: &autoscalingv2alpha1.HorizontalPodAutoscaler{
|
||||||
|
Spec: autoscalingv2alpha1.HorizontalPodAutoscalerSpec{
|
||||||
|
MinReplicas: newInt32(3),
|
||||||
|
Metrics: defaultTemplate,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: &autoscalingv2alpha1.HorizontalPodAutoscaler{
|
||||||
|
Spec: autoscalingv2alpha1.HorizontalPodAutoscalerSpec{
|
||||||
|
MinReplicas: newInt32(3),
|
||||||
|
Metrics: defaultTemplate,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{ // Metrics default value
|
||||||
|
original: &autoscalingv2alpha1.HorizontalPodAutoscaler{
|
||||||
|
Spec: autoscalingv2alpha1.HorizontalPodAutoscalerSpec{
|
||||||
|
MinReplicas: defaultReplicas,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
expected: &autoscalingv2alpha1.HorizontalPodAutoscaler{
|
||||||
|
Spec: autoscalingv2alpha1.HorizontalPodAutoscalerSpec{
|
||||||
|
MinReplicas: defaultReplicas,
|
||||||
|
Metrics: defaultTemplate,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
|
||||||
|
for i, test := range tests {
|
||||||
|
original := test.original
|
||||||
|
expected := test.expected
|
||||||
|
obj2 := roundTrip(t, runtime.Object(original))
|
||||||
|
got, ok := obj2.(*autoscalingv2alpha1.HorizontalPodAutoscaler)
|
||||||
|
if !ok {
|
||||||
|
t.Fatalf("(%d) unexpected object: %v", i, obj2)
|
||||||
|
}
|
||||||
|
if !apiequality.Semantic.DeepEqual(got.Spec, expected.Spec) {
|
||||||
|
t.Errorf("(%d) got different than expected\ngot:\n\t%+v\nexpected:\n\t%+v", i, got.Spec, expected.Spec)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func roundTrip(t *testing.T, obj runtime.Object) runtime.Object {
|
||||||
|
data, err := runtime.Encode(api.Codecs.LegacyCodec(SchemeGroupVersion), obj)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%v\n %#v", err, obj)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
obj2, err := runtime.Decode(api.Codecs.UniversalDecoder(), data)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%v\nData: %s\nSource: %#v", err, string(data), obj)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
obj3 := reflect.New(reflect.TypeOf(obj).Elem()).Interface().(runtime.Object)
|
||||||
|
err = api.Scheme.Convert(obj2, obj3, nil)
|
||||||
|
if err != nil {
|
||||||
|
t.Errorf("%v\nSource: %#v", err, obj2)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
return obj3
|
||||||
|
}
|
||||||
|
|
||||||
|
func newInt32(val int32) *int32 {
|
||||||
|
p := new(int32)
|
||||||
|
*p = val
|
||||||
|
return p
|
||||||
|
}
|
Loading…
Reference in New Issue
Block a user