2022-02-15 11:35:13 +00:00
|
|
|
package meta_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
2022-03-10 17:58:58 +00:00
|
|
|
objectcore "github.com/nspcc-dev/neofs-node/pkg/core/object"
|
2022-02-15 11:35:13 +00:00
|
|
|
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase"
|
2022-02-15 22:17:40 +00:00
|
|
|
apistatus "github.com/nspcc-dev/neofs-sdk-go/client/status"
|
2022-02-15 11:35:13 +00:00
|
|
|
cidtest "github.com/nspcc-dev/neofs-sdk-go/container/id/test"
|
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/object"
|
|
|
|
oid "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
|
|
|
oidtest "github.com/nspcc-dev/neofs-sdk-go/object/id/test"
|
|
|
|
objecttest "github.com/nspcc-dev/neofs-sdk-go/object/test"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestDB_Lock(t *testing.T) {
|
2022-05-12 16:37:46 +00:00
|
|
|
cnr := cidtest.ID()
|
2022-02-15 11:35:13 +00:00
|
|
|
db := newDB(t)
|
|
|
|
|
|
|
|
t.Run("empty locked list", func(t *testing.T) {
|
|
|
|
require.Panics(t, func() { _ = db.Lock(cnr, oid.ID{}, nil) })
|
|
|
|
require.Panics(t, func() { _ = db.Lock(cnr, oid.ID{}, []oid.ID{}) })
|
|
|
|
})
|
|
|
|
|
|
|
|
t.Run("(ir)regular", func(t *testing.T) {
|
|
|
|
for _, typ := range [...]object.Type{
|
|
|
|
object.TypeTombstone,
|
|
|
|
object.TypeStorageGroup,
|
|
|
|
object.TypeLock,
|
|
|
|
object.TypeRegular,
|
|
|
|
} {
|
2022-03-05 08:49:19 +00:00
|
|
|
obj := objecttest.Object()
|
2022-02-15 11:35:13 +00:00
|
|
|
obj.SetType(typ)
|
2022-05-12 16:37:46 +00:00
|
|
|
obj.SetContainerID(cnr)
|
2022-02-15 11:35:13 +00:00
|
|
|
|
|
|
|
// save irregular object
|
2022-02-15 12:51:56 +00:00
|
|
|
err := meta.Put(db, obj, nil)
|
2022-02-15 11:35:13 +00:00
|
|
|
require.NoError(t, err, typ)
|
|
|
|
|
2022-02-16 15:49:19 +00:00
|
|
|
var e apistatus.LockNonRegularObject
|
2022-02-15 22:17:40 +00:00
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
id, _ := obj.ID()
|
|
|
|
|
2022-02-15 11:35:13 +00:00
|
|
|
// try to lock it
|
2022-05-12 16:37:46 +00:00
|
|
|
err = db.Lock(cnr, oidtest.ID(), []oid.ID{id})
|
2022-02-15 11:35:13 +00:00
|
|
|
if typ == object.TypeRegular {
|
|
|
|
require.NoError(t, err, typ)
|
|
|
|
} else {
|
2022-02-15 22:17:40 +00:00
|
|
|
require.ErrorAs(t, err, &e, typ)
|
2022-02-15 11:35:13 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
})
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
t.Run("lock-unlock scenario", func(t *testing.T) {
|
|
|
|
cnr := cidtest.ID()
|
|
|
|
|
|
|
|
obj := generateObjectWithCID(t, cnr)
|
|
|
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
err = putBig(db, obj)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
tombID := oidtest.ID()
|
|
|
|
|
|
|
|
id, _ := obj.ID()
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
// lock the object
|
2022-05-12 16:37:46 +00:00
|
|
|
err = db.Lock(cnr, tombID, []oid.ID{id})
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2022-05-31 17:00:41 +00:00
|
|
|
var tombAddr oid.Address
|
|
|
|
tombAddr.SetContainer(cnr)
|
|
|
|
tombAddr.SetObject(tombID)
|
2022-03-10 17:58:58 +00:00
|
|
|
|
|
|
|
// try to inhume locked object using tombstone
|
2022-05-31 17:00:41 +00:00
|
|
|
err = meta.Inhume(db, objectcore.AddressOf(obj), tombAddr)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.ErrorAs(t, err, new(apistatus.ObjectLocked))
|
|
|
|
|
|
|
|
// inhume the tombstone
|
2022-05-31 17:00:41 +00:00
|
|
|
_, err = db.Inhume(new(meta.InhumePrm).WithAddresses(tombAddr).WithGCMark())
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
// now we can inhume the object
|
2022-05-31 17:00:41 +00:00
|
|
|
err = meta.Inhume(db, objectcore.AddressOf(obj), tombAddr)
|
2022-03-10 17:58:58 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
})
|
2022-02-15 11:35:13 +00:00
|
|
|
}
|