prysm-pulse/beacon-chain/internal/db_test_util.go

41 lines
935 B
Go
Raw Normal View History

package internal
2018-10-17 06:11:24 +00:00
import (
"fmt"
"math/big"
2018-10-17 06:11:24 +00:00
"os"
"path"
"testing"
"crypto/rand"
2018-10-17 06:11:24 +00:00
"github.com/prysmaticlabs/prysm/beacon-chain/db"
)
// SetupDB instantiates and returns a BeaconDB instance.
func SetupDB(t *testing.T) *db.BeaconDB {
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
if err != nil {
t.Fatalf("Could not generate random file path: %v", err)
}
path := path.Join(os.TempDir(), fmt.Sprintf("/%d", randPath))
2018-10-17 06:11:24 +00:00
if err := os.RemoveAll(path); err != nil {
t.Fatalf("Failed to remove directory: %v", err)
}
db, err := db.NewDB(path)
if err != nil {
t.Fatalf("Could not setup DB: %v", err)
2018-10-17 06:11:24 +00:00
}
return db
}
// TeardownDB cleans up a BeaconDB instance.
2018-10-17 06:11:24 +00:00
func TeardownDB(t *testing.T, db *db.BeaconDB) {
if err := db.Close(); err != nil {
t.Fatalf("Failed to close database: %v", err)
}
if err := os.RemoveAll(db.DatabasePath); err != nil {
t.Fatalf("Could not remove tmp db dir: %v", err)
2018-10-17 06:11:24 +00:00
}
}