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
|
|
|
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
|
|
|
|
"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"
|
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-11-02 07:55:35 +00:00
|
|
|
defer releaseDB(db)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
raw := generateRawObject(t)
|
|
|
|
|
|
|
|
// 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) {
|
2020-12-08 09:56:14 +00:00
|
|
|
_, err := meta.Get(db, raw.Object().Address())
|
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) {
|
2020-12-08 09:56:14 +00:00
|
|
|
err := putBig(db, raw.Object())
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
newObj, err := meta.Get(db, raw.Object().Address())
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, raw.Object(), newObj)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("put tombstone object", func(t *testing.T) {
|
|
|
|
raw.SetType(objectSDK.TypeTombstone)
|
|
|
|
raw.SetID(testOID())
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
err := putBig(db, raw.Object())
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
newObj, err := meta.Get(db, raw.Object().Address())
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, raw.Object(), newObj)
|
|
|
|
})
|
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
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
err := putBig(db, raw.Object())
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
newObj, err := meta.Get(db, raw.Object().Address())
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.Equal(t, raw.Object(), newObj)
|
2020-10-30 13:42:24 +00:00
|
|
|
})
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
t.Run("put virtual object", func(t *testing.T) {
|
|
|
|
cid := testCID()
|
2020-12-08 11:23:23 +00:00
|
|
|
splitID := objectSDK.NewSplitID()
|
|
|
|
|
2020-12-08 07:51:34 +00:00
|
|
|
parent := generateRawObjectWithCID(t, cid)
|
|
|
|
addAttribute(parent, "foo", "bar")
|
|
|
|
|
|
|
|
child := generateRawObjectWithCID(t, cid)
|
|
|
|
child.SetParent(parent.Object().SDK())
|
|
|
|
child.SetParentID(parent.ID())
|
2020-12-08 11:23:23 +00:00
|
|
|
child.SetSplitID(splitID)
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 09:56:14 +00:00
|
|
|
err := putBig(db, child.Object())
|
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) {
|
|
|
|
_, err = meta.GetRaw(db, parent.Object().Address(), true)
|
|
|
|
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())
|
|
|
|
})
|
|
|
|
|
|
|
|
newParent, err := meta.GetRaw(db, parent.Object().Address(), false)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, binaryEqual(parent.Object(), newParent))
|
2020-10-30 13:42:24 +00:00
|
|
|
|
2020-12-08 11:23:23 +00:00
|
|
|
newChild, err := meta.GetRaw(db, child.Object().Address(), true)
|
2020-12-08 07:51:34 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, binaryEqual(child.Object(), newChild))
|
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.
|
|
|
|
func binaryEqual(a, b *object.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)
|
|
|
|
}
|