2021-04-23 11:49:49 -07:00
|
|
|
package testutil
|
2020-08-03 23:41:58 -07:00
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/ioutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
|
|
|
|
"github.com/cosmos/cosmos-sdk/client"
|
|
|
|
"github.com/cosmos/cosmos-sdk/client/flags"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/network"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
|
2020-08-17 14:32:16 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/genutil/client/cli"
|
2020-08-03 23:41:58 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/staking/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
type IntegrationTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
cfg network.Config
|
|
|
|
network *network.Network
|
|
|
|
}
|
|
|
|
|
2021-04-23 11:49:49 -07:00
|
|
|
func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite {
|
|
|
|
return &IntegrationTestSuite{cfg: cfg}
|
|
|
|
}
|
|
|
|
|
2020-08-03 23:41:58 -07:00
|
|
|
func (s *IntegrationTestSuite) SetupSuite() {
|
|
|
|
s.T().Log("setting up integration test suite")
|
|
|
|
|
2021-06-29 03:41:55 -07:00
|
|
|
var err error
|
|
|
|
s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg)
|
|
|
|
s.Require().NoError(err)
|
2020-08-03 23:41:58 -07:00
|
|
|
|
2021-06-29 03:41:55 -07:00
|
|
|
_, err = s.network.WaitForHeight(1)
|
2020-08-03 23:41:58 -07:00
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestSuite) TearDownSuite() {
|
|
|
|
s.T().Log("tearing down integration test suite")
|
|
|
|
s.network.Cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestSuite) TestGenTxCmd() {
|
|
|
|
val := s.network.Validators[0]
|
2020-09-18 04:08:24 -07:00
|
|
|
dir := s.T().TempDir()
|
2020-08-03 23:41:58 -07:00
|
|
|
|
2020-08-17 14:32:16 -07:00
|
|
|
cmd := cli.GenTxCmd(
|
2020-08-03 23:41:58 -07:00
|
|
|
simapp.ModuleBasics,
|
|
|
|
val.ClientCtx.TxConfig, banktypes.GenesisBalancesIterator{}, val.ClientCtx.HomeDir)
|
|
|
|
|
|
|
|
_, out := testutil.ApplyMockIO(cmd)
|
|
|
|
clientCtx := val.ClientCtx.WithOutput(out)
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
ctx = context.WithValue(ctx, client.ClientContextKey, &clientCtx)
|
|
|
|
|
2021-01-05 07:08:56 -08:00
|
|
|
amount := sdk.NewCoin(s.cfg.BondDenom, sdk.NewInt(12))
|
2020-08-03 23:41:58 -07:00
|
|
|
genTxFile := filepath.Join(dir, "myTx")
|
|
|
|
cmd.SetArgs([]string{
|
2020-08-17 14:32:16 -07:00
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagChainID, s.network.Config.ChainID),
|
2020-08-03 23:41:58 -07:00
|
|
|
fmt.Sprintf("--%s=%s", flags.FlagOutputDocument, genTxFile),
|
|
|
|
val.Moniker,
|
2021-01-05 07:08:56 -08:00
|
|
|
amount.String(),
|
2020-08-03 23:41:58 -07:00
|
|
|
})
|
|
|
|
|
|
|
|
err := cmd.ExecuteContext(ctx)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
2021-01-21 01:33:02 -08:00
|
|
|
// validate generated transaction.
|
2020-08-03 23:41:58 -07:00
|
|
|
open, err := os.Open(genTxFile)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
all, err := ioutil.ReadAll(open)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
tx, err := val.ClientCtx.TxConfig.TxJSONDecoder()(all)
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
msgs := tx.GetMsgs()
|
|
|
|
s.Require().Len(msgs, 1)
|
|
|
|
|
2021-05-07 02:31:20 -07:00
|
|
|
s.Require().Equal(sdk.MsgTypeURL(&types.MsgCreateValidator{}), sdk.MsgTypeURL(msgs[0]))
|
2020-08-03 23:41:58 -07:00
|
|
|
s.Require().Equal([]sdk.AccAddress{val.Address}, msgs[0].GetSigners())
|
2021-01-05 07:08:56 -08:00
|
|
|
s.Require().Equal(amount, msgs[0].(*types.MsgCreateValidator).Value)
|
2020-08-03 23:41:58 -07:00
|
|
|
err = tx.ValidateBasic()
|
|
|
|
s.Require().NoError(err)
|
|
|
|
}
|