2018-03-10 09:27:21 -08:00
|
|
|
package server
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"net"
|
|
|
|
"os"
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/spf13/viper"
|
|
|
|
"github.com/stretchr/testify/require"
|
2018-07-02 13:34:06 -07:00
|
|
|
"github.com/tendermint/tendermint/libs/cli"
|
2018-03-10 09:27:21 -08:00
|
|
|
)
|
|
|
|
|
|
|
|
// Get a free address for a test tendermint server
|
2018-03-12 08:31:27 -07:00
|
|
|
// protocol is either tcp, http, etc
|
2018-05-31 18:46:25 -07:00
|
|
|
func FreeTCPAddr() (addr, port string, err error) {
|
2018-03-10 09:27:21 -08:00
|
|
|
l, err := net.Listen("tcp", "0.0.0.0:0")
|
2018-05-31 18:46:25 -07:00
|
|
|
if err != nil {
|
|
|
|
return "", "", err
|
|
|
|
}
|
2018-06-28 15:52:10 -07:00
|
|
|
|
|
|
|
closer := func() {
|
|
|
|
err := l.Close()
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Handle with #870
|
|
|
|
panic(err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
defer closer()
|
2018-03-10 09:27:21 -08:00
|
|
|
|
2018-05-31 18:46:25 -07:00
|
|
|
portI := l.Addr().(*net.TCPAddr).Port
|
|
|
|
port = fmt.Sprintf("%d", portI)
|
|
|
|
addr = fmt.Sprintf("tcp://0.0.0.0:%s", port)
|
|
|
|
return
|
2018-03-10 09:27:21 -08:00
|
|
|
}
|
|
|
|
|
2018-10-10 15:45:41 -07:00
|
|
|
// SetupViper creates a homedir to run inside,
|
2018-03-10 09:27:21 -08:00
|
|
|
// and returns a cleanup function to defer
|
2018-10-10 15:45:41 -07:00
|
|
|
func SetupViper(t *testing.T) func() {
|
2018-03-10 09:27:21 -08:00
|
|
|
rootDir, err := ioutil.TempDir("", "mock-sdk-cmd")
|
|
|
|
require.Nil(t, err)
|
|
|
|
viper.Set(cli.HomeFlag, rootDir)
|
|
|
|
return func() {
|
2018-06-28 15:52:10 -07:00
|
|
|
err := os.RemoveAll(rootDir)
|
|
|
|
if err != nil {
|
|
|
|
// TODO: Handle with #870
|
|
|
|
panic(err)
|
|
|
|
}
|
2018-03-10 09:27:21 -08:00
|
|
|
}
|
|
|
|
}
|