2020-06-27 02:37:43 +00:00
|
|
|
package testing
|
2020-01-08 18:16:17 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-04 01:14:34 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2020-01-08 18:16:17 +00:00
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"testing"
|
2020-06-27 02:37:43 +00:00
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/validator/db/kv"
|
2020-01-08 18:16:17 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func TestClearDB(t *testing.T) {
|
2020-05-04 01:14:34 +00:00
|
|
|
// Setting up manually is required, since SetupDB() will also register a teardown procedure.
|
|
|
|
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
|
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Could not generate random file path: %v", err)
|
|
|
|
}
|
|
|
|
p := filepath.Join(TempDir(), fmt.Sprintf("/%d", randPath))
|
|
|
|
if err := os.RemoveAll(p); err != nil {
|
|
|
|
t.Fatalf("Failed to remove directory: %v", err)
|
|
|
|
}
|
2020-06-27 02:37:43 +00:00
|
|
|
testDB, err := kv.NewKVStore(p, [][48]byte{})
|
2020-05-04 01:14:34 +00:00
|
|
|
if err != nil {
|
|
|
|
t.Fatalf("Failed to instantiate DB: %v", err)
|
|
|
|
}
|
2020-06-27 02:37:43 +00:00
|
|
|
if err := testDB.ClearDB(); err != nil {
|
2020-01-08 18:16:17 +00:00
|
|
|
t.Fatal(err)
|
|
|
|
}
|
|
|
|
|
2020-06-27 02:37:43 +00:00
|
|
|
if _, err := os.Stat(filepath.Join(testDB.DatabasePath(), "validator.db")); !os.IsNotExist(err) {
|
2020-01-08 18:16:17 +00:00
|
|
|
t.Fatalf("DB was not cleared: %v", err)
|
|
|
|
}
|
|
|
|
}
|