prysm-pulse/slasher/db/testing/setup_db.go

44 lines
1.1 KiB
Go
Raw Normal View History

// Package testing defines useful helper functions for unit tests with
// the slasher database.
package testing
2019-09-26 16:29:10 +00:00
import (
"crypto/rand"
"fmt"
"math/big"
"os"
"path"
"testing"
"github.com/prysmaticlabs/prysm/shared/testutil"
slasherDB "github.com/prysmaticlabs/prysm/slasher/db"
"github.com/prysmaticlabs/prysm/slasher/db/kv"
2019-09-26 16:29:10 +00:00
)
// SetupSlasherDB instantiates and returns a SlasherDB instance.
func SetupSlasherDB(t testing.TB, spanCacheEnabled bool) *kv.Store {
2019-09-26 16:29:10 +00:00
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
if err != nil {
t.Fatalf("Could not generate random file path: %v", err)
}
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
2019-09-26 16:29:10 +00:00
if err := os.RemoveAll(p); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
cfg := &kv.Config{}
db, err := slasherDB.NewDB(p, cfg)
db.EnableSpanCache(spanCacheEnabled)
if err != nil {
t.Fatalf("Failed to instantiate DB: %v", err)
}
t.Cleanup(func() {
if err := db.Close(); err != nil {
t.Fatalf("Failed to close database: %v", err)
}
if err := os.RemoveAll(db.DatabasePath()); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
})
return db
}