cosmos-sdk/tests/cli/helpers.go

195 lines
6.8 KiB
Go
Raw Normal View History

Add CLI tests for simd, distribution (#6095) * Added cli integration base setup * Added cmd to simapp * Fixed ci-lint issues * Fixed ci-lint issues * Addressed changes in Makefile * Updated simd to latest * Removed testnet and replay commands * Modified tx command for simcli * Did code cleanup * Removed duplication in Makefile * Refactored cli_test * Added build-sim to Makefile * Added test-cli to circleci * Added tests for staking txns * Addressed format issues * refctored tests code * Added tests for send, staking * Removed test_hepers file * Moved test_cover to contrib * Added codec in fixtures * Migrated tests to respective modules * Exported helper methods * Moved helpers to bank * Added codec to fixtures * Migrated tests to modules * Removed auth helpers from staking * Did minor code cleanup * Added test-cli to Makefile * Updated github actions * Did code refactor * Fixed github actions for cli-test * Added tests for recover keys and fee deduction * Did minor code cleanup * Added build flag to cli_tests * Moved cli_test to tests * Modified path in Makefile * Updated codec std in fixtures * Added doc for cli tests * Remove ibc genesis validation * Fix issue number * Added missing imports * Add tests for distribution and simd * Modified naming for test functions * Added test for withdraw rewards * Modified test function names * Fixed import format * Migrated helpers to package cli * Fixed github test actions * Fixed test coverage in actions * Added build sim to actions * Apply Alessio patch for tests * Removed unused imports * Added init for go tests * try fix tests * goimports what wasn't goimports'd * try fix ci * add relevant tags to integration tests * run integration tests separately * use go build -o flag and let compiler gemerate the binary with the right extension for the HOST platform rename cli-test to test-integration * update ci * rename Co-authored-by: atheesh <atheesh1> Co-authored-by: kaustubhkapatral <54210167+kaustubhkapatral@users.noreply.github.com> Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: anilCSE <anil@vitwit.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com>
2020-05-01 13:16:17 -07:00
package cli
2020-04-29 08:52:30 -07:00
import (
"encoding/json"
"fmt"
"os"
"path/filepath"
"strings"
"github.com/stretchr/testify/require"
clientkeys "github.com/cosmos/cosmos-sdk/client/keys"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/tests"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/x/auth"
)
var (
Add CLI tests for simd, distribution (#6095) * Added cli integration base setup * Added cmd to simapp * Fixed ci-lint issues * Fixed ci-lint issues * Addressed changes in Makefile * Updated simd to latest * Removed testnet and replay commands * Modified tx command for simcli * Did code cleanup * Removed duplication in Makefile * Refactored cli_test * Added build-sim to Makefile * Added test-cli to circleci * Added tests for staking txns * Addressed format issues * refctored tests code * Added tests for send, staking * Removed test_hepers file * Moved test_cover to contrib * Added codec in fixtures * Migrated tests to respective modules * Exported helper methods * Moved helpers to bank * Added codec to fixtures * Migrated tests to modules * Removed auth helpers from staking * Did minor code cleanup * Added test-cli to Makefile * Updated github actions * Did code refactor * Fixed github actions for cli-test * Added tests for recover keys and fee deduction * Did minor code cleanup * Added build flag to cli_tests * Moved cli_test to tests * Modified path in Makefile * Updated codec std in fixtures * Added doc for cli tests * Remove ibc genesis validation * Fix issue number * Added missing imports * Add tests for distribution and simd * Modified naming for test functions * Added test for withdraw rewards * Modified test function names * Fixed import format * Migrated helpers to package cli * Fixed github test actions * Fixed test coverage in actions * Added build sim to actions * Apply Alessio patch for tests * Removed unused imports * Added init for go tests * try fix tests * goimports what wasn't goimports'd * try fix ci * add relevant tags to integration tests * run integration tests separately * use go build -o flag and let compiler gemerate the binary with the right extension for the HOST platform rename cli-test to test-integration * update ci * rename Co-authored-by: atheesh <atheesh1> Co-authored-by: kaustubhkapatral <54210167+kaustubhkapatral@users.noreply.github.com> Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: anilCSE <anil@vitwit.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com>
2020-05-01 13:16:17 -07:00
StartCoins = sdk.NewCoins(
2020-04-29 08:52:30 -07:00
sdk.NewCoin(Fee2Denom, sdk.TokensFromConsensusPower(1000000)),
sdk.NewCoin(FeeDenom, sdk.TokensFromConsensusPower(1000000)),
sdk.NewCoin(FooDenom, sdk.TokensFromConsensusPower(1000)),
sdk.NewCoin(Denom, sdk.TokensFromConsensusPower(150)),
)
Add CLI tests for simd, distribution (#6095) * Added cli integration base setup * Added cmd to simapp * Fixed ci-lint issues * Fixed ci-lint issues * Addressed changes in Makefile * Updated simd to latest * Removed testnet and replay commands * Modified tx command for simcli * Did code cleanup * Removed duplication in Makefile * Refactored cli_test * Added build-sim to Makefile * Added test-cli to circleci * Added tests for staking txns * Addressed format issues * refctored tests code * Added tests for send, staking * Removed test_hepers file * Moved test_cover to contrib * Added codec in fixtures * Migrated tests to respective modules * Exported helper methods * Moved helpers to bank * Added codec to fixtures * Migrated tests to modules * Removed auth helpers from staking * Did minor code cleanup * Added test-cli to Makefile * Updated github actions * Did code refactor * Fixed github actions for cli-test * Added tests for recover keys and fee deduction * Did minor code cleanup * Added build flag to cli_tests * Moved cli_test to tests * Modified path in Makefile * Updated codec std in fixtures * Added doc for cli tests * Remove ibc genesis validation * Fix issue number * Added missing imports * Add tests for distribution and simd * Modified naming for test functions * Added test for withdraw rewards * Modified test function names * Fixed import format * Migrated helpers to package cli * Fixed github test actions * Fixed test coverage in actions * Added build sim to actions * Apply Alessio patch for tests * Removed unused imports * Added init for go tests * try fix tests * goimports what wasn't goimports'd * try fix ci * add relevant tags to integration tests * run integration tests separately * use go build -o flag and let compiler gemerate the binary with the right extension for the HOST platform rename cli-test to test-integration * update ci * rename Co-authored-by: atheesh <atheesh1> Co-authored-by: kaustubhkapatral <54210167+kaustubhkapatral@users.noreply.github.com> Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: anilCSE <anil@vitwit.com> Co-authored-by: Alessio Treglia <alessio@tendermint.com>
2020-05-01 13:16:17 -07:00
VestingCoins = sdk.NewCoins(
2020-04-29 08:52:30 -07:00
sdk.NewCoin(FeeDenom, sdk.TokensFromConsensusPower(500000)),
)
)
//___________________________________________________________________________________
// simd
// UnsafeResetAll is simd unsafe-reset-all
func (f *Fixtures) UnsafeResetAll(flags ...string) {
cmd := fmt.Sprintf("%s --home=%s unsafe-reset-all", f.SimdBinary, f.SimdHome)
ExecuteWrite(f.T, AddFlags(cmd, flags))
err := os.RemoveAll(filepath.Join(f.SimdHome, "config", "gentx"))
require.NoError(f.T, err)
}
// SDInit is simd init
// NOTE: SDInit sets the ChainID for the Fixtures instance
func (f *Fixtures) SDInit(moniker string, flags ...string) {
cmd := fmt.Sprintf("%s init -o --home=%s %s", f.SimdBinary, f.SimdHome, moniker)
_, stderr := tests.ExecuteT(f.T, AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
var chainID string
var initRes map[string]json.RawMessage
err := json.Unmarshal([]byte(stderr), &initRes)
require.NoError(f.T, err)
err = json.Unmarshal(initRes["chain_id"], &chainID)
require.NoError(f.T, err)
f.ChainID = chainID
}
// AddGenesisAccount is simd add-genesis-account
func (f *Fixtures) AddGenesisAccount(address sdk.AccAddress, coins sdk.Coins, flags ...string) {
cmd := fmt.Sprintf("%s add-genesis-account %s %s --home=%s --keyring-backend=test", f.SimdBinary, address, coins, f.SimdHome)
ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags))
}
// GenTx is simd gentx
func (f *Fixtures) GenTx(name string, flags ...string) {
cmd := fmt.Sprintf("%s gentx --name=%s --home=%s --home-client=%s --keyring-backend=test", f.SimdBinary, name, f.SimdHome, f.SimcliHome)
ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags))
}
// CollectGenTxs is simd collect-gentxs
func (f *Fixtures) CollectGenTxs(flags ...string) {
cmd := fmt.Sprintf("%s collect-gentxs --home=%s", f.SimdBinary, f.SimdHome)
ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags))
}
// SDStart runs simd start with the appropriate flags and returns a process
func (f *Fixtures) SDStart(flags ...string) *tests.Process {
cmd := fmt.Sprintf("%s start --home=%s --rpc.laddr=%v --p2p.laddr=%v", f.SimdBinary, f.SimdHome, f.RPCAddr, f.P2PAddr)
proc := tests.GoExecuteTWithStdout(f.T, AddFlags(cmd, flags))
tests.WaitForTMStart(f.Port)
tests.WaitForNextNBlocksTM(1, f.Port)
return proc
}
// SDTendermint returns the results of simd tendermint [query]
func (f *Fixtures) SDTendermint(query string) string {
cmd := fmt.Sprintf("%s tendermint %s --home=%s", f.SimdBinary, query, f.SimdHome)
success, stdout, stderr := ExecuteWriteRetStdStreams(f.T, cmd)
require.Empty(f.T, stderr)
require.True(f.T, success)
return strings.TrimSpace(stdout)
}
// ValidateGenesis runs simd validate-genesis
func (f *Fixtures) ValidateGenesis() {
cmd := fmt.Sprintf("%s validate-genesis --home=%s", f.SimdBinary, f.SimdHome)
ExecuteWriteCheckErr(f.T, cmd)
}
//___________________________________________________________________________________
// simcli keys
// KeysDelete is simcli keys delete
func (f *Fixtures) KeysDelete(name string, flags ...string) {
cmd := fmt.Sprintf("%s keys delete --keyring-backend=test --home=%s %s", f.SimcliBinary,
f.SimcliHome, name)
ExecuteWrite(f.T, AddFlags(cmd, append(append(flags, "-y"), "-f")))
}
// KeysAdd is simcli keys add
func (f *Fixtures) KeysAdd(name string, flags ...string) {
cmd := fmt.Sprintf("%s keys add --keyring-backend=test --home=%s %s", f.SimcliBinary,
f.SimcliHome, name)
ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags))
}
// KeysAddRecover prepares simcli keys add --recover
func (f *Fixtures) KeysAddRecover(name, mnemonic string, flags ...string) (exitSuccess bool, stdout, stderr string) {
cmd := fmt.Sprintf("%s keys add --keyring-backend=test --home=%s --recover %s",
f.SimcliBinary, f.SimcliHome, name)
return ExecuteWriteRetStdStreams(f.T, AddFlags(cmd, flags), mnemonic)
}
// KeysAddRecoverHDPath prepares simcli keys add --recover --account --index
func (f *Fixtures) KeysAddRecoverHDPath(name, mnemonic string, account uint32, index uint32, flags ...string) {
cmd := fmt.Sprintf("%s keys add --keyring-backend=test --home=%s --recover %s --account %d"+
" --index %d", f.SimcliBinary, f.SimcliHome, name, account, index)
ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags), mnemonic)
}
// KeysShow is simcli keys show
func (f *Fixtures) KeysShow(name string, flags ...string) keyring.KeyOutput {
cmd := fmt.Sprintf("%s keys show --keyring-backend=test --home=%s %s", f.SimcliBinary,
f.SimcliHome, name)
out, _ := tests.ExecuteT(f.T, AddFlags(cmd, flags), "")
var ko keyring.KeyOutput
err := clientkeys.UnmarshalJSON([]byte(out), &ko)
require.NoError(f.T, err)
return ko
}
// KeyAddress returns the SDK account address from the key
func (f *Fixtures) KeyAddress(name string) sdk.AccAddress {
ko := f.KeysShow(name)
accAddr, err := sdk.AccAddressFromBech32(ko.Address)
require.NoError(f.T, err)
return accAddr
}
//___________________________________________________________________________________
// simcli config
// CLIConfig is simcli config
func (f *Fixtures) CLIConfig(key, value string, flags ...string) {
cmd := fmt.Sprintf("%s config --home=%s %s %s", f.SimcliBinary, f.SimcliHome, key, value)
ExecuteWriteCheckErr(f.T, AddFlags(cmd, flags))
}
// TxBroadcast is simcli tx broadcast
2020-04-29 08:52:30 -07:00
func (f *Fixtures) TxBroadcast(fileName string, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx broadcast %v %v", f.SimcliBinary, f.Flags(), fileName)
return ExecuteWriteRetStdStreams(f.T, AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
// TxEncode is simcli tx encode
2020-04-29 08:52:30 -07:00
func (f *Fixtures) TxEncode(fileName string, flags ...string) (bool, string, string) {
cmd := fmt.Sprintf("%s tx encode %v %v", f.SimcliBinary, f.Flags(), fileName)
return ExecuteWriteRetStdStreams(f.T, AddFlags(cmd, flags), clientkeys.DefaultKeyPass)
}
//utils
func AddFlags(cmd string, flags []string) string {
for _, f := range flags {
cmd += " " + f
}
return strings.TrimSpace(cmd)
}
func UnmarshalStdTx(t require.TestingT, c *codec.Codec, s string) (stdTx auth.StdTx) {
2020-04-29 08:52:30 -07:00
require.Nil(t, c.UnmarshalJSON([]byte(s), &stdTx))
return
}
func MarshalStdTx(t require.TestingT, c *codec.Codec, stdTx auth.StdTx) []byte {
bz, err := c.MarshalBinaryBare(stdTx)
require.NoError(t, err)
return bz
}