210 lines
5.3 KiB
Go
210 lines
5.3 KiB
Go
package cli_test
|
|
|
|
import (
|
|
"context"
|
|
"fmt"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
tmcli "github.com/tendermint/tendermint/libs/cli"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
"github.com/cosmos/cosmos-sdk/testutil/network"
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
"github.com/cosmos/cosmos-sdk/x/slashing/client/cli"
|
|
)
|
|
|
|
type IntegrationTestSuite struct {
|
|
suite.Suite
|
|
|
|
cfg network.Config
|
|
network *network.Network
|
|
}
|
|
|
|
// SetupSuite executes bootstrapping logic before all the tests, i.e. once before
|
|
// the entire suite, start executing.
|
|
func (s *IntegrationTestSuite) SetupSuite() {
|
|
s.T().Log("setting up integration test suite")
|
|
|
|
cfg := network.DefaultConfig()
|
|
cfg.NumValidators = 1
|
|
|
|
s.cfg = cfg
|
|
s.network = network.New(s.T(), cfg)
|
|
|
|
_, err := s.network.WaitForHeight(1)
|
|
s.Require().NoError(err)
|
|
}
|
|
|
|
// TearDownSuite performs cleanup logic after all the tests, i.e. once after the
|
|
// entire suite, has finished executing.
|
|
func (s *IntegrationTestSuite) TearDownSuite() {
|
|
s.T().Log("tearing down integration test suite")
|
|
s.network.Cleanup()
|
|
}
|
|
|
|
func (s *IntegrationTestSuite) TestGetCmdQuerySigningInfo() {
|
|
val := s.network.Validators[0]
|
|
|
|
valConsPubKey, err := sdk.Bech32ifyPubKey(sdk.Bech32PubKeyTypeConsPub, val.PubKey)
|
|
s.Require().NoError(err)
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectErr bool
|
|
expectedOutput string
|
|
}{
|
|
{"invalid address", []string{"foo"}, true, ``},
|
|
{
|
|
"valid address (json output)",
|
|
[]string{
|
|
valConsPubKey,
|
|
fmt.Sprintf("--%s=json", tmcli.OutputFlag),
|
|
fmt.Sprintf("--%s=1", flags.FlagHeight),
|
|
},
|
|
false,
|
|
fmt.Sprintf(`{"address":"%s","jailed_until":"1970-01-01T00:00:00Z"}`, sdk.ConsAddress(val.PubKey.Address())),
|
|
},
|
|
{
|
|
"valid address (text output)",
|
|
[]string{
|
|
valConsPubKey,
|
|
fmt.Sprintf("--%s=text", tmcli.OutputFlag),
|
|
fmt.Sprintf("--%s=1", flags.FlagHeight),
|
|
},
|
|
false,
|
|
fmt.Sprintf(`address: %s
|
|
jailed_until: "1970-01-01T00:00:00Z"`, sdk.ConsAddress(val.PubKey.Address())),
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.GetCmdQuerySigningInfo()
|
|
_, out := testutil.ApplyMockIO(cmd)
|
|
|
|
clientCtx := val.ClientCtx.WithOutput(out)
|
|
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
|
|
|
out.Reset()
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.ExecuteContext(ctx)
|
|
if tc.expectErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *IntegrationTestSuite) TestGetCmdQueryParams() {
|
|
val := s.network.Validators[0]
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectedOutput string
|
|
}{
|
|
{
|
|
"json output",
|
|
[]string{fmt.Sprintf("--%s=json", tmcli.OutputFlag)},
|
|
`{"signed_blocks_window":"100","min_signed_per_window":"0.500000000000000000","downtime_jail_duration":"600000000000","slash_fraction_double_sign":"0.050000000000000000","slash_fraction_downtime":"0.010000000000000000"}`,
|
|
},
|
|
{
|
|
"text output",
|
|
[]string{fmt.Sprintf("--%s=text", tmcli.OutputFlag)},
|
|
`downtime_jail_duration: "600000000000"
|
|
min_signed_per_window: "0.500000000000000000"
|
|
signed_blocks_window: "100"
|
|
slash_fraction_double_sign: "0.050000000000000000"
|
|
slash_fraction_downtime: "0.010000000000000000"`,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.GetCmdQueryParams()
|
|
_, out := testutil.ApplyMockIO(cmd)
|
|
|
|
clientCtx := val.ClientCtx.WithOutput(out)
|
|
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
|
|
|
out.Reset()
|
|
cmd.SetArgs(tc.args)
|
|
|
|
s.Require().NoError(cmd.ExecuteContext(ctx))
|
|
s.Require().Equal(tc.expectedOutput, strings.TrimSpace(out.String()))
|
|
})
|
|
}
|
|
}
|
|
|
|
func (s *IntegrationTestSuite) TestNewUnjailTxCmd() {
|
|
val := s.network.Validators[0]
|
|
|
|
testCases := []struct {
|
|
name string
|
|
args []string
|
|
expectErr bool
|
|
respType fmt.Stringer
|
|
expectedCode uint32
|
|
}{
|
|
{
|
|
"valid transaction",
|
|
[]string{
|
|
fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address.String()),
|
|
fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation),
|
|
fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), // sync mode as there are no funds yet
|
|
fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()),
|
|
},
|
|
false, &sdk.TxResponse{}, 0,
|
|
},
|
|
}
|
|
|
|
for _, tc := range testCases {
|
|
tc := tc
|
|
|
|
s.Run(tc.name, func() {
|
|
cmd := cli.NewUnjailTxCmd()
|
|
_, out := testutil.ApplyMockIO(cmd)
|
|
|
|
clientCtx := val.ClientCtx.WithOutput(out)
|
|
|
|
ctx := context.Background()
|
|
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
|
|
|
out.Reset()
|
|
cmd.SetArgs(tc.args)
|
|
|
|
err := cmd.ExecuteContext(ctx)
|
|
if tc.expectErr {
|
|
s.Require().Error(err)
|
|
} else {
|
|
s.Require().NoError(err)
|
|
s.Require().NoError(clientCtx.JSONMarshaler.UnmarshalJSON(out.Bytes(), tc.respType), out.String())
|
|
|
|
txResp := tc.respType.(*sdk.TxResponse)
|
|
s.Require().Equal(tc.expectedCode, txResp.Code, out.String())
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
func TestIntegrationTestSuite(t *testing.T) {
|
|
suite.Run(t, new(IntegrationTestSuite))
|
|
}
|