Skip to content

Commit

Permalink
go-bash working
Browse files Browse the repository at this point in the history
  • Loading branch information
rigelrozanski committed Apr 18, 2018
1 parent 3ab032e commit ebb2faa
Show file tree
Hide file tree
Showing 2 changed files with 58 additions and 44 deletions.
92 changes: 52 additions & 40 deletions cmd/gaia/cmd/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,81 +3,93 @@ package common
import (
"encoding/json"
"fmt"
"io"
"strings"
"testing"
"time"

"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"

"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/tests"
"github.com/cosmos/cosmos-sdk/x/auth"
)

func password(t *testing.T, wc io.WriteCloser) {
_, err := wc.Write([]byte("1234567890\n"))
require.NoError(t, err)
}

func TestGaiaCLI(t *testing.T) {

// clear genesis/keys
_ = tests.ExecuteT(t, "gaiad unsafe_reset_all")
_, wc0, _ := tests.GoExecuteT(t, "gaiacli keys delete foo")
defer wc0.Close()
_, err := wc0.Write([]byte("1234567890\n"))
require.NoError(t, err)
_, wc1, _ := tests.GoExecuteT(t, "gaiacli keys delete bar")
defer wc1.Close()
_, err = wc1.Write([]byte("1234567890\n"))
require.NoError(t, err)
time.Sleep(time.Second)
tests.ExecuteT(t, "gaiad unsafe_reset_all")
cmd, wc0, _ := tests.GoExecuteT(t, "gaiacli keys delete foo")
password(t, wc0)
cmd.Wait()
cmd, wc1, _ := tests.GoExecuteT(t, "gaiacli keys delete bar")
password(t, wc1)
cmd.Wait()

// init genesis get master key
out := tests.ExecuteT(t, "gaiad init")
var initRes map[string]interface{}
outCut := "{" + strings.SplitN(out, "{", 2)[1]
err = json.Unmarshal([]byte(outCut), &initRes)
outCut := "{" + strings.SplitN(out, "{", 2)[1] // weird I'm sorry
err := json.Unmarshal([]byte(outCut), &initRes)
require.NoError(t, err, "out %v outCut %v err %v", out, outCut, err)
masterKey := (initRes["secret"]).(string)
chainID := (initRes["chain_id"]).(string)

servAddr := server.FreeTCPAddr(t)
gaiacliFlags := fmt.Sprintf("--node=%v --chain-id=%v", servAddr, chainID)

// start gaiad server
_, wc2, _ := tests.GoExecuteT(t, "gaiad start")
defer wc2.Close()
time.Sleep(time.Second)
cmd, _, _ = tests.GoExecuteT(t, fmt.Sprintf("gaiad start --rpc.laddr=%v", servAddr))
defer cmd.Process.Kill()

// add the master key
_, wc3, _ := tests.GoExecuteT(t, "gaiacli keys add foo --recover")
defer wc3.Close()
_, err = wc3.Write([]byte("1234567890\n"))
require.NoError(t, err)
cmd, wc3, _ := tests.GoExecuteT(t, "gaiacli keys add foo --recover")
password(t, wc3)
_, err = wc3.Write([]byte(masterKey + "\n"))
require.NoError(t, err)
time.Sleep(time.Second)
cmd.Wait()

// add a secondary key
_, wc4, _ := tests.GoExecuteT(t, "gaiacli keys add bar")
time.Sleep(time.Second * 5)
_, err = wc4.Write([]byte("1234567890\n"))
require.NoError(t, err)
time.Sleep(time.Second * 5)
cmd, wc4, _ := tests.GoExecuteT(t, "gaiacli keys add bar")
password(t, wc4)
cmd.Wait()

// get addresses
out = tests.ExecuteT(t, "gaiacli keys show foo")
fooAddr := strings.TrimLeft(out, "foo\t")
out = tests.ExecuteT(t, "gaiacli keys show bar")
barAddr := strings.TrimLeft(out, "bar\t")
fmt.Printf("debug barAddr: %v\n", barAddr)

// send money from foo to bar
cmdStr := fmt.Sprintf("gaiacli send --sequence=0 --chain-id=%v --amount=10fermion --to=%v --name=foo", chainID, barAddr)
_, wc5, rc5 := tests.GoExecuteT(t, cmdStr)
_, err = wc5.Write([]byte("1234567890\n"))
require.NoError(t, err)
fmt.Printf("debug outCh: %v\n", out)
time.Sleep(time.Second)
bz := make([]byte, 1000000)
rc5.Read(bz)
fmt.Printf("debug ex: %v\n", string(bz))
cmdStr := fmt.Sprintf("gaiacli send %v --sequence=0 --amount=10fermion --to=%v --name=foo", gaiacliFlags, barAddr)
cmd, wc5, _ := tests.GoExecuteT(t, cmdStr)
password(t, wc5)
cmd.Wait()
time.Sleep(time.Second * 3) // waiting for some blocks to pass

// verify money sent to bar
time.Sleep(time.Second)
out = tests.ExecuteT(t, fmt.Sprintf("gaiacli account %v", fooAddr))
fmt.Printf("debug out: %v\n", out)
out = tests.ExecuteT(t, fmt.Sprintf("gaiacli account %v", barAddr))
require.Fail(t, "debug out: %v\n", out)
out = tests.ExecuteT(t, fmt.Sprintf("gaiacli account %v %v", barAddr, gaiacliFlags))
barAcc := unmarshalBaseAccount(t, out)
assert.Equal(t, int64(10), barAcc.GetCoins().AmountOf("fermion"))

out = tests.ExecuteT(t, fmt.Sprintf("gaiacli account %v %v", fooAddr, gaiacliFlags))
fooAcc := unmarshalBaseAccount(t, out)
assert.Equal(t, int64(99990), fooAcc.GetCoins().AmountOf("fermion"))
}

func unmarshalBaseAccount(t *testing.T, raw string) auth.BaseAccount {
var initRes map[string]json.RawMessage
err := json.Unmarshal([]byte(raw), &initRes)
require.NoError(t, err, "raw %v, err %v", raw, err)
value := initRes["value"]
var acc auth.BaseAccount
_ = json.Unmarshal(value, &acc) //XXX pubkey can't be decoded go amino issue
require.NoError(t, err, "value %v, err %v", string(value), err)
return acc
}
10 changes: 6 additions & 4 deletions tests/gobash.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,10 +2,10 @@ package tests

import (
"io"
"os"
"os/exec"
"strings"
"testing"
"time"

"github.com/stretchr/testify/require"
)
Expand All @@ -31,12 +31,13 @@ func ExecuteT(t *testing.T, command string) (out string) {
bz, err := cmd.CombinedOutput()
require.NoError(t, err, string(bz))
out = strings.Trim(string(bz), "\n") //trim any new lines
time.Sleep(time.Second)
return out
}

// Asynchronously execute the command, return standard output and error
func GoExecuteT(t *testing.T, command string) (proc *os.Process, pipeIn io.WriteCloser, pipeOut io.ReadCloser) {
cmd := getCmd(t, command)
func GoExecuteT(t *testing.T, command string) (cmd *exec.Cmd, pipeIn io.WriteCloser, pipeOut io.ReadCloser) {
cmd = getCmd(t, command)
pipeIn, err := cmd.StdinPipe()
require.NoError(t, err)
pipeOut, err = cmd.StdoutPipe()
Expand All @@ -48,5 +49,6 @@ func GoExecuteT(t *testing.T, command string) (proc *os.Process, pipeIn io.Write
//require.NoError(t, err, string(bz))
//outChan <- strings.Trim(string(bz), "\n") //trim any new lines
}()
return nil, pipeIn, pipeOut
time.Sleep(time.Second)
return cmd, pipeIn, pipeOut
}

0 comments on commit ebb2faa

Please sign in to comment.