mirror of
https://github.com/k3s-io/kubernetes.git
synced 2025-08-05 10:19:50 +00:00
Adds tests
Signed-off-by: Arjun Naik <arjun@arjunnaik.in>
This commit is contained in:
parent
ac23d55d90
commit
8ab226263a
@ -79,6 +79,8 @@ func TestDefaulting(t *testing.T) {
|
|||||||
{Group: "autoscaling", Version: "v1", Kind: "HorizontalPodAutoscalerList"}: {},
|
{Group: "autoscaling", Version: "v1", Kind: "HorizontalPodAutoscalerList"}: {},
|
||||||
{Group: "autoscaling", Version: "v2beta1", Kind: "HorizontalPodAutoscaler"}: {},
|
{Group: "autoscaling", Version: "v2beta1", Kind: "HorizontalPodAutoscaler"}: {},
|
||||||
{Group: "autoscaling", Version: "v2beta1", Kind: "HorizontalPodAutoscalerList"}: {},
|
{Group: "autoscaling", Version: "v2beta1", Kind: "HorizontalPodAutoscalerList"}: {},
|
||||||
|
{Group: "autoscaling", Version: "v2beta2", Kind: "HorizontalPodAutoscaler"}: {},
|
||||||
|
{Group: "autoscaling", Version: "v2beta2", Kind: "HorizontalPodAutoscalerList"}: {},
|
||||||
{Group: "batch", Version: "v1", Kind: "Job"}: {},
|
{Group: "batch", Version: "v1", Kind: "Job"}: {},
|
||||||
{Group: "batch", Version: "v1", Kind: "JobList"}: {},
|
{Group: "batch", Version: "v1", Kind: "JobList"}: {},
|
||||||
{Group: "batch", Version: "v1beta1", Kind: "CronJob"}: {},
|
{Group: "batch", Version: "v1beta1", Kind: "CronJob"}: {},
|
||||||
|
@ -90,6 +90,43 @@ var Funcs = func(codecs runtimeserializer.CodecFactory) []interface{} {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
|
stabilizationWindow := int32(c.RandUint64())
|
||||||
|
maxPolicy := autoscaling.MaxPolicySelect
|
||||||
|
minPolicy := autoscaling.MinPolicySelect
|
||||||
|
s.Behavior = &autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: &stabilizationWindow,
|
||||||
|
SelectPolicy: &maxPolicy,
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: int32(c.RandUint64()),
|
||||||
|
PeriodSeconds: int32(c.RandUint64()),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: autoscaling.PercentScalingPolicy,
|
||||||
|
Value: int32(c.RandUint64()),
|
||||||
|
PeriodSeconds: int32(c.RandUint64()),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: &stabilizationWindow,
|
||||||
|
SelectPolicy: &minPolicy,
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: int32(c.RandUint64()),
|
||||||
|
PeriodSeconds: int32(c.RandUint64()),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: autoscaling.PercentScalingPolicy,
|
||||||
|
Value: int32(c.RandUint64()),
|
||||||
|
PeriodSeconds: int32(c.RandUint64()),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
},
|
},
|
||||||
func(s *autoscaling.HorizontalPodAutoscalerStatus, c fuzz.Continue) {
|
func(s *autoscaling.HorizontalPodAutoscalerStatus, c fuzz.Continue) {
|
||||||
c.FuzzNoCustom(s) // fuzz self without calling this function again
|
c.FuzzNoCustom(s) // fuzz self without calling this function again
|
||||||
|
@ -1,4 +1,4 @@
|
|||||||
load("@io_bazel_rules_go//go:def.bzl", "go_library")
|
load("@io_bazel_rules_go//go:def.bzl", "go_library", "go_test")
|
||||||
|
|
||||||
go_library(
|
go_library(
|
||||||
name = "go_default_library",
|
name = "go_default_library",
|
||||||
@ -37,3 +37,14 @@ filegroup(
|
|||||||
tags = ["automanaged"],
|
tags = ["automanaged"],
|
||||||
visibility = ["//visibility:public"],
|
visibility = ["//visibility:public"],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
go_test(
|
||||||
|
name = "go_default_test",
|
||||||
|
srcs = ["defaults_test.go"],
|
||||||
|
embed = [":go_default_library"],
|
||||||
|
deps = [
|
||||||
|
"//staging/src/k8s.io/api/autoscaling/v2beta2:go_default_library",
|
||||||
|
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||||
|
"//vendor/k8s.io/utils/pointer:go_default_library",
|
||||||
|
],
|
||||||
|
)
|
||||||
|
231
pkg/apis/autoscaling/v2beta2/defaults_test.go
Normal file
231
pkg/apis/autoscaling/v2beta2/defaults_test.go
Normal file
@ -0,0 +1,231 @@
|
|||||||
|
/*
|
||||||
|
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 v2beta2_test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
autoscalingv2 "k8s.io/api/autoscaling/v2beta2"
|
||||||
|
. "k8s.io/kubernetes/pkg/apis/autoscaling/v2beta2"
|
||||||
|
utilpointer "k8s.io/utils/pointer"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestGenerateScaleDownRules(t *testing.T) {
|
||||||
|
type TestCase struct {
|
||||||
|
rateDownPods int32
|
||||||
|
rateDownPodsPeriodSeconds int32
|
||||||
|
rateDownPercent int32
|
||||||
|
rateDownPercentPeriodSeconds int32
|
||||||
|
stabilizationSeconds *int32
|
||||||
|
selectPolicy *autoscalingv2.ScalingPolicySelect
|
||||||
|
|
||||||
|
expectedPolicies []autoscalingv2.HPAScalingPolicy
|
||||||
|
expectedStabilization *int32
|
||||||
|
expectedSelectPolicy string
|
||||||
|
annotation string
|
||||||
|
}
|
||||||
|
maxPolicy := autoscalingv2.MaxPolicySelect
|
||||||
|
minPolicy := autoscalingv2.MinPolicySelect
|
||||||
|
tests := []TestCase{
|
||||||
|
{
|
||||||
|
annotation: "Default values",
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PercentScalingPolicy, Value: 100, PeriodSeconds: 15},
|
||||||
|
},
|
||||||
|
expectedStabilization: nil,
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MaxPolicySelect),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotation: "All parameters are specified",
|
||||||
|
rateDownPods: 1,
|
||||||
|
rateDownPodsPeriodSeconds: 2,
|
||||||
|
rateDownPercent: 3,
|
||||||
|
rateDownPercentPeriodSeconds: 4,
|
||||||
|
stabilizationSeconds: utilpointer.Int32Ptr(25),
|
||||||
|
selectPolicy: &maxPolicy,
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PodsScalingPolicy, Value: 1, PeriodSeconds: 2},
|
||||||
|
{Type: autoscalingv2.PercentScalingPolicy, Value: 3, PeriodSeconds: 4},
|
||||||
|
},
|
||||||
|
expectedStabilization: utilpointer.Int32Ptr(25),
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MaxPolicySelect),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotation: "Percent policy is specified",
|
||||||
|
rateDownPercent: 1,
|
||||||
|
rateDownPercentPeriodSeconds: 2,
|
||||||
|
selectPolicy: &minPolicy,
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PercentScalingPolicy, Value: 1, PeriodSeconds: 2},
|
||||||
|
},
|
||||||
|
expectedStabilization: nil,
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MinPolicySelect),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotation: "Pods policy is specified",
|
||||||
|
rateDownPods: 3,
|
||||||
|
rateDownPodsPeriodSeconds: 4,
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PodsScalingPolicy, Value: 3, PeriodSeconds: 4},
|
||||||
|
},
|
||||||
|
expectedStabilization: nil,
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MaxPolicySelect),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.annotation, func(t *testing.T) {
|
||||||
|
scaleDownRules := &autoscalingv2.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: tc.stabilizationSeconds,
|
||||||
|
SelectPolicy: tc.selectPolicy,
|
||||||
|
}
|
||||||
|
if tc.rateDownPods != 0 || tc.rateDownPodsPeriodSeconds != 0 {
|
||||||
|
scaleDownRules.Policies = append(scaleDownRules.Policies, autoscalingv2.HPAScalingPolicy{
|
||||||
|
Type: autoscalingv2.PodsScalingPolicy, Value: tc.rateDownPods, PeriodSeconds: tc.rateDownPodsPeriodSeconds,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if tc.rateDownPercent != 0 || tc.rateDownPercentPeriodSeconds != 0 {
|
||||||
|
scaleDownRules.Policies = append(scaleDownRules.Policies, autoscalingv2.HPAScalingPolicy{
|
||||||
|
Type: autoscalingv2.PercentScalingPolicy, Value: tc.rateDownPercent, PeriodSeconds: tc.rateDownPercentPeriodSeconds,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
down := GenerateHPAScaleDownRules(scaleDownRules)
|
||||||
|
assert.EqualValues(t, tc.expectedPolicies, down.Policies)
|
||||||
|
if tc.expectedStabilization != nil {
|
||||||
|
assert.Equal(t, *tc.expectedStabilization, *down.StabilizationWindowSeconds)
|
||||||
|
} else {
|
||||||
|
assert.Equal(t, tc.expectedStabilization, down.StabilizationWindowSeconds)
|
||||||
|
}
|
||||||
|
assert.Equal(t, autoscalingv2.ScalingPolicySelect(tc.expectedSelectPolicy), *down.SelectPolicy)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGenerateScaleUpRules(t *testing.T) {
|
||||||
|
type TestCase struct {
|
||||||
|
rateUpPods int32
|
||||||
|
rateUpPodsPeriodSeconds int32
|
||||||
|
rateUpPercent int32
|
||||||
|
rateUpPercentPeriodSeconds int32
|
||||||
|
stabilizationSeconds *int32
|
||||||
|
selectPolicy *autoscalingv2.ScalingPolicySelect
|
||||||
|
|
||||||
|
expectedPolicies []autoscalingv2.HPAScalingPolicy
|
||||||
|
expectedStabilization *int32
|
||||||
|
expectedSelectPolicy string
|
||||||
|
annotation string
|
||||||
|
}
|
||||||
|
maxPolicy := autoscalingv2.MaxPolicySelect
|
||||||
|
minPolicy := autoscalingv2.MinPolicySelect
|
||||||
|
tests := []TestCase{
|
||||||
|
{
|
||||||
|
annotation: "Default values",
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PodsScalingPolicy, Value: 4, PeriodSeconds: 15},
|
||||||
|
{Type: autoscalingv2.PercentScalingPolicy, Value: 100, PeriodSeconds: 15},
|
||||||
|
},
|
||||||
|
expectedStabilization: utilpointer.Int32Ptr(0),
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MaxPolicySelect),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotation: "All parameters are specified",
|
||||||
|
rateUpPods: 1,
|
||||||
|
rateUpPodsPeriodSeconds: 2,
|
||||||
|
rateUpPercent: 3,
|
||||||
|
rateUpPercentPeriodSeconds: 4,
|
||||||
|
stabilizationSeconds: utilpointer.Int32Ptr(25),
|
||||||
|
selectPolicy: &maxPolicy,
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PodsScalingPolicy, Value: 1, PeriodSeconds: 2},
|
||||||
|
{Type: autoscalingv2.PercentScalingPolicy, Value: 3, PeriodSeconds: 4},
|
||||||
|
},
|
||||||
|
expectedStabilization: utilpointer.Int32Ptr(25),
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MaxPolicySelect),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotation: "Pod policy is specified",
|
||||||
|
rateUpPods: 1,
|
||||||
|
rateUpPodsPeriodSeconds: 2,
|
||||||
|
selectPolicy: &minPolicy,
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PodsScalingPolicy, Value: 1, PeriodSeconds: 2},
|
||||||
|
},
|
||||||
|
expectedStabilization: utilpointer.Int32Ptr(0),
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MinPolicySelect),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotation: "Percent policy is specified",
|
||||||
|
rateUpPercent: 7,
|
||||||
|
rateUpPercentPeriodSeconds: 10,
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PercentScalingPolicy, Value: 7, PeriodSeconds: 10},
|
||||||
|
},
|
||||||
|
expectedStabilization: utilpointer.Int32Ptr(0),
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MaxPolicySelect),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotation: "Pod policy and stabilization window are specified",
|
||||||
|
rateUpPodsPeriodSeconds: 2,
|
||||||
|
stabilizationSeconds: utilpointer.Int32Ptr(25),
|
||||||
|
rateUpPods: 4,
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PodsScalingPolicy, Value: 4, PeriodSeconds: 2},
|
||||||
|
},
|
||||||
|
expectedStabilization: utilpointer.Int32Ptr(25),
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MaxPolicySelect),
|
||||||
|
},
|
||||||
|
{
|
||||||
|
annotation: "Percent policy and stabilization window are specified",
|
||||||
|
rateUpPercent: 7,
|
||||||
|
rateUpPercentPeriodSeconds: 60,
|
||||||
|
stabilizationSeconds: utilpointer.Int32Ptr(25),
|
||||||
|
expectedPolicies: []autoscalingv2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2.PercentScalingPolicy, Value: 7, PeriodSeconds: 60},
|
||||||
|
},
|
||||||
|
expectedStabilization: utilpointer.Int32Ptr(25),
|
||||||
|
expectedSelectPolicy: string(autoscalingv2.MaxPolicySelect),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, tc := range tests {
|
||||||
|
t.Run(tc.annotation, func(t *testing.T) {
|
||||||
|
scaleUpRules := &autoscalingv2.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: tc.stabilizationSeconds,
|
||||||
|
SelectPolicy: tc.selectPolicy,
|
||||||
|
}
|
||||||
|
if tc.rateUpPods != 0 || tc.rateUpPodsPeriodSeconds != 0 {
|
||||||
|
scaleUpRules.Policies = append(scaleUpRules.Policies, autoscalingv2.HPAScalingPolicy{
|
||||||
|
Type: autoscalingv2.PodsScalingPolicy, Value: tc.rateUpPods, PeriodSeconds: tc.rateUpPodsPeriodSeconds,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
if tc.rateUpPercent != 0 || tc.rateUpPercentPeriodSeconds != 0 {
|
||||||
|
scaleUpRules.Policies = append(scaleUpRules.Policies, autoscalingv2.HPAScalingPolicy{
|
||||||
|
Type: autoscalingv2.PercentScalingPolicy, Value: tc.rateUpPercent, PeriodSeconds: tc.rateUpPercentPeriodSeconds,
|
||||||
|
})
|
||||||
|
}
|
||||||
|
up := GenerateHPAScaleUpRules(scaleUpRules)
|
||||||
|
assert.Equal(t, tc.expectedPolicies, up.Policies)
|
||||||
|
if tc.expectedStabilization != nil {
|
||||||
|
assert.Equal(t, *tc.expectedStabilization, *up.StabilizationWindowSeconds)
|
||||||
|
} else {
|
||||||
|
assert.Equal(t, tc.expectedStabilization, up.StabilizationWindowSeconds)
|
||||||
|
}
|
||||||
|
|
||||||
|
assert.Equal(t, autoscalingv2.ScalingPolicySelect(tc.expectedSelectPolicy), *up.SelectPolicy)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
@ -94,6 +94,376 @@ func TestValidateScale(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestValidateBehavior(t *testing.T) {
|
||||||
|
maxPolicy := autoscaling.MaxPolicySelect
|
||||||
|
minPolicy := autoscaling.MinPolicySelect
|
||||||
|
disabledPolicy := autoscaling.DisabledPolicySelect
|
||||||
|
incorrectPolicy := autoscaling.ScalingPolicySelect("incorrect")
|
||||||
|
simplePoliciesList := []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PercentScalingPolicy,
|
||||||
|
Value: 10,
|
||||||
|
PeriodSeconds: 1,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 1,
|
||||||
|
PeriodSeconds: 1800,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
successCases := []autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
{
|
||||||
|
ScaleUp: nil,
|
||||||
|
ScaleDown: nil,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: utilpointer.Int32Ptr(3600),
|
||||||
|
SelectPolicy: &minPolicy,
|
||||||
|
Policies: simplePoliciesList,
|
||||||
|
},
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: utilpointer.Int32Ptr(0),
|
||||||
|
SelectPolicy: &disabledPolicy,
|
||||||
|
Policies: simplePoliciesList,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: utilpointer.Int32Ptr(120),
|
||||||
|
SelectPolicy: &maxPolicy,
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 1,
|
||||||
|
PeriodSeconds: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: autoscaling.PercentScalingPolicy,
|
||||||
|
Value: 3,
|
||||||
|
PeriodSeconds: 4,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 5,
|
||||||
|
PeriodSeconds: 6,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: autoscaling.PercentScalingPolicy,
|
||||||
|
Value: 7,
|
||||||
|
PeriodSeconds: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: utilpointer.Int32Ptr(120),
|
||||||
|
SelectPolicy: &maxPolicy,
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 1,
|
||||||
|
PeriodSeconds: 2,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: autoscaling.PercentScalingPolicy,
|
||||||
|
Value: 3,
|
||||||
|
PeriodSeconds: 4,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 5,
|
||||||
|
PeriodSeconds: 6,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
Type: autoscaling.PercentScalingPolicy,
|
||||||
|
Value: 7,
|
||||||
|
PeriodSeconds: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, behavior := range successCases {
|
||||||
|
hpa := prepareHPAWithBehavior(behavior)
|
||||||
|
if errs := ValidateHorizontalPodAutoscaler(&hpa); len(errs) != 0 {
|
||||||
|
t.Errorf("expected success: %v", errs)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
errorCases := []struct {
|
||||||
|
behavior autoscaling.HorizontalPodAutoscalerBehavior
|
||||||
|
msg string
|
||||||
|
}{
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
SelectPolicy: &minPolicy,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleUp.policies: Required value: must specify at least one Policy",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: utilpointer.Int32Ptr(3601),
|
||||||
|
SelectPolicy: &minPolicy,
|
||||||
|
Policies: simplePoliciesList,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleUp.stabilizationWindowSeconds: Invalid value: 3601: must be less than or equal to 3600",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 7,
|
||||||
|
PeriodSeconds: 1801,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleUp.policies[0].periodSeconds: Invalid value: 1801: must be less than or equal to 1800",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
SelectPolicy: &incorrectPolicy,
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 7,
|
||||||
|
PeriodSeconds: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: `spec.behavior.scaleUp.selectPolicy: Unsupported value: "incorrect": supported values: "Disabled", "Max", "Min"`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.HPAScalingPolicyType("hm"),
|
||||||
|
Value: 7,
|
||||||
|
PeriodSeconds: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: `spec.behavior.scaleUp.policies[0].type: Unsupported value: "hm": supported values: "Percent", "Pods"`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleUp.policies[0].periodSeconds: Invalid value: 0: must be greater than zero",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
PeriodSeconds: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleUp.policies[0].value: Invalid value: 0: must be greater than zero",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
PeriodSeconds: -1,
|
||||||
|
Value: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleUp.policies[0].periodSeconds: Invalid value: -1: must be greater than zero",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
PeriodSeconds: 1,
|
||||||
|
Value: -1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleUp.policies[0].value: Invalid value: -1: must be greater than zero",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
SelectPolicy: &minPolicy,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleDown.policies: Required value: must specify at least one Policy",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: utilpointer.Int32Ptr(3601),
|
||||||
|
SelectPolicy: &minPolicy,
|
||||||
|
Policies: simplePoliciesList,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleDown.stabilizationWindowSeconds: Invalid value: 3601: must be less than or equal to 3600",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PercentScalingPolicy,
|
||||||
|
Value: 7,
|
||||||
|
PeriodSeconds: 1801,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleDown.policies[0].periodSeconds: Invalid value: 1801: must be less than or equal to 1800",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
SelectPolicy: &incorrectPolicy,
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 7,
|
||||||
|
PeriodSeconds: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: `spec.behavior.scaleDown.selectPolicy: Unsupported value: "incorrect": supported values: "Disabled", "Max", "Min"`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.HPAScalingPolicyType("hm"),
|
||||||
|
Value: 7,
|
||||||
|
PeriodSeconds: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: `spec.behavior.scaleDown.policies[0].type: Unsupported value: "hm": supported values: "Percent", "Pods"`,
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
Value: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleDown.policies[0].periodSeconds: Invalid value: 0: must be greater than zero",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
PeriodSeconds: 8,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleDown.policies[0].value: Invalid value: 0: must be greater than zero",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
PeriodSeconds: -1,
|
||||||
|
Value: 1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleDown.policies[0].periodSeconds: Invalid value: -1: must be greater than zero",
|
||||||
|
},
|
||||||
|
{
|
||||||
|
behavior: autoscaling.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscaling.HPAScalingRules{
|
||||||
|
Policies: []autoscaling.HPAScalingPolicy{
|
||||||
|
{
|
||||||
|
Type: autoscaling.PodsScalingPolicy,
|
||||||
|
PeriodSeconds: 1,
|
||||||
|
Value: -1,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
msg: "spec.behavior.scaleDown.policies[0].value: Invalid value: -1: must be greater than zero",
|
||||||
|
},
|
||||||
|
}
|
||||||
|
for _, c := range errorCases {
|
||||||
|
hpa := prepareHPAWithBehavior(c.behavior)
|
||||||
|
if errs := ValidateHorizontalPodAutoscaler(&hpa); 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 prepareHPAWithBehavior(b autoscaling.HorizontalPodAutoscalerBehavior) autoscaling.HorizontalPodAutoscaler {
|
||||||
|
return autoscaling.HorizontalPodAutoscaler{
|
||||||
|
ObjectMeta: metav1.ObjectMeta{
|
||||||
|
Name: "myautoscaler",
|
||||||
|
Namespace: metav1.NamespaceDefault,
|
||||||
|
},
|
||||||
|
Spec: autoscaling.HorizontalPodAutoscalerSpec{
|
||||||
|
ScaleTargetRef: autoscaling.CrossVersionObjectReference{
|
||||||
|
Kind: "ReplicationController",
|
||||||
|
Name: "myrc",
|
||||||
|
},
|
||||||
|
MinReplicas: utilpointer.Int32Ptr(1),
|
||||||
|
MaxReplicas: 5,
|
||||||
|
Metrics: []autoscaling.MetricSpec{
|
||||||
|
{
|
||||||
|
Type: autoscaling.ResourceMetricSourceType,
|
||||||
|
Resource: &autoscaling.ResourceMetricSource{
|
||||||
|
Name: api.ResourceCPU,
|
||||||
|
Target: autoscaling.MetricTarget{
|
||||||
|
Type: autoscaling.UtilizationMetricType,
|
||||||
|
AverageUtilization: utilpointer.Int32Ptr(70),
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Behavior: &b,
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func TestValidateHorizontalPodAutoscaler(t *testing.T) {
|
func TestValidateHorizontalPodAutoscaler(t *testing.T) {
|
||||||
metricLabelSelector, err := metav1.ParseToLabelSelector("label=value")
|
metricLabelSelector, err := metav1.ParseToLabelSelector("label=value")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
@ -58,6 +58,7 @@ go_test(
|
|||||||
"//pkg/apis/apps/install:go_default_library",
|
"//pkg/apis/apps/install:go_default_library",
|
||||||
"//pkg/apis/autoscaling:go_default_library",
|
"//pkg/apis/autoscaling:go_default_library",
|
||||||
"//pkg/apis/autoscaling/install:go_default_library",
|
"//pkg/apis/autoscaling/install:go_default_library",
|
||||||
|
"//pkg/apis/autoscaling/v2beta2:go_default_library",
|
||||||
"//pkg/apis/core/install:go_default_library",
|
"//pkg/apis/core/install:go_default_library",
|
||||||
"//pkg/controller:go_default_library",
|
"//pkg/controller:go_default_library",
|
||||||
"//pkg/controller/podautoscaler/metrics:go_default_library",
|
"//pkg/controller/podautoscaler/metrics:go_default_library",
|
||||||
@ -89,6 +90,7 @@ go_test(
|
|||||||
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
"//vendor/github.com/stretchr/testify/assert:go_default_library",
|
||||||
"//vendor/github.com/stretchr/testify/require:go_default_library",
|
"//vendor/github.com/stretchr/testify/require:go_default_library",
|
||||||
"//vendor/k8s.io/heapster/metrics/api/v1/types:go_default_library",
|
"//vendor/k8s.io/heapster/metrics/api/v1/types:go_default_library",
|
||||||
|
"//vendor/k8s.io/utils/pointer:go_default_library",
|
||||||
],
|
],
|
||||||
)
|
)
|
||||||
|
|
||||||
|
File diff suppressed because it is too large
Load Diff
@ -29,7 +29,7 @@ import (
|
|||||||
|
|
||||||
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
autoscalingv1 "k8s.io/api/autoscaling/v1"
|
||||||
autoscalingv2 "k8s.io/api/autoscaling/v2beta1"
|
autoscalingv2 "k8s.io/api/autoscaling/v2beta1"
|
||||||
"k8s.io/api/core/v1"
|
v1 "k8s.io/api/core/v1"
|
||||||
"k8s.io/apimachinery/pkg/api/meta/testrestmapper"
|
"k8s.io/apimachinery/pkg/api/meta/testrestmapper"
|
||||||
"k8s.io/apimachinery/pkg/api/resource"
|
"k8s.io/apimachinery/pkg/api/resource"
|
||||||
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
|
||||||
|
@ -1773,6 +1773,7 @@ func TestDescribeHorizontalPodAutoscaler(t *testing.T) {
|
|||||||
minReplicasVal := int32(2)
|
minReplicasVal := int32(2)
|
||||||
targetUtilizationVal := int32(80)
|
targetUtilizationVal := int32(80)
|
||||||
currentUtilizationVal := int32(50)
|
currentUtilizationVal := int32(50)
|
||||||
|
maxSelectPolicy := autoscalingv2beta2.MaxPolicySelect
|
||||||
metricLabelSelector, err := metav1.ParseToLabelSelector("label=value")
|
metricLabelSelector, err := metav1.ParseToLabelSelector("label=value")
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.Errorf("unable to parse label selector: %v", err)
|
t.Errorf("unable to parse label selector: %v", err)
|
||||||
@ -2417,6 +2418,75 @@ func TestDescribeHorizontalPodAutoscaler(t *testing.T) {
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
"scale up behavior specified",
|
||||||
|
autoscalingv2beta2.HorizontalPodAutoscaler{
|
||||||
|
Spec: autoscalingv2beta2.HorizontalPodAutoscalerSpec{
|
||||||
|
ScaleTargetRef: autoscalingv2beta2.CrossVersionObjectReference{
|
||||||
|
Name: "behavior-target",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
MinReplicas: &minReplicasVal,
|
||||||
|
MaxReplicas: 10,
|
||||||
|
Metrics: []autoscalingv2beta2.MetricSpec{
|
||||||
|
{
|
||||||
|
Type: autoscalingv2beta2.ResourceMetricSourceType,
|
||||||
|
Resource: &autoscalingv2beta2.ResourceMetricSource{
|
||||||
|
Name: corev1.ResourceCPU,
|
||||||
|
Target: autoscalingv2beta2.MetricTarget{
|
||||||
|
Type: autoscalingv2beta2.UtilizationMetricType,
|
||||||
|
AverageUtilization: &targetUtilizationVal,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Behavior: &autoscalingv2beta2.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleUp: &autoscalingv2beta2.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: utilpointer.Int32Ptr(30),
|
||||||
|
SelectPolicy: &maxSelectPolicy,
|
||||||
|
Policies: []autoscalingv2beta2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2beta2.PodsScalingPolicy, Value: 10, PeriodSeconds: 10},
|
||||||
|
{Type: autoscalingv2beta2.PercentScalingPolicy, Value: 10, PeriodSeconds: 10},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
{
|
||||||
|
"scale down behavior specified",
|
||||||
|
autoscalingv2beta2.HorizontalPodAutoscaler{
|
||||||
|
Spec: autoscalingv2beta2.HorizontalPodAutoscalerSpec{
|
||||||
|
ScaleTargetRef: autoscalingv2beta2.CrossVersionObjectReference{
|
||||||
|
Name: "behavior-target",
|
||||||
|
Kind: "Deployment",
|
||||||
|
},
|
||||||
|
MinReplicas: &minReplicasVal,
|
||||||
|
MaxReplicas: 10,
|
||||||
|
Metrics: []autoscalingv2beta2.MetricSpec{
|
||||||
|
{
|
||||||
|
Type: autoscalingv2beta2.ResourceMetricSourceType,
|
||||||
|
Resource: &autoscalingv2beta2.ResourceMetricSource{
|
||||||
|
Name: corev1.ResourceCPU,
|
||||||
|
Target: autoscalingv2beta2.MetricTarget{
|
||||||
|
Type: autoscalingv2beta2.UtilizationMetricType,
|
||||||
|
AverageUtilization: &targetUtilizationVal,
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
Behavior: &autoscalingv2beta2.HorizontalPodAutoscalerBehavior{
|
||||||
|
ScaleDown: &autoscalingv2beta2.HPAScalingRules{
|
||||||
|
StabilizationWindowSeconds: utilpointer.Int32Ptr(30),
|
||||||
|
Policies: []autoscalingv2beta2.HPAScalingPolicy{
|
||||||
|
{Type: autoscalingv2beta2.PodsScalingPolicy, Value: 10, PeriodSeconds: 10},
|
||||||
|
{Type: autoscalingv2beta2.PercentScalingPolicy, Value: 10, PeriodSeconds: 10},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
|
},
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, test := range testsV2beta2 {
|
for _, test := range testsV2beta2 {
|
||||||
|
Loading…
Reference in New Issue
Block a user