2021-08-09 08:53:58 +00:00
|
|
|
package errors
|
2021-07-21 09:05:24 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
2024-09-27 09:26:08 +00:00
|
|
|
"fmt"
|
2021-07-21 09:05:24 +00:00
|
|
|
"testing"
|
2024-09-27 09:26:08 +00:00
|
|
|
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/frostfs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer/tree"
|
|
|
|
"github.com/stretchr/testify/require"
|
2021-07-21 09:05:24 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func BenchmarkErrCode(b *testing.B) {
|
|
|
|
err := GetAPIError(ErrNoSuchKey)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if IsS3Error(err, ErrNoSuchKey) {
|
|
|
|
_ = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func BenchmarkErrorsIs(b *testing.B) {
|
|
|
|
err := GetAPIError(ErrNoSuchKey)
|
|
|
|
|
|
|
|
for i := 0; i < b.N; i++ {
|
|
|
|
if errors.Is(err, GetAPIError(ErrNoSuchKey)) {
|
|
|
|
_ = err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2024-09-27 09:26:08 +00:00
|
|
|
|
|
|
|
func TestTransformS3Errors(t *testing.T) {
|
|
|
|
for _, tc := range []struct {
|
|
|
|
name string
|
|
|
|
err error
|
|
|
|
expected ErrorCode
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "simple std error to internal error",
|
|
|
|
err: errors.New("some error"),
|
|
|
|
expected: ErrInternalError,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "layer access denied error to s3 access denied error",
|
|
|
|
err: frostfs.ErrAccessDenied,
|
|
|
|
expected: ErrAccessDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrapped layer access denied error to s3 access denied error",
|
|
|
|
err: fmt.Errorf("wrap: %w", frostfs.ErrAccessDenied),
|
|
|
|
expected: ErrAccessDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "layer node access denied error to s3 access denied error",
|
|
|
|
err: tree.ErrNodeAccessDenied,
|
|
|
|
expected: ErrAccessDenied,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "layer gateway timeout error to s3 gateway timeout error",
|
|
|
|
err: frostfs.ErrGatewayTimeout,
|
|
|
|
expected: ErrGatewayTimeout,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "s3 error to s3 error",
|
|
|
|
err: GetAPIError(ErrInvalidPart),
|
|
|
|
expected: ErrInvalidPart,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "wrapped s3 error to s3 error",
|
|
|
|
err: fmt.Errorf("wrap: %w", GetAPIError(ErrInvalidPart)),
|
|
|
|
expected: ErrInvalidPart,
|
|
|
|
},
|
|
|
|
} {
|
|
|
|
t.Run(tc.name, func(t *testing.T) {
|
|
|
|
err := TransformToS3Error(tc.err)
|
|
|
|
s3err, ok := err.(Error)
|
|
|
|
require.True(t, ok, "error must be s3 error")
|
|
|
|
require.Equalf(t, tc.expected, s3err.ErrCode,
|
|
|
|
"expected: '%s', got: '%s'",
|
|
|
|
GetAPIError(tc.expected).Code, GetAPIError(s3err.ErrCode).Code)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|