2019-02-23 06:06:20 +00:00
|
|
|
package db
|
2018-11-07 19:07:41 +00:00
|
|
|
|
|
|
|
import (
|
2018-11-19 01:59:11 +00:00
|
|
|
"crypto/rand"
|
2018-11-07 19:07:41 +00:00
|
|
|
"fmt"
|
|
|
|
"math/big"
|
|
|
|
"os"
|
|
|
|
"path"
|
|
|
|
)
|
|
|
|
|
2019-02-23 06:06:20 +00:00
|
|
|
// SetupDB instantiates and returns a simulated backend BeaconDB instance.
|
|
|
|
func SetupDB() (*BeaconDB, error) {
|
2018-11-07 19:07:41 +00:00
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("could not generate random file path: %v", err)
|
|
|
|
}
|
|
|
|
path := path.Join(os.TempDir(), fmt.Sprintf("/%d", randPath))
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
return nil, fmt.Errorf("failed to remove directory: %v", err)
|
|
|
|
}
|
2019-02-23 06:06:20 +00:00
|
|
|
return NewDB(path)
|
2018-11-07 19:07:41 +00:00
|
|
|
}
|
|
|
|
|
2019-02-23 06:06:20 +00:00
|
|
|
// TeardownDB cleans up a simulated backend BeaconDB instance.
|
|
|
|
func TeardownDB(db *BeaconDB) {
|
2018-11-07 19:07:41 +00:00
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
log.Fatalf("failed to close database: %v", err)
|
|
|
|
}
|
|
|
|
if err := os.RemoveAll(db.DatabasePath); err != nil {
|
|
|
|
log.Fatalf("could not remove tmp db dir: %v", err)
|
|
|
|
}
|
|
|
|
}
|