Skip to content

Commit

Permalink
Merge pull request #5896 from kwiesmueller/test-util-builder-cleanup
Browse files Browse the repository at this point in the history
Replace `BuildTestContainer` with use of builder
  • Loading branch information
k8s-ci-robot authored Jun 28, 2023
2 parents 5a23f69 + db507cd commit 8f83f7e
Show file tree
Hide file tree
Showing 6 changed files with 59 additions and 72 deletions.
3 changes: 2 additions & 1 deletion vertical-pod-autoscaler/pkg/updater/logic/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"github.com/golang/mock/gomock"
"github.com/stretchr/testify/assert"
apiv1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
metav1 "k8s.io/apimachinery/pkg/apis/meta/v1"
"k8s.io/apimachinery/pkg/labels"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
Expand Down Expand Up @@ -144,7 +145,7 @@ func testRunOnceBase(

for i := range pods {
pods[i] = test.Pod().WithName("test_"+strconv.Itoa(i)).
AddContainer(test.BuildTestContainer(containerName, "1", "100M")).
AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("1")).WithMemRequest(resource.MustParse("100M")).Get()).
WithCreator(&rc.ObjectMeta, &rc.TypeMeta).
Get()

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ import (
"testing"

corev1 "k8s.io/api/core/v1"
"k8s.io/apimachinery/pkg/api/resource"
vpa_types "k8s.io/autoscaler/vertical-pod-autoscaler/pkg/apis/autoscaling.k8s.io/v1"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/annotations"
"k8s.io/autoscaler/vertical-pod-autoscaler/pkg/utils/test"
Expand All @@ -37,7 +38,7 @@ func TestGetUpdatePriority(t *testing.T) {
}{
{
name: "simple scale up",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "2", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("2")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).WithTarget("10", "").Get(),
expectedPrio: PodPriority{
OutsideRecommendedRange: false,
Expand All @@ -46,7 +47,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "simple scale down",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "4", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("4")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).WithTarget("2", "").Get(),
expectedPrio: PodPriority{
OutsideRecommendedRange: false,
Expand All @@ -55,7 +56,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "no resource diff",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "2", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("2")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).WithTarget("2", "").Get(),
expectedPrio: PodPriority{
OutsideRecommendedRange: false,
Expand All @@ -64,7 +65,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "scale up on milliquanitites",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "10m", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("10m")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).WithTarget("900m", "").Get(),
expectedPrio: PodPriority{
OutsideRecommendedRange: false,
Expand All @@ -73,7 +74,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "scale up outside recommended range",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "4", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("4")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).
WithTarget("10", "").
WithLowerBound("6", "").
Expand All @@ -85,7 +86,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "scale down outside recommended range",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "8", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("8")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).
WithTarget("2", "").
WithLowerBound("1", "").
Expand All @@ -97,7 +98,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "scale up with multiple quantities",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "2", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("2")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).WithTarget("10", "").Get(),
expectedPrio: PodPriority{
OutsideRecommendedRange: false,
Expand All @@ -106,7 +107,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "multiple resources, both scale up",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "3", "10M")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("3")).WithMemRequest(resource.MustParse("10M")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).WithTarget("6", "20M").Get(),
expectedPrio: PodPriority{
OutsideRecommendedRange: false,
Expand All @@ -115,7 +116,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "multiple resources, only one scale up",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "4", "10M")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("4")).WithMemRequest(resource.MustParse("10M")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).WithTarget("2", "20M").Get(),
expectedPrio: PodPriority{
OutsideRecommendedRange: false,
Expand All @@ -124,7 +125,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "multiple resources, both scale down",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "4", "20M")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("4")).WithMemRequest(resource.MustParse("20M")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).WithTarget("2", "10M").Get(),
expectedPrio: PodPriority{
OutsideRecommendedRange: false,
Expand All @@ -133,7 +134,7 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "multiple resources, one outside recommended range",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "4", "20M")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("4")).WithMemRequest(resource.MustParse("20M")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).
WithTarget("2", "10M").
WithLowerBound("1", "5M").
Expand All @@ -145,8 +146,8 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "multiple containers, both scale up",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "1", "")).
AddContainer(test.BuildTestContainer("test-container-2", "2", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("1")).Get()).
AddContainer(test.Container().WithName("test-container-2").WithCPURequest(resource.MustParse("2")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).
WithTarget("4", "").AppendRecommendation(
test.Recommendation().
Expand All @@ -159,8 +160,8 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "multiple containers, both scale down",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "3", "")).
AddContainer(test.BuildTestContainer("test-container-2", "7", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("3")).Get()).
AddContainer(test.Container().WithName("test-container-2").WithCPURequest(resource.MustParse("7")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).
WithTarget("1", "").AppendRecommendation(
test.Recommendation().
Expand All @@ -173,8 +174,8 @@ func TestGetUpdatePriority(t *testing.T) {
},
}, {
name: "multiple containers, both scale up, one outside range",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "1", "")).
AddContainer(test.BuildTestContainer("test-container-2", "2", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("1")).Get()).
AddContainer(test.Container().WithName("test-container-2").WithCPURequest(resource.MustParse("2")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).
WithTarget("4", "").
WithLowerBound("1", "").AppendRecommendation(
Expand All @@ -193,8 +194,8 @@ func TestGetUpdatePriority(t *testing.T) {
// container1: request={6 CPU, 10 MB}, recommended={8 CPU, 20 MB}
// container2: request={4 CPU, 30 MB}, recommended={7 CPU, 30 MB}
// total: request={10 CPU, 40 MB}, recommended={15 CPU, 50 MB}
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "6", "10M")).
AddContainer(test.BuildTestContainer("test-container-2", "4", "30M")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("6")).WithMemRequest(resource.MustParse("10M")).Get()).
AddContainer(test.Container().WithName("test-container-2").WithCPURequest(resource.MustParse("4")).WithMemRequest(resource.MustParse("30M")).Get()).Get(),
vpa: test.VerticalPodAutoscaler().WithContainer(containerName).
WithTarget("8", "20M").AppendRecommendation(
test.Recommendation().
Expand All @@ -221,7 +222,7 @@ func TestGetUpdatePriority(t *testing.T) {
// recommendation for a container.
func TestGetUpdatePriority_NoRecommendationForContainer(t *testing.T) {
p := NewProcessor()
pod := test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer("test-container", "5", "10")).Get()
pod := test.Pod().WithName("POD1").AddContainer(test.Container().WithName("test-container").WithCPURequest(resource.MustParse("5")).WithMemRequest(resource.MustParse("10")).Get()).Get()
vpa := test.VerticalPodAutoscaler().WithName("test-vpa").WithContainer("test-container").Get()
result := p.GetUpdatePriority(pod, vpa, nil)
assert.NotNil(t, result)
Expand All @@ -245,28 +246,28 @@ func TestGetUpdatePriority_VpaObservedContainers(t *testing.T) {
}{
{
name: "with no VpaObservedContainers annotation",
pod: test.Pod().WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "1", "")).Get(),
pod: test.Pod().WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("1")).Get()).Get(),
recommendation: test.Recommendation().WithContainer(containerName).WithTarget("10", "").Get(),
want: optedInContainerDiff,
},
{
name: "with container listed in VpaObservedContainers annotation",
pod: test.Pod().WithAnnotations(map[string]string{annotations.VpaObservedContainersLabel: containerName}).
WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "1", "")).Get(),
WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("1")).Get()).Get(),
recommendation: test.Recommendation().WithContainer(containerName).WithTarget("10", "").Get(),
want: optedInContainerDiff,
},
{
name: "with container not listed in VpaObservedContainers annotation",
pod: test.Pod().WithAnnotations(map[string]string{annotations.VpaObservedContainersLabel: ""}).
WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "1", "")).Get(),
WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("1")).Get()).Get(),
recommendation: test.Recommendation().WithContainer(containerName).WithTarget("10", "").Get(),
want: optedOutContainerDiff,
},
{
name: "with incorrect VpaObservedContainers annotation",
pod: test.Pod().WithAnnotations(map[string]string{annotations.VpaObservedContainersLabel: "abcd;';"}).
WithName("POD1").AddContainer(test.BuildTestContainer(containerName, "1", "")).Get(),
WithName("POD1").AddContainer(test.Container().WithName(containerName).WithCPURequest(resource.MustParse("1")).Get()).Get(),
recommendation: test.Recommendation().WithContainer(containerName).WithTarget("10", "").Get(),
want: optedInContainerDiff,
},
Expand Down
Loading

0 comments on commit 8f83f7e

Please sign in to comment.