2018-02-08 12:52:47 +00:00
|
|
|
package utils
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
2018-04-09 07:16:43 +00:00
|
|
|
"context"
|
2018-04-04 17:39:38 +00:00
|
|
|
"encoding/json"
|
2017-10-25 12:08:33 +00:00
|
|
|
"errors"
|
2017-10-24 10:23:53 +00:00
|
|
|
"flag"
|
2017-10-24 15:13:19 +00:00
|
|
|
"fmt"
|
2017-10-11 14:20:51 +00:00
|
|
|
"io"
|
2018-04-04 17:39:38 +00:00
|
|
|
"io/ioutil"
|
2017-10-11 14:20:51 +00:00
|
|
|
"os"
|
2018-08-21 12:46:10 +00:00
|
|
|
"path"
|
2017-10-11 14:20:51 +00:00
|
|
|
"path/filepath"
|
2018-03-02 09:25:30 +00:00
|
|
|
"runtime"
|
|
|
|
"strconv"
|
2017-10-11 14:20:51 +00:00
|
|
|
"strings"
|
2018-03-02 09:25:30 +00:00
|
|
|
"testing"
|
2017-10-21 17:04:07 +00:00
|
|
|
"time"
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2020-01-02 09:10:19 +00:00
|
|
|
_ "github.com/stretchr/testify/suite" // required to register testify flags
|
|
|
|
|
2019-01-18 09:01:14 +00:00
|
|
|
"github.com/status-im/status-go/logutils"
|
2018-06-08 11:29:50 +00:00
|
|
|
"github.com/status-im/status-go/params"
|
2019-06-09 07:24:20 +00:00
|
|
|
"github.com/status-im/status-go/static"
|
|
|
|
"github.com/status-im/status-go/t"
|
2017-10-11 14:20:51 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
2017-10-26 13:11:24 +00:00
|
|
|
networkSelected = flag.String("network", "statuschain", "-network=NETWORKID or -network=NETWORKNAME to select network used for tests")
|
2018-04-22 16:50:34 +00:00
|
|
|
logLevel = flag.String("log", "INFO", `Log level, one of: "ERROR", "WARN", "INFO", "DEBUG", and "TRACE"`)
|
|
|
|
)
|
2017-10-25 12:08:33 +00:00
|
|
|
|
2018-04-22 16:50:34 +00:00
|
|
|
var (
|
2017-10-26 13:11:24 +00:00
|
|
|
// ErrNoRemoteURL is returned when network id has no associated url.
|
|
|
|
ErrNoRemoteURL = errors.New("network id requires a remote URL")
|
2017-10-24 10:23:53 +00:00
|
|
|
|
2018-01-18 16:55:17 +00:00
|
|
|
// ErrTimeout is returned when test times out
|
|
|
|
ErrTimeout = errors.New("timeout")
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// TestConfig defines the default config usable at package-level.
|
2018-04-04 17:39:38 +00:00
|
|
|
TestConfig *testConfig
|
2017-10-11 14:20:51 +00:00
|
|
|
|
|
|
|
// 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{
|
2017-10-16 21:54:56 +00:00
|
|
|
params.MainNetworkID: "Mainnet",
|
|
|
|
params.StatusChainNetworkID: "StatusChain",
|
2019-03-14 08:43:32 +00:00
|
|
|
params.GoerliNetworkID: "Goerli",
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
2018-03-20 18:35:28 +00:00
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
syncTimeout = 50 * time.Minute
|
2017-10-11 14:20:51 +00:00
|
|
|
)
|
|
|
|
|
2019-10-04 15:21:24 +00:00
|
|
|
func Init() {
|
2017-10-11 14:20:51 +00:00
|
|
|
pwd, err := os.Getwd()
|
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
flag.Parse()
|
|
|
|
|
2018-04-22 16:50:34 +00:00
|
|
|
// set up logger
|
2018-04-26 16:28:42 +00:00
|
|
|
loggerEnabled := *logLevel != ""
|
2019-03-01 13:37:13 +00:00
|
|
|
if err := logutils.OverrideRootLog(loggerEnabled, *logLevel, logutils.FileOptions{}, true); err != nil {
|
2018-04-22 16:50:34 +00:00
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
|
2017-10-11 14:20:51 +00:00
|
|
|
// setup root directory
|
2017-11-07 17:46:11 +00:00
|
|
|
const pathSeparator = string(os.PathSeparator)
|
2017-10-11 14:20:51 +00:00
|
|
|
RootDir = filepath.Dir(pwd)
|
2017-11-07 17:46:11 +00:00
|
|
|
pathDirs := strings.Split(RootDir, pathSeparator)
|
2018-03-27 15:06:36 +00:00
|
|
|
for i := len(pathDirs) - 1; i >= 0; i-- {
|
2017-11-07 17:46:11 +00:00
|
|
|
if pathDirs[i] == "status-go" {
|
|
|
|
RootDir = filepath.Join(pathDirs[:i+1]...)
|
|
|
|
RootDir = filepath.Join(pathSeparator, RootDir)
|
|
|
|
break
|
|
|
|
}
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// setup auxiliary directories
|
|
|
|
TestDataDir = filepath.Join(RootDir, ".ethereumtest")
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
TestConfig, err = loadTestConfig()
|
2017-10-11 14:20:51 +00:00
|
|
|
if err != nil {
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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)
|
2018-01-30 11:51:48 +00:00
|
|
|
io.Copy(buf, f) // nolint: gas
|
|
|
|
f.Close() // nolint: gas
|
2017-10-11 14:20:51 +00:00
|
|
|
|
2018-01-30 11:51:48 +00:00
|
|
|
return buf.String()
|
2017-10-11 14:20:51 +00:00
|
|
|
}
|
2017-10-21 17:04:07 +00:00
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
// EnsureSync waits until blockchain synchronization is complete and returns.
|
|
|
|
type EnsureSync func(context.Context) error
|
2018-03-27 16:30:37 +00:00
|
|
|
|
2017-10-21 17:04:07 +00:00
|
|
|
// EnsureNodeSync waits until node synchronzation is done to continue
|
2017-10-23 21:39:13 +00:00
|
|
|
// with tests afterwards. Panics in case of an error or a timeout.
|
2018-04-09 07:16:43 +00:00
|
|
|
func EnsureNodeSync(ensureSync EnsureSync) {
|
|
|
|
ctx, cancel := context.WithTimeout(context.Background(), syncTimeout)
|
|
|
|
defer cancel()
|
2017-10-23 11:05:52 +00:00
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
if err := ensureSync(ctx); err != nil {
|
2017-10-23 21:39:13 +00:00
|
|
|
panic(err)
|
2017-10-21 17:04:07 +00:00
|
|
|
}
|
|
|
|
}
|
2017-10-24 10:23:53 +00:00
|
|
|
|
2017-11-07 17:46:11 +00:00
|
|
|
// GetRemoteURLFromNetworkID returns associated network url for giving network id.
|
2017-10-26 13:11:24 +00:00
|
|
|
func GetRemoteURLFromNetworkID(id int) (url string, err error) {
|
2017-10-25 12:08:33 +00:00
|
|
|
switch id {
|
|
|
|
case params.MainNetworkID:
|
2017-10-26 13:11:24 +00:00
|
|
|
url = params.MainnetEthereumNetworkURL
|
2019-03-14 08:43:32 +00:00
|
|
|
case params.GoerliNetworkID:
|
|
|
|
url = params.GoerliEthereumNetworkURL
|
2017-11-07 17:46:11 +00:00
|
|
|
default:
|
|
|
|
err = ErrNoRemoteURL
|
2017-10-25 12:08:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
return
|
2017-10-25 12:08:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-25 22:07:50 +00:00
|
|
|
// GetHeadHashFromNetworkID returns the hash associated with a given network id.
|
|
|
|
func GetHeadHashFromNetworkID(id int) string {
|
2017-10-25 12:08:33 +00:00
|
|
|
switch id {
|
2018-04-18 15:13:27 +00:00
|
|
|
case params.MainNetworkID:
|
|
|
|
return "0xd4e56740f876aef8c010b86a40d5f56745a118d0906a34e69aec8c0db1cb8fa3"
|
2017-10-25 12:08:33 +00:00
|
|
|
case params.StatusChainNetworkID:
|
2017-11-20 18:21:30 +00:00
|
|
|
return "0xe9d8920a99dc66a9557a87d51f9d14a34ec50aae04298e0f142187427d3c832e"
|
2019-03-14 08:43:32 +00:00
|
|
|
case params.GoerliNetworkID:
|
|
|
|
return "0xbf7e331f7f7c1dd2e05159666b3bf8bc7a8a3a9eb1d518969eab529dd9b88c1a"
|
2017-10-25 12:08:33 +00:00
|
|
|
}
|
2018-03-15 11:36:28 +00:00
|
|
|
// Every other ID must break the test.
|
|
|
|
panic(fmt.Sprintf("invalid network id: %d", id))
|
2017-10-25 12:08:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
// GetRemoteURL returns the url associated with a given network id.
|
|
|
|
func GetRemoteURL() (string, error) {
|
|
|
|
return GetRemoteURLFromNetworkID(GetNetworkID())
|
2017-10-25 22:07:50 +00:00
|
|
|
}
|
|
|
|
|
2017-10-26 13:11:24 +00:00
|
|
|
// GetHeadHash returns the hash associated with a given network id.
|
|
|
|
func GetHeadHash() string {
|
2017-10-25 22:07:50 +00:00
|
|
|
return GetHeadHashFromNetworkID(GetNetworkID())
|
2017-10-25 12:08:33 +00:00
|
|
|
}
|
|
|
|
|
2017-10-24 10:23:53 +00:00
|
|
|
// GetNetworkID returns appropriate network id for test based on
|
2018-04-18 15:13:27 +00:00
|
|
|
// default or provided -network flag.
|
2017-10-24 10:23:53 +00:00
|
|
|
func GetNetworkID() int {
|
2017-10-24 15:13:19 +00:00
|
|
|
switch strings.ToLower(*networkSelected) {
|
2018-04-18 15:13:27 +00:00
|
|
|
case fmt.Sprintf("%d", params.MainNetworkID), "mainnet":
|
|
|
|
return params.MainNetworkID
|
2017-10-24 15:13:19 +00:00
|
|
|
case fmt.Sprintf("%d", params.StatusChainNetworkID), "statuschain":
|
2017-10-24 10:23:53 +00:00
|
|
|
return params.StatusChainNetworkID
|
2019-03-14 08:43:32 +00:00
|
|
|
case fmt.Sprintf("%d", params.GoerliNetworkID), "goerli":
|
|
|
|
return params.GoerliNetworkID
|
2017-10-24 10:23:53 +00:00
|
|
|
}
|
2018-03-15 11:36:28 +00:00
|
|
|
// Every other selected network must break the test.
|
|
|
|
panic(fmt.Sprintf("invalid selected network: %q", *networkSelected))
|
2017-10-24 10:23:53 +00:00
|
|
|
}
|
2017-11-20 18:21:30 +00:00
|
|
|
|
2018-04-18 15:13:27 +00:00
|
|
|
// CheckTestSkipForNetworks checks if network for test is one of the
|
|
|
|
// prohibited ones and skips the test in this case.
|
|
|
|
func CheckTestSkipForNetworks(t *testing.T, networks ...int) {
|
|
|
|
id := GetNetworkID()
|
|
|
|
for _, network := range networks {
|
|
|
|
if network == id {
|
|
|
|
t.Skipf("skipping test for network %d", network)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-11-20 18:21:30 +00:00
|
|
|
// GetAccount1PKFile returns the filename for Account1 keystore based
|
|
|
|
// on the current network. This allows running the e2e tests on the
|
|
|
|
// private network w/o access to the ACCOUNT_PASSWORD env variable
|
|
|
|
func GetAccount1PKFile() string {
|
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
return "test-account1-status-chain.pk"
|
|
|
|
}
|
2018-01-24 08:25:28 +00:00
|
|
|
return "test-account1.pk"
|
2017-11-20 18:21:30 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetAccount2PKFile returns the filename for Account2 keystore based
|
|
|
|
// on the current network. This allows running the e2e tests on the
|
|
|
|
// private network w/o access to the ACCOUNT_PASSWORD env variable
|
|
|
|
func GetAccount2PKFile() string {
|
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
|
|
|
return "test-account2-status-chain.pk"
|
|
|
|
}
|
2018-01-24 08:25:28 +00:00
|
|
|
return "test-account2.pk"
|
2017-11-20 18:21:30 +00:00
|
|
|
}
|
2018-01-18 16:55:17 +00:00
|
|
|
|
|
|
|
// WaitClosed used to wait on a channel in tests
|
2018-02-09 13:37:56 +00:00
|
|
|
func WaitClosed(c <-chan struct{}, d time.Duration) error {
|
2018-01-18 16:55:17 +00:00
|
|
|
timer := time.NewTimer(d)
|
|
|
|
defer timer.Stop()
|
|
|
|
select {
|
|
|
|
case <-c:
|
|
|
|
return nil
|
|
|
|
case <-timer.C:
|
|
|
|
return ErrTimeout
|
|
|
|
}
|
2018-03-02 09:25:30 +00:00
|
|
|
}
|
|
|
|
|
2018-07-21 15:32:53 +00:00
|
|
|
// MakeTestNodeConfig defines a function to return a params.NodeConfig
|
2018-03-02 09:25:30 +00:00
|
|
|
// where specific network addresses are assigned based on provided network id.
|
|
|
|
func MakeTestNodeConfig(networkID int) (*params.NodeConfig, error) {
|
|
|
|
testDir := filepath.Join(TestDataDir, TestNetworkNames[networkID])
|
2018-01-18 16:55:17 +00:00
|
|
|
|
2018-03-02 09:25:30 +00:00
|
|
|
if runtime.GOOS == "windows" {
|
|
|
|
testDir = filepath.ToSlash(testDir)
|
|
|
|
}
|
|
|
|
|
|
|
|
// run tests with "INFO" log level only
|
|
|
|
// when `go test` invoked with `-v` flag
|
|
|
|
errorLevel := "ERROR"
|
|
|
|
if testing.Verbose() {
|
|
|
|
errorLevel = "INFO"
|
|
|
|
}
|
|
|
|
|
|
|
|
configJSON := `{
|
2018-09-13 16:31:29 +00:00
|
|
|
"Name": "test",
|
2018-03-02 09:25:30 +00:00
|
|
|
"NetworkId": ` + strconv.Itoa(networkID) + `,
|
|
|
|
"DataDir": "` + testDir + `",
|
2018-09-13 16:31:29 +00:00
|
|
|
"KeyStoreDir": "` + path.Join(testDir, "keystore") + `",
|
2018-03-02 09:25:30 +00:00
|
|
|
"HTTPPort": ` + strconv.Itoa(TestConfig.Node.HTTPPort) + `,
|
|
|
|
"WSPort": ` + strconv.Itoa(TestConfig.Node.WSPort) + `,
|
2018-09-13 16:31:29 +00:00
|
|
|
"LogLevel": "` + errorLevel + `",
|
|
|
|
"NoDiscovery": true,
|
|
|
|
"LightEthConfig": {
|
|
|
|
"Enabled": true
|
|
|
|
},
|
2019-01-17 12:56:22 +00:00
|
|
|
"ShhextConfig": {
|
|
|
|
"BackupDisabledDataDir": "` + testDir + `"
|
2018-09-13 16:31:29 +00:00
|
|
|
}
|
2018-03-02 09:25:30 +00:00
|
|
|
}`
|
|
|
|
|
2018-09-13 16:31:29 +00:00
|
|
|
nodeConfig, err := params.NewConfigFromJSON(configJSON)
|
2018-03-02 09:25:30 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-06-13 11:24:04 +00:00
|
|
|
|
2018-03-02 09:25:30 +00:00
|
|
|
return nodeConfig, nil
|
2018-01-18 16:55:17 +00:00
|
|
|
}
|
2018-04-04 17:39:38 +00:00
|
|
|
|
2018-07-21 15:32:53 +00:00
|
|
|
// MakeTestNodeConfigWithDataDir defines a function to return a params.NodeConfig
|
|
|
|
// where specific network addresses are assigned based on provided network id, and assigns
|
|
|
|
// a given name and data dir.
|
2018-09-21 14:09:31 +00:00
|
|
|
func MakeTestNodeConfigWithDataDir(name, dataDir string, networkID uint64) (*params.NodeConfig, error) {
|
|
|
|
cfg, err := params.NewNodeConfig(dataDir, networkID)
|
2018-07-21 15:32:53 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-09-13 16:31:29 +00:00
|
|
|
if name == "" {
|
|
|
|
cfg.Name = "test"
|
|
|
|
} else {
|
|
|
|
cfg.Name = name
|
|
|
|
}
|
|
|
|
cfg.NoDiscovery = true
|
2018-07-21 15:32:53 +00:00
|
|
|
cfg.LightEthConfig.Enabled = false
|
2018-09-13 16:31:29 +00:00
|
|
|
if dataDir != "" {
|
|
|
|
cfg.KeyStoreDir = path.Join(dataDir, "keystore")
|
|
|
|
}
|
|
|
|
|
|
|
|
// Only attempt to validate if a dataDir is specified, we only support in-memory DB for tests
|
|
|
|
if dataDir != "" {
|
|
|
|
if err := cfg.Validate(); err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
}
|
2018-07-21 15:32:53 +00:00
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:39:38 +00:00
|
|
|
type account struct {
|
2019-01-18 09:01:14 +00:00
|
|
|
WalletAddress string
|
|
|
|
ChatAddress string
|
|
|
|
Password string
|
2018-04-04 17:39:38 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// testConfig contains shared (among different test packages) parameters
|
|
|
|
type testConfig struct {
|
|
|
|
Node struct {
|
|
|
|
SyncSeconds time.Duration
|
|
|
|
HTTPPort int
|
|
|
|
WSPort int
|
|
|
|
}
|
|
|
|
Account1 account
|
|
|
|
Account2 account
|
|
|
|
Account3 account
|
|
|
|
}
|
|
|
|
|
|
|
|
const passphraseEnvName = "ACCOUNT_PASSWORD"
|
|
|
|
|
|
|
|
// loadTestConfig loads test configuration values from disk
|
2018-04-09 07:16:43 +00:00
|
|
|
func loadTestConfig() (*testConfig, error) {
|
2018-04-04 17:39:38 +00:00
|
|
|
var config testConfig
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
err := parseTestConfigFromFile("config/test-data.json", &config)
|
2018-08-21 12:46:10 +00:00
|
|
|
if err != nil {
|
2018-04-04 17:39:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-09 07:16:43 +00:00
|
|
|
if GetNetworkID() == params.StatusChainNetworkID {
|
2019-06-09 07:24:20 +00:00
|
|
|
err := parseTestConfigFromFile("config/status-chain-accounts.json", &config)
|
2018-08-21 12:46:10 +00:00
|
|
|
if err != nil {
|
2018-04-04 17:39:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
} else {
|
2019-06-09 07:24:20 +00:00
|
|
|
err := parseTestConfigFromFile("public-chain-accounts.json", &config)
|
2018-08-21 12:46:10 +00:00
|
|
|
if err != nil {
|
2018-04-04 17:39:38 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-06-08 10:11:10 +00:00
|
|
|
pass, ok := os.LookupEnv(passphraseEnvName)
|
|
|
|
if !ok {
|
|
|
|
err := fmt.Errorf("Missing %s environment variable", passphraseEnvName)
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-04-04 17:39:38 +00:00
|
|
|
config.Account1.Password = pass
|
|
|
|
config.Account2.Password = pass
|
|
|
|
}
|
|
|
|
|
|
|
|
return &config, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportTestAccount imports keystore from static resources, see "static/keys" folder
|
|
|
|
func ImportTestAccount(keystoreDir, accountFile string) error {
|
|
|
|
// make sure that keystore folder exists
|
|
|
|
if _, err := os.Stat(keystoreDir); os.IsNotExist(err) {
|
|
|
|
os.MkdirAll(keystoreDir, os.ModePerm) // nolint: errcheck, gas
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
err error
|
|
|
|
)
|
2018-08-21 12:46:10 +00:00
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
// Allow to read keys from a custom dir.
|
|
|
|
// Fallback to embedded data.
|
|
|
|
if dir := os.Getenv("TEST_KEYS_DIR"); dir != "" {
|
|
|
|
data, err = ioutil.ReadFile(filepath.Join(dir, accountFile))
|
|
|
|
} else {
|
|
|
|
data, err = static.Asset(filepath.Join("keys", accountFile))
|
2018-08-21 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
if err != nil {
|
2018-08-21 12:46:10 +00:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
return createFile(data, filepath.Join(keystoreDir, accountFile))
|
2018-08-21 12:46:10 +00:00
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
func parseTestConfigFromFile(file string, config *testConfig) error {
|
|
|
|
var (
|
|
|
|
data []byte
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
|
|
|
|
// Allow to read config from a custom dir.
|
|
|
|
// Fallback to embedded data.
|
|
|
|
if dir := os.Getenv("TEST_CONFIG_DIR"); dir != "" {
|
|
|
|
data, err = ioutil.ReadFile(filepath.Join(dir, file))
|
|
|
|
} else {
|
|
|
|
data, err = t.Asset(file)
|
|
|
|
}
|
|
|
|
|
2018-08-21 12:46:10 +00:00
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
return json.Unmarshal(data, &config)
|
|
|
|
}
|
|
|
|
|
|
|
|
func createFile(data []byte, dst string) error {
|
2018-08-21 12:46:10 +00:00
|
|
|
out, err := os.Create(dst)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
defer out.Close()
|
|
|
|
|
2019-06-09 07:24:20 +00:00
|
|
|
_, err = io.Copy(out, bytes.NewBuffer(data))
|
|
|
|
return err
|
2018-08-21 12:46:10 +00:00
|
|
|
}
|
2018-10-23 05:11:11 +00:00
|
|
|
|
|
|
|
// Eventually will raise error if condition won't be met during the given timeout.
|
|
|
|
func Eventually(f func() error, timeout, period time.Duration) (err error) {
|
|
|
|
timer := time.NewTimer(timeout)
|
|
|
|
defer timer.Stop()
|
|
|
|
ticker := time.NewTicker(period)
|
|
|
|
defer ticker.Stop()
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case <-timer.C:
|
|
|
|
return
|
|
|
|
case <-ticker.C:
|
|
|
|
err = f()
|
|
|
|
if err == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|