2018-02-08 12:52:47 +00:00
|
|
|
package t
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"github.com/ethereum/go-ethereum/les"
|
|
|
|
whisper "github.com/ethereum/go-ethereum/whisper/whisperv5"
|
|
|
|
"github.com/status-im/status-go/geth/api"
|
|
|
|
"github.com/status-im/status-go/geth/common"
|
2017-11-07 17:46:11 +00:00
|
|
|
"github.com/status-im/status-go/geth/log"
|
2018-02-09 13:37:56 +00:00
|
|
|
"github.com/status-im/status-go/geth/node"
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/status-im/status-go/geth/signal"
|
2018-01-22 09:31:24 +00:00
|
|
|
"github.com/status-im/status-go/geth/transactions"
|
2018-02-08 12:52:47 +00:00
|
|
|
. "github.com/status-im/status-go/t/utils" //nolint: golint
|
2017-10-11 14:20:51 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
// NodeManagerTestSuite defines a test suit with NodeManager.
|
|
|
|
type NodeManagerTestSuite struct {
|
|
|
|
suite.Suite
|
2018-02-09 13:37:56 +00:00
|
|
|
NodeManager *node.NodeManager
|
2017-10-16 22:36:36 +00:00
|
|
|
}
|
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
func init() {
|
|
|
|
for id := range TestNetworkNames {
|
|
|
|
nodeConfig, err := MakeTestNodeConfig(id)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err = importTestAccouns(nodeConfig.KeyStoreDir)
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// StartTestNode initiazes a NodeManager instances with configuration retrieved
|
|
|
|
// from the test config.
|
2017-10-23 16:03:07 +00:00
|
|
|
func (s *NodeManagerTestSuite) StartTestNode(opts ...TestNodeOption) {
|
|
|
|
nodeConfig, err := MakeTestNodeConfig(GetNetworkID())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// Apply any options altering node config.
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](nodeConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// import account keys
|
|
|
|
s.NoError(importTestAccouns(nodeConfig.KeyStoreDir))
|
|
|
|
|
|
|
|
s.False(s.NodeManager.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StartNode(nodeConfig))
|
2017-10-11 14:20:51 +00:00
|
|
|
s.True(s.NodeManager.IsNodeRunning())
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopTestNode attempts to stop initialized NodeManager.
|
|
|
|
func (s *NodeManagerTestSuite) StopTestNode() {
|
|
|
|
s.NotNil(s.NodeManager)
|
|
|
|
s.True(s.NodeManager.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.NodeManager.StopNode())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(s.NodeManager.IsNodeRunning())
|
|
|
|
}
|
|
|
|
|
|
|
|
// BackendTestSuite is a test suite with api.StatusBackend initialized
|
|
|
|
// and a few utility methods to start and stop node or get various services.
|
|
|
|
type BackendTestSuite struct {
|
|
|
|
suite.Suite
|
2017-10-23 10:41:41 +00:00
|
|
|
Backend *api.StatusBackend
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// SetupTest initializes Backend.
|
|
|
|
func (s *BackendTestSuite) SetupTest() {
|
|
|
|
s.Backend = api.NewStatusBackend()
|
|
|
|
s.NotNil(s.Backend)
|
|
|
|
}
|
|
|
|
|
|
|
|
// TearDownTest cleans up the packages state.
|
|
|
|
func (s *BackendTestSuite) TearDownTest() {
|
|
|
|
signal.ResetDefaultNodeNotificationHandler()
|
|
|
|
}
|
|
|
|
|
|
|
|
// StartTestBackend imports some keys and starts a node.
|
2017-10-23 16:03:07 +00:00
|
|
|
func (s *BackendTestSuite) StartTestBackend(opts ...TestNodeOption) {
|
|
|
|
nodeConfig, err := MakeTestNodeConfig(GetNetworkID())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.NoError(err)
|
|
|
|
|
|
|
|
// Apply any options altering node config.
|
|
|
|
for i := range opts {
|
|
|
|
opts[i](nodeConfig)
|
|
|
|
}
|
|
|
|
|
|
|
|
// import account keys
|
|
|
|
s.NoError(importTestAccouns(nodeConfig.KeyStoreDir))
|
|
|
|
|
|
|
|
// start node
|
|
|
|
s.False(s.Backend.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.Backend.StartNode(nodeConfig))
|
2017-10-11 14:20:51 +00:00
|
|
|
s.True(s.Backend.IsNodeRunning())
|
|
|
|
}
|
|
|
|
|
|
|
|
// StopTestBackend stops the node.
|
|
|
|
func (s *BackendTestSuite) StopTestBackend() {
|
|
|
|
s.True(s.Backend.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.Backend.StopNode())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.False(s.Backend.IsNodeRunning())
|
|
|
|
}
|
|
|
|
|
|
|
|
// RestartTestNode restarts a currently running node.
|
|
|
|
func (s *BackendTestSuite) RestartTestNode() {
|
|
|
|
s.True(s.Backend.IsNodeRunning())
|
2018-02-09 13:37:56 +00:00
|
|
|
s.NoError(s.Backend.RestartNode())
|
2017-10-11 14:20:51 +00:00
|
|
|
s.True(s.Backend.IsNodeRunning())
|
|
|
|
}
|
|
|
|
|
|
|
|
// WhisperService returns a reference to the Whisper service.
|
|
|
|
func (s *BackendTestSuite) WhisperService() *whisper.Whisper {
|
|
|
|
whisperService, err := s.Backend.NodeManager().WhisperService()
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(whisperService)
|
|
|
|
|
|
|
|
return whisperService
|
|
|
|
}
|
|
|
|
|
|
|
|
// LightEthereumService returns a reference to the LES service.
|
|
|
|
func (s *BackendTestSuite) LightEthereumService() *les.LightEthereum {
|
|
|
|
lightEthereum, err := s.Backend.NodeManager().LightEthereumService()
|
|
|
|
s.NoError(err)
|
|
|
|
s.NotNil(lightEthereum)
|
|
|
|
|
|
|
|
return lightEthereum
|
|
|
|
}
|
|
|
|
|
|
|
|
// TxQueueManager returns a reference to the TxQueueManager.
|
2018-01-22 09:31:24 +00:00
|
|
|
func (s *BackendTestSuite) TxQueueManager() *transactions.Manager {
|
2017-10-11 14:20:51 +00:00
|
|
|
return s.Backend.TxQueueManager()
|
|
|
|
}
|
|
|
|
|
|
|
|
func importTestAccouns(keyStoreDir string) (err error) {
|
2017-11-07 17:46:11 +00:00
|
|
|
log.Debug("Import accounts to", keyStoreDir)
|
|
|
|
|
2017-11-20 18:21:30 +00:00
|
|
|
err = common.ImportTestAccount(keyStoreDir, GetAccount1PKFile())
|
2017-10-11 14:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2017-11-20 18:21:30 +00:00
|
|
|
return common.ImportTestAccount(keyStoreDir, GetAccount2PKFile())
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|