Skip to content

Commit

Permalink
better tooling for cli, couple lsd fixes
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Jun 1, 2018
1 parent a4e1e49 commit 5515b60
Show file tree
Hide file tree
Showing 6 changed files with 63 additions and 49 deletions.
10 changes: 7 additions & 3 deletions client/lcd/lcd_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,7 @@ import (
client "github.com/cosmos/cosmos-sdk/client"
keys "github.com/cosmos/cosmos-sdk/client/keys"
gapp "github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/server"
tests "github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/wire"
Expand All @@ -51,7 +52,7 @@ var (
// XXX bad globals
name = "test"
password = "0123456789"
port string // XXX: but it's the int ...
port string
seed string
sendAddr string
)
Expand Down Expand Up @@ -456,8 +457,11 @@ func startTMAndLCD() (*nm.Node, net.Listener, error) {
genDoc.AppStateJSON = appState

// LCD listen address
port = fmt.Sprintf("%d", 17377) // XXX
listenAddr := fmt.Sprintf("tcp://localhost:%s", port) // XXX
var listenAddr string
listenAddr, port, err = server.FreeTCPAddr()
if err != nil {
return nil, nil, err
}

// XXX: need to set this so LCD knows the tendermint node address!
viper.Set(client.FlagNode, config.RPC.ListenAddress)
Expand Down
13 changes: 9 additions & 4 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
)

func TestGaiaCLISend(t *testing.T) {
fmt.Println("wackydebugoutput TestGaiaCLISend 0")

tests.ExecuteT(t, "gaiad unsafe_reset_all")
pass := "1234567890"
Expand All @@ -30,13 +31,14 @@ func TestGaiaCLISend(t *testing.T) {
executeWrite(t, "gaiacli keys add bar", pass)

// get a free port, also setup some common flags
servAddr := server.FreeTCPAddr(t)
servAddr, port, err := server.FreeTCPAddr()
require.NoError(t, err)
flags := fmt.Sprintf("--node=%v --chain-id=%v", servAddr, chainID)

// start gaiad server
proc := tests.GoExecuteT(t, fmt.Sprintf("gaiad start --rpc.laddr=%v", servAddr))
defer proc.Stop(false)
time.Sleep(time.Second * 5) // Wait for RPC server to start.
tests.WaitForStart(port)

fooAddr, _ := executeGetAddrPK(t, "gaiacli keys show foo --output=json")
fooCech, err := sdk.Bech32CosmosifyAcc(fooAddr)
Expand All @@ -49,6 +51,8 @@ func TestGaiaCLISend(t *testing.T) {
assert.Equal(t, int64(50), fooAcc.GetCoins().AmountOf("steak"))

executeWrite(t, fmt.Sprintf("gaiacli send %v --amount=10steak --to=%v --name=foo", flags, barCech), pass)
fmt.Println("wackydebugoutput TestGaiaCLISend 1")
fmt.Println("wackydebugoutput TestGaiaCLISend 2")
time.Sleep(time.Second * 2) // waiting for some blocks to pass

barAcc := executeGetAccount(t, fmt.Sprintf("gaiacli account %v %v", barCech, flags))
Expand Down Expand Up @@ -76,13 +80,14 @@ func TestGaiaCLICreateValidator(t *testing.T) {
executeWrite(t, "gaiacli keys add bar", pass)

// get a free port, also setup some common flags
servAddr := server.FreeTCPAddr(t)
servAddr, port, err := server.FreeTCPAddr()
require.NoError(t, err)
flags := fmt.Sprintf("--node=%v --chain-id=%v", servAddr, chainID)

// start gaiad server
proc := tests.GoExecuteT(t, fmt.Sprintf("gaiad start --rpc.laddr=%v", servAddr))
defer proc.Stop(false)
time.Sleep(time.Second * 5) // Wait for RPC server to start.
tests.WaitForStart(port)

fooAddr, _ := executeGetAddrPK(t, "gaiacli keys show foo --output=json")
fooCech, err := sdk.Bech32CosmosifyAcc(fooAddr)
Expand Down
8 changes: 6 additions & 2 deletions server/start_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,9 @@ func TestStartStandAlone(t *testing.T) {

app, err := mock.NewApp(home, logger)
require.Nil(t, err)
svr, err := server.NewServer(FreeTCPAddr(t), "socket", app)
svrAddr, _, err := FreeTCPAddr()
require.Nil(t, err)
svr, err := server.NewServer(svrAddr, "socket", app)
require.Nil(t, err, "Error creating listener")
svr.SetLogger(logger.With("module", "abci-server"))
svr.Start()
Expand Down Expand Up @@ -69,7 +71,9 @@ func TestStartWithTendermint(t *testing.T) {
// set up app and start up
viper.Set(flagWithTendermint, true)
startCmd := StartCmd(ctx, mock.NewApp)
startCmd.Flags().Set(flagAddress, FreeTCPAddr(t)) // set to a new free address
svrAddr, _, err := FreeTCPAddr()
require.NoError(t, err)
startCmd.Flags().Set(flagAddress, svrAddr) // set to a new free address
timeout := time.Duration(5) * time.Second

close(RunOrTimeout(startCmd, timeout, t))
Expand Down
13 changes: 8 additions & 5 deletions server/test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,14 +16,17 @@ import (

// Get a free address for a test tendermint server
// protocol is either tcp, http, etc
func FreeTCPAddr(t *testing.T) string {
func FreeTCPAddr() (addr, port string, err error) {
l, err := net.Listen("tcp", "0.0.0.0:0")
defer l.Close()
require.Nil(t, err)
if err != nil {
return "", "", err
}

port := l.Addr().(*net.TCPAddr).Port
addr := fmt.Sprintf("tcp://0.0.0.0:%d", port)
return addr
portI := l.Addr().(*net.TCPAddr).Port
port = fmt.Sprintf("%d", portI)
addr = fmt.Sprintf("tcp://0.0.0.0:%s", port)
return
}

// setupViper creates a homedir to run inside,
Expand Down
7 changes: 4 additions & 3 deletions tests/tests.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,6 @@ import (
"path"
"path/filepath"
"strings"
//"strings"
"testing"
"time"

Expand Down Expand Up @@ -239,7 +238,9 @@ func StartNodeServerForTest(t *testing.T, home string) *exec.Cmd {
// expects TestInitBaseCoin to have been run
func StartLCDServerForTest(t *testing.T, home, chainID string) (cmd *exec.Cmd, port string) {
cmdName := whereIsBasecli()
port = strings.Split(server.FreeTCPAddr(t), ":")[2]
var err error
_, port, err = server.FreeTCPAddr()
require.NoError(t, err)
cmdArgs := []string{
"rest-server",
"--home",
Expand All @@ -252,7 +253,7 @@ func StartLCDServerForTest(t *testing.T, home, chainID string) (cmd *exec.Cmd, p
cmd = exec.Command(cmdName, cmdArgs...)
cmd.Stdout = os.Stdout
cmd.Stderr = os.Stderr
err := cmd.Start()
err = cmd.Start()
require.Nil(t, err)
time.Sleep(time.Second * 2) // TODO: LOL
return cmd, port
Expand Down
61 changes: 29 additions & 32 deletions tests/util.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,16 +11,22 @@ import (
rpcclient "github.com/tendermint/tendermint/rpc/lib/client"
)

// TODO: these functions just print to Stdout.
// consider using the logger.

// Uses localhost
func WaitForHeight(height int64, port string) {
for {
var resultBlock ctypes.ResultBlock

url := fmt.Sprintf("http://localhost:%v%v", port, "/blocks/latest")
res, err := http.Get(url)
url := fmt.Sprintf("http://localhost:%v/blocks/latest", port)

// get url, try a few times
var res *http.Response
var err error
for i := 0; i < 5; i++ {
res, err = http.Get(url)
if err == nil {
break
}
time.Sleep(time.Second)
}
if err != nil {
panic(err)
}
Expand All @@ -31,6 +37,7 @@ func WaitForHeight(height int64, port string) {
}
res.Body.Close()

var resultBlock ctypes.ResultBlock
err = cdc.UnmarshalJSON([]byte(body), &resultBlock)
if err != nil {
fmt.Println("RES", res)
Expand All @@ -45,45 +52,35 @@ func WaitForHeight(height int64, port string) {
}
}

// wait for 2 blocks.
// uses localhost
// wait for tendermint to start
func WaitForStart(port string) {
waitHeight := int64(2)
for {
var err error
for i := 0; i < 5; i++ {
time.Sleep(time.Second)

url := fmt.Sprintf("http://localhost:%v%v", port, "/blocks/latest")
res, err := http.Get(url)
if err != nil {
panic(err)
url := fmt.Sprintf("http://localhost:%v/blocks/latest", port)

// get url, try a few times
var res *http.Response
res, err = http.Get(url)
if err == nil || res == nil {
continue
}

// waiting for server to start ...
if res.StatusCode != http.StatusOK {
res.Body.Close()
continue
}

body, err := ioutil.ReadAll(res.Body)
if err != nil {
panic(err)
}
res.Body.Close()

resultBlock := new(ctypes.ResultBlock)
err = cdc.UnmarshalJSON([]byte(body), &resultBlock)
if err != nil {
fmt.Println("RES", res)
fmt.Println("BODY", string(body))
panic(err)
}

if resultBlock.Block.Height >= waitHeight {
return
}
}
if err != nil {
panic(err)
}
}

// TODO: these functions just print to Stdout.
// consider using the logger.

// Wait for the RPC server to respond to /status
func WaitForRPC(laddr string) {
fmt.Println("LADDR", laddr)
Expand Down

0 comments on commit 5515b60

Please sign in to comment.