2020-12-01 13:58:34 +00:00
|
|
|
package shard_test
|
|
|
|
|
|
|
|
import (
|
2020-12-08 11:42:44 +00:00
|
|
|
"errors"
|
2020-12-01 13:58:34 +00:00
|
|
|
"testing"
|
2021-04-06 10:56:06 +00:00
|
|
|
"time"
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/local_object_storage/shard"
|
2021-11-10 07:08:33 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
objectSDK "github.com/nspcc-dev/neofs-sdk-go/object"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShard_Head(t *testing.T) {
|
|
|
|
t.Run("without write cache", func(t *testing.T) {
|
2021-04-06 10:56:06 +00:00
|
|
|
testShardHead(t, false)
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with write cache", func(t *testing.T) {
|
2021-04-06 10:56:06 +00:00
|
|
|
testShardHead(t, true)
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
func testShardHead(t *testing.T, hasWriteCache bool) {
|
|
|
|
sh := newShard(t, hasWriteCache)
|
|
|
|
defer releaseShard(sh, t)
|
|
|
|
|
2020-12-01 13:58:34 +00:00
|
|
|
putPrm := new(shard.PutPrm)
|
2020-12-08 11:42:44 +00:00
|
|
|
headPrm := new(shard.HeadPrm)
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2020-12-08 11:42:44 +00:00
|
|
|
t.Run("regular object", func(t *testing.T) {
|
|
|
|
obj := generateRawObject(t)
|
|
|
|
addAttribute(obj, "foo", "bar")
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2020-12-08 11:42:44 +00:00
|
|
|
putPrm.WithObject(obj.Object())
|
|
|
|
|
|
|
|
_, err := sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
headPrm.WithAddress(obj.Object().Address())
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
res, err := testHead(t, sh, headPrm, hasWriteCache)
|
2020-12-08 11:42:44 +00:00
|
|
|
require.NoError(t, err)
|
2022-01-18 13:40:11 +00:00
|
|
|
require.Equal(t, obj.CutPayload().Object(), res.Object())
|
2020-12-08 11:42:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("virtual object", func(t *testing.T) {
|
2021-12-01 13:56:48 +00:00
|
|
|
cid := cidtest.ID()
|
2020-12-08 11:42:44 +00:00
|
|
|
splitID := objectSDK.NewSplitID()
|
|
|
|
|
|
|
|
parent := generateRawObjectWithCID(t, cid)
|
|
|
|
addAttribute(parent, "foo", "bar")
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2020-12-08 11:42:44 +00:00
|
|
|
child := generateRawObjectWithCID(t, cid)
|
|
|
|
child.SetParent(parent.Object().SDK())
|
|
|
|
child.SetParentID(parent.ID())
|
|
|
|
child.SetSplitID(splitID)
|
|
|
|
|
|
|
|
putPrm.WithObject(child.Object())
|
|
|
|
|
|
|
|
_, err := sh.Put(putPrm)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
headPrm.WithAddress(parent.Object().Address())
|
|
|
|
headPrm.WithRaw(true)
|
|
|
|
|
|
|
|
var siErr *objectSDK.SplitInfoError
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
_, err = testHead(t, sh, headPrm, hasWriteCache)
|
2020-12-08 11:42:44 +00:00
|
|
|
require.True(t, errors.As(err, &siErr))
|
|
|
|
|
|
|
|
headPrm.WithAddress(parent.Object().Address())
|
|
|
|
headPrm.WithRaw(false)
|
|
|
|
|
|
|
|
head, err := sh.Head(headPrm)
|
|
|
|
require.NoError(t, err)
|
2022-01-18 13:40:11 +00:00
|
|
|
require.Equal(t, parent.CutPayload().Object(), head.Object())
|
2020-12-08 11:42:44 +00:00
|
|
|
})
|
2020-12-01 13:58:34 +00:00
|
|
|
}
|
2021-04-06 10:56:06 +00:00
|
|
|
|
|
|
|
func testHead(t *testing.T, sh *shard.Shard, headPrm *shard.HeadPrm, hasWriteCache bool) (*shard.HeadRes, error) {
|
|
|
|
res, err := sh.Head(headPrm)
|
|
|
|
if hasWriteCache {
|
|
|
|
require.Eventually(t, func() bool {
|
|
|
|
if errors.Is(err, object.ErrNotFound) {
|
|
|
|
res, err = sh.Head(headPrm)
|
|
|
|
}
|
|
|
|
return !errors.Is(err, object.ErrNotFound)
|
|
|
|
}, time.Second, time.Millisecond*100)
|
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|