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) {
|
2023-05-23 06:34:02 +00:00
|
|
|
type statusConstructor func() error
|
2021-11-16 18:15:56 +00:00
|
|
|
|
|
|
|
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
|
|
|
}{
|
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
|
|
|
return errors.New("some error")
|
|
|
|
}),
|
2022-03-16 13:54:20 +00:00
|
|
|
codeV2: 1024,
|
|
|
|
messageV2: "some error",
|
2021-11-16 18:15:56 +00:00
|
|
|
},
|
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
|
|
|
return nil
|
|
|
|
}),
|
2021-11-16 18:15:56 +00:00
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
2022-02-15 05:41:03 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
2022-02-15 05:41:03 +00:00
|
|
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
2022-02-28 08:45:33 +00:00
|
|
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
2022-02-28 08:49:34 +00:00
|
|
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: statusConstructor(func() error {
|
2022-06-29 17:24:56 +00:00
|
|
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
2022-02-28 08:55:25 +00:00
|
|
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
2022-08-01 14:17:48 +00:00
|
|
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
2022-02-28 10:19:54 +00:00
|
|
|
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
|
|
|
},
|
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
2022-02-28 10:19:54 +00:00
|
|
|
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
|
|
|
{
|
2023-05-23 06:34:02 +00:00
|
|
|
status: (statusConstructor)(func() error {
|
2022-09-19 17:34:14 +00:00
|
|
|
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
|
|
|
} {
|
2023-05-23 06:34:02 +00:00
|
|
|
var st error
|
|
|
|
cons, ok := testItem.status.(statusConstructor)
|
|
|
|
require.True(t, ok)
|
2021-11-16 18:15:56 +00:00
|
|
|
|
2023-05-23 06:34:02 +00:00
|
|
|
st = cons()
|
2021-11-16 18:15:56 +00:00
|
|
|
|
2023-05-24 05:50:14 +00:00
|
|
|
stv2 := apistatus.ErrorToV2(st)
|
2021-11-16 18:15:56 +00:00
|
|
|
|
|
|
|
// 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
|
|
|
|
2023-05-23 06:34:02 +00:00
|
|
|
_, ok = st.(apistatus.StatusV2)
|
2021-11-16 18:15:56 +00:00
|
|
|
if ok {
|
|
|
|
// restore and convert again
|
2023-05-24 05:50:14 +00:00
|
|
|
restored := apistatus.ErrorFromV2(stv2)
|
2021-11-16 18:15:56 +00:00
|
|
|
|
2023-05-24 05:50:14 +00:00
|
|
|
res := apistatus.ErrorToV2(restored)
|
2021-11-16 18:15:56 +00:00
|
|
|
|
|
|
|
// must generate the same status.Status message
|
|
|
|
require.Equal(t, stv2, res)
|
|
|
|
}
|
2023-05-16 08:33:59 +00:00
|
|
|
|
|
|
|
randomError := errors.New("garbage")
|
2023-05-23 06:34:02 +00:00
|
|
|
for _, err := range testItem.compatibleErrs {
|
|
|
|
require.ErrorIs(t, st, err)
|
|
|
|
require.NotErrorIs(t, randomError, err)
|
|
|
|
}
|
2023-05-22 12:07:57 +00:00
|
|
|
|
2023-05-23 06:34:02 +00:00
|
|
|
if testItem.checkAsErr != nil {
|
|
|
|
require.True(t, testItem.checkAsErr(st))
|
2023-05-16 08:33:59 +00:00
|
|
|
}
|
2021-11-16 18:15:56 +00:00
|
|
|
}
|
|
|
|
}
|