Skip to content

Commit

Permalink
tests: cli_tests no longer reset call unsafe_reset_all on local env
Browse files Browse the repository at this point in the history
* Makes all cli tests use .test_gaiad, .test_gaiacli instead of the
	same directories as the default binaries
* Abstracts alot of the functionality for setting up the server into
	a single function / file-wide constants. This is to reduce
	code duplication, especially since some of this functionality
	depends on each test setting up the keys in the same way.

Closes #1461
  • Loading branch information
ValarDragon committed Jul 4, 2018
1 parent f2c2032 commit e41f2bc
Show file tree
Hide file tree
Showing 2 changed files with 55 additions and 48 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -98,6 +98,7 @@ BUG FIXES
* \#1353 - CLI: Show pool shares fractions in human-readable format
* \#1258 - printing big.rat's can no longer overflow int64
* \#887 - limit the size of rationals that can be passed in from user input
* \#1461 - CLI tests now no longer reset your local environment data

## 0.19.0

Expand Down
102 changes: 54 additions & 48 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,9 @@ package clitest
import (
"encoding/json"
"fmt"
"log"
"os"
"os/user"
"testing"

"github.com/stretchr/testify/require"
Expand All @@ -21,30 +24,27 @@ import (
"github.com/cosmos/cosmos-sdk/x/stake"
)

func TestGaiaCLISend(t *testing.T) {
var (
pass = "1234567890"
gaiadHome = ""
gaiacliHome = ""
)

tests.ExecuteT(t, "gaiad unsafe_reset_all")
pass := "1234567890"
executeWrite(t, "gaiacli keys delete foo", pass)
executeWrite(t, "gaiacli keys delete bar", pass)
chainID := executeInit(t, "gaiad init -o --name=foo")
executeWrite(t, "gaiacli keys add bar", pass)
func init() {
gaiadHome, gaiacliHome = getTestingHomeDirs()
}

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

// start gaiad server
proc := tests.GoExecuteTWithStdout(t, fmt.Sprintf("gaiad start --rpc.laddr=%v", servAddr))
defer proc.Stop(false)
tests.WaitForTMStart(port)
tests.WaitForNextHeightTM(port)

fooAddr, _ := executeGetAddrPK(t, "gaiacli keys show foo --output=json")
fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show foo --output=json --home=%s", gaiacliHome))
fooCech, err := sdk.Bech32ifyAcc(fooAddr)
require.NoError(t, err)
barAddr, _ := executeGetAddrPK(t, "gaiacli keys show bar --output=json")
barAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show bar --output=json --home=%s", gaiacliHome))
barCech, err := sdk.Bech32ifyAcc(barAddr)
require.NoError(t, err)

Expand Down Expand Up @@ -79,29 +79,15 @@ func TestGaiaCLISend(t *testing.T) {
}

func TestGaiaCLICreateValidator(t *testing.T) {

tests.ExecuteT(t, "gaiad unsafe_reset_all")
pass := "1234567890"
executeWrite(t, "gaiacli keys delete foo", pass)
executeWrite(t, "gaiacli keys delete bar", pass)
chainID := executeInit(t, "gaiad init -o --name=foo")
executeWrite(t, "gaiacli keys add bar", pass)

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

// start gaiad server
proc := tests.GoExecuteTWithStdout(t, fmt.Sprintf("gaiad start --rpc.laddr=%v", servAddr))
flags, port, proc := resetTestEnv(t)
defer proc.Stop(false)
tests.WaitForTMStart(port)
tests.WaitForNextHeightTM(port)

fooAddr, _ := executeGetAddrPK(t, "gaiacli keys show foo --output=json")
fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show foo --output=json --home=%s", gaiacliHome))
fooCech, err := sdk.Bech32ifyAcc(fooAddr)
require.NoError(t, err)
barAddr, barPubKey := executeGetAddrPK(t, "gaiacli keys show bar --output=json")
barAddr, barPubKey := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show bar --output=json --home=%s", gaiacliHome))
barCech, err := sdk.Bech32ifyAcc(barAddr)
require.NoError(t, err)
barCeshPubKey, err := sdk.Bech32ifyValPub(barPubKey)
Expand Down Expand Up @@ -153,26 +139,12 @@ func TestGaiaCLICreateValidator(t *testing.T) {
}

func TestGaiaCLISubmitProposal(t *testing.T) {

tests.ExecuteT(t, "gaiad unsafe_reset_all")
pass := "1234567890"
executeWrite(t, "gaiacli keys delete foo", pass)
executeWrite(t, "gaiacli keys delete bar", pass)
chainID := executeInit(t, "gaiad init -o --name=foo")
executeWrite(t, "gaiacli keys add bar", pass)

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

// start gaiad server
proc := tests.GoExecuteTWithStdout(t, fmt.Sprintf("gaiad start --rpc.laddr=%v", servAddr))
flags, port, proc := resetTestEnv(t)
defer proc.Stop(false)
tests.WaitForTMStart(port)
tests.WaitForNextHeightTM(port)

fooAddr, _ := executeGetAddrPK(t, "gaiacli keys show foo --output=json")
fooAddr, _ := executeGetAddrPK(t, fmt.Sprintf("gaiacli keys show foo --output=json --home=%s", gaiacliHome))
fooCech, err := sdk.Bech32ifyAcc(fooAddr)
require.NoError(t, err)

Expand Down Expand Up @@ -206,6 +178,40 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
require.Equal(t, gov.VoteOptionToString(gov.OptionYes), vote.Option)
}

//___________________________________________________________________________________
// helper methods

func getTestingHomeDirs() (string, string) {
usr, err := user.Current()
if err != nil {
log.Fatal(err)
}
// Users home directory
home := usr.HomeDir
gaiadHome := fmt.Sprintf("%s%s.test_gaiad", home, string(os.PathSeparator))
gaiacliHome := fmt.Sprintf("%s%s.test_gaiacli", home, string(os.PathSeparator))
return gaiadHome, gaiacliHome
}

// returns flags, port, process
func resetTestEnv(t *testing.T) (flags string, port string, proc *tests.Process) {
tests.ExecuteT(t, fmt.Sprintf("gaiad --home=%s unsafe_reset_all", gaiadHome))
executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s foo", gaiacliHome), pass)
executeWrite(t, fmt.Sprintf("gaiacli keys delete --home=%s bar", gaiacliHome), pass)
chainID := executeInit(t, "gaiad init -o --name=foo --home="+gaiadHome)
executeWrite(t, fmt.Sprintf("gaiacli keys add --home=%s bar", gaiacliHome), pass)

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

// start gaiad server
proc = tests.GoExecuteTWithStdout(t, fmt.Sprintf("gaiad start --home=%s --rpc.laddr=%v", gaiadHome, servAddr))

return flags, port, proc
}

//___________________________________________________________________________________
// executors

Expand Down

0 comments on commit e41f2bc

Please sign in to comment.