Skip to content
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
187 changes: 79 additions & 108 deletions vertical-pod-autoscaler/pkg/updater/logic/updater_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,126 +42,92 @@ func parseLabelSelector(selector string) labels.Selector {
return parsedSelector
}

func TestRunOnce(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

replicas := int32(5)
livePods := 5
labels := map[string]string{"app": "testingApp"}
selector := parseLabelSelector("app = testingApp")
containerName := "container1"
rc := apiv1.ReplicationController{
ObjectMeta: metav1.ObjectMeta{
Name: "rc",
Namespace: "default",
func TestRunOnce_Mode(t *testing.T) {
tests := []struct {
name string
updateMode vpa_types.UpdateMode
expectFetchCalls bool
expectedEvictionCount int
}{
{
name: "with Auto mode",
updateMode: vpa_types.UpdateModeAuto,
expectFetchCalls: true,
expectedEvictionCount: 5,
},
Spec: apiv1.ReplicationControllerSpec{
Replicas: &replicas,
{
name: "with Initial mode",
updateMode: vpa_types.UpdateModeInitial,
expectFetchCalls: false,
expectedEvictionCount: 0,
},
{
name: "with Off mode",
updateMode: vpa_types.UpdateModeOff,
expectFetchCalls: false,
expectedEvictionCount: 0,
},
}
pods := make([]*apiv1.Pod, livePods)
eviction := &test.PodsEvictionRestrictionMock{}

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

pods[i].Labels = labels
eviction.On("CanEvict", pods[i]).Return(true)
eviction.On("Evict", pods[i], nil).Return(nil)
}

factory := &fakeEvictFactory{eviction}
vpaLister := &test.VerticalPodAutoscalerListerMock{}

podLister := &test.PodListerMock{}
podLister.On("List").Return(pods, nil)

vpaObj := test.VerticalPodAutoscaler().
WithContainer(containerName).
WithTarget("2", "200M").
WithMinAllowed("1", "100M").
WithMaxAllowed("3", "1G").
Get()
updateMode := vpa_types.UpdateModeAuto
vpaObj.Spec.UpdatePolicy = &vpa_types.PodUpdatePolicy{UpdateMode: &updateMode}
vpaLister.On("List").Return([]*vpa_types.VerticalPodAutoscaler{vpaObj}, nil).Once()

mockSelectorFetcher := target_mock.NewMockVpaTargetSelectorFetcher(ctrl)

updater := &updater{
vpaLister: vpaLister,
podLister: podLister,
evictionFactory: factory,
evictionRateLimiter: rate.NewLimiter(rate.Inf, 0),
recommendationProcessor: &test.FakeRecommendationProcessor{},
selectorFetcher: mockSelectorFetcher,
useAdmissionControllerStatus: true,
statusValidator: newFakeValidator(true),
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
testRunOnceBase(
t,
tc.updateMode,
newFakeValidator(true),
tc.expectFetchCalls,
tc.expectedEvictionCount,
)
})
}

mockSelectorFetcher.EXPECT().Fetch(gomock.Eq(vpaObj)).Return(selector, nil)
updater.RunOnce(context.Background())
eviction.AssertNumberOfCalls(t, "Evict", 5)
}

func TestVPAOff(t *testing.T) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

livePods := 5
labels := map[string]string{"app": "testingApp"}
containerName := "container1"
pods := make([]*apiv1.Pod, livePods)
eviction := &test.PodsEvictionRestrictionMock{}

for i := range pods {
pods[i] = test.Pod().WithName("test_" + strconv.Itoa(i)).AddContainer(test.BuildTestContainer(containerName, "1", "100M")).Get()
pods[i].Labels = labels
eviction.On("CanEvict", pods[i]).Return(true)
eviction.On("Evict", pods[i], nil).Return(nil)
func TestRunOnce_Status(t *testing.T) {
tests := []struct {
name string
statusValidator status.Validator
expectFetchCalls bool
expectedEvictionCount int
}{
{
name: "with valid status",
statusValidator: newFakeValidator(true),
expectFetchCalls: true,
expectedEvictionCount: 5,
},
{
name: "with invalid status",
statusValidator: newFakeValidator(false),
expectFetchCalls: false,
expectedEvictionCount: 0,
},
}

factory := &fakeEvictFactory{eviction}
vpaLister := &test.VerticalPodAutoscalerListerMock{}

podLister := &test.PodListerMock{}
podLister.On("List").Return(pods, nil)

vpaObj := test.VerticalPodAutoscaler().
WithContainer(containerName).
WithTarget("2", "200M").
WithMinAllowed("1", "100M").
WithMaxAllowed("3", "1G").
Get()
vpaObj.Namespace = "default"
updateMode := vpa_types.UpdateModeInitial
vpaObj.Spec.UpdatePolicy = &vpa_types.PodUpdatePolicy{UpdateMode: &updateMode}
vpaLister.On("List").Return([]*vpa_types.VerticalPodAutoscaler{vpaObj}, nil).Once()

updater := &updater{
vpaLister: vpaLister,
podLister: podLister,
evictionFactory: factory,
evictionRateLimiter: rate.NewLimiter(rate.Inf, 0),
recommendationProcessor: &test.FakeRecommendationProcessor{},
selectorFetcher: target_mock.NewMockVpaTargetSelectorFetcher(ctrl),
useAdmissionControllerStatus: true,
statusValidator: newFakeValidator(true),
for _, tc := range tests {
t.Run(tc.name, func(t *testing.T) {
testRunOnceBase(
t,
vpa_types.UpdateModeAuto,
tc.statusValidator,
tc.expectFetchCalls,
tc.expectedEvictionCount,
)
})
}

updater.RunOnce(context.Background())
eviction.AssertNumberOfCalls(t, "Evict", 0)
}

// TODO(krzysied): Merge tests so there is less copy-pasted code.
func TestRunOnce_InvalidStatus(t *testing.T) {
func testRunOnceBase(
t *testing.T,
updateMode vpa_types.UpdateMode,
statusValidator status.Validator,
expectFetchCalls bool,
expectedEvictionCount int,
) {
ctrl := gomock.NewController(t)
defer ctrl.Finish()

replicas := int32(5)
livePods := 5
labels := map[string]string{"app": "testingApp"}
selector := parseLabelSelector("app = testingApp")
containerName := "container1"
rc := apiv1.ReplicationController{
ObjectMeta: metav1.ObjectMeta{
Expand All @@ -176,7 +142,10 @@ func TestRunOnce_InvalidStatus(t *testing.T) {
eviction := &test.PodsEvictionRestrictionMock{}

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

pods[i].Labels = labels
eviction.On("CanEvict", pods[i]).Return(true)
Expand All @@ -195,7 +164,6 @@ func TestRunOnce_InvalidStatus(t *testing.T) {
WithMinAllowed("1", "100M").
WithMaxAllowed("3", "1G").
Get()
updateMode := vpa_types.UpdateModeAuto
vpaObj.Spec.UpdatePolicy = &vpa_types.PodUpdatePolicy{UpdateMode: &updateMode}
vpaLister.On("List").Return([]*vpa_types.VerticalPodAutoscaler{vpaObj}, nil).Once()

Expand All @@ -209,11 +177,14 @@ func TestRunOnce_InvalidStatus(t *testing.T) {
recommendationProcessor: &test.FakeRecommendationProcessor{},
selectorFetcher: mockSelectorFetcher,
useAdmissionControllerStatus: true,
statusValidator: newFakeValidator(false),
statusValidator: statusValidator,
}

if expectFetchCalls {
mockSelectorFetcher.EXPECT().Fetch(gomock.Eq(vpaObj)).Return(selector, nil)
}
updater.RunOnce(context.Background())
eviction.AssertNumberOfCalls(t, "Evict", 0)
eviction.AssertNumberOfCalls(t, "Evict", expectedEvictionCount)
}

func TestRunOnceNotingToProcess(t *testing.T) {
Expand Down