2020-06-27 02:37:43 +00:00
|
|
|
package kv
|
2020-01-08 18:16:17 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-06-26 14:58:47 +00:00
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/rand"
|
2020-07-23 01:13:52 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-01-08 18:16:17 +00:00
|
|
|
)
|
|
|
|
|
2020-06-27 02:37:43 +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-06-27 02:37:43 +00:00
|
|
|
p := filepath.Join(tempdir(), fmt.Sprintf("/%d", randPath))
|
2020-07-23 01:13:52 +00:00
|
|
|
require.NoError(t, os.RemoveAll(p), "Failed to remove directory")
|
2020-05-29 15:57:10 +00:00
|
|
|
db, err := NewKVStore(p, pubkeys)
|
2020-07-23 01:13:52 +00:00
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
2020-10-15 19:35:31 +00:00
|
|
|
err = db.OldUpdatePublicKeysBuckets(pubkeys)
|
|
|
|
require.NoError(t, err, "Failed to create old buckets for public keys")
|
2020-05-04 01:14:34 +00:00
|
|
|
t.Cleanup(func() {
|
2020-07-23 01:13:52 +00:00
|
|
|
require.NoError(t, db.Close(), "Failed to close database")
|
|
|
|
require.NoError(t, db.ClearDB(), "Failed to clear database")
|
2020-05-04 01:14:34 +00:00
|
|
|
})
|
2020-01-08 18:16:17 +00:00
|
|
|
return db
|
|
|
|
}
|
|
|
|
|
2020-06-27 02:37:43 +00:00
|
|
|
// tempdir returns a directory path for temporary test storage.
|
|
|
|
func tempdir() string {
|
2020-01-08 18:16:17 +00:00
|
|
|
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
|
|
|
|
}
|