2020-02-13 16:19:46 +00:00
|
|
|
package testing
|
2019-09-26 16:29:10 +00:00
|
|
|
|
|
|
|
import (
|
2020-05-04 01:14:34 +00:00
|
|
|
"crypto/rand"
|
|
|
|
"fmt"
|
|
|
|
"math/big"
|
2019-09-26 16:29:10 +00:00
|
|
|
"os"
|
2020-05-04 01:14:34 +00:00
|
|
|
"path"
|
2019-09-26 16:29:10 +00:00
|
|
|
"testing"
|
2020-05-04 01:14:34 +00:00
|
|
|
|
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil"
|
2020-08-18 12:41:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-05-04 01:14:34 +00:00
|
|
|
slasherDB "github.com/prysmaticlabs/prysm/slasher/db"
|
|
|
|
"github.com/prysmaticlabs/prysm/slasher/db/kv"
|
2019-09-26 16:29:10 +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))
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err, "Could not generate random file path")
|
2020-05-04 01:14:34 +00:00
|
|
|
p := path.Join(testutil.TempDir(), fmt.Sprintf("/%d", randPath))
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, os.RemoveAll(p), "Failed to remove directory")
|
2020-05-04 01:14:34 +00:00
|
|
|
cfg := &kv.Config{}
|
|
|
|
db, err := slasherDB.NewDB(p, cfg)
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err, "Failed to instantiate DB")
|
2020-05-29 12:09:04 +00:00
|
|
|
db.EnableSpanCache(false)
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, db.ClearDB())
|
|
|
|
_, err = os.Stat(db.DatabasePath())
|
|
|
|
require.Equal(t, true, os.IsNotExist(err), "Db wasnt cleared %v", err)
|
2019-09-26 16:29:10 +00:00
|
|
|
}
|