refactor(crisis): CLI tests using Tendermint mock (#12874)
## Description Closes: #12873 - Rewrite existing x/crisis e2e tests as CLI tests using Tendermint mock - Move existing e2e tests to `tests/e2e` - Refactor x/bank account test utils for reuse in x/crisis (will also be useful elsewhere) --- ### Author Checklist *All items are required. Please add a note to the item if the item is not applicable and please add links to any relevant follow up issues.* I have... - [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] added `!` to the type prefix if API or client breaking change - [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting)) - [ ] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing) - [ ] added a changelog entry to `CHANGELOG.md` - [ ] included comments for [documenting Go code](https://blog.golang.org/godoc) - [ ] updated the relevant documentation or specification - [ ] reviewed "Files changed" and left comments if necessary - [ ] confirmed all CI checks have passed ### Reviewers Checklist *All items are required. Please add a note if the item is not applicable and please add your handle next to the items reviewed if you only reviewed selected items.* I have... - [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [ ] confirmed `!` in the type prefix if API or client breaking change - [ ] confirmed all author checklist items have been addressed - [ ] reviewed state machine logic - [ ] reviewed API design and naming - [ ] reviewed documentation is accurate - [ ] reviewed tests and test coverage - [ ] manually tested (if applicable)
This commit is contained in:
parent
014bfae00f
commit
50cec67475
@ -1,7 +1,7 @@
|
||||
//go:build e2e
|
||||
// +build e2e
|
||||
|
||||
package testsuite
|
||||
package crisis
|
||||
|
||||
import (
|
||||
"testing"
|
||||
@ -10,11 +10,10 @@ import (
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/simapp"
|
||||
"github.com/cosmos/cosmos-sdk/testutil/network"
|
||||
"github.com/cosmos/cosmos-sdk/x/crisis/client/testsuite"
|
||||
)
|
||||
|
||||
func TestIntegrationTestSuite(t *testing.T) {
|
||||
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
|
||||
cfg.NumValidators = 1
|
||||
suite.Run(t, testutil.NewIntegrationTestSuite(cfg))
|
||||
suite.Run(t, NewIntegrationTestSuite(cfg))
|
||||
}
|
||||
@ -1,4 +1,4 @@
|
||||
package testutil
|
||||
package crisis
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
37
testutil/account.go
Normal file
37
testutil/account.go
Normal file
@ -0,0 +1,37 @@
|
||||
package testutil
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
"github.com/cosmos/cosmos-sdk/types"
|
||||
)
|
||||
|
||||
type TestAccount struct {
|
||||
Name string
|
||||
Address types.AccAddress
|
||||
}
|
||||
|
||||
func CreateKeyringAccounts(t *testing.T, kr keyring.Keyring, num int) []TestAccount {
|
||||
accounts := make([]TestAccount, num)
|
||||
for i := range accounts {
|
||||
record, _, err := kr.NewMnemonic(
|
||||
fmt.Sprintf("key-%d", i),
|
||||
keyring.English,
|
||||
types.FullFundraiserPath,
|
||||
keyring.DefaultBIP39Passphrase,
|
||||
hd.Secp256k1)
|
||||
assert.NoError(t, err)
|
||||
|
||||
addr, err := record.GetAddress()
|
||||
assert.NoError(t, err)
|
||||
|
||||
accounts[i] = TestAccount{Name: record.Name, Address: addr}
|
||||
}
|
||||
|
||||
return accounts
|
||||
}
|
||||
@ -12,13 +12,14 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank/client/cli"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank/types"
|
||||
)
|
||||
|
||||
func (s *CLITestSuite) TestGetBalancesCmd() {
|
||||
accounts := s.createKeyringAccounts(1)
|
||||
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
|
||||
|
||||
cmd := cli.GetBalancesCmd()
|
||||
cmd.SetOutput(io.Discard)
|
||||
@ -40,7 +41,7 @@ func (s *CLITestSuite) TestGetBalancesCmd() {
|
||||
return s.baseCtx.WithClient(c)
|
||||
},
|
||||
[]string{
|
||||
accounts[0].address.String(),
|
||||
accounts[0].Address.String(),
|
||||
fmt.Sprintf("--%s=json", flags.FlagOutput),
|
||||
},
|
||||
&types.QueryAllBalancesResponse{},
|
||||
@ -58,7 +59,7 @@ func (s *CLITestSuite) TestGetBalancesCmd() {
|
||||
return s.baseCtx.WithClient(c)
|
||||
},
|
||||
[]string{
|
||||
accounts[0].address.String(),
|
||||
accounts[0].Address.String(),
|
||||
fmt.Sprintf("--%s=photon", cli.FlagDenom),
|
||||
fmt.Sprintf("--%s=json", flags.FlagOutput),
|
||||
},
|
||||
@ -66,7 +67,7 @@ func (s *CLITestSuite) TestGetBalancesCmd() {
|
||||
false,
|
||||
},
|
||||
{
|
||||
"invalid address",
|
||||
"invalid Address",
|
||||
func() client.Context {
|
||||
return s.baseCtx
|
||||
},
|
||||
@ -85,7 +86,7 @@ func (s *CLITestSuite) TestGetBalancesCmd() {
|
||||
return s.baseCtx.WithClient(c)
|
||||
},
|
||||
[]string{
|
||||
accounts[0].address.String(),
|
||||
accounts[0].Address.String(),
|
||||
fmt.Sprintf("--%s=foo", cli.FlagDenom),
|
||||
},
|
||||
nil,
|
||||
|
||||
@ -2,7 +2,6 @@ package cli_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"testing"
|
||||
|
||||
@ -15,9 +14,7 @@ import (
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/hd"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank"
|
||||
)
|
||||
@ -46,11 +43,6 @@ func (m mockTendermintRPC) ABCIQueryWithOptions(
|
||||
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
|
||||
}
|
||||
|
||||
type account struct {
|
||||
name string
|
||||
address sdk.AccAddress
|
||||
}
|
||||
|
||||
type CLITestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
@ -74,23 +66,3 @@ func (s *CLITestSuite) SetupSuite() {
|
||||
WithAccountRetriever(client.MockAccountRetriever{}).
|
||||
WithOutput(io.Discard)
|
||||
}
|
||||
|
||||
func (s *CLITestSuite) createKeyringAccounts(num int) []account {
|
||||
accounts := make([]account, num)
|
||||
for i := range accounts {
|
||||
record, _, err := s.kr.NewMnemonic(
|
||||
fmt.Sprintf("key-%d", i),
|
||||
keyring.English,
|
||||
sdk.FullFundraiserPath,
|
||||
keyring.DefaultBIP39Passphrase,
|
||||
hd.Secp256k1)
|
||||
s.Require().NoError(err)
|
||||
|
||||
addr, err := record.GetAddress()
|
||||
s.Require().NoError(err)
|
||||
|
||||
accounts[i] = account{name: record.Name, address: addr}
|
||||
}
|
||||
|
||||
return accounts
|
||||
}
|
||||
|
||||
@ -8,12 +8,13 @@ import (
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/cosmos/cosmos-sdk/x/bank/client/cli"
|
||||
)
|
||||
|
||||
func (s *CLITestSuite) TestSendTxCmd() {
|
||||
accounts := s.createKeyringAccounts(1)
|
||||
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
|
||||
cmd := cli.NewSendTxCmd()
|
||||
cmd.SetOutput(io.Discard)
|
||||
|
||||
@ -38,8 +39,8 @@ func (s *CLITestSuite) TestSendTxCmd() {
|
||||
func() client.Context {
|
||||
return s.baseCtx
|
||||
},
|
||||
accounts[0].address,
|
||||
accounts[0].address,
|
||||
accounts[0].Address,
|
||||
accounts[0].Address,
|
||||
sdk.NewCoins(
|
||||
sdk.NewCoin("stake", sdk.NewInt(10)),
|
||||
sdk.NewCoin("photon", sdk.NewInt(40)),
|
||||
@ -48,11 +49,11 @@ func (s *CLITestSuite) TestSendTxCmd() {
|
||||
false,
|
||||
},
|
||||
{
|
||||
"invalid to address",
|
||||
"invalid to Address",
|
||||
func() client.Context {
|
||||
return s.baseCtx
|
||||
},
|
||||
accounts[0].address,
|
||||
accounts[0].Address,
|
||||
sdk.AccAddress{},
|
||||
sdk.NewCoins(
|
||||
sdk.NewCoin("stake", sdk.NewInt(10)),
|
||||
@ -66,8 +67,8 @@ func (s *CLITestSuite) TestSendTxCmd() {
|
||||
func() client.Context {
|
||||
return s.baseCtx
|
||||
},
|
||||
accounts[0].address,
|
||||
accounts[0].address,
|
||||
accounts[0].Address,
|
||||
accounts[0].Address,
|
||||
nil,
|
||||
extraArgs,
|
||||
true,
|
||||
@ -95,7 +96,7 @@ func (s *CLITestSuite) TestSendTxCmd() {
|
||||
}
|
||||
|
||||
func (s *CLITestSuite) TestMultiSendTxCmd() {
|
||||
accounts := s.createKeyringAccounts(3)
|
||||
accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 3)
|
||||
|
||||
cmd := cli.NewMultiSendTxCmd()
|
||||
cmd.SetOutput(io.Discard)
|
||||
@ -122,10 +123,10 @@ func (s *CLITestSuite) TestMultiSendTxCmd() {
|
||||
func() client.Context {
|
||||
return s.baseCtx
|
||||
},
|
||||
accounts[0].address.String(),
|
||||
accounts[0].Address.String(),
|
||||
[]string{
|
||||
accounts[1].address.String(),
|
||||
accounts[2].address.String(),
|
||||
accounts[1].Address.String(),
|
||||
accounts[2].Address.String(),
|
||||
},
|
||||
sdk.NewCoins(
|
||||
sdk.NewCoin("stake", sdk.NewInt(10)),
|
||||
@ -135,14 +136,14 @@ func (s *CLITestSuite) TestMultiSendTxCmd() {
|
||||
false,
|
||||
},
|
||||
{
|
||||
"invalid from address",
|
||||
"invalid from Address",
|
||||
func() client.Context {
|
||||
return s.baseCtx
|
||||
},
|
||||
"foo",
|
||||
[]string{
|
||||
accounts[1].address.String(),
|
||||
accounts[2].address.String(),
|
||||
accounts[1].Address.String(),
|
||||
accounts[2].Address.String(),
|
||||
},
|
||||
sdk.NewCoins(
|
||||
sdk.NewCoin("stake", sdk.NewInt(10)),
|
||||
@ -156,9 +157,9 @@ func (s *CLITestSuite) TestMultiSendTxCmd() {
|
||||
func() client.Context {
|
||||
return s.baseCtx
|
||||
},
|
||||
accounts[0].address.String(),
|
||||
accounts[0].Address.String(),
|
||||
[]string{
|
||||
accounts[1].address.String(),
|
||||
accounts[1].Address.String(),
|
||||
"bar",
|
||||
},
|
||||
sdk.NewCoins(
|
||||
@ -173,10 +174,10 @@ func (s *CLITestSuite) TestMultiSendTxCmd() {
|
||||
func() client.Context {
|
||||
return s.baseCtx
|
||||
},
|
||||
accounts[0].address.String(),
|
||||
accounts[0].Address.String(),
|
||||
[]string{
|
||||
accounts[1].address.String(),
|
||||
accounts[2].address.String(),
|
||||
accounts[1].Address.String(),
|
||||
accounts[2].Address.String(),
|
||||
},
|
||||
nil,
|
||||
extraArgs,
|
||||
|
||||
117
x/crisis/client/cli/tx_test.go
Normal file
117
x/crisis/client/cli/tx_test.go
Normal file
@ -0,0 +1,117 @@
|
||||
package cli_test
|
||||
|
||||
import (
|
||||
"context"
|
||||
"fmt"
|
||||
"io"
|
||||
"io/ioutil"
|
||||
"testing"
|
||||
|
||||
"github.com/stretchr/testify/assert"
|
||||
abci "github.com/tendermint/tendermint/abci/types"
|
||||
rpcclientmock "github.com/tendermint/tendermint/rpc/client/mock"
|
||||
"github.com/tendermint/tendermint/rpc/coretypes"
|
||||
tmtypes "github.com/tendermint/tendermint/types"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client"
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
||||
svrcmd "github.com/cosmos/cosmos-sdk/server/cmd"
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
||||
"github.com/cosmos/cosmos-sdk/x/crisis"
|
||||
"github.com/cosmos/cosmos-sdk/x/crisis/client/cli"
|
||||
)
|
||||
|
||||
var _ client.TendermintRPC = (*mockTendermintRPC)(nil)
|
||||
|
||||
type mockTendermintRPC struct {
|
||||
rpcclientmock.Client
|
||||
|
||||
responseQuery abci.ResponseQuery
|
||||
}
|
||||
|
||||
func (_ mockTendermintRPC) BroadcastTxCommit(_ context.Context, _ tmtypes.Tx) (*coretypes.ResultBroadcastTxCommit, error) {
|
||||
return &coretypes.ResultBroadcastTxCommit{}, nil
|
||||
}
|
||||
|
||||
func TestNewMsgVerifyInvariantTxCmd(t *testing.T) {
|
||||
encCfg := testutilmod.MakeTestEncodingConfig(crisis.AppModuleBasic{})
|
||||
kr := keyring.NewInMemory(encCfg.Codec)
|
||||
baseCtx := client.Context{}.
|
||||
WithKeyring(kr).
|
||||
WithTxConfig(encCfg.TxConfig).
|
||||
WithCodec(encCfg.Codec).
|
||||
WithClient(mockTendermintRPC{Client: rpcclientmock.New()}).
|
||||
WithAccountRetriever(client.MockAccountRetriever{}).
|
||||
WithOutput(io.Discard).
|
||||
WithChainID("test-chain")
|
||||
|
||||
accounts := testutil.CreateKeyringAccounts(t, kr, 1)
|
||||
testCases := []struct {
|
||||
name string
|
||||
args []string
|
||||
expectErr bool
|
||||
errString string
|
||||
expectedCode uint32
|
||||
}{
|
||||
{
|
||||
"missing module",
|
||||
[]string{
|
||||
"", "total-supply",
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFrom, accounts[0].Address.String()),
|
||||
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()),
|
||||
},
|
||||
true, "invalid module name", 0,
|
||||
},
|
||||
{
|
||||
"missing invariant route",
|
||||
[]string{
|
||||
"bank", "",
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFrom, accounts[0].Address.String()),
|
||||
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()),
|
||||
},
|
||||
true, "invalid invariant route", 0,
|
||||
},
|
||||
{
|
||||
"valid transaction",
|
||||
[]string{
|
||||
"bank", "total-supply",
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFrom, accounts[0].Address.String()),
|
||||
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastBlock),
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(sdk.DefaultBondDenom, sdk.NewInt(10))).String()),
|
||||
},
|
||||
false, "", 0,
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
tc := tc
|
||||
t.Run(tc.name, func(t *testing.T) {
|
||||
ctx := svrcmd.CreateExecuteContext(context.Background())
|
||||
|
||||
cmd := cli.NewMsgVerifyInvariantTxCmd()
|
||||
cmd.SetOut(ioutil.Discard)
|
||||
assert.NotNil(t, cmd)
|
||||
|
||||
cmd.SetContext(ctx)
|
||||
cmd.SetArgs(tc.args)
|
||||
|
||||
assert.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))
|
||||
|
||||
err := cmd.Execute()
|
||||
if tc.expectErr {
|
||||
assert.Error(t, err)
|
||||
assert.Contains(t, err.Error(), tc.errString)
|
||||
} else {
|
||||
assert.NoError(t, err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
Loading…
Reference in New Issue
Block a user