2020-11-23 13:30:56 +00:00
|
|
|
package meta_test
|
|
|
|
|
|
|
|
import (
|
2020-12-03 09:31:51 +00:00
|
|
|
"errors"
|
2020-11-23 13:30:56 +00:00
|
|
|
"testing"
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
"github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-07-01 11:21:33 +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"
|
2022-07-01 11:21:33 +00:00
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
2020-11-23 13:30:56 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDB_Exists(t *testing.T) {
|
|
|
|
db := newDB(t)
|
|
|
|
|
2020-11-27 07:59:47 +00:00
|
|
|
t.Run("no object", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
nonExist := generateObject(t)
|
2022-07-12 14:42:55 +00:00
|
|
|
exists, err := metaExists(db, object.AddressOf(nonExist))
|
2020-11-27 07:59:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, exists)
|
|
|
|
})
|
2020-11-23 13:30:56 +00:00
|
|
|
|
2020-11-27 07:59:47 +00:00
|
|
|
t.Run("regular object", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
regular := generateObject(t)
|
|
|
|
err := putBig(db, regular)
|
2020-11-27 07:59:47 +00:00
|
|
|
require.NoError(t, err)
|
2020-11-23 13:30:56 +00:00
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
exists, err := metaExists(db, object.AddressOf(regular))
|
2020-11-27 07:59:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, exists)
|
2022-07-01 11:21:33 +00:00
|
|
|
|
|
|
|
t.Run("removed object", func(t *testing.T) {
|
|
|
|
err := metaInhume(db, object.AddressOf(regular), oidtest.Address())
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
exists, err := metaExists(db, object.AddressOf(regular))
|
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectAlreadyRemoved))
|
|
|
|
require.False(t, exists)
|
|
|
|
})
|
2020-11-27 07:59:47 +00:00
|
|
|
})
|
2020-11-23 13:30:56 +00:00
|
|
|
|
2020-11-27 07:59:47 +00:00
|
|
|
t.Run("tombstone object", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
ts := generateObject(t)
|
2020-11-27 07:59:47 +00:00
|
|
|
ts.SetType(objectSDK.TypeTombstone)
|
2020-11-23 13:30:56 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, ts)
|
2020-11-27 07:59:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
exists, err := metaExists(db, object.AddressOf(ts))
|
2020-11-27 07:59:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, exists)
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("storage group object", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
sg := generateObject(t)
|
2020-11-27 07:59:47 +00:00
|
|
|
sg.SetType(objectSDK.TypeStorageGroup)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, sg)
|
2020-11-27 07:59:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
exists, err := metaExists(db, object.AddressOf(sg))
|
2020-11-27 07:59:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, exists)
|
|
|
|
})
|
|
|
|
|
2022-02-15 12:51:56 +00:00
|
|
|
t.Run("lock object", func(t *testing.T) {
|
|
|
|
lock := generateObject(t)
|
|
|
|
lock.SetType(objectSDK.TypeLock)
|
|
|
|
|
|
|
|
err := putBig(db, lock)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
exists, err := metaExists(db, object.AddressOf(lock))
|
2022-02-15 12:51:56 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
require.True(t, exists)
|
|
|
|
})
|
|
|
|
|
2020-11-27 07:59:47 +00:00
|
|
|
t.Run("virtual object", func(t *testing.T) {
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := cidtest.ID()
|
|
|
|
parent := generateObjectWithCID(t, cnr)
|
2020-11-27 07:59:47 +00:00
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
child := generateObjectWithCID(t, 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-11-27 07:59:47 +00:00
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, child)
|
2020-11-27 07:59:47 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
_, err = metaExists(db, object.AddressOf(parent))
|
2020-12-03 09:31:51 +00:00
|
|
|
|
|
|
|
var expectedErr *objectSDK.SplitInfoError
|
|
|
|
require.True(t, errors.As(err, &expectedErr))
|
2020-11-27 07:59:47 +00:00
|
|
|
})
|
2020-12-04 09:01:41 +00:00
|
|
|
|
|
|
|
t.Run("merge split info", func(t *testing.T) {
|
2022-05-31 17:00:41 +00:00
|
|
|
cnr := cidtest.ID()
|
2020-12-04 09:01:41 +00:00
|
|
|
splitID := objectSDK.NewSplitID()
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
parent := generateObjectWithCID(t, cnr)
|
2020-12-04 09:01:41 +00:00
|
|
|
addAttribute(parent, "foo", "bar")
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
child := generateObjectWithCID(t, 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-04 09:01:41 +00:00
|
|
|
child.SetSplitID(splitID)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
link := generateObjectWithCID(t, cnr)
|
2022-03-03 14:19:05 +00:00
|
|
|
link.SetParent(parent)
|
2022-05-12 16:37:46 +00:00
|
|
|
link.SetParentID(idParent)
|
|
|
|
idChild, _ := child.ID()
|
|
|
|
link.SetChildren(idChild)
|
2020-12-04 09:01:41 +00:00
|
|
|
link.SetSplitID(splitID)
|
|
|
|
|
|
|
|
t.Run("direct order", func(t *testing.T) {
|
2022-03-03 14:19:05 +00:00
|
|
|
err := putBig(db, child)
|
2020-12-04 09:01:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err = putBig(db, link)
|
2020-12-04 09:01:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
_, err = metaExists(db, object.AddressOf(parent))
|
2020-12-04 09:01:41 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
si, ok := err.(*objectSDK.SplitInfoError)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, splitID, si.SplitInfo().SplitID())
|
2022-05-12 16:37:46 +00:00
|
|
|
|
|
|
|
id1, _ := child.ID()
|
|
|
|
id2, _ := si.SplitInfo().LastPart()
|
|
|
|
require.Equal(t, id1, id2)
|
|
|
|
|
|
|
|
id1, _ = link.ID()
|
|
|
|
id2, _ = si.SplitInfo().Link()
|
|
|
|
require.Equal(t, id1, id2)
|
2020-12-04 09:01:41 +00:00
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("reverse order", func(t *testing.T) {
|
2022-07-12 14:42:55 +00:00
|
|
|
err := metaPut(db, link, nil)
|
2020-12-04 09:01:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-03-03 14:19:05 +00:00
|
|
|
err = putBig(db, child)
|
2020-12-04 09:01:41 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-07-12 14:42:55 +00:00
|
|
|
_, err = metaExists(db, object.AddressOf(parent))
|
2020-12-04 09:01:41 +00:00
|
|
|
require.Error(t, err)
|
|
|
|
|
|
|
|
si, ok := err.(*objectSDK.SplitInfoError)
|
|
|
|
require.True(t, ok)
|
|
|
|
|
|
|
|
require.Equal(t, splitID, si.SplitInfo().SplitID())
|
2022-05-12 16:37:46 +00:00
|
|
|
|
|
|
|
id1, _ := child.ID()
|
|
|
|
id2, _ := si.SplitInfo().LastPart()
|
|
|
|
require.Equal(t, id1, id2)
|
|
|
|
|
|
|
|
id1, _ = link.ID()
|
|
|
|
id2, _ = si.SplitInfo().Link()
|
|
|
|
require.Equal(t, id1, id2)
|
2020-12-04 09:01:41 +00:00
|
|
|
})
|
|
|
|
})
|
2022-07-01 11:21:33 +00:00
|
|
|
|
|
|
|
t.Run("random object", func(t *testing.T) {
|
|
|
|
addr := oidtest.Address()
|
|
|
|
|
|
|
|
exists, err := metaExists(db, addr)
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.False(t, exists)
|
|
|
|
})
|
2020-11-23 13:30:56 +00:00
|
|
|
}
|