2023-08-31 12:47:06 +00:00
|
|
|
package shard
|
2020-12-01 13:58:34 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-03 09:50:20 +00:00
|
|
|
"bytes"
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
2020-12-03 09:50:20 +00:00
|
|
|
"errors"
|
2020-12-01 13:58:34 +00:00
|
|
|
"testing"
|
|
|
|
|
2023-03-07 13:38:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
|
2023-03-20 14:10:26 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil"
|
2023-03-07 13:38:26 +00:00
|
|
|
cidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/container/id/test"
|
|
|
|
objectSDK "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oidtest "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id/test"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShard_Get(t *testing.T) {
|
2023-05-05 11:08:10 +00:00
|
|
|
t.Parallel()
|
|
|
|
|
2020-12-01 13:58:34 +00:00
|
|
|
t.Run("without write cache", func(t *testing.T) {
|
2023-05-05 11:08:10 +00:00
|
|
|
t.Parallel()
|
2021-04-06 10:56:06 +00:00
|
|
|
testShardGet(t, false)
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("with write cache", func(t *testing.T) {
|
2023-05-05 11:08:10 +00:00
|
|
|
t.Parallel()
|
2021-04-06 10:56:06 +00:00
|
|
|
testShardGet(t, true)
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
|
|
|
}
|
|
|
|
|
2021-04-06 10:56:06 +00:00
|
|
|
func testShardGet(t *testing.T, hasWriteCache bool) {
|
|
|
|
sh := newShard(t, hasWriteCache)
|
2024-10-21 08:56:38 +00:00
|
|
|
defer func() { require.NoError(t, sh.Close(context.Background())) }()
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
var putPrm PutPrm
|
|
|
|
var getPrm GetPrm
|
2020-12-01 13:58:34 +00:00
|
|
|
|
|
|
|
t.Run("small object", func(t *testing.T) {
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObject()
|
|
|
|
testutil.AddAttribute(obj, "foo", "bar")
|
|
|
|
testutil.AddPayload(obj, 1<<5)
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(obj)
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := sh.Put(context.Background(), putPrm)
|
2020-12-01 13:58:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
getPrm.SetAddress(object.AddressOf(obj))
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2024-09-17 08:24:48 +00:00
|
|
|
res, err := sh.Get(context.Background(), getPrm)
|
2020-12-01 13:58:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, obj, res.Object())
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("big object", func(t *testing.T) {
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObject()
|
|
|
|
testutil.AddAttribute(obj, "foo", "bar")
|
2022-05-12 16:37:46 +00:00
|
|
|
obj.SetID(oidtest.ID())
|
2023-03-20 14:10:26 +00:00
|
|
|
testutil.AddPayload(obj, 1<<20) // big obj
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(obj)
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := sh.Put(context.Background(), putPrm)
|
2020-12-01 13:58:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
getPrm.SetAddress(object.AddressOf(obj))
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2024-09-17 08:24:48 +00:00
|
|
|
res, err := sh.Get(context.Background(), getPrm)
|
2020-12-01 13:58:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, obj, res.Object())
|
2020-12-01 13:58:34 +00:00
|
|
|
})
|
2020-12-03 09:50:20 +00:00
|
|
|
|
|
|
|
t.Run("parent object", func(t *testing.T) {
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObject()
|
|
|
|
testutil.AddAttribute(obj, "foo", "bar")
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr := cidtest.ID()
|
2020-12-03 09:50:20 +00:00
|
|
|
splitID := objectSDK.NewSplitID()
|
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
parent := testutil.GenerateObjectWithCID(cnr)
|
|
|
|
testutil.AddAttribute(parent, "parent", "attribute")
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
child := testutil.GenerateObjectWithCID(cnr)
|
2022-03-03 14:19:05 +00:00
|
|
|
child.SetParent(parent)
|
2022-05-12 16:37:46 +00:00
|
|
|
idParent, _ := parent.ID()
|
|
|
|
child.SetParentID(idParent)
|
2020-12-03 09:50:20 +00:00
|
|
|
child.SetSplitID(splitID)
|
2023-03-20 14:10:26 +00:00
|
|
|
testutil.AddPayload(child, 1<<5)
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(child)
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := sh.Put(context.Background(), putPrm)
|
2020-12-03 09:50:20 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
getPrm.SetAddress(object.AddressOf(child))
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2024-09-17 08:24:48 +00:00
|
|
|
res, err := sh.Get(context.Background(), getPrm)
|
2020-12-03 09:50:20 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.True(t, binaryEqual(child, res.Object()))
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
getPrm.SetAddress(object.AddressOf(parent))
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2024-09-17 08:24:48 +00:00
|
|
|
_, err = sh.Get(context.Background(), getPrm)
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2022-10-26 12:23:12 +00:00
|
|
|
var si *objectSDK.SplitInfoError
|
|
|
|
require.True(t, errors.As(err, &si))
|
2020-12-03 09:50:20 +00:00
|
|
|
|
2022-10-26 12:23:12 +00:00
|
|
|
_, ok := si.SplitInfo().Link()
|
2022-05-12 16:37:46 +00:00
|
|
|
require.False(t, ok)
|
|
|
|
id1, _ := child.ID()
|
|
|
|
id2, _ := si.SplitInfo().LastPart()
|
|
|
|
require.Equal(t, id1, id2)
|
2020-12-03 09:50:20 +00:00
|
|
|
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 <nil> and []{} lists.
|
2022-03-03 14:19:05 +00:00
|
|
|
func binaryEqual(a, b *objectSDK.Object) bool {
|
2020-12-03 09:50:20 +00:00
|
|
|
binaryA, err := a.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
binaryB, err := b.Marshal()
|
|
|
|
if err != nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
return bytes.Equal(binaryA, binaryB)
|
2020-12-01 13:58:34 +00:00
|
|
|
}
|