Skip to content
This repository has been archived by the owner on May 5, 2021. It is now read-only.

Commit

Permalink
Add test for VPA validation in Admission Controller
Browse files Browse the repository at this point in the history
  • Loading branch information
jbartosik committed Apr 11, 2019
1 parent a7ad350 commit 278c304
Showing 1 changed file with 129 additions and 0 deletions.
129 changes: 129 additions & 0 deletions vertical-pod-autoscaler/pkg/admission-controller/logic/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ import (

apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1beta2"
vpa_api_util "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/vpa"
)

Expand Down Expand Up @@ -296,3 +297,131 @@ func TestGetPatchesForResourceRequest_TwoReplacementResources(t *testing.T) {
assert.True(t, eqPatch(patches[2], addAnnotationRequest([][]string{{cpu, unobtanium}})) || eqPatch(patches[2], addAnnotationRequest([][]string{{unobtanium, cpu}})))
}
}

func TestValidateVPA(t *testing.T) {
badUpdateMode := vpa_types.UpdateMode("bad")
validUpdateMode := vpa_types.UpdateModeOff
badScalingMode := vpa_types.ContainerScalingMode("bad")
validScalingMode := vpa_types.ContainerScalingModeAuto
tests := []struct {
name string
vpa vpa_types.VerticalPodAutoscaler
isCreate bool
expectError error
}{
{
name: "empty update",
vpa: vpa_types.VerticalPodAutoscaler{},
},
{
name: "empty create",
vpa: vpa_types.VerticalPodAutoscaler{},
isCreate: true,
expectError: fmt.Errorf("TargetRef is required. If you're using v1beta1 version of the API, please migrate to v1beta2."),
},
{
name: "no update mode",
vpa: vpa_types.VerticalPodAutoscaler{
Spec: vpa_types.VerticalPodAutoscalerSpec{
UpdatePolicy: &vpa_types.PodUpdatePolicy{},
},
},
expectError: fmt.Errorf("UpdateMode is required if UpdatePolicy is used"),
},
{
name: "bad update mode",
vpa: vpa_types.VerticalPodAutoscaler{
Spec: vpa_types.VerticalPodAutoscalerSpec{
UpdatePolicy: &vpa_types.PodUpdatePolicy{
UpdateMode: &badUpdateMode,
},
},
},
expectError: fmt.Errorf("unexpected UpdateMode value bad"),
},
{
name: "no policy name",
vpa: vpa_types.VerticalPodAutoscaler{
Spec: vpa_types.VerticalPodAutoscalerSpec{
ResourcePolicy: &vpa_types.PodResourcePolicy{
ContainerPolicies: []vpa_types.ContainerResourcePolicy{{}},
},
},
},
expectError: fmt.Errorf("ContainerPolicies.ContainerName is required"),
},
{
name: "invalid scaling mode",
vpa: vpa_types.VerticalPodAutoscaler{
Spec: vpa_types.VerticalPodAutoscalerSpec{
ResourcePolicy: &vpa_types.PodResourcePolicy{
ContainerPolicies: []vpa_types.ContainerResourcePolicy{
{
ContainerName: "loot box",
Mode: &badScalingMode,
},
},
},
},
},
expectError: fmt.Errorf("unexpected Mode value bad"),
},
{
name: "bad limits",
vpa: vpa_types.VerticalPodAutoscaler{
Spec: vpa_types.VerticalPodAutoscalerSpec{
ResourcePolicy: &vpa_types.PodResourcePolicy{
ContainerPolicies: []vpa_types.ContainerResourcePolicy{
{
ContainerName: "loot box",
MinAllowed: apiv1.ResourceList{
cpu: resource.MustParse("100"),
},
MaxAllowed: apiv1.ResourceList{
cpu: resource.MustParse("10"),
},
},
},
},
},
},
expectError: fmt.Errorf("max resource for cpu is lower than min"),
},
{
name: "all valid",
vpa: vpa_types.VerticalPodAutoscaler{
Spec: vpa_types.VerticalPodAutoscalerSpec{
ResourcePolicy: &vpa_types.PodResourcePolicy{
ContainerPolicies: []vpa_types.ContainerResourcePolicy{
{
ContainerName: "loot box",
Mode: &validScalingMode,
MinAllowed: apiv1.ResourceList{
cpu: resource.MustParse("10"),
},
MaxAllowed: apiv1.ResourceList{
cpu: resource.MustParse("100"),
},
},
},
},
UpdatePolicy: &vpa_types.PodUpdatePolicy{
UpdateMode: &validUpdateMode,
},
},
},
},
}
for _, tc := range tests {
t.Run(fmt.Sprintf("test case: %s", tc.name), func(t *testing.T) {
err := validateVPA(&tc.vpa, tc.isCreate)
if tc.expectError == nil {
assert.NoError(t, err)
} else {
if assert.Error(t, err) {
assert.Equal(t, tc.expectError.Error(), err.Error())
}
}
})
}
}

0 comments on commit 278c304

Please sign in to comment.