frostfs-node/pkg/local_object_storage/shard/get_test.go
Dmitrii Stepanov 0b87be804a
All checks were successful
DCO action / DCO (pull_request) Successful in 1m35s
Tests and linters / Run gofumpt (pull_request) Successful in 1m37s
Pre-commit hooks / Pre-commit (pull_request) Successful in 2m40s
Vulncheck / Vulncheck (pull_request) Successful in 2m35s
Tests and linters / Staticcheck (pull_request) Successful in 2m52s
Build / Build Components (pull_request) Successful in 3m33s
Tests and linters / gopls check (pull_request) Successful in 3m35s
Tests and linters / Lint (pull_request) Successful in 4m44s
Tests and linters / Tests (pull_request) Successful in 5m14s
Tests and linters / Tests with -race (pull_request) Successful in 6m1s
[#1381] engine: Fix tests
Drop not required `Eventually` calls.

Signed-off-by: Dmitrii Stepanov <d.stepanov@yadro.com>
2024-09-23 11:10:29 +03:00

130 lines
3.2 KiB
Go

package shard
import (
"bytes"
"context"
"errors"
"testing"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/core/object"
"git.frostfs.info/TrueCloudLab/frostfs-node/pkg/local_object_storage/internal/testutil"
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"
"github.com/stretchr/testify/require"
)
func TestShard_Get(t *testing.T) {
t.Parallel()
t.Run("without write cache", func(t *testing.T) {
t.Parallel()
testShardGet(t, false)
})
t.Run("with write cache", func(t *testing.T) {
t.Parallel()
testShardGet(t, true)
})
}
func testShardGet(t *testing.T, hasWriteCache bool) {
sh := newShard(t, hasWriteCache)
defer func() { require.NoError(t, sh.Close()) }()
var putPrm PutPrm
var getPrm GetPrm
t.Run("small object", func(t *testing.T) {
obj := testutil.GenerateObject()
testutil.AddAttribute(obj, "foo", "bar")
testutil.AddPayload(obj, 1<<5)
putPrm.SetObject(obj)
_, err := sh.Put(context.Background(), putPrm)
require.NoError(t, err)
getPrm.SetAddress(object.AddressOf(obj))
res, err := sh.Get(context.Background(), getPrm)
require.NoError(t, err)
require.Equal(t, obj, res.Object())
})
t.Run("big object", func(t *testing.T) {
obj := testutil.GenerateObject()
testutil.AddAttribute(obj, "foo", "bar")
obj.SetID(oidtest.ID())
testutil.AddPayload(obj, 1<<20) // big obj
putPrm.SetObject(obj)
_, err := sh.Put(context.Background(), putPrm)
require.NoError(t, err)
getPrm.SetAddress(object.AddressOf(obj))
res, err := sh.Get(context.Background(), getPrm)
require.NoError(t, err)
require.Equal(t, obj, res.Object())
})
t.Run("parent object", func(t *testing.T) {
obj := testutil.GenerateObject()
testutil.AddAttribute(obj, "foo", "bar")
cnr := cidtest.ID()
splitID := objectSDK.NewSplitID()
parent := testutil.GenerateObjectWithCID(cnr)
testutil.AddAttribute(parent, "parent", "attribute")
child := testutil.GenerateObjectWithCID(cnr)
child.SetParent(parent)
idParent, _ := parent.ID()
child.SetParentID(idParent)
child.SetSplitID(splitID)
testutil.AddPayload(child, 1<<5)
putPrm.SetObject(child)
_, err := sh.Put(context.Background(), putPrm)
require.NoError(t, err)
getPrm.SetAddress(object.AddressOf(child))
res, err := sh.Get(context.Background(), getPrm)
require.NoError(t, err)
require.True(t, binaryEqual(child, res.Object()))
getPrm.SetAddress(object.AddressOf(parent))
_, err = sh.Get(context.Background(), getPrm)
var si *objectSDK.SplitInfoError
require.True(t, errors.As(err, &si))
_, ok := si.SplitInfo().Link()
require.False(t, ok)
id1, _ := child.ID()
id2, _ := si.SplitInfo().LastPart()
require.Equal(t, id1, id2)
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.
func binaryEqual(a, b *objectSDK.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)
}