prysm-pulse/slasher/node/node_test.go
dv8silencer c944f29c7c
Fix --clear-db on Windows (#7474)
* fix with text

* fix also for validator and add test

* handle error

* fix another test

* handle error

* gazelle

* fix bug in slasher as well

* gazelle
2020-10-09 09:28:35 +00:00

74 lines
1.9 KiB
Go

package node
import (
"crypto/rand"
"flag"
"fmt"
"io/ioutil"
"math/big"
"os"
"path/filepath"
"testing"
"github.com/prysmaticlabs/prysm/shared/cmd"
"github.com/prysmaticlabs/prysm/shared/testutil"
"github.com/prysmaticlabs/prysm/shared/testutil/require"
"github.com/sirupsen/logrus"
logTest "github.com/sirupsen/logrus/hooks/test"
"github.com/urfave/cli/v2"
)
func TestMain(m *testing.M) {
logrus.SetLevel(logrus.DebugLevel)
logrus.SetOutput(ioutil.Discard)
os.Exit(m.Run())
}
// Test that slasher node can close.
func TestNodeClose_OK(t *testing.T) {
hook := logTest.NewGlobal()
tmp := fmt.Sprintf("%s/datadirtest2", testutil.TempDir())
require.NoError(t, os.RemoveAll(tmp))
app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String("beacon-rpc-provider", "localhost:4232", "beacon node RPC server")
set.String("datadir", tmp, "node data directory")
context := cli.NewContext(&app, set, nil)
node, err := NewSlasherNode(context)
require.NoError(t, err, "Failed to create slasher node")
node.Close()
require.LogsContain(t, hook, "Stopping hash slinging slasher")
require.NoError(t, os.RemoveAll(tmp))
}
// TestClearDB tests clearing the database
func TestClearDB(t *testing.T) {
hook := logTest.NewGlobal()
randPath, err := rand.Int(rand.Reader, big.NewInt(1000000))
require.NoError(t, err, "Could not generate random number for file path")
tmp := filepath.Join(testutil.TempDir(), fmt.Sprintf("datadirtest%d", randPath))
require.NoError(t, os.RemoveAll(tmp))
app := cli.App{}
set := flag.NewFlagSet("test", 0)
set.String("datadir", tmp, "node data directory")
set.Bool(cmd.ForceClearDB.Name, true, "force clear db")
context := cli.NewContext(&app, set, nil)
slasherNode, err := NewSlasherNode(context)
require.NoError(t, err)
require.LogsContain(t, hook, "Removing database")
err = slasherNode.db.Close()
require.NoError(t, err)
require.NoError(t, os.RemoveAll(tmp))
}