Skip to content

Commit

Permalink
Fix integration tests, move DefaultKeyPass into app package
Browse files Browse the repository at this point in the history
  • Loading branch information
Alessio Treglia committed Oct 17, 2018
1 parent 6b6b7f3 commit 4c85964
Show file tree
Hide file tree
Showing 8 changed files with 32 additions and 30 deletions.
2 changes: 2 additions & 0 deletions cmd/gaia/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@ import (

const (
appName = "GaiaApp"
// DefaultKeyPass contains the default key password for genesis transactions
DefaultKeyPass = "12345678"
)

// default home directories for expected binaries
Expand Down
30 changes: 15 additions & 15 deletions cmd/gaia/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -311,7 +311,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
fooAcc := executeGetAccount(t, fmt.Sprintf("gaiacli query account %s %v", fooAddr, flags))
require.Equal(t, int64(50), fooAcc.GetCoins().AmountOf("steak").Int64())

proposalsQuery := tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals %v", flags), "")
proposalsQuery, _ := tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals %v", flags), "")
require.Equal(t, "No matching proposals found", proposalsQuery)

// submit a test proposal
Expand Down Expand Up @@ -346,7 +346,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
require.Equal(t, int64(1), proposal1.GetProposalID())
require.Equal(t, gov.StatusDepositPeriod, proposal1.GetStatus())

proposalsQuery = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals %v", flags), "")
proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals %v", flags), "")
require.Equal(t, " 1 - Test", proposalsQuery)

depositStr := fmt.Sprintf("gaiacli tx deposit %v", flags)
Expand Down Expand Up @@ -400,10 +400,10 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
require.Equal(t, int64(1), votes[0].ProposalID)
require.Equal(t, gov.OptionYes, votes[0].Option)

proposalsQuery = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --status=DepositPeriod %v", flags), "")
proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --status=DepositPeriod %v", flags), "")
require.Equal(t, "No matching proposals found", proposalsQuery)

proposalsQuery = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --status=VotingPeriod %v", flags), "")
proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --status=VotingPeriod %v", flags), "")
require.Equal(t, " 1 - Test", proposalsQuery)

// submit a second test proposal
Expand All @@ -417,7 +417,7 @@ func TestGaiaCLISubmitProposal(t *testing.T) {
executeWrite(t, spStr, app.DefaultKeyPass)
tests.WaitForNextNBlocksTM(2, port)

proposalsQuery = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --latest=1 %v", flags), "")
proposalsQuery, _ = tests.ExecuteT(t, fmt.Sprintf("gaiacli query proposals --latest=1 %v", flags), "")
require.Equal(t, " 2 - Apples", proposalsQuery)
}

Expand Down Expand Up @@ -628,10 +628,10 @@ func executeWriteRetStdStreams(t *testing.T, cmdStr string, writes ...string) (b
}

func executeInit(t *testing.T, cmdStr string) (chainID string) {
out := tests.ExecuteT(t, cmdStr, app.DefaultKeyPass)
_, stderr := tests.ExecuteT(t, cmdStr, app.DefaultKeyPass)

var initRes map[string]json.RawMessage
err := json.Unmarshal([]byte(out), &initRes)
err := json.Unmarshal([]byte(stderr), &initRes)
require.NoError(t, err)

err = json.Unmarshal(initRes["chain_id"], &chainID)
Expand All @@ -641,7 +641,7 @@ func executeInit(t *testing.T, cmdStr string) (chainID string) {
}

func executeGetAddrPK(t *testing.T, cmdStr string) (sdk.AccAddress, crypto.PubKey) {
out := tests.ExecuteT(t, cmdStr, "")
out, _ := tests.ExecuteT(t, cmdStr, "")
var ko keys.KeyOutput
keys.UnmarshalJSON([]byte(out), &ko)

Expand All @@ -655,7 +655,7 @@ func executeGetAddrPK(t *testing.T, cmdStr string) (sdk.AccAddress, crypto.PubKe
}

func executeGetAccount(t *testing.T, cmdStr string) auth.BaseAccount {
out := tests.ExecuteT(t, cmdStr, "")
out, _ := tests.ExecuteT(t, cmdStr, "")
var initRes map[string]json.RawMessage
err := json.Unmarshal([]byte(out), &initRes)
require.NoError(t, err, "out %v, err %v", out, err)
Expand All @@ -672,7 +672,7 @@ func executeGetAccount(t *testing.T, cmdStr string) auth.BaseAccount {
// stake

func executeGetValidator(t *testing.T, cmdStr string) stake.Validator {
out := tests.ExecuteT(t, cmdStr, "")
out, _ := tests.ExecuteT(t, cmdStr, "")
var validator stake.Validator
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(out), &validator)
Expand All @@ -681,7 +681,7 @@ func executeGetValidator(t *testing.T, cmdStr string) stake.Validator {
}

func executeGetPool(t *testing.T, cmdStr string) stake.Pool {
out := tests.ExecuteT(t, cmdStr, "")
out, _ := tests.ExecuteT(t, cmdStr, "")
var pool stake.Pool
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(out), &pool)
Expand All @@ -690,7 +690,7 @@ func executeGetPool(t *testing.T, cmdStr string) stake.Pool {
}

func executeGetParams(t *testing.T, cmdStr string) stake.Params {
out := tests.ExecuteT(t, cmdStr, "")
out, _ := tests.ExecuteT(t, cmdStr, "")
var params stake.Params
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(out), &params)
Expand All @@ -702,7 +702,7 @@ func executeGetParams(t *testing.T, cmdStr string) stake.Params {
// gov

func executeGetProposal(t *testing.T, cmdStr string) gov.Proposal {
out := tests.ExecuteT(t, cmdStr, "")
out, _ := tests.ExecuteT(t, cmdStr, "")
var proposal gov.Proposal
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(out), &proposal)
Expand All @@ -711,7 +711,7 @@ func executeGetProposal(t *testing.T, cmdStr string) gov.Proposal {
}

func executeGetVote(t *testing.T, cmdStr string) gov.Vote {
out := tests.ExecuteT(t, cmdStr, "")
out, _ := tests.ExecuteT(t, cmdStr, "")
var vote gov.Vote
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(out), &vote)
Expand All @@ -720,7 +720,7 @@ func executeGetVote(t *testing.T, cmdStr string) gov.Vote {
}

func executeGetVotes(t *testing.T, cmdStr string) []gov.Vote {
out := tests.ExecuteT(t, cmdStr, "")
out, _ := tests.ExecuteT(t, cmdStr, "")
var votes []gov.Vote
cdc := app.MakeCodec()
err := cdc.UnmarshalJSON([]byte(out), &votes)
Expand Down
4 changes: 2 additions & 2 deletions cmd/gaia/init/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -151,7 +151,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig) (
}
memo := fmt.Sprintf("%s@%s:26656", nodeID, ip)
buf := client.BufferStdin()
prompt := fmt.Sprintf("Password for account '%s' (default %s):", moniker, server.DefaultKeyPass)
prompt := fmt.Sprintf("Password for account '%s' (default %s):", moniker, app.DefaultKeyPass)
keyPass, err = client.GetPassword(prompt, buf)
if err != nil && keyPass != "" {
// An error was returned that either failed to read the password from
Expand All @@ -160,7 +160,7 @@ func initWithConfig(cdc *codec.Codec, config *cfg.Config, initCfg InitConfig) (
return
}
if keyPass == "" {
keyPass = server.DefaultKeyPass
keyPass = app.DefaultKeyPass
}

addr, secret, err = server.GenerateSaveCoinKey(initCfg.ClientHome, moniker, keyPass, initCfg.OverwriteKeys)
Expand Down
7 changes: 4 additions & 3 deletions cmd/gaia/init/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"encoding/json"
"fmt"
"github.com/cosmos/cosmos-sdk/client"
"github.com/cosmos/cosmos-sdk/cmd/gaia/app"
"github.com/cosmos/cosmos-sdk/codec"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
Expand Down Expand Up @@ -116,7 +117,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI
memo := fmt.Sprintf("%s@%s:26656", nodeID, ip)

buf := client.BufferStdin()
prompt := fmt.Sprintf("Password for account '%s' (default %s):", nodeDirName, server.DefaultKeyPass)
prompt := fmt.Sprintf("Password for account '%s' (default %s):", nodeDirName, app.DefaultKeyPass)
keyPass, err := client.GetPassword(prompt, buf)
if err != nil && keyPass != "" {
// An error was returned that either failed to read the password from
Expand All @@ -125,7 +126,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI
return err
}
if keyPass == "" {
keyPass = server.DefaultKeyPass
keyPass = app.DefaultKeyPass
}

addr, secret, err := server.GenerateSaveCoinKey(clientDir, nodeDirName, keyPass, true)
Expand Down Expand Up @@ -154,7 +155,7 @@ func testnetWithConfig(config *cfg.Config, cdc *codec.Codec, appInit server.AppI
)
tx := auth.NewStdTx([]sdk.Msg{msg}, auth.StdFee{}, []auth.StdSignature{}, memo)
txBldr := authtx.NewTxBuilderFromCLI().WithChainID(chainID).WithMemo(memo)
signedTx, err := txBldr.SignStdTx(nodeDirName, server.DefaultKeyPass, tx, false)
signedTx, err := txBldr.SignStdTx(nodeDirName, app.DefaultKeyPass, tx, false)
if err != nil {
_ = os.RemoveAll(outDir)
return err
Expand Down
4 changes: 2 additions & 2 deletions examples/basecoin/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func executeInit(t *testing.T) {
chainID string
initRes map[string]json.RawMessage
)
out := tests.ExecuteT(t, fmt.Sprintf("basecoind --home=%s init", basecoindHome), "")
err := json.Unmarshal([]byte(out), &initRes)
_, stderr := tests.ExecuteT(t, fmt.Sprintf("basecoind --home=%s init", basecoindHome), "")
err := json.Unmarshal([]byte(stderr), &initRes)
require.NoError(t, err)
err = json.Unmarshal(initRes["chain_id"], &chainID)
require.NoError(t, err)
Expand Down
4 changes: 2 additions & 2 deletions examples/democoin/cli_test/cli_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -32,8 +32,8 @@ func executeInit(t *testing.T) {
chainID string
initRes map[string]json.RawMessage
)
out := tests.ExecuteT(t, fmt.Sprintf("democoind --home=%s init", democoindHome), "")
err := json.Unmarshal([]byte(out), &initRes)
_, stderr := tests.ExecuteT(t, fmt.Sprintf("democoind --home=%s init", democoindHome), "")
err := json.Unmarshal([]byte(stderr), &initRes)
require.NoError(t, err)
err = json.Unmarshal(initRes["chain_id"], &chainID)
require.NoError(t, err)
Expand Down
3 changes: 0 additions & 3 deletions server/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -23,9 +23,6 @@ const (
FlagChainID = "chain-id"
)

// DefaultKeyPass contains the default key password for genesis transactions
const DefaultKeyPass = "12345678"

// Storage for init command input parameters
type InitConfig struct {
ChainID string
Expand Down
8 changes: 5 additions & 3 deletions tests/gobash.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,7 +14,7 @@ import (
// ExecuteT executes the command, pipes any input to STDIN and return STDOUT,
// logging STDOUT/STDERR to t.
// nolint: errcheck
func ExecuteT(t *testing.T, cmd, input string) (out string) {
func ExecuteT(t *testing.T, cmd, input string) (stdout, stderr string) {
t.Log("Running", cmn.Cyan(cmd))

// split cmd to name and args
Expand Down Expand Up @@ -50,8 +50,10 @@ func ExecuteT(t *testing.T, cmd, input string) (out string) {
t.Log("Stderr:", cmn.Red(string(errbz)))
}

out = strings.Trim(string(outbz), "\n")
return out
stdout = strings.Trim(string(outbz), "\n")
stderr = strings.Trim(string(errbz), "\n")

return
}

// Execute the command, launch goroutines to log stdout/err to t.
Expand Down

0 comments on commit 4c85964

Please sign in to comment.