forked from TrueCloudLab/frostfs-s3-gw
101 lines
2.7 KiB
Go
101 lines
2.7 KiB
Go
package services
|
|
|
|
import (
|
|
"context"
|
|
"errors"
|
|
"google.golang.org/grpc"
|
|
"google.golang.org/grpc/credentials/insecure"
|
|
"testing"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services/client"
|
|
grpcService "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/services/tree"
|
|
"github.com/stretchr/testify/require"
|
|
"go.uber.org/zap/zaptest"
|
|
)
|
|
|
|
func TestHandleError(t *testing.T) {
|
|
defaultError := errors.New("default error")
|
|
for _, tc := range []struct {
|
|
err error
|
|
expectedError error
|
|
}{
|
|
{
|
|
err: defaultError,
|
|
expectedError: defaultError,
|
|
},
|
|
{
|
|
err: errors.New("something not found"),
|
|
expectedError: layer.ErrNodeNotFound,
|
|
},
|
|
{
|
|
err: errors.New("something is denied by some acl rule"),
|
|
expectedError: layer.ErrNodeAccessDenied,
|
|
},
|
|
} {
|
|
t.Run("", func(t *testing.T) {
|
|
err := handleError("err message", tc.err)
|
|
require.True(t, errors.Is(err, tc.expectedError))
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestRetry(t *testing.T) {
|
|
ctx := context.Background()
|
|
log := zaptest.NewLogger(t)
|
|
|
|
grpcDialOpt := grpc.WithTransportCredentials(insecure.NewCredentials())
|
|
cl := &ServiceClientGRPC{
|
|
log: zaptest.NewLogger(t),
|
|
clients: []*client.TreeClient{
|
|
client.NewTreeClient("node0", grpcDialOpt),
|
|
client.NewTreeClient("node1", grpcDialOpt),
|
|
client.NewTreeClient("node2", grpcDialOpt),
|
|
client.NewTreeClient("node3", grpcDialOpt),
|
|
},
|
|
}
|
|
|
|
makeFn := func(shouldFail []string) func(grpcService.TreeServiceClient) error {
|
|
return func(client grpcService.TreeServiceClient) error {
|
|
|
|
//for _, item := range shouldFail {
|
|
//if item == client.address {
|
|
// return errors.New("not found")
|
|
//}
|
|
//}
|
|
return nil
|
|
}
|
|
}
|
|
|
|
t.Run("first ok", func(t *testing.T) {
|
|
err := cl.requestWithRetry(ctx, log, makeFn([]string{}))
|
|
require.NoError(t, err)
|
|
require.Equal(t, 0, cl.getStartIndex())
|
|
cl.setStartIndex(0)
|
|
})
|
|
|
|
t.Run("first failed", func(t *testing.T) {
|
|
err := cl.requestWithRetry(ctx, log, makeFn([]string{"node0"}))
|
|
require.NoError(t, err)
|
|
require.Equal(t, 1, cl.getStartIndex())
|
|
cl.setStartIndex(0)
|
|
})
|
|
|
|
t.Run("all failed", func(t *testing.T) {
|
|
err := cl.requestWithRetry(ctx, log, makeFn([]string{"node0", "node1", "node2", "node3"}))
|
|
require.Error(t, err)
|
|
require.Equal(t, 0, cl.getStartIndex())
|
|
cl.setStartIndex(0)
|
|
})
|
|
|
|
t.Run("round", func(t *testing.T) {
|
|
err := cl.requestWithRetry(ctx, log, makeFn([]string{"node0", "node1"}))
|
|
require.NoError(t, err)
|
|
require.Equal(t, 2, cl.getStartIndex())
|
|
|
|
err = cl.requestWithRetry(ctx, log, makeFn([]string{"node2", "node3"}))
|
|
require.NoError(t, err)
|
|
require.Equal(t, 0, cl.getStartIndex())
|
|
cl.setStartIndex(0)
|
|
})
|
|
}
|