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

View file

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