From f4acd90223bc102c847f7881000c589869aa9be6 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Mon, 18 Mar 2024 08:21:08 +0000 Subject: [PATCH 1/3] Add prototype tests --- internal/test/testify/testify.go | 4 + pkg/index/operator/service/operator_test.go | 311 ++++++++++++++++++++ 2 files changed, 315 insertions(+) create mode 100644 pkg/index/operator/service/operator_test.go diff --git a/internal/test/testify/testify.go b/internal/test/testify/testify.go index 49a7a6193b..3170a18889 100644 --- a/internal/test/testify/testify.go +++ b/internal/test/testify/testify.go @@ -24,3 +24,7 @@ type ( const ( Anything = mock.Anything ) + +var ( + AnythingOfType = mock.AnythingOfType +) diff --git a/pkg/index/operator/service/operator_test.go b/pkg/index/operator/service/operator_test.go new file mode 100644 index 0000000000..a275b48a63 --- /dev/null +++ b/pkg/index/operator/service/operator_test.go @@ -0,0 +1,311 @@ +// +// Copyright (C) 2019-2024 vdaas.org vald team +// +// 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 +// +// https://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 service + +import ( + "context" + "testing" + "time" + + tmock "github.com/stretchr/testify/mock" + "github.com/stretchr/testify/require" + "github.com/vdaas/vald/internal/k8s/client" + "github.com/vdaas/vald/internal/k8s/vald" + "github.com/vdaas/vald/internal/test/mock/k8s" + "github.com/vdaas/vald/internal/test/testify" + + batchv1 "k8s.io/api/batch/v1" +) + +func Test_operator_podOnReconcile(t *testing.T) { + t.Parallel() + + type test struct { + name string + testfunc func(t *testing.T) + } + + tests := []test{ + { + "returns client.Result{} when read replica is not enabled", + func(t *testing.T) { + mock := &k8s.ValdK8sClientMock{} + o, err := New( + "namespace", + "agentName", + "rotatorName", + "targetReadReplicaIDEnvName", + nil, + WithK8sClient(mock), + WithReadReplicaEnabled(false), + ) + require.NoError(t, err) + + op, ok := o.(*operator) + require.True(t, ok) + + res, err := op.podOnReconcile(context.Background(), nil) + require.NoError(t, err) + + require.Equal(t, client.Result{}, res) + }, + }, + { + "returns client.Result{} when pod is not a statefulset", + func(t *testing.T) { + mock := &k8s.ValdK8sClientMock{} + o, err := New( + "namespace", + "agentName", + "rotatorName", + "targetReadReplicaIDEnvName", + nil, + WithK8sClient(mock), + WithReadReplicaEnabled(true), + ) + require.NoError(t, err) + + op, ok := o.(*operator) + require.True(t, ok) + + pod := &client.Pod{} + res, err := op.podOnReconcile(context.Background(), pod) + require.NoError(t, err) + + require.Equal(t, client.Result{}, res) + }, + }, + { + "returns requeue: false when last snapshot time is after the last save time", + func(t *testing.T) { + saveTime := time.Now() + rotateTime := saveTime.Add(1 * time.Second) + + mock := &k8s.ValdK8sClientMock{} + mock.On("LabelSelector", testify.Anything, testify.Anything, testify.Anything).Return(client.NewSelector(), nil) + mock.On("List", testify.Anything, testify.AnythingOfType("*v1.DeploymentList"), testify.Anything).Run(func(args tmock.Arguments) { + arg, ok := args.Get(1).(*client.DeploymentList) + require.True(t, ok) + + arg.Items = []client.Deployment{ + { + ObjectMeta: client.ObjectMeta{ + Name: "deploymentName", + Annotations: map[string]string{ + vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), + }, + }, + }, + } + }).Return(nil) + + agentPod := &client.Pod{ + ObjectMeta: client.ObjectMeta{ + Labels: map[string]string{ + client.PodIndexLabel: "0", + }, + Annotations: map[string]string{ + vald.LastTimeSaveIndexTimestampAnnotationsKey: saveTime.Format(vald.TimeFormat), + }, + }, + } + + o, err := New( + "namespace", + "agentName", + "rotatorName", + "targetReadReplicaIDEnvName", + nil, + WithK8sClient(mock), + WithReadReplicaEnabled(true), + ) + require.NoError(t, err) + + op, ok := o.(*operator) + require.True(t, ok) + + res, err := op.podOnReconcile(context.Background(), agentPod) + require.NoError(t, err) + + require.Equal(t, client.Result{ + Requeue: false, + }, res) + }, + }, + { + "returns requeue: false and calls client.Create once when last snapshot time is before the last save time", + func(t *testing.T) { + saveTime := time.Now() + rotateTime := saveTime.Add(-1 * time.Second) + + mock := &k8s.ValdK8sClientMock{} + mock.On("LabelSelector", testify.Anything, testify.Anything, testify.Anything).Return(client.NewSelector(), nil) + mock.On("List", testify.Anything, testify.AnythingOfType("*v1.DeploymentList"), testify.Anything).Run(func(args tmock.Arguments) { + arg, ok := args.Get(1).(*client.DeploymentList) + require.True(t, ok) + + arg.Items = []client.Deployment{ + { + ObjectMeta: client.ObjectMeta{ + Name: "deploymentName", + Annotations: map[string]string{ + vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), + }, + }, + }, + } + }).Return(nil) + + mock.On("List", testify.Anything, testify.AnythingOfType("*v1.JobList"), testify.Anything).Run(func(args tmock.Arguments) { + arg, ok := args.Get(1).(*client.JobList) + require.True(t, ok) + + arg.Items = []client.Job{} + }).Return(nil) + + mock.On("Create", testify.Anything, testify.Anything, testify.Anything).Return(nil).Once() + + agentPod := &client.Pod{ + ObjectMeta: client.ObjectMeta{ + Labels: map[string]string{ + client.PodIndexLabel: "0", + }, + Annotations: map[string]string{ + vald.LastTimeSaveIndexTimestampAnnotationsKey: saveTime.Format(vald.TimeFormat), + }, + }, + } + + o, err := New( + "namespace", + "agentName", + "rotatorName", + "targetReadReplicaIDEnvName", + nil, + WithK8sClient(mock), + WithReadReplicaEnabled(true), + ) + require.NoError(t, err) + + op, ok := o.(*operator) + require.True(t, ok) + + op.rotatorJob = &client.Job{ + ObjectMeta: client.ObjectMeta{ + Name: "foo job", + }, + } + + res, err := op.podOnReconcile(context.Background(), agentPod) + require.NoError(t, err) + + require.Equal(t, client.Result{ + Requeue: false, + }, res) + }, + }, + { + "returns requeue: true when there is already one running job when rotation job concurrency is 1", + func(t *testing.T) { + saveTime := time.Now() + rotateTime := saveTime.Add(-1 * time.Second) + + mock := &k8s.ValdK8sClientMock{} + mock.On("LabelSelector", testify.Anything, testify.Anything, testify.Anything).Return(client.NewSelector(), nil) + mock.On("List", testify.Anything, testify.AnythingOfType("*v1.DeploymentList"), testify.Anything).Run(func(args tmock.Arguments) { + arg, ok := args.Get(1).(*client.DeploymentList) + require.True(t, ok) + + arg.Items = []client.Deployment{ + { + ObjectMeta: client.ObjectMeta{ + Name: "deploymentName", + Annotations: map[string]string{ + vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), + }, + }, + }, + } + }).Return(nil) + + mock.On("List", testify.Anything, testify.AnythingOfType("*v1.JobList"), testify.Anything).Run(func(args tmock.Arguments) { + arg, ok := args.Get(1).(*client.JobList) + require.True(t, ok) + + arg.Items = []client.Job{ + { + ObjectMeta: client.ObjectMeta{ + Name: "already running job1", + }, + Status: batchv1.JobStatus{ + Active: 1, + }, + }, + } + }).Return(nil) + + mock.On("Create", testify.Anything, testify.Anything, testify.Anything).Return(nil).Once() + + agentPod := &client.Pod{ + ObjectMeta: client.ObjectMeta{ + Labels: map[string]string{ + client.PodIndexLabel: "0", + }, + Annotations: map[string]string{ + vald.LastTimeSaveIndexTimestampAnnotationsKey: saveTime.Format(vald.TimeFormat), + }, + }, + } + + o, err := New( + "namespace", + "agentName", + "rotatorName", + "targetReadReplicaIDEnvName", + nil, + WithK8sClient(mock), + WithReadReplicaEnabled(true), + ) + require.NoError(t, err) + + op, ok := o.(*operator) + require.True(t, ok) + + op.rotatorJob = &client.Job{ + ObjectMeta: client.ObjectMeta{ + Name: "foo job", + }, + } + + res, err := op.podOnReconcile(context.Background(), agentPod) + require.NoError(t, err) + + require.Equal(t, client.Result{ + Requeue: true, + }, res) + }, + }, + } + + for _, tc := range tests { + test := tc + t.Run(test.name, func(tt *testing.T) { + tt.Parallel() + test.testfunc(tt) + }) + } +} From cab44aff0832ebe8aecad09b7e68070ac99f4aa7 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Mon, 18 Mar 2024 09:15:55 +0000 Subject: [PATCH 2/3] Refactor --- internal/k8s/client/client.go | 1 + internal/test/testify/testify.go | 4 +- pkg/index/operator/service/operator_test.go | 479 ++++++++++---------- 3 files changed, 254 insertions(+), 230 deletions(-) diff --git a/internal/k8s/client/client.go b/internal/k8s/client/client.go index b788ef327b..10a91a5224 100644 --- a/internal/k8s/client/client.go +++ b/internal/k8s/client/client.go @@ -67,6 +67,7 @@ type ( EnvVar = corev1.EnvVar Job = batchv1.Job JobList = batchv1.JobList + JobStatus = batchv1.JobStatus CronJob = batchv1.CronJob Result = reconcile.Result ) diff --git a/internal/test/testify/testify.go b/internal/test/testify/testify.go index 3170a18889..4b85ddaa71 100644 --- a/internal/test/testify/testify.go +++ b/internal/test/testify/testify.go @@ -25,6 +25,4 @@ const ( Anything = mock.Anything ) -var ( - AnythingOfType = mock.AnythingOfType -) +var AnythingOfType = mock.AnythingOfType diff --git a/pkg/index/operator/service/operator_test.go b/pkg/index/operator/service/operator_test.go index a275b48a63..f212dbdcce 100644 --- a/pkg/index/operator/service/operator_test.go +++ b/pkg/index/operator/service/operator_test.go @@ -21,99 +21,91 @@ import ( "testing" "time" - tmock "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "github.com/vdaas/vald/internal/k8s/client" "github.com/vdaas/vald/internal/k8s/vald" "github.com/vdaas/vald/internal/test/mock/k8s" "github.com/vdaas/vald/internal/test/testify" - - batchv1 "k8s.io/api/batch/v1" ) func Test_operator_podOnReconcile(t *testing.T) { t.Parallel() + type want struct { + res client.Result + createCalled bool + err error + } type test struct { - name string - testfunc func(t *testing.T) + name string + agentPod *client.Pod + readReplicaEnabled bool + readReplicaDeployment *client.Deployment + runningJobs []client.Job + rotationJobConcurrency uint + want want } tests := []test{ { - "returns client.Result{} when read replica is not enabled", - func(t *testing.T) { - mock := &k8s.ValdK8sClientMock{} - o, err := New( - "namespace", - "agentName", - "rotatorName", - "targetReadReplicaIDEnvName", - nil, - WithK8sClient(mock), - WithReadReplicaEnabled(false), - ) - require.NoError(t, err) - - op, ok := o.(*operator) - require.True(t, ok) - - res, err := op.podOnReconcile(context.Background(), nil) - require.NoError(t, err) - - require.Equal(t, client.Result{}, res) + name: "returns client.Result{} when read replica is not enabled", + readReplicaEnabled: false, + want: want{ + res: client.Result{}, + createCalled: false, + err: nil, }, }, { - "returns client.Result{} when pod is not a statefulset", - func(t *testing.T) { - mock := &k8s.ValdK8sClientMock{} - o, err := New( - "namespace", - "agentName", - "rotatorName", - "targetReadReplicaIDEnvName", - nil, - WithK8sClient(mock), - WithReadReplicaEnabled(true), - ) - require.NoError(t, err) - - op, ok := o.(*operator) - require.True(t, ok) - - pod := &client.Pod{} - res, err := op.podOnReconcile(context.Background(), pod) - require.NoError(t, err) - - require.Equal(t, client.Result{}, res) + name: "returns client.Result{} when pod is not a statefulset", + readReplicaEnabled: true, + agentPod: &client.Pod{}, + want: want{ + res: client.Result{}, + createCalled: false, + err: nil, }, }, - { - "returns requeue: false when last snapshot time is after the last save time", - func(t *testing.T) { - saveTime := time.Now() - rotateTime := saveTime.Add(1 * time.Second) - - mock := &k8s.ValdK8sClientMock{} - mock.On("LabelSelector", testify.Anything, testify.Anything, testify.Anything).Return(client.NewSelector(), nil) - mock.On("List", testify.Anything, testify.AnythingOfType("*v1.DeploymentList"), testify.Anything).Run(func(args tmock.Arguments) { - arg, ok := args.Get(1).(*client.DeploymentList) - require.True(t, ok) - - arg.Items = []client.Deployment{ - { - ObjectMeta: client.ObjectMeta{ - Name: "deploymentName", - Annotations: map[string]string{ - vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), - }, - }, + func() test { + saveTime := time.Now() + rotateTime := saveTime.Add(1 * time.Second) + return test{ + name: "returns requeue: false when last snapshot time is after the last save time", + readReplicaEnabled: true, + agentPod: &client.Pod{ + ObjectMeta: client.ObjectMeta{ + Labels: map[string]string{ + client.PodIndexLabel: "0", }, - } - }).Return(nil) - - agentPod := &client.Pod{ + Annotations: map[string]string{ + vald.LastTimeSaveIndexTimestampAnnotationsKey: saveTime.Format(vald.TimeFormat), + }, + }, + }, + readReplicaDeployment: &client.Deployment{ + ObjectMeta: client.ObjectMeta{ + Name: "deploymentName", + Annotations: map[string]string{ + vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), + }, + }, + }, + want: want{ + res: client.Result{ + Requeue: false, + }, + createCalled: false, + err: nil, + }, + } + }(), + func() test { + saveTime := time.Now() + rotateTime := saveTime.Add(-1 * time.Second) + return test{ + name: "returns requeue: false and calls client.Create once when last snapshot time is before the last save time", + readReplicaEnabled: true, + agentPod: &client.Pod{ ObjectMeta: client.ObjectMeta{ Labels: map[string]string{ client.PodIndexLabel: "0", @@ -122,64 +114,31 @@ func Test_operator_podOnReconcile(t *testing.T) { vald.LastTimeSaveIndexTimestampAnnotationsKey: saveTime.Format(vald.TimeFormat), }, }, - } - - o, err := New( - "namespace", - "agentName", - "rotatorName", - "targetReadReplicaIDEnvName", - nil, - WithK8sClient(mock), - WithReadReplicaEnabled(true), - ) - require.NoError(t, err) - - op, ok := o.(*operator) - require.True(t, ok) - - res, err := op.podOnReconcile(context.Background(), agentPod) - require.NoError(t, err) - - require.Equal(t, client.Result{ - Requeue: false, - }, res) - }, - }, - { - "returns requeue: false and calls client.Create once when last snapshot time is before the last save time", - func(t *testing.T) { - saveTime := time.Now() - rotateTime := saveTime.Add(-1 * time.Second) - - mock := &k8s.ValdK8sClientMock{} - mock.On("LabelSelector", testify.Anything, testify.Anything, testify.Anything).Return(client.NewSelector(), nil) - mock.On("List", testify.Anything, testify.AnythingOfType("*v1.DeploymentList"), testify.Anything).Run(func(args tmock.Arguments) { - arg, ok := args.Get(1).(*client.DeploymentList) - require.True(t, ok) - - arg.Items = []client.Deployment{ - { - ObjectMeta: client.ObjectMeta{ - Name: "deploymentName", - Annotations: map[string]string{ - vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), - }, - }, + }, + readReplicaDeployment: &client.Deployment{ + ObjectMeta: client.ObjectMeta{ + Name: "deploymentName", + Annotations: map[string]string{ + vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), }, - } - }).Return(nil) - - mock.On("List", testify.Anything, testify.AnythingOfType("*v1.JobList"), testify.Anything).Run(func(args tmock.Arguments) { - arg, ok := args.Get(1).(*client.JobList) - require.True(t, ok) - - arg.Items = []client.Job{} - }).Return(nil) - - mock.On("Create", testify.Anything, testify.Anything, testify.Anything).Return(nil).Once() - - agentPod := &client.Pod{ + }, + }, + want: want{ + res: client.Result{ + Requeue: false, + }, + createCalled: true, + err: nil, + }, + } + }(), + func() test { + saveTime := time.Now() + rotateTime := saveTime.Add(-1 * time.Second) + return test{ + name: "returns requeue: true when there is already one running job when rotation job concurrency is 1", + readReplicaEnabled: true, + agentPod: &client.Pod{ ObjectMeta: client.ObjectMeta{ Labels: map[string]string{ client.PodIndexLabel: "0", @@ -188,79 +147,42 @@ func Test_operator_podOnReconcile(t *testing.T) { vald.LastTimeSaveIndexTimestampAnnotationsKey: saveTime.Format(vald.TimeFormat), }, }, - } - - o, err := New( - "namespace", - "agentName", - "rotatorName", - "targetReadReplicaIDEnvName", - nil, - WithK8sClient(mock), - WithReadReplicaEnabled(true), - ) - require.NoError(t, err) - - op, ok := o.(*operator) - require.True(t, ok) - - op.rotatorJob = &client.Job{ + }, + readReplicaDeployment: &client.Deployment{ ObjectMeta: client.ObjectMeta{ - Name: "foo job", + Name: "deploymentName", + Annotations: map[string]string{ + vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), + }, }, - } - - res, err := op.podOnReconcile(context.Background(), agentPod) - require.NoError(t, err) - - require.Equal(t, client.Result{ - Requeue: false, - }, res) - }, - }, - { - "returns requeue: true when there is already one running job when rotation job concurrency is 1", - func(t *testing.T) { - saveTime := time.Now() - rotateTime := saveTime.Add(-1 * time.Second) - - mock := &k8s.ValdK8sClientMock{} - mock.On("LabelSelector", testify.Anything, testify.Anything, testify.Anything).Return(client.NewSelector(), nil) - mock.On("List", testify.Anything, testify.AnythingOfType("*v1.DeploymentList"), testify.Anything).Run(func(args tmock.Arguments) { - arg, ok := args.Get(1).(*client.DeploymentList) - require.True(t, ok) - - arg.Items = []client.Deployment{ - { - ObjectMeta: client.ObjectMeta{ - Name: "deploymentName", - Annotations: map[string]string{ - vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), - }, - }, + }, + runningJobs: []client.Job{ + { + ObjectMeta: client.ObjectMeta{ + Name: "already running job1", }, - } - }).Return(nil) - - mock.On("List", testify.Anything, testify.AnythingOfType("*v1.JobList"), testify.Anything).Run(func(args tmock.Arguments) { - arg, ok := args.Get(1).(*client.JobList) - require.True(t, ok) - - arg.Items = []client.Job{ - { - ObjectMeta: client.ObjectMeta{ - Name: "already running job1", - }, - Status: batchv1.JobStatus{ - Active: 1, - }, + Status: client.JobStatus{ + Active: 1, }, - } - }).Return(nil) - - mock.On("Create", testify.Anything, testify.Anything, testify.Anything).Return(nil).Once() - - agentPod := &client.Pod{ + }, + }, + rotationJobConcurrency: 1, + want: want{ + res: client.Result{ + Requeue: true, + }, + createCalled: false, + err: nil, + }, + } + }(), + func() test { + saveTime := time.Now() + rotateTime := saveTime.Add(-1 * time.Second) + return test{ + name: "returns requeue: false and create job when there is one running job when rotation job concurrency is 2", + readReplicaEnabled: true, + agentPod: &client.Pod{ ObjectMeta: client.ObjectMeta{ Labels: map[string]string{ client.PodIndexLabel: "0", @@ -269,43 +191,146 @@ func Test_operator_podOnReconcile(t *testing.T) { vald.LastTimeSaveIndexTimestampAnnotationsKey: saveTime.Format(vald.TimeFormat), }, }, - } - - o, err := New( - "namespace", - "agentName", - "rotatorName", - "targetReadReplicaIDEnvName", - nil, - WithK8sClient(mock), - WithReadReplicaEnabled(true), - ) - require.NoError(t, err) - - op, ok := o.(*operator) - require.True(t, ok) - - op.rotatorJob = &client.Job{ + }, + readReplicaDeployment: &client.Deployment{ ObjectMeta: client.ObjectMeta{ - Name: "foo job", + Name: "deploymentName", + Annotations: map[string]string{ + vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), + }, }, - } - - res, err := op.podOnReconcile(context.Background(), agentPod) - require.NoError(t, err) - - require.Equal(t, client.Result{ - Requeue: true, - }, res) - }, - }, + }, + runningJobs: []client.Job{ + { + ObjectMeta: client.ObjectMeta{ + Name: "already running job1", + }, + Status: client.JobStatus{ + Active: 1, + }, + }, + }, + rotationJobConcurrency: 2, + want: want{ + res: client.Result{ + Requeue: false, + }, + createCalled: true, + err: nil, + }, + } + }(), + func() test { + saveTime := time.Now() + rotateTime := saveTime.Add(-1 * time.Second) + return test{ + name: "returns requeue: true when there are two running jobs when rotation job concurrency is 2", + readReplicaEnabled: true, + agentPod: &client.Pod{ + ObjectMeta: client.ObjectMeta{ + Labels: map[string]string{ + client.PodIndexLabel: "0", + }, + Annotations: map[string]string{ + vald.LastTimeSaveIndexTimestampAnnotationsKey: saveTime.Format(vald.TimeFormat), + }, + }, + }, + readReplicaDeployment: &client.Deployment{ + ObjectMeta: client.ObjectMeta{ + Name: "deploymentName", + Annotations: map[string]string{ + vald.LastTimeSnapshotTimestampAnnotationsKey: rotateTime.Format(vald.TimeFormat), + }, + }, + }, + runningJobs: []client.Job{ + { + ObjectMeta: client.ObjectMeta{ + Name: "already running job1", + }, + Status: client.JobStatus{ + Active: 1, + }, + }, + { + ObjectMeta: client.ObjectMeta{ + Name: "already running job2", + }, + Status: client.JobStatus{ + Active: 1, + }, + }, + }, + rotationJobConcurrency: 2, + want: want{ + res: client.Result{ + Requeue: true, + }, + createCalled: false, + err: nil, + }, + } + }(), } for _, tc := range tests { test := tc t.Run(test.name, func(tt *testing.T) { tt.Parallel() - test.testfunc(tt) + + mock := &k8s.ValdK8sClientMock{} + mock.On("LabelSelector", testify.Anything, testify.Anything, testify.Anything).Return(client.NewSelector(), nil).Maybe() + mock.On("List", testify.Anything, testify.AnythingOfType("*v1.DeploymentList"), testify.Anything).Run(func(args testify.Arguments) { + arg, ok := args.Get(1).(*client.DeploymentList) + require.True(t, ok) + + arg.Items = []client.Deployment{*test.readReplicaDeployment} + }).Return(nil).Maybe() + + mock.On("List", testify.Anything, testify.AnythingOfType("*v1.JobList"), testify.Anything).Run(func(args testify.Arguments) { + arg, ok := args.Get(1).(*client.JobList) + require.True(t, ok) + + arg.Items = test.runningJobs + }).Return(nil).Maybe() + + // testify/mock does not accept to set Times(0) so you cannot do things like .Return(nil).Once(calledTimes) + // ref: https://github.com/stretchr/testify/issues/566 + if test.want.createCalled { + mock.On("Create", testify.Anything, testify.Anything, testify.Anything).Return(nil).Once() + } + defer mock.AssertExpectations(tt) + + opts := []Option{ + WithK8sClient(mock), + WithReadReplicaEnabled(test.readReplicaEnabled), + } + if test.rotationJobConcurrency != 0 { + opts = append(opts, WithRotationJobConcurrency(test.rotationJobConcurrency)) + } + o, err := New( + "namespace", + "agentName", + "rotatorName", + "targetReadReplicaIDEnvName", + nil, + opts..., + ) + require.NoError(t, err) + + op, ok := o.(*operator) + require.True(t, ok) + + op.rotatorJob = &client.Job{ + ObjectMeta: client.ObjectMeta{ + Name: "foo job", + }, + } + + res, err := op.podOnReconcile(context.Background(), test.agentPod) + require.Equal(t, test.want.err, err) + require.Equal(t, test.want.res, res) }) } } From a0b86825a7456b4a8745115cc5b5a2455afb65a7 Mon Sep 17 00:00:00 2001 From: ykadowak Date: Tue, 19 Mar 2024 01:49:36 +0000 Subject: [PATCH 3/3] Fix not to use kubeconfig --- pkg/index/operator/service/operator_test.go | 24 ++++++--------------- 1 file changed, 7 insertions(+), 17 deletions(-) diff --git a/pkg/index/operator/service/operator_test.go b/pkg/index/operator/service/operator_test.go index f212dbdcce..f96fe89fa6 100644 --- a/pkg/index/operator/service/operator_test.go +++ b/pkg/index/operator/service/operator_test.go @@ -302,25 +302,15 @@ func Test_operator_podOnReconcile(t *testing.T) { } defer mock.AssertExpectations(tt) - opts := []Option{ - WithK8sClient(mock), - WithReadReplicaEnabled(test.readReplicaEnabled), - } + concurrency := uint(1) if test.rotationJobConcurrency != 0 { - opts = append(opts, WithRotationJobConcurrency(test.rotationJobConcurrency)) + concurrency = test.rotationJobConcurrency + } + op := operator{ + client: mock, + readReplicaEnabled: test.readReplicaEnabled, + rotationJobConcurrency: concurrency, } - o, err := New( - "namespace", - "agentName", - "rotatorName", - "targetReadReplicaIDEnvName", - nil, - opts..., - ) - require.NoError(t, err) - - op, ok := o.(*operator) - require.True(t, ok) op.rotatorJob = &client.Job{ ObjectMeta: client.ObjectMeta{