2020-12-08 07:51:34 +00:00
|
|
|
package meta_test
|
2020-10-30 13:42:24 +00:00
|
|
|
|
|
|
|
import (
|
2020-12-08 07:51:34 +00:00
|
|
|
"bytes"
|
2020-10-30 13:42:24 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2020-12-08 09:56:14 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-03-17 08:03:58 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
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-10-30 13:42:24 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
func TestDB_Get(t *testing.T) {
|
|
|
|
db := newDB(t)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
raw := generateObject(t)
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// equal fails on diff of <nil> attributes and <{}> attributes,
|
|
|
|
/* so we make non empty attribute slice in parent*/
|
|
|
|
addAttribute(raw, "foo", "bar")
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("object not found", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
_, err := meta.Get(db, object.AddressOf(raw))
|
2020-12-08 07:51:34 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
})
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("put regular object", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, raw)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
newObj, err := meta.Get(db, object.AddressOf(raw))
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, raw.CutPayload(), newObj)
|
2020-12-08 07:51:34 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("put tombstone object", func(t *testing.T) {
|
|
|
|
raw.SetType(objectSDK.TypeTombstone)
|
|
|
|
raw.SetID(testOID())
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, raw)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
newObj, err := meta.Get(db, object.AddressOf(raw))
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, raw.CutPayload(), newObj)
|
2020-12-08 07:51:34 +00:00
|
|
|
})
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("put storage group object", func(t *testing.T) {
|
|
|
|
raw.SetType(objectSDK.TypeStorageGroup)
|
|
|
|
raw.SetID(testOID())
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, raw)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
newObj, err := meta.Get(db, object.AddressOf(raw))
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.Equal(t, raw.CutPayload(), newObj)
|
2020-10-30 13:42:24 +00:00
|
|
|
})
|
|
|
|
|
2022-02-15 12:51:56 +00:00
|
|
|
t.Run("put lock object", func(t *testing.T) {
|
|
|
|
raw.SetType(objectSDK.TypeLock)
|
|
|
|
raw.SetID(testOID())
|
|
|
|
|
2022-03-05 08:49:19 +00:00
|
|
|
err := putBig(db, raw)
|
2022-02-15 12:51:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
newObj, err := meta.Get(db, object.AddressOf(raw))
|
|
|
|
require.NoError(t, err)
|
2022-03-05 08:49:19 +00:00
|
|
|
require.Equal(t, raw.CutPayload(), newObj)
|
2022-02-15 12:51:56 +00:00
|
|
|
})
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("put virtual object", func(t *testing.T) {
|
2021-12-01 13:56:48 +00:00
|
|
|
cid := cidtest.ID()
|
2020-12-08 11:23:23 +00:00
|
|
|
splitID := objectSDK.NewSplitID()
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
parent := generateObjectWithCID(t, cid)
|
2020-12-08 07:51:34 +00:00
|
|
|
addAttribute(parent, "foo", "bar")
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
child := generateObjectWithCID(t, cid)
|
|
|
|
child.SetParent(parent)
|
2020-12-08 07:51:34 +00:00
|
|
|
child.SetParentID(parent.ID())
|
2020-12-08 11:23:23 +00:00
|
|
|
child.SetSplitID(splitID)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, child)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
t.Run("raw is true", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
_, err = meta.GetRaw(db, object.AddressOf(parent), true)
|
2020-12-08 11:23:23 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
siErr, ok := err.(*objectSDK.SplitInfoError)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, splitID, siErr.SplitInfo().SplitID())
|
|
|
|
require.Equal(t, child.ID(), siErr.SplitInfo().LastPart())
|
|
|
|
require.Nil(t, siErr.SplitInfo().Link())
|
|
|
|
})
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
newParent, err := meta.GetRaw(db, object.AddressOf(parent), false)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.True(t, binaryEqual(parent.CutPayload(), newParent))
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
newChild, err := meta.GetRaw(db, object.AddressOf(child), true)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2022-03-03 14:19:05 +00:00
|
|
|
require.True(t, binaryEqual(child.CutPayload(), newChild))
|
2020-10-30 13:42:24 +00:00
|
|
|
})
|
2021-09-24 15:18:04 +00:00
|
|
|
|
|
|
|
t.Run("get removed object", func(t *testing.T) {
|
|
|
|
obj := generateAddress()
|
|
|
|
ts := generateAddress()
|
|
|
|
|
|
|
|
require.NoError(t, meta.Inhume(db, obj, ts))
|
|
|
|
_, err := meta.Get(db, obj)
|
2022-03-17 08:03:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectAlreadyRemoved))
|
2021-09-24 15:18:04 +00:00
|
|
|
|
|
|
|
obj = generateAddress()
|
|
|
|
require.NoError(t, meta.Inhume(db, obj, nil))
|
|
|
|
_, err = meta.Get(db, obj)
|
2022-03-17 08:03:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectNotFound))
|
2021-09-24 15:18:04 +00:00
|
|
|
})
|
2020-10-30 13:42:24 +00:00
|
|
|
}
|
2020-12-08 07:51:34 +00:00
|
|
|
|
|
|
|
// 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-08 07:51:34 +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)
|
|
|
|
}
|