Skip to content

Commit

Permalink
Add unit test
Browse files Browse the repository at this point in the history
  • Loading branch information
jbartosik committed May 29, 2019
1 parent 2437763 commit 77d7762
Showing 1 changed file with 69 additions and 3 deletions.
72 changes: 69 additions & 3 deletions vertical-pod-autoscaler/pkg/utils/vpa/capping_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ func TestRecommendationNotAvailable(t *testing.T) {
}
policy := vpa_types.PodResourcePolicy{}

res, annotations, err := NewCappingRecommendationProcessor().Apply(&podRecommendation, &policy, nil, pod)
res, annotations, err := NewCappingRecommendationProcessor(&fakeLimitRangeCalculator{}).Apply(&podRecommendation, &policy, nil, pod)
assert.Nil(t, err)
assert.Empty(t, annotations)
assert.Empty(t, res.ContainerRecommendations)
Expand Down Expand Up @@ -84,7 +84,7 @@ func TestRecommendationCappedToMinMaxPolicy(t *testing.T) {
},
}

res, annotations, err := NewCappingRecommendationProcessor().Apply(&podRecommendation, &policy, nil, pod)
res, annotations, err := NewCappingRecommendationProcessor(&fakeLimitRangeCalculator{}).Apply(&podRecommendation, &policy, nil, pod)
assert.Nil(t, err)
assert.Equal(t, apiv1.ResourceList{
apiv1.ResourceCPU: *resource.NewScaledQuantity(40, 1),
Expand Down Expand Up @@ -146,7 +146,7 @@ func TestApply(t *testing.T) {
pod := test.Pod().WithName("pod1").AddContainer(test.BuildTestContainer("ctr-name", "", "")).Get()

for _, testCase := range applyTestCases {
res, _, err := NewCappingRecommendationProcessor().Apply(
res, _, err := NewCappingRecommendationProcessor(&fakeLimitRangeCalculator{}).Apply(
testCase.PodRecommendation, testCase.Policy, nil, pod)
assert.Equal(t, testCase.ExpectedPodRecommendation, res)
assert.Equal(t, testCase.ExpectedError, err)
Expand Down Expand Up @@ -215,3 +215,69 @@ func TestApplyVpa(t *testing.T) {
apiv1.ResourceMemory: *resource.NewScaledQuantity(4500, 1),
}, res.ContainerRecommendations[0].UpperBound)
}

type fakeLimitRangeCalculator struct {
limitRange apiv1.LimitRangeItem
}

func (nlrc *fakeLimitRangeCalculator) GetContainerLimitRangeItem(namespace string) (*apiv1.LimitRangeItem, error) {
return &nlrc.limitRange, nil
}

func TestApplyCapsToLimitRange(t *testing.T) {
limitRange := apiv1.LimitRangeItem{
Type: apiv1.LimitTypeContainer,
Max: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("1"),
apiv1.ResourceMemory: resource.MustParse("1G"),
},
}
recommendation := vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{
{
ContainerName: "container",
Target: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("2"),
apiv1.ResourceMemory: resource.MustParse("10G"),
},
},
},
}
pod := apiv1.Pod{
Spec: apiv1.PodSpec{
Containers: []apiv1.Container{
{
Name: "container",
Resources: apiv1.ResourceRequirements{
Requests: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("1"),
apiv1.ResourceMemory: resource.MustParse("1G"),
},
Limits: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("1"),
apiv1.ResourceMemory: resource.MustParse("1G"),
},
},
},
},
},
}
expectedRecommendation := vpa_types.RecommendedPodResources{
ContainerRecommendations: []vpa_types.RecommendedContainerResources{
{
ContainerName: "container",
Target: apiv1.ResourceList{
apiv1.ResourceCPU: resource.MustParse("1000m"),
apiv1.ResourceMemory: resource.MustParse("1000000000000m"),
},
},
},
}

calculator := fakeLimitRangeCalculator{limitRange}
processor := NewCappingRecommendationProcessor(&calculator)
processedRecommendation, annotations, err := processor.Apply(&recommendation, nil, nil, &pod)
assert.NoError(t, err)
assert.Equal(t, map[string][]string{}, annotations)
assert.Equal(t, expectedRecommendation, *processedRecommendation)
}

0 comments on commit 77d7762

Please sign in to comment.