465 lines
10 KiB
Go
465 lines
10 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"fmt"
|
|
"io"
|
|
"strings"
|
|
"testing"
|
|
|
|
abci "github.com/cometbft/cometbft/abci/types"
|
|
rpcclientmock "github.com/cometbft/cometbft/rpc/client/mock"
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
"github.com/cosmos/cosmos-sdk/codec/address"
|
|
"github.com/cosmos/cosmos-sdk/crypto/keyring"
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
|
"github.com/cosmos/cosmos-sdk/testutil/network"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil"
|
|
"github.com/cosmos/cosmos-sdk/x/bank"
|
|
"github.com/cosmos/cosmos-sdk/x/distribution/client/cli"
|
|
distrtestutil "github.com/cosmos/cosmos-sdk/x/distribution/testutil"
|
|
"github.com/cosmos/cosmos-sdk/x/gov"
|
|
minttypes "github.com/cosmos/cosmos-sdk/x/mint/types"
|
|
)
|
|
|
|
type CLITestSuite struct {
|
|
suite.Suite
|
|
|
|
kr keyring.Keyring
|
|
encCfg testutilmod.TestEncodingConfig
|
|
baseCtx client.Context
|
|
clientCtx client.Context
|
|
}
|
|
|
|
func TestCLITestSuite(t *testing.T) {
|
|
suite.Run(t, new(CLITestSuite))
|
|
}
|
|
|
|
func (s *CLITestSuite) SetupSuite() {
|
|
s.encCfg = testutilmod.MakeTestEncodingConfig(gov.AppModuleBasic{}, bank.AppModuleBasic{})
|
|
s.kr = keyring.NewInMemory(s.encCfg.Codec)
|
|
s.baseCtx = client.Context{}.
|
|
WithKeyring(s.kr).
|
|
WithTxConfig(s.encCfg.TxConfig).
|
|
WithCodec(s.encCfg.Codec).
|
|
WithClient(clitestutil.MockCometRPC{Client: rpcclientmock.Client{}}).
|
|
WithAccountRetriever(client.MockAccountRetriever{}).
|
|
WithOutput(io.Discard).
|
|
WithChainID("test-chain")
|
|
|
|
ctxGen := func() client.Context {
|
|
bz, _ := s.encCfg.Codec.Marshal(&sdk.TxResponse{})
|
|
c := clitestutil.NewMockCometRPC(abci.ResponseQuery{
|
|
Value: bz,
|
|
})
|
|
return s.baseCtx.WithClient(c)
|
|
}
|
|
s.clientCtx = ctxGen()
|
|
|
|
cfg, err := network.DefaultConfigWithAppConfig(distrtestutil.AppConfig)
|
|
s.Require().NoError(err)
|
|
|
|
genesisState := cfg.GenesisState
|
|
var mintData minttypes.GenesisState
|
|
s.Require().NoError(cfg.Codec.UnmarshalJSON(genesisState[minttypes.ModuleName], &mintData))
|
|
|
|
inflation := sdk.MustNewDecFromStr("1.0")
|
|
mintData.Minter.Inflation = inflation
|
|
mintData.Params.InflationMin = inflation
|
|
mintData.Params.InflationMax = inflation
|
|
|
|
mintDataBz, err := cfg.Codec.MarshalJSON(&mintData)
|
|
s.Require().NoError(err)
|
|
genesisState[minttypes.ModuleName] = mintDataBz
|
|
cfg.GenesisState = genesisState
|
|
}
|
|
|
|
func (s *CLITestSuite) TestGetCmdQueryParams() {
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectedOutput string
|
|
}{
|
|
{
|
|
"json output",
|
|
[]string{fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
|
`{"community_tax":"0","base_proposer_reward":"0","bonus_proposer_reward":"0","withdraw_addr_enabled":false}`,
|
|
},
|
|
{
|
|
"text output",
|
|
[]string{fmt.Sprintf("--%s=text", flags.FlagOutput)},
|
|
`base_proposer_reward: "0"
|
|
bonus_proposer_reward: "0"
|
|
community_tax: "0"
|
|
withdraw_addr_enabled: false`,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.GetCmdQueryParams()
|
|
|
|
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *CLITestSuite) TestGetCmdQueryValidatorDistributionInfo() {
|
|
addr := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
|
|
val := sdk.ValAddress(addr[0].Address.String())
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expErr bool
|
|
}{
|
|
{
|
|
"invalid val address",
|
|
[]string{"invalid address", fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
|
true,
|
|
},
|
|
{
|
|
"json output",
|
|
[]string{val.String(), fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
|
false,
|
|
},
|
|
{
|
|
"text output",
|
|
[]string{val.String(), fmt.Sprintf("--%s=text", flags.FlagOutput)},
|
|
false,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.GetCmdQueryValidatorDistributionInfo()
|
|
|
|
_, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
|
if tc.expErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *CLITestSuite) TestGetCmdQueryValidatorOutstandingRewards() {
|
|
val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectErr bool
|
|
expectedOutput string
|
|
}{
|
|
{
|
|
"invalid validator address",
|
|
[]string{
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
"foo",
|
|
},
|
|
true,
|
|
"",
|
|
},
|
|
{
|
|
"json output",
|
|
[]string{
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
sdk.ValAddress(val[0].Address).String(),
|
|
fmt.Sprintf("--%s=json", flags.FlagOutput),
|
|
},
|
|
false,
|
|
`{"rewards":[]}`,
|
|
},
|
|
{
|
|
"text output",
|
|
[]string{
|
|
fmt.Sprintf("--%s=text", flags.FlagOutput),
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
sdk.ValAddress(val[0].Address).String(),
|
|
},
|
|
false,
|
|
`rewards: []`,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.GetCmdQueryValidatorOutstandingRewards()
|
|
|
|
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
|
if tc.expectErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *CLITestSuite) TestGetCmdQueryValidatorCommission() {
|
|
val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectErr bool
|
|
expectedOutput string
|
|
}{
|
|
{
|
|
"invalid validator address",
|
|
[]string{
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
"foo",
|
|
},
|
|
true,
|
|
"",
|
|
},
|
|
{
|
|
"json output",
|
|
[]string{
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
sdk.ValAddress(val[0].Address).String(),
|
|
fmt.Sprintf("--%s=json", flags.FlagOutput),
|
|
},
|
|
false,
|
|
`{"commission":[]}`,
|
|
},
|
|
{
|
|
"text output",
|
|
[]string{
|
|
fmt.Sprintf("--%s=text", flags.FlagOutput),
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
sdk.ValAddress(val[0].Address).String(),
|
|
},
|
|
false,
|
|
`commission: []`,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.GetCmdQueryValidatorCommission()
|
|
|
|
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
|
if tc.expectErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *CLITestSuite) TestGetCmdQueryValidatorSlashes() {
|
|
val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectErr bool
|
|
expectedOutput string
|
|
}{
|
|
{
|
|
"invalid validator address",
|
|
[]string{
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
"foo", "1", "3",
|
|
},
|
|
true,
|
|
"",
|
|
},
|
|
{
|
|
"invalid start height",
|
|
[]string{
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
sdk.ValAddress(val[0].Address).String(), "-1", "3",
|
|
},
|
|
true,
|
|
"",
|
|
},
|
|
{
|
|
"invalid end height",
|
|
[]string{
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
sdk.ValAddress(val[0].Address).String(), "1", "-3",
|
|
},
|
|
true,
|
|
"",
|
|
},
|
|
{
|
|
"json output",
|
|
[]string{
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
sdk.ValAddress(val[0].Address).String(), "1", "3",
|
|
fmt.Sprintf("--%s=json", flags.FlagOutput),
|
|
},
|
|
false,
|
|
"{\"slashes\":[],\"pagination\":null}",
|
|
},
|
|
{
|
|
"text output",
|
|
[]string{
|
|
fmt.Sprintf("--%s=text", flags.FlagOutput),
|
|
fmt.Sprintf("--%s=3", flags.FlagHeight),
|
|
sdk.ValAddress(val[0].Address).String(), "1", "3",
|
|
},
|
|
false,
|
|
"pagination: null\nslashes: []",
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.GetCmdQueryValidatorSlashes()
|
|
|
|
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
|
if tc.expectErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *CLITestSuite) TestGetCmdQueryDelegatorRewards() {
|
|
val := testutil.CreateKeyringAccounts(s.T(), s.kr, 1)
|
|
addr := val[0].Address
|
|
valAddr := sdk.ValAddress(addr)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectErr bool
|
|
expectedOutput string
|
|
}{
|
|
{
|
|
"invalid delegator address",
|
|
[]string{
|
|
fmt.Sprintf("--%s=5", flags.FlagHeight),
|
|
"foo", valAddr.String(),
|
|
},
|
|
true,
|
|
"",
|
|
},
|
|
{
|
|
"invalid validator address",
|
|
[]string{
|
|
fmt.Sprintf("--%s=5", flags.FlagHeight),
|
|
addr.String(), "foo",
|
|
},
|
|
true,
|
|
"",
|
|
},
|
|
{
|
|
"json output",
|
|
[]string{
|
|
fmt.Sprintf("--%s=5", flags.FlagHeight),
|
|
addr.String(),
|
|
fmt.Sprintf("--%s=json", flags.FlagOutput),
|
|
},
|
|
false,
|
|
`{"rewards":[],"total":[]}`,
|
|
},
|
|
{
|
|
"json output (specific validator)",
|
|
[]string{
|
|
fmt.Sprintf("--%s=5", flags.FlagHeight),
|
|
addr.String(), valAddr.String(),
|
|
fmt.Sprintf("--%s=json", flags.FlagOutput),
|
|
},
|
|
false,
|
|
`{"rewards":[]}`,
|
|
},
|
|
{
|
|
"text output",
|
|
[]string{
|
|
fmt.Sprintf("--%s=text", flags.FlagOutput),
|
|
fmt.Sprintf("--%s=5", flags.FlagHeight),
|
|
addr.String(),
|
|
},
|
|
false,
|
|
`rewards: []
|
|
total: []`,
|
|
},
|
|
{
|
|
"text output (specific validator)",
|
|
[]string{
|
|
fmt.Sprintf("--%s=text", flags.FlagOutput),
|
|
fmt.Sprintf("--%s=5", flags.FlagHeight),
|
|
addr.String(), valAddr.String(),
|
|
},
|
|
false,
|
|
`rewards: []`,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.GetCmdQueryDelegatorRewards(address.NewBech32Codec("cosmos"))
|
|
|
|
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
|
if tc.expectErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *CLITestSuite) TestGetCmdQueryCommunityPool() {
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectedOutput string
|
|
}{
|
|
{
|
|
"json output",
|
|
[]string{fmt.Sprintf("--%s=3", flags.FlagHeight), fmt.Sprintf("--%s=json", flags.FlagOutput)},
|
|
`{"pool":[]}`,
|
|
},
|
|
{
|
|
"text output",
|
|
[]string{fmt.Sprintf("--%s=text", flags.FlagOutput), fmt.Sprintf("--%s=3", flags.FlagHeight)},
|
|
`pool: []`,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.GetCmdQueryCommunityPool()
|
|
|
|
out, err := clitestutil.ExecTestCLICmd(s.clientCtx, cmd, tc.args)
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
|
})
|
|
}
|
|
}
|