2021-11-16 18:15:56 +00:00
|
|
|
package apistatus_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestFromStatusV2(t *testing.T) {
|
|
|
|
type statusConstructor func() apistatus.Status
|
|
|
|
|
|
|
|
for _, testItem := range [...]struct {
|
2023-05-16 08:33:59 +00:00
|
|
|
status any // Status or statusConstructor
|
|
|
|
codeV2 uint64
|
|
|
|
messageV2 string
|
|
|
|
compatibleErrs []error
|
|
|
|
checkAsErr func(error) bool
|
2021-11-16 18:15:56 +00:00
|
|
|
}{
|
|
|
|
{
|
2022-03-16 13:54:20 +00:00
|
|
|
status: errors.New("some error"),
|
|
|
|
codeV2: 1024,
|
|
|
|
messageV2: "some error",
|
2021-11-16 18:15:56 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
status: 1,
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: "text",
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
2023-05-16 08:33:59 +00:00
|
|
|
status: false,
|
2021-11-16 18:15:56 +00:00
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: true,
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: nil,
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
2023-05-16 08:33:59 +00:00
|
|
|
st := new(apistatus.ServerInternal)
|
2021-11-16 18:15:56 +00:00
|
|
|
st.SetMessage("internal error message")
|
|
|
|
|
|
|
|
return st
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 1024,
|
|
|
|
compatibleErrs: []error{apistatus.ErrServerInternal, apistatus.ServerInternal{}, &apistatus.ServerInternal{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.ServerInternal
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2021-11-16 18:15:56 +00:00
|
|
|
},
|
2022-01-25 12:37:30 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
2023-05-16 08:33:59 +00:00
|
|
|
st := new(apistatus.WrongMagicNumber)
|
2022-01-25 12:37:30 +00:00
|
|
|
st.WriteCorrectMagic(322)
|
|
|
|
|
|
|
|
return st
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 1025,
|
|
|
|
compatibleErrs: []error{apistatus.ErrWrongMagicNumber, apistatus.WrongMagicNumber{}, &apistatus.WrongMagicNumber{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.WrongMagicNumber
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-01-25 12:37:30 +00:00
|
|
|
},
|
2022-02-15 05:41:03 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectLocked)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 2050,
|
|
|
|
compatibleErrs: []error{apistatus.ErrObjectLocked, apistatus.ObjectLocked{}, &apistatus.ObjectLocked{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.ObjectLocked
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-02-15 05:41:03 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.LockNonRegularObject)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 2051,
|
|
|
|
compatibleErrs: []error{apistatus.ErrLockNonRegularObject, apistatus.LockNonRegularObject{}, &apistatus.LockNonRegularObject{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.LockNonRegularObject
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-02-15 05:41:03 +00:00
|
|
|
},
|
2022-02-28 08:39:22 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
2023-05-16 08:33:59 +00:00
|
|
|
st := new(apistatus.ObjectAccessDenied)
|
2022-02-28 08:39:22 +00:00
|
|
|
st.WriteReason("any reason")
|
|
|
|
|
|
|
|
return st
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 2048,
|
|
|
|
compatibleErrs: []error{apistatus.ErrObjectAccessDenied, apistatus.ObjectAccessDenied{}, &apistatus.ObjectAccessDenied{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.ObjectAccessDenied
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-02-28 08:39:22 +00:00
|
|
|
},
|
2022-02-28 08:45:33 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectNotFound)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 2049,
|
|
|
|
compatibleErrs: []error{apistatus.ErrObjectNotFound, apistatus.ObjectNotFound{}, &apistatus.ObjectNotFound{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.ObjectNotFound
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-02-28 08:45:33 +00:00
|
|
|
},
|
2022-02-28 08:49:34 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectAlreadyRemoved)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 2052,
|
|
|
|
compatibleErrs: []error{apistatus.ErrObjectAlreadyRemoved, apistatus.ObjectAlreadyRemoved{}, &apistatus.ObjectAlreadyRemoved{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.ObjectAlreadyRemoved
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-02-28 08:49:34 +00:00
|
|
|
},
|
2022-06-29 17:24:56 +00:00
|
|
|
{
|
|
|
|
status: statusConstructor(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectOutOfRange)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 2053,
|
|
|
|
compatibleErrs: []error{apistatus.ErrObjectOutOfRange, apistatus.ObjectOutOfRange{}, &apistatus.ObjectOutOfRange{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.ObjectOutOfRange
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-06-29 17:24:56 +00:00
|
|
|
},
|
2022-02-28 08:55:25 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ContainerNotFound)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 3072,
|
|
|
|
compatibleErrs: []error{apistatus.ErrContainerNotFound, apistatus.ContainerNotFound{}, &apistatus.ContainerNotFound{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.ContainerNotFound
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-02-28 08:55:25 +00:00
|
|
|
},
|
2022-08-01 14:17:48 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.EACLNotFound)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 3073,
|
|
|
|
compatibleErrs: []error{apistatus.ErrEACLNotFound, apistatus.EACLNotFound{}, &apistatus.EACLNotFound{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.EACLNotFound
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-08-01 14:17:48 +00:00
|
|
|
},
|
2022-02-28 10:19:54 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.SessionTokenNotFound)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 4096,
|
|
|
|
compatibleErrs: []error{apistatus.ErrSessionTokenNotFound, apistatus.SessionTokenNotFound{}, &apistatus.SessionTokenNotFound{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.SessionTokenNotFound
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-02-28 10:19:54 +00:00
|
|
|
},
|
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.SessionTokenExpired)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 4097,
|
|
|
|
compatibleErrs: []error{apistatus.ErrSessionTokenExpired, apistatus.SessionTokenExpired{}, &apistatus.SessionTokenExpired{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.SessionTokenExpired
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-02-28 10:19:54 +00:00
|
|
|
},
|
2022-09-19 17:34:14 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.NodeUnderMaintenance)
|
|
|
|
}),
|
2023-05-16 08:33:59 +00:00
|
|
|
codeV2: 1027,
|
|
|
|
compatibleErrs: []error{apistatus.ErrNodeUnderMaintenance, apistatus.NodeUnderMaintenance{}, &apistatus.NodeUnderMaintenance{}, apistatus.Error},
|
|
|
|
checkAsErr: func(err error) bool {
|
|
|
|
var target *apistatus.NodeUnderMaintenance
|
|
|
|
return errors.As(err, &target)
|
|
|
|
},
|
2022-09-19 17:34:14 +00:00
|
|
|
},
|
2021-11-16 18:15:56 +00:00
|
|
|
} {
|
|
|
|
var st apistatus.Status
|
|
|
|
|
|
|
|
if cons, ok := testItem.status.(statusConstructor); ok {
|
|
|
|
st = cons()
|
|
|
|
} else {
|
|
|
|
st = testItem.status
|
|
|
|
}
|
|
|
|
|
|
|
|
stv2 := apistatus.ToStatusV2(st)
|
|
|
|
|
|
|
|
// must generate the same status.Status message
|
|
|
|
require.EqualValues(t, testItem.codeV2, stv2.Code())
|
2022-03-16 13:54:20 +00:00
|
|
|
if len(testItem.messageV2) > 0 {
|
|
|
|
require.Equal(t, testItem.messageV2, stv2.Message())
|
|
|
|
}
|
2021-11-16 18:15:56 +00:00
|
|
|
|
|
|
|
_, ok := st.(apistatus.StatusV2)
|
|
|
|
if ok {
|
|
|
|
// restore and convert again
|
|
|
|
restored := apistatus.FromStatusV2(stv2)
|
|
|
|
|
|
|
|
res := apistatus.ToStatusV2(restored)
|
|
|
|
|
|
|
|
// must generate the same status.Status message
|
|
|
|
require.Equal(t, stv2, res)
|
|
|
|
}
|
2023-05-16 08:33:59 +00:00
|
|
|
|
|
|
|
randomError := errors.New("garbage")
|
|
|
|
errFromStatus := apistatus.ErrFromStatus(st)
|
|
|
|
|
|
|
|
for _, err := range testItem.compatibleErrs {
|
|
|
|
require.ErrorIs(t, errFromStatus, err)
|
|
|
|
require.NotErrorIs(t, randomError, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if testItem.checkAsErr != nil {
|
|
|
|
require.True(t, testItem.checkAsErr(errFromStatus))
|
|
|
|
}
|
2021-11-16 18:15:56 +00:00
|
|
|
}
|
|
|
|
}
|