2022-12-20 08:38:58 +00:00
|
|
|
package frostfs
|
2022-04-21 08:49:56 +00:00
|
|
|
|
|
|
|
import (
|
2023-06-13 14:47:31 +00:00
|
|
|
"context"
|
|
|
|
"errors"
|
2022-04-21 08:49:56 +00:00
|
|
|
"fmt"
|
|
|
|
"testing"
|
2023-06-13 14:47:31 +00:00
|
|
|
"time"
|
2022-04-21 08:49:56 +00:00
|
|
|
|
2024-09-27 08:14:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
|
2023-03-07 14:38:08 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2022-04-21 08:49:56 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2023-06-13 14:47:31 +00:00
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
2022-04-21 08:49:56 +00:00
|
|
|
)
|
|
|
|
|
2024-12-22 12:14:21 +00:00
|
|
|
func TestHandleObjectError(t *testing.T) {
|
|
|
|
msg := "some msg"
|
2022-04-21 08:49:56 +00:00
|
|
|
|
2024-12-22 12:14:21 +00:00
|
|
|
t.Run("nil error", func(t *testing.T) {
|
|
|
|
err := handleObjectError(msg, nil)
|
|
|
|
require.Nil(t, err)
|
|
|
|
})
|
2022-04-21 08:49:56 +00:00
|
|
|
|
2024-12-22 12:14:21 +00:00
|
|
|
t.Run("simple access denied", func(t *testing.T) {
|
|
|
|
reason := "some reason"
|
|
|
|
inputErr := new(apistatus.ObjectAccessDenied)
|
|
|
|
inputErr.WriteReason(reason)
|
2022-04-21 08:49:56 +00:00
|
|
|
|
2024-12-22 12:14:21 +00:00
|
|
|
err := handleObjectError(msg, inputErr)
|
|
|
|
require.ErrorIs(t, err, frostfs.ErrAccessDenied)
|
|
|
|
require.Contains(t, err.Error(), reason)
|
|
|
|
require.Contains(t, err.Error(), msg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("access denied - quota reached", func(t *testing.T) {
|
|
|
|
reason := "Quota limit reached"
|
|
|
|
inputErr := new(apistatus.ObjectAccessDenied)
|
|
|
|
inputErr.WriteReason(reason)
|
|
|
|
|
|
|
|
err := handleObjectError(msg, inputErr)
|
|
|
|
require.ErrorIs(t, err, frostfs.ErrQuotaLimitReached)
|
|
|
|
require.Contains(t, err.Error(), reason)
|
|
|
|
require.Contains(t, err.Error(), msg)
|
|
|
|
})
|
2023-06-13 14:47:31 +00:00
|
|
|
|
|
|
|
t.Run("simple timeout", func(t *testing.T) {
|
2024-12-22 12:14:21 +00:00
|
|
|
inputErr := errors.New("timeout")
|
|
|
|
|
|
|
|
err := handleObjectError(msg, inputErr)
|
|
|
|
require.ErrorIs(t, err, frostfs.ErrGatewayTimeout)
|
|
|
|
require.Contains(t, err.Error(), inputErr.Error())
|
|
|
|
require.Contains(t, err.Error(), msg)
|
2023-06-13 14:47:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("deadline exceeded", func(t *testing.T) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), 10*time.Millisecond)
|
|
|
|
defer cancel()
|
2024-02-14 11:32:17 +00:00
|
|
|
<-ctx.Done()
|
2023-06-13 14:47:31 +00:00
|
|
|
|
2024-12-22 12:14:21 +00:00
|
|
|
err := handleObjectError(msg, ctx.Err())
|
|
|
|
require.ErrorIs(t, err, frostfs.ErrGatewayTimeout)
|
|
|
|
require.Contains(t, err.Error(), ctx.Err().Error())
|
|
|
|
require.Contains(t, err.Error(), msg)
|
2023-06-13 14:47:31 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("grpc deadline exceeded", func(t *testing.T) {
|
2024-12-22 12:14:21 +00:00
|
|
|
inputErr := fmt.Errorf("wrap grpc error: %w", status.Error(codes.DeadlineExceeded, "error"))
|
|
|
|
|
|
|
|
err := handleObjectError(msg, inputErr)
|
|
|
|
require.ErrorIs(t, err, frostfs.ErrGatewayTimeout)
|
|
|
|
require.Contains(t, err.Error(), inputErr.Error())
|
|
|
|
require.Contains(t, err.Error(), msg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("global domain already", func(t *testing.T) {
|
|
|
|
inputErr := errors.New("global domain is already taken")
|
|
|
|
|
|
|
|
err := handleObjectError(msg, inputErr)
|
|
|
|
require.ErrorIs(t, err, frostfs.ErrGlobalDomainIsAlreadyTaken)
|
|
|
|
require.Contains(t, err.Error(), inputErr.Error())
|
|
|
|
require.Contains(t, err.Error(), msg)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("unknown error", func(t *testing.T) {
|
|
|
|
inputErr := errors.New("unknown error")
|
|
|
|
|
|
|
|
err := handleObjectError(msg, inputErr)
|
|
|
|
require.ErrorIs(t, err, inputErr)
|
|
|
|
require.Contains(t, err.Error(), msg)
|
2023-06-13 14:47:31 +00:00
|
|
|
})
|
|
|
|
}
|