Skip to content

Commit

Permalink
Add tests for parseReplicaID
Browse files Browse the repository at this point in the history
  • Loading branch information
ykadowak committed Feb 20, 2024
1 parent 1d91f05 commit 08de607
Show file tree
Hide file tree
Showing 3 changed files with 92 additions and 3 deletions.
23 changes: 23 additions & 0 deletions internal/errors/rotator.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,23 @@
//
// Copyright (C) 2019-2024 vdaas.org vald team <[email protected]>
//
// 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")
)
8 changes: 5 additions & 3 deletions pkg/index/job/readreplica/rotate/service/rotator.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -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 {
Expand Down
64 changes: 64 additions & 0 deletions pkg/index/job/readreplica/rotate/service/rotator_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down

0 comments on commit 08de607

Please sign in to comment.