From 122a31cadbdf8a21b842d48e48fa42df586541c8 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Tue, 8 Jun 2021 14:38:38 +0300 Subject: [PATCH] [#293] pkg/container: Implement and use generators of the messages Signed-off-by: Leonard Lyubich --- pkg/container/announcement_test.go | 9 +---- pkg/container/container_test.go | 60 +++++++++--------------------- pkg/container/test/generate.go | 48 ++++++++++++++++++++++++ 3 files changed, 67 insertions(+), 50 deletions(-) create mode 100644 pkg/container/test/generate.go diff --git a/pkg/container/announcement_test.go b/pkg/container/announcement_test.go index 9345c28..26164bb 100644 --- a/pkg/container/announcement_test.go +++ b/pkg/container/announcement_test.go @@ -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() diff --git a/pkg/container/container_test.go b/pkg/container/container_test.go index 4792db0..1456b0e 100644 --- a/pkg/container/container_test.go +++ b/pkg/container/container_test.go @@ -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) diff --git a/pkg/container/test/generate.go b/pkg/container/test/generate.go new file mode 100644 index 0000000..0f5f9b0 --- /dev/null +++ b/pkg/container/test/generate.go @@ -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 +}