2020-01-08 18:16:17 +00:00
|
|
|
package db
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-06-26 14:58:47 +00:00
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/rand"
|
2020-01-08 18:16:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
// SetupDB instantiates and returns a DB instance for the validator client.
|
|
|
|
func SetupDB(t testing.TB, pubkeys [][48]byte) *Store {
|
2020-06-26 14:58:47 +00:00
|
|
|
randPath := rand.NewDeterministicGenerator().Int()
|
2020-01-08 18:16:17 +00:00
|
|
|
p := filepath.Join(TempDir(), fmt.Sprintf("/%d", randPath))
|
|
|
|
if err := os.RemoveAll(p); err != nil {
|
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
|
|
}
|
2020-05-29 15:57:10 +00:00
|
|
|
db, err := NewKVStore(p, pubkeys)
|
2020-01-08 18:16:17 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to instantiate DB: %v", err)
|
|
|
|
}
|
2020-05-04 01:14:34 +00:00
|
|
|
t.Cleanup(func() {
|
|
|
|
if err := db.Close(); err != nil {
|
|
|
|
t.Fatalf("Failed to close database: %v", err)
|
|
|
|
}
|
|
|
|
if err := db.ClearDB(); err != nil {
|
|
|
|
t.Fatalf("Failed to clear database: %v", err)
|
|
|
|
}
|
|
|
|
})
|
2020-01-08 18:16:17 +00:00
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
|
|
|
// TempDir returns a directory path for temporary test storage.
|
|
|
|
func TempDir() string {
|
|
|
|
d := os.Getenv("TEST_TMPDIR")
|
|
|
|
|
|
|
|
// If the test is not run via bazel, the environment var won't be set.
|
|
|
|
if d == "" {
|
|
|
|
return os.TempDir()
|
|
|
|
}
|
|
|
|
return d
|
|
|
|
}
|