2021-11-16 18:15:56 +00:00
|
|
|
package apistatus_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"errors"
|
|
|
|
"testing"
|
|
|
|
|
2023-03-07 11:20:03 +00:00
|
|
|
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
|
2021-11-16 18:15:56 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestToStatusV2(t *testing.T) {
|
|
|
|
type statusConstructor func() apistatus.Status
|
|
|
|
|
|
|
|
for _, testItem := range [...]struct {
|
2023-05-15 14:21:49 +00:00
|
|
|
status any // Status or statusConstructor
|
2022-03-16 13:54:20 +00:00
|
|
|
codeV2 uint64
|
|
|
|
messageV2 string
|
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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: true,
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: true,
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: nil,
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
var st apistatus.ServerInternal
|
|
|
|
|
|
|
|
st.SetMessage("internal error message")
|
|
|
|
|
|
|
|
return st
|
|
|
|
}),
|
|
|
|
codeV2: 1024,
|
|
|
|
},
|
2022-01-25 12:37:30 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
var st apistatus.WrongMagicNumber
|
|
|
|
|
|
|
|
st.WriteCorrectMagic(322)
|
|
|
|
|
|
|
|
return st
|
|
|
|
}),
|
|
|
|
codeV2: 1025,
|
|
|
|
},
|
2022-02-15 05:41:03 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectLocked)
|
|
|
|
}),
|
|
|
|
codeV2: 2050,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.LockNonRegularObject)
|
|
|
|
}),
|
|
|
|
codeV2: 2051,
|
|
|
|
},
|
2022-02-28 08:39:22 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
var st apistatus.ObjectAccessDenied
|
|
|
|
|
|
|
|
st.WriteReason("any reason")
|
|
|
|
|
|
|
|
return st
|
|
|
|
}),
|
|
|
|
codeV2: 2048,
|
|
|
|
},
|
2022-02-28 08:45:33 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectNotFound)
|
|
|
|
}),
|
|
|
|
codeV2: 2049,
|
|
|
|
},
|
2022-02-28 08:49:34 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectAlreadyRemoved)
|
|
|
|
}),
|
|
|
|
codeV2: 2052,
|
|
|
|
},
|
2022-06-29 17:24:56 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectOutOfRange)
|
|
|
|
}),
|
|
|
|
codeV2: 2053,
|
|
|
|
},
|
2022-02-28 08:55:25 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ContainerNotFound)
|
|
|
|
}),
|
|
|
|
codeV2: 3072,
|
|
|
|
},
|
2022-08-01 14:17:48 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.EACLNotFound)
|
|
|
|
}),
|
|
|
|
codeV2: 3073,
|
|
|
|
},
|
2022-02-28 10:19:54 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.SessionTokenNotFound)
|
|
|
|
}),
|
|
|
|
codeV2: 4096,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.SessionTokenExpired)
|
|
|
|
}),
|
|
|
|
codeV2: 4097,
|
|
|
|
},
|
2024-05-06 13:33:20 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.APEManagerAccessDenied)
|
|
|
|
}),
|
|
|
|
codeV2: 5120,
|
|
|
|
},
|
2022-09-19 17:34:14 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.NodeUnderMaintenance)
|
|
|
|
}),
|
|
|
|
codeV2: 1027,
|
|
|
|
},
|
2024-10-02 08:12:49 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.InvalidArgument)
|
|
|
|
}),
|
|
|
|
codeV2: 1028,
|
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestFromStatusV2(t *testing.T) {
|
|
|
|
type statusConstructor func() apistatus.Status
|
|
|
|
|
|
|
|
for _, testItem := range [...]struct {
|
2023-05-15 14:21:49 +00:00
|
|
|
status any // Status or statusConstructor
|
2022-03-16 13:54:20 +00:00
|
|
|
codeV2 uint64
|
|
|
|
messageV2 string
|
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,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: true,
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: true,
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: nil,
|
|
|
|
codeV2: 0,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
var st apistatus.ServerInternal
|
|
|
|
|
|
|
|
st.SetMessage("internal error message")
|
|
|
|
|
|
|
|
return st
|
|
|
|
}),
|
|
|
|
codeV2: 1024,
|
|
|
|
},
|
2022-01-25 12:37:30 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
var st apistatus.WrongMagicNumber
|
|
|
|
|
|
|
|
st.WriteCorrectMagic(322)
|
|
|
|
|
|
|
|
return st
|
|
|
|
}),
|
|
|
|
codeV2: 1025,
|
|
|
|
},
|
2022-02-15 05:41:03 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectLocked)
|
|
|
|
}),
|
|
|
|
codeV2: 2050,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.LockNonRegularObject)
|
|
|
|
}),
|
|
|
|
codeV2: 2051,
|
|
|
|
},
|
2022-02-28 08:39:22 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
var st apistatus.ObjectAccessDenied
|
|
|
|
|
|
|
|
st.WriteReason("any reason")
|
|
|
|
|
|
|
|
return st
|
|
|
|
}),
|
|
|
|
codeV2: 2048,
|
|
|
|
},
|
2022-02-28 08:45:33 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectNotFound)
|
|
|
|
}),
|
|
|
|
codeV2: 2049,
|
|
|
|
},
|
2022-02-28 08:49:34 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectAlreadyRemoved)
|
|
|
|
}),
|
|
|
|
codeV2: 2052,
|
|
|
|
},
|
2022-06-29 17:24:56 +00:00
|
|
|
{
|
|
|
|
status: statusConstructor(func() apistatus.Status {
|
|
|
|
return new(apistatus.ObjectOutOfRange)
|
|
|
|
}),
|
|
|
|
codeV2: 2053,
|
|
|
|
},
|
2022-02-28 08:55:25 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.ContainerNotFound)
|
|
|
|
}),
|
|
|
|
codeV2: 3072,
|
|
|
|
},
|
2022-08-01 14:17:48 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.EACLNotFound)
|
|
|
|
}),
|
|
|
|
codeV2: 3073,
|
|
|
|
},
|
2022-02-28 10:19:54 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.SessionTokenNotFound)
|
|
|
|
}),
|
|
|
|
codeV2: 4096,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.SessionTokenExpired)
|
|
|
|
}),
|
|
|
|
codeV2: 4097,
|
|
|
|
},
|
2024-05-06 13:33:20 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.APEManagerAccessDenied)
|
|
|
|
}),
|
|
|
|
codeV2: 5120,
|
|
|
|
},
|
2022-09-19 17:34:14 +00:00
|
|
|
{
|
|
|
|
status: (statusConstructor)(func() apistatus.Status {
|
|
|
|
return new(apistatus.NodeUnderMaintenance)
|
|
|
|
}),
|
|
|
|
codeV2: 1027,
|
|
|
|
},
|
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)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|