From c4489d9785de8b24efe12ba9209669e9dce9e23f Mon Sep 17 00:00:00 2001 From: Marko Date: Fri, 14 Apr 2023 15:32:32 +0200 Subject: [PATCH] refactor: remove duplicate vesting tests (#15844) --- tests/e2e/auth/vesting/cli_test.go | 19 -- tests/e2e/auth/vesting/suite.go | 221 --------------------- x/auth/vesting/client/cli/test.json | 13 ++ x/auth/vesting/client/cli/tx.go | 9 + x/auth/vesting/client/cli/tx_test.go | 276 +++++++++++++++++++++++++++ 5 files changed, 298 insertions(+), 240 deletions(-) delete mode 100644 tests/e2e/auth/vesting/cli_test.go delete mode 100644 tests/e2e/auth/vesting/suite.go create mode 100644 x/auth/vesting/client/cli/test.json create mode 100644 x/auth/vesting/client/cli/tx_test.go diff --git a/tests/e2e/auth/vesting/cli_test.go b/tests/e2e/auth/vesting/cli_test.go deleted file mode 100644 index 80081e2bcaf8..000000000000 --- a/tests/e2e/auth/vesting/cli_test.go +++ /dev/null @@ -1,19 +0,0 @@ -//go:build e2e -// +build e2e - -package testutil - -import ( - "testing" - - "github.com/stretchr/testify/suite" - - "cosmossdk.io/simapp" - "github.com/cosmos/cosmos-sdk/testutil/network" -) - -func TestE2ETestSuite(t *testing.T) { - cfg := network.DefaultConfig(simapp.NewTestNetworkFixture) - cfg.NumValidators = 1 - suite.Run(t, NewE2ETestSuite(cfg)) -} diff --git a/tests/e2e/auth/vesting/suite.go b/tests/e2e/auth/vesting/suite.go deleted file mode 100644 index e2bbd722ea8a..000000000000 --- a/tests/e2e/auth/vesting/suite.go +++ /dev/null @@ -1,221 +0,0 @@ -package testutil - -import ( - "fmt" - - "github.com/cosmos/gogoproto/proto" - "github.com/stretchr/testify/suite" - - "github.com/cosmos/cosmos-sdk/client/flags" - clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" - "github.com/cosmos/cosmos-sdk/testutil/network" - sdk "github.com/cosmos/cosmos-sdk/types" - "github.com/cosmos/cosmos-sdk/x/auth/vesting/client/cli" -) - -type E2ETestSuite struct { - suite.Suite - - cfg network.Config - network *network.Network -} - -func NewE2ETestSuite(cfg network.Config) *E2ETestSuite { - return &E2ETestSuite{cfg: cfg} -} - -func (s *E2ETestSuite) SetupSuite() { - s.T().Log("setting up e2e test suite") - - var err error - s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg) - s.Require().NoError(err) - s.Require().NoError(s.network.WaitForNextBlock()) -} - -func (s *E2ETestSuite) TearDownSuite() { - s.T().Log("tearing down e2e test suite") - s.network.Cleanup() -} - -func (s *E2ETestSuite) TestNewMsgCreateVestingAccountCmd() { - val := s.network.Validators[0] - - testCases := map[string]struct { - args []string - expectErr bool - expectedCode uint32 - respType proto.Message - }{ - "create a continuous vesting account": { - args: []string{ - sdk.AccAddress("addr2_______________").String(), - sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String(), - "4070908800", - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), - }, - expectErr: false, - expectedCode: 0, - respType: &sdk.TxResponse{}, - }, - "create a delayed vesting account": { - args: []string{ - sdk.AccAddress("addr3_______________").String(), - sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String(), - "4070908800", - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), - fmt.Sprintf("--%s=true", cli.FlagDelayed), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), - }, - expectErr: false, - expectedCode: 0, - respType: &sdk.TxResponse{}, - }, - "invalid address": { - args: []string{ - "addr4", - sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String(), - "4070908800", - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), - }, - expectErr: true, - expectedCode: 0, - respType: &sdk.TxResponse{}, - }, - "invalid coins": { - args: []string{ - sdk.AccAddress("addr4_______________").String(), - "fooo", - "4070908800", - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), - }, - expectErr: true, - expectedCode: 0, - respType: &sdk.TxResponse{}, - }, - "invalid end time": { - args: []string{ - sdk.AccAddress("addr4_______________").String(), - sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String(), - "-4070908800", - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), - }, - expectErr: true, - expectedCode: 0, - respType: &sdk.TxResponse{}, - }, - } - - // Synchronize height between test runs, to ensure sequence numbers are - // properly updated. - height, err := s.network.LatestHeight() - if err != nil { - s.T().Fatalf("Getting initial latest height: %v", err) - } - s.T().Logf("Initial latest height: %d", height) - for name, tc := range testCases { - tc := tc - - s.Run(name, func() { - clientCtx := val.ClientCtx - - bw, err := clitestutil.ExecTestCLICmd(clientCtx, cli.NewMsgCreateVestingAccountCmd(), tc.args) - if tc.expectErr { - s.Require().Error(err) - } else { - s.Require().NoError(err) - s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bw.Bytes(), tc.respType), bw.String()) - - txResp := tc.respType.(*sdk.TxResponse) - s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, txResp.TxHash, tc.expectedCode)) - } - }) - - next, err := s.network.WaitForHeight(height + 1) - if err != nil { - s.T().Fatalf("Waiting for height %d: %v", height+1, err) - } - height = next - s.T().Logf("Height now: %d", height) - } -} - -func (s *E2ETestSuite) TestNewMsgCreatePermanentLockedAccountCmd() { - val := s.network.Validators[0] - - testCases := map[string]struct { - args []string - expectErr bool - expectedCode uint32 - respType proto.Message - }{ - "create a permanent locked account": { - args: []string{ - sdk.AccAddress("addr4_______________").String(), - sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(100))).String(), - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), - fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), - fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), - fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String()), - }, - expectErr: false, - expectedCode: 0, - respType: &sdk.TxResponse{}, - }, - "invalid address": { - args: []string{ - "addr4", - sdk.NewCoins(sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(10))).String(), - "4070908800", - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), - }, - expectErr: true, - expectedCode: 0, - respType: &sdk.TxResponse{}, - }, - "invalid coins": { - args: []string{ - sdk.AccAddress("addr4_______________").String(), - "fooo", - "4070908800", - fmt.Sprintf("--%s=%s", flags.FlagFrom, val.Address), - }, - expectErr: true, - expectedCode: 0, - respType: &sdk.TxResponse{}, - }, - } - - // Synchronize height between test runs, to ensure sequence numbers are - // properly updated. - height, err := s.network.LatestHeight() - s.Require().NoError(err, "Getting initial latest height") - s.T().Logf("Initial latest height: %d", height) - for name, tc := range testCases { - tc := tc - - s.Run(name, func() { - clientCtx := val.ClientCtx - - bw, err := clitestutil.ExecTestCLICmd(clientCtx, cli.NewMsgCreatePermanentLockedAccountCmd(), tc.args) - if tc.expectErr { - s.Require().Error(err) - } else { - s.Require().NoError(err) - s.Require().NoError(clientCtx.Codec.UnmarshalJSON(bw.Bytes(), tc.respType), bw.String()) - - txResp := tc.respType.(*sdk.TxResponse) - s.Require().NoError(clitestutil.CheckTxCode(s.network, clientCtx, txResp.TxHash, tc.expectedCode)) - } - }) - next, err := s.network.WaitForHeight(height + 1) - s.Require().NoError(err, "Waiting for height...") - height = next - s.T().Logf("Height now: %d", height) - } -} diff --git a/x/auth/vesting/client/cli/test.json b/x/auth/vesting/client/cli/test.json new file mode 100644 index 000000000000..1ed078e28df7 --- /dev/null +++ b/x/auth/vesting/client/cli/test.json @@ -0,0 +1,13 @@ +{ + "start_time": 1625204910, + "period": [ + { + "coins": "10test", + "length_seconds": 2592000 + }, + { + "coins": "10test", + "length_seconds": 2592000 + } + ] +} diff --git a/x/auth/vesting/client/cli/tx.go b/x/auth/vesting/client/cli/tx.go index 816916cdb522..0b057d1f04d6 100644 --- a/x/auth/vesting/client/cli/tx.go +++ b/x/auth/vesting/client/cli/tx.go @@ -2,6 +2,7 @@ package cli import ( "encoding/json" + "errors" "fmt" "os" "strconv" @@ -61,6 +62,10 @@ timestamp.`, return err } + if args[1] == "" { + return errors.New("amount is empty") + } + amount, err := sdk.ParseCoinsNormalized(args[1]) if err != nil { return err @@ -104,6 +109,10 @@ tokens.`, return err } + if args[1] == "" { + return errors.New("amount is empty") + } + amount, err := sdk.ParseCoinsNormalized(args[1]) if err != nil { return err diff --git a/x/auth/vesting/client/cli/tx_test.go b/x/auth/vesting/client/cli/tx_test.go new file mode 100644 index 000000000000..f0205d20a36d --- /dev/null +++ b/x/auth/vesting/client/cli/tx_test.go @@ -0,0 +1,276 @@ +package cli_test + +import ( + "context" + "fmt" + "io" + "testing" + "time" + + sdkmath "cosmossdk.io/math" + 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/crypto/keyring" + svrcmd "github.com/cosmos/cosmos-sdk/server/cmd" + "github.com/cosmos/cosmos-sdk/testutil" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + sdk "github.com/cosmos/cosmos-sdk/types" + testutilmod "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/cosmos/cosmos-sdk/x/auth/vesting" + "github.com/cosmos/cosmos-sdk/x/auth/vesting/client/cli" +) + +type CLITestSuite struct { + suite.Suite + + kr keyring.Keyring + encCfg testutilmod.TestEncodingConfig + baseCtx client.Context +} + +func TestMigrateTestSuite(t *testing.T) { + suite.Run(t, new(CLITestSuite)) +} + +func (s *CLITestSuite) SetupSuite() { + s.encCfg = testutilmod.MakeTestEncodingConfig(vesting.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) +} + +func (s *CLITestSuite) TestNewMsgCreateVestingAccountCmd() { + accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1) + cmd := cli.NewMsgCreateVestingAccountCmd() + cmd.SetOutput(io.Discard) + + extraArgs := []string{ + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("photon", sdkmath.NewInt(10))).String()), + fmt.Sprintf("--%s=test-chain", flags.FlagChainID), + fmt.Sprintf("--%s=%s", flags.FlagFrom, accounts[0].Address), + } + + t := time.Date(2033, time.April, 1, 12, 34, 56, 789, time.UTC).Unix() + + testCases := []struct { + name string + ctxGen func() client.Context + from, to sdk.AccAddress + amount sdk.Coins + endTime int64 + extraArgs []string + expectErr bool + }{ + { + "valid transaction", + func() client.Context { + return s.baseCtx + }, + accounts[0].Address, + accounts[0].Address, + sdk.NewCoins( + sdk.NewCoin("stake", sdkmath.NewInt(10)), + sdk.NewCoin("photon", sdkmath.NewInt(40)), + ), + t, + extraArgs, + false, + }, + { + "invalid to Address", + func() client.Context { + return s.baseCtx + }, + accounts[0].Address, + sdk.AccAddress{}, + sdk.NewCoins( + sdk.NewCoin("stake", sdkmath.NewInt(10)), + sdk.NewCoin("photon", sdkmath.NewInt(40)), + ), + t, + extraArgs, + true, + }, + { + "invalid coins", + func() client.Context { + return s.baseCtx + }, + accounts[0].Address, + accounts[0].Address, + nil, + t, + extraArgs, + true, + }, + } + + for _, tc := range testCases { + tc := tc + s.Run(tc.name, func() { + ctx := svrcmd.CreateExecuteContext(context.Background()) + + cmd.SetContext(ctx) + fmt.Println(tc.amount.String()) + cmd.SetArgs(append([]string{tc.to.String(), tc.amount.String(), fmt.Sprint(tc.endTime)}, tc.extraArgs...)) + + s.Require().NoError(client.SetCmdClientContextHandler(tc.ctxGen(), cmd)) + + err := cmd.Execute() + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + } + }) + } +} + +func (s *CLITestSuite) TestNewMsgCreatePermanentLockedAccountCmd() { + accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1) + cmd := cli.NewMsgCreatePermanentLockedAccountCmd() + cmd.SetOutput(io.Discard) + + extraArgs := []string{ + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("photon", sdkmath.NewInt(10))).String()), + fmt.Sprintf("--%s=test-chain", flags.FlagChainID), + fmt.Sprintf("--%s=%s", flags.FlagFrom, accounts[0].Address), + } + + testCases := []struct { + name string + ctxGen func() client.Context + to sdk.AccAddress + amount sdk.Coins + extraArgs []string + expectErr bool + }{ + { + "valid transaction", + func() client.Context { + return s.baseCtx + }, + accounts[0].Address, + sdk.NewCoins( + sdk.NewCoin("stake", sdkmath.NewInt(10)), + sdk.NewCoin("photon", sdkmath.NewInt(40)), + ), + extraArgs, + false, + }, + { + "invalid to Address", + func() client.Context { + return s.baseCtx + }, + sdk.AccAddress{}, + sdk.NewCoins( + sdk.NewCoin("stake", sdkmath.NewInt(10)), + sdk.NewCoin("photon", sdkmath.NewInt(40)), + ), + extraArgs, + true, + }, + { + "invalid coins", + func() client.Context { + return s.baseCtx + }, + accounts[0].Address, + nil, + extraArgs, + true, + }, + } + + for _, tc := range testCases { + tc := tc + s.Run(tc.name, func() { + ctx := svrcmd.CreateExecuteContext(context.Background()) + + cmd.SetContext(ctx) + cmd.SetArgs(append([]string{tc.to.String(), tc.amount.String()}, tc.extraArgs...)) + + s.Require().NoError(client.SetCmdClientContextHandler(tc.ctxGen(), cmd)) + + err := cmd.Execute() + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + } + }) + } +} + +func (s *CLITestSuite) TestNewMsgCreatePeriodicVestingAccountCmd() { + accounts := testutil.CreateKeyringAccounts(s.T(), s.kr, 1) + cmd := cli.NewMsgCreatePeriodicVestingAccountCmd() + cmd.SetOutput(io.Discard) + + extraArgs := []string{ + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin("photon", sdkmath.NewInt(10))).String()), + fmt.Sprintf("--%s=test-chain", flags.FlagChainID), + fmt.Sprintf("--%s=%s", flags.FlagFrom, accounts[0].Address), + } + + testCases := []struct { + name string + ctxGen func() client.Context + to sdk.AccAddress + extraArgs []string + expectErr bool + }{ + { + "valid transaction", + func() client.Context { + return s.baseCtx + }, + accounts[0].Address, + extraArgs, + false, + }, + { + "invalid to Address", + func() client.Context { + return s.baseCtx + }, + sdk.AccAddress{}, + extraArgs, + true, + }, + } + + for _, tc := range testCases { + tc := tc + s.Run(tc.name, func() { + ctx := svrcmd.CreateExecuteContext(context.Background()) + + cmd.SetContext(ctx) + cmd.SetArgs(append([]string{tc.to.String(), "./test.json"}, tc.extraArgs...)) + + s.Require().NoError(client.SetCmdClientContextHandler(tc.ctxGen(), cmd)) + + err := cmd.Execute() + if tc.expectErr { + s.Require().Error(err) + } else { + s.Require().NoError(err) + } + }) + } +}