2018-11-07 19:07:41 +00:00
|
|
|
package internal
|
2018-10-17 06:11:24 +00:00
|
|
|
|
|
|
|
import (
|
2018-11-19 01:59:11 +00:00
|
|
|
"crypto/rand"
|
2018-11-07 19:07:41 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2018-10-17 06:11:24 +00:00
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/beacon-chain/db"
|
2019-02-13 23:49:06 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2018-10-17 06:11:24 +00:00
|
|
|
)
|
|
|
|
|
2019-08-14 18:48:28 +00:00
|
|
|
// SetupDBDeprecated instantiates and returns a BeaconDB instance.
|
|
|
|
// This is deprecated and used to set up the pre refactored db for testing.
|
2019-08-15 21:41:51 +00:00
|
|
|
// DEPRECATED: Use beacon-chain/db/testing.SetupDB
|
2019-08-14 18:48:28 +00:00
|
|
|
func SetupDBDeprecated(t testing.TB) *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(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
|
|
}
|
|
|
|
db, err := db.NewDBDeprecated(path)
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not setup DB: %v", err)
|
|
|
|
}
|
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
// TeardownDBDeprecated cleans up a BeaconDB instance.
|
|
|
|
// This is deprecated and used to tear up the pre refactored db for testing.
|
2019-08-15 21:41:51 +00:00
|
|
|
// DEPRECATED: Use beacon-chain/db/testing.TeardownDB
|
2019-08-14 18:48:28 +00:00
|
|
|
func TeardownDBDeprecated(t testing.TB, db *db.BeaconDB) {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Fatalf("Failed to close database: %v", err)
|
|
|
|
}
|
2019-08-21 16:04:00 +00:00
|
|
|
if err := os.RemoveAll(db.DatabasePath()); err != nil {
|
2019-08-14 18:48:28 +00:00
|
|
|
t.Fatalf("Could not remove tmp db dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|