From 7704811654bb2273d95f4b59b0eb627aa5770ee7 Mon Sep 17 00:00:00 2001 From: Leonard Lyubich Date: Mon, 2 Nov 2020 10:55:35 +0300 Subject: [PATCH] [#137] metabase: Implement and useful test funcs for work with DB Signed-off-by: Leonard Lyubich --- pkg/local_object_storage/metabase/db_test.go | 59 ++++++++----------- .../metabase/delete_test.go | 14 +---- pkg/local_object_storage/metabase/get_test.go | 14 +---- pkg/local_object_storage/metabase/put_test.go | 14 +---- .../metabase/select_test.go | 14 +---- 5 files changed, 31 insertions(+), 84 deletions(-) diff --git a/pkg/local_object_storage/metabase/db_test.go b/pkg/local_object_storage/metabase/db_test.go index 2b5104c43..068c6d4b3 100644 --- a/pkg/local_object_storage/metabase/db_test.go +++ b/pkg/local_object_storage/metabase/db_test.go @@ -76,17 +76,9 @@ func TestDB(t *testing.T) { obj.SetAttributes(a) - path := "test.db" + db := newDB(t) - bdb, err := bbolt.Open(path, 0600, nil) - require.NoError(t, err) - - defer func() { - bdb.Close() - os.Remove(path) - }() - - db := NewDB(bdb) + defer releaseDB(db) o := obj.Object() @@ -117,17 +109,9 @@ func TestDB(t *testing.T) { } func TestDB_Delete(t *testing.T) { - path := "test.db" + db := newDB(t) - bdb, err := bbolt.Open(path, 0600, nil) - require.NoError(t, err) - - defer func() { - bdb.Close() - os.Remove(path) - }() - - db := NewDB(bdb) + defer releaseDB(db) obj := object.NewRaw() obj.SetContainerID(testCID()) @@ -139,7 +123,7 @@ func TestDB_Delete(t *testing.T) { addr := o.Address() - _, err = db.Get(addr) + _, err := db.Get(addr) require.NoError(t, err) fs := objectSDK.SearchFilters{} @@ -156,17 +140,9 @@ func TestDB_Delete(t *testing.T) { } func TestDB_SelectProperties(t *testing.T) { - path := "test.db" + db := newDB(t) - bdb, err := bbolt.Open(path, 0600, nil) - require.NoError(t, err) - - defer func() { - bdb.Close() - os.Remove(path) - }() - - db := NewDB(bdb) + defer releaseDB(db) parent := object.NewRaw() parent.SetContainerID(testCID()) @@ -243,12 +219,23 @@ func TestDB_Path(t *testing.T) { bdb, err := bbolt.Open(path, 0600, nil) require.NoError(t, err) - defer func() { - bdb.Close() - os.Remove(path) - }() - db := NewDB(bdb) + defer releaseDB(db) + require.Equal(t, path, db.Path()) } + +func newDB(t testing.TB) *DB { + path := t.Name() + + bdb, err := bbolt.Open(path, 0600, nil) + require.NoError(t, err) + + return NewDB(bdb) +} + +func releaseDB(db *DB) { + db.Close() + os.Remove(db.Path()) +} diff --git a/pkg/local_object_storage/metabase/delete_test.go b/pkg/local_object_storage/metabase/delete_test.go index 712fbcbf9..f065f2387 100644 --- a/pkg/local_object_storage/metabase/delete_test.go +++ b/pkg/local_object_storage/metabase/delete_test.go @@ -1,26 +1,16 @@ package meta import ( - "os" "testing" "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/stretchr/testify/require" - "go.etcd.io/bbolt" ) func BenchmarkDB_Delete(b *testing.B) { - path := "delete_test.db" + db := newDB(b) - bdb, err := bbolt.Open(path, 0600, nil) - require.NoError(b, err) - - defer func() { - bdb.Close() - os.Remove(path) - }() - - db := NewDB(bdb) + defer releaseDB(db) var existingAddr *object.Address diff --git a/pkg/local_object_storage/metabase/get_test.go b/pkg/local_object_storage/metabase/get_test.go index 1d6dab77b..919b73055 100644 --- a/pkg/local_object_storage/metabase/get_test.go +++ b/pkg/local_object_storage/metabase/get_test.go @@ -1,26 +1,16 @@ package meta import ( - "os" "testing" "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/stretchr/testify/require" - "go.etcd.io/bbolt" ) func BenchmarkDB_Get(b *testing.B) { - path := "get_test.db" + db := newDB(b) - bdb, err := bbolt.Open(path, 0600, nil) - require.NoError(b, err) - - defer func() { - bdb.Close() - os.Remove(path) - }() - - db := NewDB(bdb) + defer releaseDB(db) var existingAddr *object.Address diff --git a/pkg/local_object_storage/metabase/put_test.go b/pkg/local_object_storage/metabase/put_test.go index db315fc57..c378dbff2 100644 --- a/pkg/local_object_storage/metabase/put_test.go +++ b/pkg/local_object_storage/metabase/put_test.go @@ -4,7 +4,6 @@ import ( "crypto/rand" "crypto/sha256" "fmt" - "os" "testing" "github.com/nspcc-dev/neofs-api-go/pkg" @@ -13,7 +12,6 @@ import ( "github.com/nspcc-dev/neofs-node/pkg/core/object" "github.com/nspcc-dev/neofs-node/pkg/util/test" "github.com/stretchr/testify/require" - "go.etcd.io/bbolt" ) type testPrm struct { @@ -87,17 +85,9 @@ func generateObject(t require.TestingT, prm testPrm) *object.Object { } func BenchmarkDB_Put(b *testing.B) { - path := "put_test.db" + db := newDB(b) - bdb, err := bbolt.Open(path, 0600, nil) - require.NoError(b, err) - - defer func() { - bdb.Close() - os.Remove(path) - }() - - db := NewDB(bdb) + defer releaseDB(db) for _, prm := range []testPrm{ { diff --git a/pkg/local_object_storage/metabase/select_test.go b/pkg/local_object_storage/metabase/select_test.go index 94b93907a..73ab600a8 100644 --- a/pkg/local_object_storage/metabase/select_test.go +++ b/pkg/local_object_storage/metabase/select_test.go @@ -2,12 +2,10 @@ package meta import ( "crypto/rand" - "os" "testing" objectSDK "github.com/nspcc-dev/neofs-api-go/pkg/object" "github.com/stretchr/testify/require" - "go.etcd.io/bbolt" ) func addNFilters(fs *objectSDK.SearchFilters, n int) { @@ -23,17 +21,9 @@ func addNFilters(fs *objectSDK.SearchFilters, n int) { } func BenchmarkDB_Select(b *testing.B) { - path := "select_test.db" + db := newDB(b) - bdb, err := bbolt.Open(path, 0600, nil) - require.NoError(b, err) - - defer func() { - bdb.Close() - os.Remove(path) - }() - - db := NewDB(bdb) + defer releaseDB(db) for i := 0; i < 100; i++ { obj := generateObject(b, testPrm{