2017-05-16 12:09:52 +00:00
|
|
|
package testing
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"io"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
"strings"
|
|
|
|
|
|
|
|
gethcommon "github.com/ethereum/go-ethereum/common"
|
|
|
|
"github.com/status-im/status-go/geth/common"
|
|
|
|
"github.com/status-im/status-go/geth/params"
|
2017-05-25 13:14:52 +00:00
|
|
|
assertions "github.com/stretchr/testify/require"
|
2017-05-16 12:09:52 +00:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-08-15 10:27:12 +00:00
|
|
|
// TestConfig defines the default config usable at package-level.
|
2017-05-16 12:09:52 +00:00
|
|
|
TestConfig *common.TestConfig
|
|
|
|
|
|
|
|
// RootDir is the main application directory
|
|
|
|
RootDir string
|
|
|
|
|
|
|
|
// TestDataDir is data directory used for tests
|
|
|
|
TestDataDir string
|
|
|
|
|
|
|
|
// TestNetworkNames network ID to name mapping
|
|
|
|
TestNetworkNames = map[int]string{
|
|
|
|
params.MainNetworkID: "Mainnet",
|
|
|
|
params.RopstenNetworkID: "Ropsten",
|
|
|
|
params.RinkebyNetworkID: "Rinkeby",
|
|
|
|
}
|
|
|
|
)
|
|
|
|
|
|
|
|
func init() {
|
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup root directory
|
|
|
|
RootDir = filepath.Dir(pwd)
|
|
|
|
if strings.HasSuffix(RootDir, "geth") || strings.HasSuffix(RootDir, "cmd") { // we need to hop one more level
|
|
|
|
RootDir = filepath.Join(RootDir, "..")
|
|
|
|
}
|
|
|
|
|
|
|
|
// setup auxiliary directories
|
|
|
|
TestDataDir = filepath.Join(RootDir, ".ethereumtest")
|
|
|
|
|
|
|
|
TestConfig, err = common.LoadTestConfig()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-08-15 10:27:12 +00:00
|
|
|
// BaseTestSuite defines a base tests suit which others suites can embedded to
|
|
|
|
// access initialization methods useful for testing.
|
2017-05-16 12:09:52 +00:00
|
|
|
type BaseTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
NodeManager common.NodeManager
|
|
|
|
}
|
|
|
|
|
2017-08-15 10:27:12 +00:00
|
|
|
// StartTestNode initiazes a NodeManager instances with configuration retrieved
|
|
|
|
// from the test config.
|
|
|
|
func (s *BaseTestSuite) StartTestNode(networkID int, upstream bool) {
|
2017-05-16 12:09:52 +00:00
|
|
|
require := s.Require()
|
|
|
|
require.NotNil(s.NodeManager)
|
|
|
|
|
|
|
|
nodeConfig, err := MakeTestNodeConfig(networkID)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2017-08-15 10:27:12 +00:00
|
|
|
nodeConfig.UpstreamConfig.Enabled = upstream
|
|
|
|
require.Equal(nodeConfig.UpstreamConfig.Enabled, upstream)
|
|
|
|
|
2017-05-16 12:09:52 +00:00
|
|
|
keyStoreDir := filepath.Join(TestDataDir, TestNetworkNames[networkID], "keystore")
|
|
|
|
require.NoError(common.ImportTestAccount(keyStoreDir, "test-account1.pk"))
|
|
|
|
require.NoError(common.ImportTestAccount(keyStoreDir, "test-account2.pk"))
|
|
|
|
|
|
|
|
require.False(s.NodeManager.IsNodeRunning())
|
|
|
|
nodeStarted, err := s.NodeManager.StartNode(nodeConfig)
|
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(nodeStarted)
|
|
|
|
<-nodeStarted
|
|
|
|
require.True(s.NodeManager.IsNodeRunning())
|
|
|
|
}
|
|
|
|
|
2017-08-15 10:27:12 +00:00
|
|
|
// StopTestNode attempts to stop initialized NodeManager.
|
2017-05-16 12:09:52 +00:00
|
|
|
func (s *BaseTestSuite) StopTestNode() {
|
|
|
|
require := s.Require()
|
|
|
|
require.NotNil(s.NodeManager)
|
|
|
|
require.True(s.NodeManager.IsNodeRunning())
|
2017-05-25 13:14:52 +00:00
|
|
|
nodeStopped, err := s.NodeManager.StopNode()
|
|
|
|
require.NoError(err)
|
|
|
|
<-nodeStopped
|
2017-05-16 12:09:52 +00:00
|
|
|
require.False(s.NodeManager.IsNodeRunning())
|
|
|
|
}
|
|
|
|
|
2017-08-15 10:27:12 +00:00
|
|
|
// FirstBlockHash validates Attach operation for the NodeManager.
|
2017-05-25 13:14:52 +00:00
|
|
|
func FirstBlockHash(require *assertions.Assertions, nodeManager common.NodeManager, expectedHash string) {
|
|
|
|
require.NotNil(nodeManager)
|
2017-05-16 12:09:52 +00:00
|
|
|
|
|
|
|
var firstBlock struct {
|
|
|
|
Hash gethcommon.Hash `json:"hash"`
|
|
|
|
}
|
|
|
|
|
|
|
|
// obtain RPC client for running node
|
2017-05-25 13:14:52 +00:00
|
|
|
runningNode, err := nodeManager.Node()
|
2017-05-16 12:09:52 +00:00
|
|
|
require.NoError(err)
|
|
|
|
require.NotNil(runningNode)
|
|
|
|
|
|
|
|
rpcClient, err := runningNode.Attach()
|
|
|
|
require.NoError(err)
|
|
|
|
|
|
|
|
// get first block
|
|
|
|
err = rpcClient.CallContext(context.Background(), &firstBlock, "eth_getBlockByNumber", "0x0", true)
|
|
|
|
require.NoError(err)
|
|
|
|
|
2017-05-25 13:14:52 +00:00
|
|
|
require.Equal(expectedHash, firstBlock.Hash.Hex())
|
2017-05-16 12:09:52 +00:00
|
|
|
}
|
|
|
|
|
2017-08-15 10:27:12 +00:00
|
|
|
// MakeTestNodeConfig defines a function to return a giving params.NodeConfig
|
|
|
|
// where specific network addresses are assigned based on provieded network id.
|
2017-05-16 12:09:52 +00:00
|
|
|
func MakeTestNodeConfig(networkID int) (*params.NodeConfig, error) {
|
|
|
|
configJSON := `{
|
|
|
|
"NetworkId": ` + strconv.Itoa(networkID) + `,
|
|
|
|
"DataDir": "` + filepath.Join(TestDataDir, TestNetworkNames[networkID]) + `",
|
|
|
|
"HTTPPort": ` + strconv.Itoa(TestConfig.Node.HTTPPort) + `,
|
|
|
|
"WSPort": ` + strconv.Itoa(TestConfig.Node.WSPort) + `,
|
|
|
|
"LogEnabled": true,
|
|
|
|
"LogLevel": "ERROR"
|
|
|
|
}`
|
|
|
|
nodeConfig, err := params.LoadNodeConfig(configJSON)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return nodeConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadFromFile is useful for loading test data, from testdata/filename into a variable
|
|
|
|
// nolint: errcheck
|
|
|
|
func LoadFromFile(filename string) string {
|
|
|
|
f, err := os.Open(filename)
|
|
|
|
if err != nil {
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
|
|
|
|
buf := bytes.NewBuffer(nil)
|
|
|
|
io.Copy(buf, f)
|
|
|
|
f.Close()
|
|
|
|
|
|
|
|
return string(buf.Bytes())
|
|
|
|
}
|