From 7c97bd13602dd755a3c68bde7da9e5f604c2ec71 Mon Sep 17 00:00:00 2001 From: Alex Vanin Date: Thu, 3 Dec 2020 12:50:20 +0300 Subject: [PATCH] [#230] shard: Add test for getting SplitInfo Signed-off-by: Alex Vanin --- pkg/local_object_storage/shard/get_test.go | 58 ++++++++++++++++++++++ pkg/local_object_storage/shard/shard.go | 2 +- 2 files changed, 59 insertions(+), 1 deletion(-) diff --git a/pkg/local_object_storage/shard/get_test.go b/pkg/local_object_storage/shard/get_test.go index a6839381a..122d2a80e 100644 --- a/pkg/local_object_storage/shard/get_test.go +++ b/pkg/local_object_storage/shard/get_test.go @@ -1,8 +1,12 @@ package shard_test import ( + "bytes" + "errors" "testing" + objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" + "github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard" "github.com/stretchr/testify/require" ) @@ -62,4 +66,58 @@ func testShardGet(t *testing.T, sh *shard.Shard) { require.NoError(t, err) require.Equal(t, obj.Object(), res.Object()) }) + + t.Run("parent object", func(t *testing.T) { + cid := generateCID() + splitID := objectSDK.NewSplitID() + + parent := generateRawObjectWithCID(t, cid) + addAttribute(parent, "parent", "attribute") + + child := generateRawObjectWithCID(t, cid) + child.SetParent(parent.Object().SDK()) + child.SetParentID(parent.ID()) + child.SetSplitID(splitID) + addPayload(child, 1<<5) + + putPrm.WithObject(child.Object()) + + _, err := sh.Put(putPrm) + require.NoError(t, err) + + getPrm.WithAddress(child.Object().Address()) + + res, err := sh.Get(getPrm) + require.NoError(t, err) + require.True(t, binaryEqual(child.Object(), res.Object())) + + getPrm.WithAddress(parent.Object().Address()) + + _, err = sh.Get(getPrm) + + var expectedErr *objectSDK.SplitInfoError + require.True(t, errors.As(err, &expectedErr)) + + si, ok := err.(*objectSDK.SplitInfoError) + require.True(t, ok) + require.Nil(t, si.SplitInfo().Link()) + require.Equal(t, child.ID(), si.SplitInfo().LastPart()) + require.Equal(t, splitID, si.SplitInfo().SplitID()) + }) +} + +// binary equal is used when object contains empty lists in the structure and +// requre.Equal fails on comparing and []{} lists. +func binaryEqual(a, b *object.Object) bool { + binaryA, err := a.Marshal() + if err != nil { + return false + } + + binaryB, err := b.Marshal() + if err != nil { + return false + } + + return bytes.Equal(binaryA, binaryB) } diff --git a/pkg/local_object_storage/shard/shard.go b/pkg/local_object_storage/shard/shard.go index 5496a7bf7..2579117f7 100644 --- a/pkg/local_object_storage/shard/shard.go +++ b/pkg/local_object_storage/shard/shard.go @@ -108,7 +108,7 @@ func WithWriteCache(use bool) Option { } } -// hasWriteCache returns bool if write cache exists on shars. +// hasWriteCache returns bool if write cache exists on shards. func (s Shard) hasWriteCache() bool { return s.cfg.useWriteCache }