From 08de607f2e0d587c4682b00361d95f7bb638dd2d Mon Sep 17 00:00:00 2001 From: "yusuke.kadowaki" Date: Tue, 20 Feb 2024 13:47:12 +0900 Subject: [PATCH] Add tests for parseReplicaID --- internal/errors/rotator.go | 23 +++++++ .../job/readreplica/rotate/service/rotator.go | 8 ++- .../rotate/service/rotator_test.go | 64 +++++++++++++++++++ 3 files changed, 92 insertions(+), 3 deletions(-) create mode 100644 internal/errors/rotator.go diff --git a/internal/errors/rotator.go b/internal/errors/rotator.go new file mode 100644 index 0000000000..4dc1955fed --- /dev/null +++ b/internal/errors/rotator.go @@ -0,0 +1,23 @@ +// +// 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 errors provides error types and function +package errors + +var ( + // ErrReadReplicaIDEmpty represents error when trying to rotate agents with empty replicaID + ErrReadReplicaIDEmpty = New("readreplica id is empty. it should be set via MY_TARGET_REPLICA_ID env var") +) diff --git a/pkg/index/job/readreplica/rotate/service/rotator.go b/pkg/index/job/readreplica/rotate/service/rotator.go index 84b3dc5b4b..c478e12965 100644 --- a/pkg/index/job/readreplica/rotate/service/rotator.go +++ b/pkg/index/job/readreplica/rotate/service/rotator.go @@ -406,7 +406,7 @@ func getNewBaseName(old string) string { func (r *rotator) parseReplicaID(replicaID string, c client.Client) ([]string, error) { if replicaID == "" { - return nil, fmt.Errorf("readreplica id is empty. it should be set via MY_TARGET_REPLICA_ID env var") + return nil, errors.ErrReadReplicaIDEmpty } if replicaID == rotateAllID { @@ -415,10 +415,12 @@ func (r *rotator) parseReplicaID(replicaID string, c client.Client) ([]string, e if err != nil { return nil, err } - c.List(context.Background(), &deploymentList, &client.ListOptions{ + if err := c.List(context.Background(), &deploymentList, &client.ListOptions{ Namespace: r.namespace, LabelSelector: selector, - }) + }); err != nil { + return nil, fmt.Errorf("failed to List deployments in parseReplicaID: %w", err) + } deployments := deploymentList.Items if len(deployments) == 0 { diff --git a/pkg/index/job/readreplica/rotate/service/rotator_test.go b/pkg/index/job/readreplica/rotate/service/rotator_test.go index 3a975ae0f2..8ed40cb13a 100644 --- a/pkg/index/job/readreplica/rotate/service/rotator_test.go +++ b/pkg/index/job/readreplica/rotate/service/rotator_test.go @@ -15,6 +15,10 @@ package service import ( "testing" + + "github.com/stretchr/testify/require" + "github.com/vdaas/vald/internal/errors" + "github.com/vdaas/vald/internal/k8s/client" ) func Test_getNewBaseName(t *testing.T) { @@ -77,6 +81,66 @@ func Test_getNewBaseName(t *testing.T) { } } +func Test_parseReplicaID(t *testing.T) { + type args struct { + replicaID string + c client.Client + } + type want struct { + ids []string + err error + } + tests := []struct { + name string + args args + want want + }{ + { + name: "single replicaID", + args: args{ + replicaID: "0", + c: nil, + }, + want: want{ + ids: []string{"0"}, + err: nil, + }, + }, + { + name: "multiple replicaIDs", + args: args{ + replicaID: "0,1", + c: nil, + }, + want: want{ + ids: []string{"0", "1"}, + err: nil, + }, + }, + { + name: "returns error when replicaID is empty", + args: args{ + replicaID: "", + c: nil, + }, + want: want{ + ids: nil, + err: errors.ErrReadReplicaIDEmpty, + }, + }, + } + for _, test := range tests { + tt := test + t.Run(tt.name, func(t *testing.T) { + t.Parallel() + r := &rotator{} + ids, err := r.parseReplicaID(tt.args.replicaID, tt.args.c) + require.Equal(t, tt.want.ids, ids) + require.Equal(t, tt.want.err, err) + }) + } +} + // NOT IMPLEMENTED BELOW // // func TestNew(t *testing.T) {