2020-02-14 07:09:54 +00:00
|
|
|
package node
|
|
|
|
|
|
|
|
import (
|
|
|
|
"flag"
|
|
|
|
"fmt"
|
2020-08-18 12:41:25 +00:00
|
|
|
"io/ioutil"
|
2020-02-14 07:09:54 +00:00
|
|
|
"os"
|
2020-10-09 09:28:35 +00:00
|
|
|
"path/filepath"
|
2020-02-14 07:09:54 +00:00
|
|
|
"testing"
|
|
|
|
|
2020-10-09 09:28:35 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/cmd"
|
2020-08-13 16:22:25 +00:00
|
|
|
"github.com/prysmaticlabs/prysm/shared/testutil/require"
|
2020-08-18 12:41:25 +00:00
|
|
|
"github.com/sirupsen/logrus"
|
2020-02-14 07:09:54 +00:00
|
|
|
logTest "github.com/sirupsen/logrus/hooks/test"
|
2020-05-31 06:44:34 +00:00
|
|
|
"github.com/urfave/cli/v2"
|
2020-02-14 07:09:54 +00:00
|
|
|
)
|
|
|
|
|
2020-08-18 12:41:25 +00:00
|
|
|
func TestMain(m *testing.M) {
|
2020-12-04 16:10:07 +00:00
|
|
|
logrus.SetLevel(logrus.DebugLevel)
|
|
|
|
logrus.SetOutput(ioutil.Discard)
|
2020-08-18 12:41:25 +00:00
|
|
|
|
2020-12-04 16:10:07 +00:00
|
|
|
m.Run()
|
2020-08-18 12:41:25 +00:00
|
|
|
}
|
|
|
|
|
2020-02-14 07:09:54 +00:00
|
|
|
// Test that slasher node can close.
|
|
|
|
func TestNodeClose_OK(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
2020-11-10 22:45:17 +00:00
|
|
|
tmp := fmt.Sprintf("%s/datadirtest2", t.TempDir())
|
2020-02-14 07:09:54 +00:00
|
|
|
|
2020-03-19 21:46:44 +00:00
|
|
|
app := cli.App{}
|
2020-02-14 07:09:54 +00:00
|
|
|
set := flag.NewFlagSet("test", 0)
|
|
|
|
set.String("beacon-rpc-provider", "localhost:4232", "beacon node RPC server")
|
|
|
|
set.String("datadir", tmp, "node data directory")
|
|
|
|
|
2020-03-19 21:46:44 +00:00
|
|
|
context := cli.NewContext(&app, set, nil)
|
2020-02-14 07:09:54 +00:00
|
|
|
|
2021-02-01 17:12:52 +00:00
|
|
|
node, err := New(context)
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, err, "Failed to create slasher node")
|
2020-02-14 07:09:54 +00:00
|
|
|
|
|
|
|
node.Close()
|
|
|
|
|
2020-08-13 16:22:25 +00:00
|
|
|
require.LogsContain(t, hook, "Stopping hash slinging slasher")
|
2020-08-18 12:41:25 +00:00
|
|
|
require.NoError(t, os.RemoveAll(tmp))
|
2020-02-14 07:09:54 +00:00
|
|
|
}
|
2020-10-09 09:28:35 +00:00
|
|
|
|
|
|
|
// TestClearDB tests clearing the database
|
|
|
|
func TestClearDB(t *testing.T) {
|
|
|
|
hook := logTest.NewGlobal()
|
|
|
|
|
2020-11-10 22:45:17 +00:00
|
|
|
tmp := filepath.Join(t.TempDir(), "datadirtest")
|
2020-10-09 09:28:35 +00:00
|
|
|
|
|
|
|
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)
|
2021-02-01 17:12:52 +00:00
|
|
|
slasherNode, err := New(context)
|
2020-10-09 09:28:35 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.LogsContain(t, hook, "Removing database")
|
|
|
|
err = slasherNode.db.Close()
|
|
|
|
require.NoError(t, err)
|
|
|
|
require.NoError(t, os.RemoveAll(tmp))
|
|
|
|
}
|