2023-08-31 12:47:06 +00:00
|
|
|
package shard
|
2020-12-01 13:58:34 +00:00
|
|
|
|
|
|
|
import (
|
2023-03-13 11:37:35 +00:00
|
|
|
"context"
|
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
|
|
|
|
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-08-04 11:14:07 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/client"
|
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"
|
2020-12-01 13:58:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestShard_Head(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
|
|
|
testShardHead(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
|
|
|
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)
|
2024-01-09 13:26:43 +00:00
|
|
|
defer func() { require.NoError(t, sh.Close()) }()
|
2021-04-06 10:56:06 +00:00
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
var putPrm PutPrm
|
|
|
|
var headPrm 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) {
|
2023-03-20 14:10:26 +00:00
|
|
|
obj := testutil.GenerateObject()
|
|
|
|
testutil.AddAttribute(obj, "foo", "bar")
|
2020-12-01 13:58:34 +00:00
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(obj)
|
2020-12-08 11:42:44 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := sh.Put(context.Background(), putPrm)
|
2020-12-08 11:42:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
headPrm.SetAddress(object.AddressOf(obj))
|
2020-12-08 11:42:44 +00:00
|
|
|
|
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-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, obj.CutPayload(), res.Object())
|
2020-12-08 11:42:44 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("virtual object", func(t *testing.T) {
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := cidtest.ID()
|
2020-12-08 11:42:44 +00:00
|
|
|
splitID := objectSDK.NewSplitID()
|
|
|
|
|
2023-03-20 14:10:26 +00:00
|
|
|
parent := testutil.GenerateObjectWithCID(cnr)
|
|
|
|
testutil.AddAttribute(parent, "foo", "bar")
|
2020-12-01 13:58:34 +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-08 11:42:44 +00:00
|
|
|
child.SetSplitID(splitID)
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
putPrm.SetObject(child)
|
2020-12-08 11:42:44 +00:00
|
|
|
|
2023-04-12 14:01:29 +00:00
|
|
|
_, err := sh.Put(context.Background(), putPrm)
|
2020-12-08 11:42:44 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
headPrm.SetAddress(object.AddressOf(parent))
|
|
|
|
headPrm.SetRaw(true)
|
2020-12-08 11:42:44 +00:00
|
|
|
|
|
|
|
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))
|
|
|
|
|
2022-07-13 12:43:04 +00:00
|
|
|
headPrm.SetAddress(object.AddressOf(parent))
|
|
|
|
headPrm.SetRaw(false)
|
2020-12-08 11:42:44 +00:00
|
|
|
|
2023-03-13 11:37:35 +00:00
|
|
|
head, err := sh.Head(context.Background(), headPrm)
|
2020-12-08 11:42:44 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, parent.CutPayload(), 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
|
|
|
|
2023-08-31 12:47:06 +00:00
|
|
|
func testHead(t *testing.T, sh *Shard, headPrm HeadPrm, hasWriteCache bool) (HeadRes, error) {
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err := sh.Head(context.Background(), headPrm)
|
2021-04-06 10:56:06 +00:00
|
|
|
if hasWriteCache {
|
|
|
|
require.Eventually(t, func() bool {
|
2023-08-04 11:14:07 +00:00
|
|
|
if client.IsErrObjectNotFound(err) {
|
2023-03-13 11:37:35 +00:00
|
|
|
res, err = sh.Head(context.Background(), headPrm)
|
2021-04-06 10:56:06 +00:00
|
|
|
}
|
2023-08-04 11:14:07 +00:00
|
|
|
return !client.IsErrObjectNotFound(err)
|
2021-04-06 10:56:06 +00:00
|
|
|
}, time.Second, time.Millisecond*100)
|
|
|
|
}
|
|
|
|
return res, err
|
|
|
|
}
|