2018-10-05 17:14:50 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
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"
|
2018-10-05 17:14:50 +00:00
|
|
|
"testing"
|
|
|
|
)
|
|
|
|
|
2018-11-07 19:07:41 +00:00
|
|
|
// setupDB instantiates and returns a BeaconDB instance.
|
|
|
|
func setupDB(t *testing.T) *BeaconDB {
|
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
2018-10-05 17:14:50 +00:00
|
|
|
if err != nil {
|
2018-11-07 19:07:41 +00:00
|
|
|
t.Fatalf("Could not generate random file path: %v", err)
|
|
|
|
}
|
|
|
|
path := path.Join(os.TempDir(), fmt.Sprintf("/%d", randPath))
|
|
|
|
if err := os.RemoveAll(path); err != nil {
|
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-10-17 06:11:24 +00:00
|
|
|
db, err := NewDB(path)
|
2018-10-05 17:14:50 +00:00
|
|
|
if err != nil {
|
2018-10-17 06:11:24 +00:00
|
|
|
t.Fatalf("Failed to instantiate DB: %v", err)
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|
2018-10-17 06:11:24 +00:00
|
|
|
return db
|
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
|
2018-11-07 19:07:41 +00:00
|
|
|
// teardownDB cleans up a test BeaconDB instance.
|
2018-10-17 06:11:24 +00:00
|
|
|
func teardownDB(t *testing.T, db *BeaconDB) {
|
2018-11-07 19:07:41 +00:00
|
|
|
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)
|
|
|
|
}
|
2018-10-05 17:14:50 +00:00
|
|
|
}
|