refactor(upgrade): CLI tests using Tendermint Mock (#13170)

* wip

* add cli tests

Co-authored-by: Marko <marbar3778@yahoo.com>
This commit is contained in:
likhita-809 2022-09-07 20:57:12 +05:30 committed by GitHub
parent aa1b6d1450
commit ff3803fdc6
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 114 additions and 4 deletions

View File

@ -1,7 +1,7 @@
//go:build e2e
// +build e2e
package testutil
package upgrade
import (
"testing"
@ -11,7 +11,6 @@ import (
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/x/upgrade/client/testutil"
)
func TestIntegrationTestSuite(t *testing.T) {
@ -24,5 +23,5 @@ func TestIntegrationTestSuite(t *testing.T) {
app.UpgradeKeeper.SetVersionSetter(app.BaseApp)
app.UpgradeKeeper.SetModuleVersionMap(ctx, app.ModuleManager.GetVersionMap())
suite.Run(t, testutil.NewIntegrationTestSuite(cfg, app.UpgradeKeeper, ctx))
suite.Run(t, NewIntegrationTestSuite(cfg, app.UpgradeKeeper, ctx))
}

View File

@ -1,4 +1,4 @@
package testutil
package upgrade
import (
"fmt"

View File

@ -0,0 +1,111 @@
package cli_test
import (
"context"
"fmt"
"io"
"testing"
"github.com/stretchr/testify/require"
abci "github.com/tendermint/tendermint/abci/types"
"github.com/tendermint/tendermint/libs/bytes"
tmcli "github.com/tendermint/tendermint/libs/cli"
rpcclient "github.com/tendermint/tendermint/rpc/client"
rpcclientmock "github.com/tendermint/tendermint/rpc/client/mock"
coretypes "github.com/tendermint/tendermint/rpc/core/types"
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"
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
"github.com/cosmos/cosmos-sdk/x/upgrade"
upgradecli "github.com/cosmos/cosmos-sdk/x/upgrade/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 (m mockTendermintRPC) ABCIQueryWithOptions(
_ context.Context,
_ string, _ bytes.HexBytes,
_ rpcclient.ABCIQueryOptions,
) (*coretypes.ResultABCIQuery, error) {
return &coretypes.ResultABCIQuery{Response: m.responseQuery}, nil
}
func TestModuleVersionsCLI(t *testing.T) {
cmd := upgradecli.GetModuleVersionsCmd()
cmd.SetOut(io.Discard)
require.NotNil(t, cmd)
encCfg := testutilmod.MakeTestEncodingConfig(upgrade.AppModuleBasic{})
kr := keyring.NewInMemory(encCfg.Codec)
baseCtx := client.Context{}.
WithKeyring(kr).
WithTxConfig(encCfg.TxConfig).
WithCodec(encCfg.Codec).
WithClient(mockTendermintRPC{Client: rpcclientmock.Client{}}).
WithAccountRetriever(client.MockAccountRetriever{}).
WithOutput(io.Discard).
WithChainID("test-chain")
testCases := []struct {
msg string
args []string
expCmdOuptut string
}{
{
msg: "test full query with json output",
args: []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
expCmdOuptut: `--height=1 --output=json`,
},
{
msg: "test full query with text output",
args: []string{fmt.Sprintf("--%s=1", flags.FlagHeight), fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
expCmdOuptut: `--height=1 --output=text`,
},
{
msg: "test single module",
args: []string{"bank", fmt.Sprintf("--%s=1", flags.FlagHeight)},
expCmdOuptut: `bank --height=1`,
},
{
msg: "test non-existent module",
args: []string{"abcdefg", fmt.Sprintf("--%s=1", flags.FlagHeight)},
expCmdOuptut: `abcdefg --height=1`,
},
}
for _, tc := range testCases {
tc := tc
t.Run(tc.msg, func(t *testing.T) {
ctx := svrcmd.CreateExecuteContext(context.Background())
cmd.SetOut(io.Discard)
require.NotNil(t, cmd)
cmd.SetContext(ctx)
cmd.SetArgs(tc.args)
require.NoError(t, client.SetCmdClientContextHandler(baseCtx, cmd))
if len(tc.args) != 0 {
require.Contains(t, fmt.Sprint(cmd), tc.expCmdOuptut)
}
})
}
}