|
1 | 1 | package testutil
|
2 | 2 |
|
3 | 3 | import (
|
4 |
| - "context" |
5 | 4 | "fmt"
|
6 |
| - "io/ioutil" |
| 5 | + "io" |
7 | 6 | "os"
|
8 | 7 | "path/filepath"
|
9 | 8 |
|
10 | 9 | "github.com/stretchr/testify/suite"
|
11 | 10 |
|
12 |
| - "github.com/cosmos/cosmos-sdk/client" |
13 | 11 | "github.com/cosmos/cosmos-sdk/client/flags"
|
14 | 12 | "github.com/cosmos/cosmos-sdk/simapp"
|
15 |
| - "github.com/cosmos/cosmos-sdk/testutil" |
| 13 | + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" |
16 | 14 | "github.com/cosmos/cosmos-sdk/testutil/network"
|
17 | 15 | sdk "github.com/cosmos/cosmos-sdk/types"
|
18 | 16 | banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
@@ -48,84 +46,97 @@ func (s *IntegrationTestSuite) TearDownSuite() {
|
48 | 46 |
|
49 | 47 | func (s *IntegrationTestSuite) TestGenTxCmd() {
|
50 | 48 | val := s.network.Validators[0]
|
51 |
| - dir := s.T().TempDir() |
52 |
| - |
53 |
| - cmd := cli.GenTxCmd( |
54 |
| - simapp.ModuleBasics, |
55 |
| - val.ClientCtx.TxConfig, banktypes.GenesisBalancesIterator{}, val.ClientCtx.HomeDir) |
56 |
| - |
57 |
| - _, out := testutil.ApplyMockIO(cmd) |
58 |
| - clientCtx := val.ClientCtx.WithOutput(out) |
59 |
| - |
60 |
| - ctx := context.Background() |
61 |
| - ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) |
62 |
| - |
| 49 | + clientCtx := val.ClientCtx |
63 | 50 | amount := sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(12))
|
64 |
| - genTxFile := filepath.Join(dir, "myTx") |
65 |
| - cmd.SetArgs([]string{ |
66 |
| - fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID), |
67 |
| - fmt.Sprintf("--%s=%s", flags.FlagOutputDocument, genTxFile), |
68 |
| - val.Moniker, |
69 |
| - amount.String(), |
70 |
| - }) |
71 |
| - |
72 |
| - err := cmd.ExecuteContext(ctx) |
73 |
| - s.Require().NoError(err) |
74 |
| - |
75 |
| - // validate generated transaction. |
76 |
| - open, err := os.Open(genTxFile) |
77 |
| - s.Require().NoError(err) |
78 |
| - |
79 |
| - all, err := ioutil.ReadAll(open) |
80 |
| - s.Require().NoError(err) |
81 |
| - |
82 |
| - tx, err := val.ClientCtx.TxConfig.TxJSONDecoder()(all) |
83 |
| - s.Require().NoError(err) |
84 |
| - |
85 |
| - msgs := tx.GetMsgs() |
86 |
| - s.Require().Len(msgs, 1) |
87 |
| - |
88 |
| - s.Require().Equal(sdk.MsgTypeURL(&types.MsgCreateValidator{}), sdk.MsgTypeURL(msgs[0])) |
89 |
| - s.Require().Equal([]sdk.AccAddress{val.Address}, msgs[0].GetSigners()) |
90 |
| - s.Require().Equal(amount, msgs[0].(*types.MsgCreateValidator).Value) |
91 |
| - s.Require().NoError(tx.ValidateBasic()) |
92 |
| -} |
93 |
| - |
94 |
| -func (s *IntegrationTestSuite) TestGenTxCmdPubkey() { |
95 |
| - val := s.network.Validators[0] |
96 |
| - dir := s.T().TempDir() |
97 |
| - |
98 |
| - cmd := cli.GenTxCmd( |
99 |
| - simapp.ModuleBasics, |
100 |
| - val.ClientCtx.TxConfig, |
101 |
| - banktypes.GenesisBalancesIterator{}, |
102 |
| - val.ClientCtx.HomeDir, |
103 |
| - ) |
104 |
| - |
105 |
| - _, out := testutil.ApplyMockIO(cmd) |
106 |
| - clientCtx := val.ClientCtx.WithOutput(out) |
107 | 51 |
|
108 |
| - ctx := context.Background() |
109 |
| - ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx) |
110 |
| - |
111 |
| - amount := sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(12)) |
112 |
| - genTxFile := filepath.Join(dir, "myTx") |
113 |
| - |
114 |
| - cmd.SetArgs([]string{ |
115 |
| - fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID), |
116 |
| - fmt.Sprintf("--%s=%s", flags.FlagOutputDocument, genTxFile), |
117 |
| - fmt.Sprintf("--%s={\"key\":\"BOIkjkFruMpfOFC9oNPhiJGfmY2pHF/gwHdLDLnrnS0=\"}", stakingcli.FlagPubKey), |
118 |
| - val.Moniker, |
119 |
| - amount.String(), |
120 |
| - }) |
121 |
| - s.Require().Error(cmd.ExecuteContext(ctx)) |
122 |
| - |
123 |
| - cmd.SetArgs([]string{ |
124 |
| - fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID), |
125 |
| - fmt.Sprintf("--%s=%s", flags.FlagOutputDocument, genTxFile), |
126 |
| - fmt.Sprintf("--%s={\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"BOIkjkFruMpfOFC9oNPhiJGfmY2pHF/gwHdLDLnrnS0=\"}", stakingcli.FlagPubKey), |
127 |
| - val.Moniker, |
128 |
| - amount.String(), |
129 |
| - }) |
130 |
| - s.Require().NoError(cmd.ExecuteContext(ctx)) |
| 52 | + tests := []struct { |
| 53 | + name string |
| 54 | + args []string |
| 55 | + expError bool |
| 56 | + }{ |
| 57 | + { |
| 58 | + name: "invalid commission rate returns error", |
| 59 | + args: []string{ |
| 60 | + fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID), |
| 61 | + fmt.Sprintf("--%s=1", stakingcli.FlagCommissionRate), |
| 62 | + val.Moniker, |
| 63 | + amount.String(), |
| 64 | + }, |
| 65 | + expError: true, |
| 66 | + }, |
| 67 | + { |
| 68 | + name: "valid gentx", |
| 69 | + args: []string{ |
| 70 | + fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID), |
| 71 | + val.Moniker, |
| 72 | + amount.String(), |
| 73 | + }, |
| 74 | + expError: false, |
| 75 | + }, |
| 76 | + { |
| 77 | + name: "invalid pubkey", |
| 78 | + args: []string{ |
| 79 | + fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID), |
| 80 | + fmt.Sprintf("--%s={\"key\":\"BOIkjkFruMpfOFC9oNPhiJGfmY2pHF/gwHdLDLnrnS0=\"}", stakingcli.FlagPubKey), |
| 81 | + val.Moniker, |
| 82 | + amount.String(), |
| 83 | + }, |
| 84 | + expError: true, |
| 85 | + }, |
| 86 | + { |
| 87 | + name: "valid pubkey flag", |
| 88 | + args: []string{ |
| 89 | + fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID), |
| 90 | + fmt.Sprintf("--%s={\"@type\":\"/cosmos.crypto.ed25519.PubKey\",\"key\":\"BOIkjkFruMpfOFC9oNPhiJGfmY2pHF/gwHdLDLnrnS0=\"}", stakingcli.FlagPubKey), |
| 91 | + val.Moniker, |
| 92 | + amount.String(), |
| 93 | + }, |
| 94 | + expError: false, |
| 95 | + }, |
| 96 | + } |
| 97 | + |
| 98 | + for _, tc := range tests { |
| 99 | + tc := tc |
| 100 | + |
| 101 | + dir := s.T().TempDir() |
| 102 | + genTxFile := filepath.Join(dir, "myTx") |
| 103 | + tc.args = append(tc.args, fmt.Sprintf("--%s=%s", flags.FlagOutputDocument, genTxFile)) |
| 104 | + |
| 105 | + s.Run(tc.name, func() { |
| 106 | + cmd := cli.GenTxCmd( |
| 107 | + simapp.ModuleBasics, |
| 108 | + val.ClientCtx.TxConfig, |
| 109 | + banktypes.GenesisBalancesIterator{}, |
| 110 | + val.ClientCtx.HomeDir) |
| 111 | + |
| 112 | + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, tc.args) |
| 113 | + |
| 114 | + if tc.expError { |
| 115 | + s.Require().Error(err) |
| 116 | + |
| 117 | + _, err = os.Open(genTxFile) |
| 118 | + s.Require().Error(err) |
| 119 | + } else { |
| 120 | + s.Require().NoError(err, "test: %s\noutput: %s", tc.name, out.String()) |
| 121 | + |
| 122 | + // validate generated transaction. |
| 123 | + open, err := os.Open(genTxFile) |
| 124 | + s.Require().NoError(err) |
| 125 | + |
| 126 | + all, err := io.ReadAll(open) |
| 127 | + s.Require().NoError(err) |
| 128 | + |
| 129 | + tx, err := val.ClientCtx.TxConfig.TxJSONDecoder()(all) |
| 130 | + s.Require().NoError(err) |
| 131 | + |
| 132 | + msgs := tx.GetMsgs() |
| 133 | + s.Require().Len(msgs, 1) |
| 134 | + |
| 135 | + s.Require().Equal(sdk.MsgTypeURL(&types.MsgCreateValidator{}), sdk.MsgTypeURL(msgs[0])) |
| 136 | + s.Require().True(val.Address.Equals(msgs[0].GetSigners()[0])) |
| 137 | + s.Require().Equal(amount, msgs[0].(*types.MsgCreateValidator).Value) |
| 138 | + s.Require().NoError(tx.ValidateBasic()) |
| 139 | + } |
| 140 | + }) |
| 141 | + } |
131 | 142 | }
|
0 commit comments