frostfs-sdk-go/client/status/v2_test.go
2023-07-12 19:08:37 +03:00

316 lines
6.2 KiB
Go

package apistatus_test
import (
"errors"
"testing"
apistatus "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client/status"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/require"
)
func TestToStatusV2(t *testing.T) {
type statusConstructor func() apistatus.Status
for _, testItem := range [...]struct {
status any // Status or statusConstructor
codeV2 uint32
messageV2 string
}{
{
status: errors.New("some error"),
codeV2: 1024,
messageV2: "some error",
},
{
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 {
st := apistatus.NewServerInternal()
st.SetMessage("internal error message")
return st
}),
codeV2: 1024,
},
{
status: (statusConstructor)(func() apistatus.Status {
st := apistatus.NewWrongMagicNumber()
st.WriteCorrectMagic(322)
return st
}),
codeV2: 1025,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewObjectLocked()
}),
codeV2: 2050,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewLockNonRegularObject()
}),
codeV2: 2051,
},
{
status: (statusConstructor)(func() apistatus.Status {
st := apistatus.NewObjectAccessDenied()
st.WriteReason("any reason")
return st
}),
codeV2: 2048,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewObjectNotFound()
}),
codeV2: 2049,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewObjectAlreadyRemoved()
}),
codeV2: 2052,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewObjectOutOfRange()
}),
codeV2: 2053,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewContainerNotFound()
}),
codeV2: 3072,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewEACLNotFound()
}),
codeV2: 3073,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewSessionTokenNotFound()
}),
codeV2: 4096,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewSessionTokenExpired()
}),
codeV2: 4097,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewNodeUnderMaintenance()
}),
codeV2: 1027,
},
} {
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.GetCode())
if len(testItem.messageV2) > 0 {
require.Equal(t, testItem.messageV2, stv2.GetMessage())
}
_, 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.True(t, proto.Equal(stv2, res))
}
}
}
func TestFromStatusV2(t *testing.T) {
type statusConstructor func() apistatus.Status
for _, testItem := range [...]struct {
status any // Status or statusConstructor
codeV2 uint64
messageV2 string
}{
{
status: errors.New("some error"),
codeV2: 1024,
messageV2: "some error",
},
{
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 {
st := apistatus.NewServerInternal()
st.SetMessage("internal error message")
return st
}),
codeV2: 1024,
},
{
status: (statusConstructor)(func() apistatus.Status {
st := apistatus.NewWrongMagicNumber()
st.WriteCorrectMagic(322)
return st
}),
codeV2: 1025,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewObjectLocked()
}),
codeV2: 2050,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewLockNonRegularObject()
}),
codeV2: 2051,
},
{
status: (statusConstructor)(func() apistatus.Status {
st := apistatus.NewObjectAccessDenied()
st.WriteReason("any reason")
return st
}),
codeV2: 2048,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewObjectNotFound()
}),
codeV2: 2049,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewObjectAlreadyRemoved()
}),
codeV2: 2052,
},
{
status: statusConstructor(func() apistatus.Status {
return apistatus.NewObjectOutOfRange()
}),
codeV2: 2053,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewContainerNotFound()
}),
codeV2: 3072,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewEACLNotFound()
}),
codeV2: 3073,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewSessionTokenNotFound()
}),
codeV2: 4096,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewSessionTokenExpired()
}),
codeV2: 4097,
},
{
status: (statusConstructor)(func() apistatus.Status {
return apistatus.NewNodeUnderMaintenance()
}),
codeV2: 1027,
},
} {
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.GetCode())
if len(testItem.messageV2) > 0 {
require.Equal(t, testItem.messageV2, stv2.GetMessage())
}
_, 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.True(t, proto.Equal(stv2, res))
}
}
}