frostfs-node/pkg/local_object_storage/metabase/v2/db_test.go
Alex Vanin e478c0d024 [#199] Refactor metabase internal structure
Accoring to MetaBase-Plan-B storage engine specification.

Signed-off-by: Alex Vanin <alexey@nspcc.ru>
2020-12-11 17:19:37 +03:00

113 lines
2.4 KiB
Go

package meta_test
import (
"crypto/rand"
"crypto/sha256"
"os"
"testing"
"github.com/nspcc-dev/neofs-api-go/pkg"
"github.com/nspcc-dev/neofs-api-go/pkg/container"
objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object"
"github.com/nspcc-dev/neofs-api-go/pkg/owner"
"github.com/nspcc-dev/neofs-node/pkg/core/object"
meta "github.com/nspcc-dev/neofs-node/pkg/local_object_storage/metabase/v2"
"github.com/nspcc-dev/neofs-node/pkg/util/test"
"github.com/nspcc-dev/tzhash/tz"
"github.com/stretchr/testify/require"
"go.etcd.io/bbolt"
)
func testSelect(t *testing.T, db *meta.DB, fs objectSDK.SearchFilters, exp ...*objectSDK.Address) {
res, err := db.Select(fs)
require.NoError(t, err)
require.Len(t, res, len(exp))
for i := range exp {
require.Contains(t, res, exp[i])
}
}
func testCID() *container.ID {
cs := [sha256.Size]byte{}
_, _ = rand.Read(cs[:])
id := container.NewID()
id.SetSHA256(cs)
return id
}
func testOID() *objectSDK.ID {
cs := [sha256.Size]byte{}
_, _ = rand.Read(cs[:])
id := objectSDK.NewID()
id.SetSHA256(cs)
return id
}
func newDB(t testing.TB) *meta.DB {
path := t.Name()
bdb, err := bbolt.Open(path, 0600, nil)
require.NoError(t, err)
return meta.NewDB(meta.FromBoltDB(bdb))
}
func releaseDB(db *meta.DB) {
db.Close()
os.Remove(db.DumpInfo().Path)
}
func generateRawObject(t *testing.T) *object.RawObject {
return generateRawObjectWithCID(t, testCID())
}
func generateRawObjectWithCID(t *testing.T, cid *container.ID) *object.RawObject {
version := pkg.NewVersion()
version.SetMajor(2)
version.SetMinor(1)
w, err := owner.NEO3WalletFromPublicKey(&test.DecodeKey(-1).PublicKey)
require.NoError(t, err)
ownerID := owner.NewID()
ownerID.SetNeo3Wallet(w)
csum := new(pkg.Checksum)
csum.SetSHA256(sha256.Sum256(w.Bytes()))
csumTZ := new(pkg.Checksum)
csumTZ.SetTillichZemor(tz.Sum(csum.Sum()))
obj := object.NewRaw()
obj.SetID(testOID())
obj.SetOwnerID(ownerID)
obj.SetContainerID(cid)
obj.SetVersion(version)
obj.SetPayloadChecksum(csum)
obj.SetPayloadHomomorphicHash(csumTZ)
return obj
}
func generateAddress() *objectSDK.Address {
addr := objectSDK.NewAddress()
addr.SetContainerID(testCID())
addr.SetObjectID(testOID())
return addr
}
func addAttribute(obj *object.RawObject, key, val string) {
attr := objectSDK.NewAttribute()
attr.SetKey(key)
attr.SetValue(val)
attrs := obj.Attributes()
attrs = append(attrs, attr)
obj.SetAttributes(attrs...)
}