[#293] pkg/container: Implement and use generators of the messages

Signed-off-by: Leonard Lyubich <leonard@nspcc.ru>
This commit is contained in:
Leonard Lyubich 2021-06-08 14:38:38 +03:00 committed by Alex Vanin
parent 3cf0ea022d
commit 122a31cadb
3 changed files with 67 additions and 50 deletions

View file

@ -7,6 +7,7 @@ import (
"github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-api-go/pkg/container"
cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id" cid "github.com/nspcc-dev/neofs-api-go/pkg/container/id"
cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test" cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test"
containertest "github.com/nspcc-dev/neofs-api-go/pkg/container/test"
"github.com/nspcc-dev/neofs-api-go/v2/refs" "github.com/nspcc-dev/neofs-api-go/v2/refs"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -51,13 +52,7 @@ func TestAnnouncement(t *testing.T) {
} }
func TestUsedSpaceEncoding(t *testing.T) { func TestUsedSpaceEncoding(t *testing.T) {
a := container.NewAnnouncement() a := containertest.UsedSpaceAnnouncement()
a.SetUsedSpace(13)
a.SetEpoch(666)
id := cidtest.Generate()
a.SetContainerID(id)
t.Run("binary", func(t *testing.T) { t.Run("binary", func(t *testing.T) {
data, err := a.Marshal() data, err := a.Marshal()

View file

@ -1,17 +1,16 @@
package container_test package container_test
import ( import (
"strconv"
"testing" "testing"
"github.com/google/uuid" "github.com/google/uuid"
"github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/acl" "github.com/nspcc-dev/neofs-api-go/pkg/acl"
"github.com/nspcc-dev/neofs-api-go/pkg/container" "github.com/nspcc-dev/neofs-api-go/pkg/container"
"github.com/nspcc-dev/neofs-api-go/pkg/netmap" containertest "github.com/nspcc-dev/neofs-api-go/pkg/container/test"
"github.com/nspcc-dev/neofs-api-go/pkg/owner" netmaptest "github.com/nspcc-dev/neofs-api-go/pkg/netmap/test"
ownertest "github.com/nspcc-dev/neofs-api-go/pkg/owner/test"
sessiontest "github.com/nspcc-dev/neofs-api-go/pkg/session/test" sessiontest "github.com/nspcc-dev/neofs-api-go/pkg/session/test"
"github.com/nspcc-dev/neofs-crypto/test" refstest "github.com/nspcc-dev/neofs-api-go/pkg/test"
"github.com/stretchr/testify/require" "github.com/stretchr/testify/require"
) )
@ -20,24 +19,26 @@ func TestNewContainer(t *testing.T) {
nonce := uuid.New() nonce := uuid.New()
wallet, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(1).PublicKey) ownerID := ownertest.Generate()
require.NoError(t, err) policy := netmaptest.PlacementPolicy()
ownerID := owner.NewIDFromNeo3Wallet(wallet)
policy := generatePlacementPolicy()
c.SetBasicACL(acl.PublicBasicRule) c.SetBasicACL(acl.PublicBasicRule)
c.SetAttributes(generateAttributes(5))
attrs := containertest.Attributes()
c.SetAttributes(attrs)
c.SetPlacementPolicy(policy) c.SetPlacementPolicy(policy)
c.SetNonceUUID(nonce) c.SetNonceUUID(nonce)
c.SetOwnerID(ownerID) c.SetOwnerID(ownerID)
c.SetVersion(pkg.SDKVersion())
ver := refstest.Version()
c.SetVersion(ver)
v2 := c.ToV2() v2 := c.ToV2()
newContainer := container.NewContainerFromV2(v2) newContainer := container.NewContainerFromV2(v2)
require.EqualValues(t, newContainer.PlacementPolicy(), policy) require.EqualValues(t, newContainer.PlacementPolicy(), policy)
require.EqualValues(t, newContainer.Attributes(), generateAttributes(5)) require.EqualValues(t, newContainer.Attributes(), attrs)
require.EqualValues(t, newContainer.BasicACL(), acl.PublicBasicRule) require.EqualValues(t, newContainer.BasicACL(), acl.PublicBasicRule)
newNonce, err := newContainer.NonceUUID() newNonce, err := newContainer.NonceUUID()
@ -45,36 +46,11 @@ func TestNewContainer(t *testing.T) {
require.EqualValues(t, newNonce, nonce) require.EqualValues(t, newNonce, nonce)
require.EqualValues(t, newContainer.OwnerID(), ownerID) require.EqualValues(t, newContainer.OwnerID(), ownerID)
require.EqualValues(t, newContainer.Version(), pkg.SDKVersion()) require.EqualValues(t, newContainer.Version(), ver)
}
func generateAttributes(n int) container.Attributes {
attrs := make(container.Attributes, 0, n)
for i := 0; i < n; i++ {
strN := strconv.Itoa(n)
attr := container.NewAttribute()
attr.SetKey("key" + strN)
attr.SetValue("val" + strN)
attrs = append(attrs, attr)
}
return attrs
}
func generatePlacementPolicy() *netmap.PlacementPolicy {
p := new(netmap.PlacementPolicy)
p.SetContainerBackupFactor(10)
return p
} }
func TestContainerEncoding(t *testing.T) { func TestContainerEncoding(t *testing.T) {
c := container.New( c := containertest.Container()
container.WithAttribute("key", "value"),
)
t.Run("binary", func(t *testing.T) { t.Run("binary", func(t *testing.T) {
data, err := c.Marshal() data, err := c.Marshal()
@ -108,9 +84,7 @@ func TestContainer_SessionToken(t *testing.T) {
} }
func TestContainer_Signature(t *testing.T) { func TestContainer_Signature(t *testing.T) {
sig := pkg.NewSignature() sig := refstest.Signature()
sig.SetKey([]byte{1, 2, 3})
sig.SetSign([]byte{4, 5, 6})
cnr := container.New() cnr := container.New()
cnr.SetSignature(sig) cnr.SetSignature(sig)

View file

@ -0,0 +1,48 @@
package containertest
import (
"github.com/nspcc-dev/neofs-api-go/pkg/container"
cidtest "github.com/nspcc-dev/neofs-api-go/pkg/container/id/test"
netmaptest "github.com/nspcc-dev/neofs-api-go/pkg/netmap/test"
ownertest "github.com/nspcc-dev/neofs-api-go/pkg/owner/test"
refstest "github.com/nspcc-dev/neofs-api-go/pkg/test"
)
// Attribute returns random container.Attribute.
func Attribute() *container.Attribute {
x := container.NewAttribute()
x.SetKey("key")
x.SetValue("value")
return x
}
// Attributes returns random container.Attributes.
func Attributes() container.Attributes {
return container.Attributes{Attribute(), Attribute()}
}
// Container returns random container.Container.
func Container() *container.Container {
x := container.New()
x.SetVersion(refstest.Version())
x.SetAttributes(Attributes())
x.SetOwnerID(ownertest.Generate())
x.SetBasicACL(123)
x.SetPlacementPolicy(netmaptest.PlacementPolicy())
return x
}
// UsedSpaceAnnouncement returns random container.UsedSpaceAnnouncement.
func UsedSpaceAnnouncement() *container.UsedSpaceAnnouncement {
x := container.NewAnnouncement()
x.SetContainerID(cidtest.Generate())
x.SetEpoch(55)
x.SetUsedSpace(999)
return x
}