From cc6f2070b5c30a9fb86ee8c576aff7f1bd5f9b66 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Sat, 2 Mar 2024 11:27:46 +0530 Subject: [PATCH] Setup e2e tests for bond module --- tests/e2e/auction/cli_test.go | 2 +- tests/e2e/auction/suite.go | 2 +- tests/e2e/bond/cli_test.go | 17 ++++++ tests/e2e/bond/grpc.go | 24 ++++++++ tests/e2e/bond/suite.go | 106 ++++++++++++++++++++++++++++++++++ x/bond/client/cli/tx.go | 38 ++++++++++++ x/bond/msgs.go | 17 ++++++ 7 files changed, 204 insertions(+), 2 deletions(-) create mode 100644 tests/e2e/bond/cli_test.go create mode 100644 tests/e2e/bond/grpc.go create mode 100644 tests/e2e/bond/suite.go create mode 100644 x/bond/client/cli/tx.go create mode 100644 x/bond/msgs.go diff --git a/tests/e2e/auction/cli_test.go b/tests/e2e/auction/cli_test.go index 2ba78f32..aac55bcc 100644 --- a/tests/e2e/auction/cli_test.go +++ b/tests/e2e/auction/cli_test.go @@ -9,7 +9,7 @@ import ( "git.vdb.to/cerc-io/laconic2d/tests/e2e" ) -func TestE2ETestSuite(t *testing.T) { +func TestAuctionE2ETestSuite(t *testing.T) { cfg := network.DefaultConfig(e2e.NewTestNetworkFixture) cfg.NumValidators = 1 diff --git a/tests/e2e/auction/suite.go b/tests/e2e/auction/suite.go index d408b253..ce322258 100644 --- a/tests/e2e/auction/suite.go +++ b/tests/e2e/auction/suite.go @@ -133,7 +133,7 @@ func (ets *E2ETestSuite) createAuctionAndBid(createAuction, createBid bool) stri func (ets *E2ETestSuite) cleanupBidFiles() error { matches, err := filepath.Glob(fmt.Sprintf("%s-*.json", bidderAccount)) if err != nil { - fmt.Printf("Error matching bidder files: %v\n", err) + ets.T().Errorf("Error matching bidder files: %v\n", err) return err } diff --git a/tests/e2e/bond/cli_test.go b/tests/e2e/bond/cli_test.go new file mode 100644 index 00000000..c5e5a9f8 --- /dev/null +++ b/tests/e2e/bond/cli_test.go @@ -0,0 +1,17 @@ +package bond + +import ( + "testing" + + "github.com/cosmos/cosmos-sdk/testutil/network" + "github.com/stretchr/testify/suite" + + "git.vdb.to/cerc-io/laconic2d/tests/e2e" +) + +func TestBondE2ETestSuite(t *testing.T) { + cfg := network.DefaultConfig(e2e.NewTestNetworkFixture) + cfg.NumValidators = 1 + + suite.Run(t, NewE2ETestSuite(cfg)) +} diff --git a/tests/e2e/bond/grpc.go b/tests/e2e/bond/grpc.go new file mode 100644 index 00000000..e1f6d143 --- /dev/null +++ b/tests/e2e/bond/grpc.go @@ -0,0 +1,24 @@ +package bond + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/testutil" + + bondtypes "git.vdb.to/cerc-io/laconic2d/x/bond" +) + +func (ets *E2ETestSuite) TestGRPCGetParams() { + val := ets.network.Validators[0] + sr := ets.Require() + reqURL := fmt.Sprintf("%s/cerc/bond/v1/params", val.APIAddress) + + resp, err := testutil.GetRequest(reqURL) + ets.Require().NoError(err) + + var params bondtypes.QueryParamsResponse + err = val.ClientCtx.Codec.UnmarshalJSON(resp, ¶ms) + + sr.NoError(err) + sr.Equal(params.GetParams().MaxBondAmount, bondtypes.DefaultParams().MaxBondAmount) +} diff --git a/tests/e2e/bond/suite.go b/tests/e2e/bond/suite.go new file mode 100644 index 00000000..766940ed --- /dev/null +++ b/tests/e2e/bond/suite.go @@ -0,0 +1,106 @@ +package bond + +import ( + "fmt" + + "cosmossdk.io/math" + "github.com/stretchr/testify/suite" + + "github.com/cosmos/cosmos-sdk/client/flags" + addresscodec "github.com/cosmos/cosmos-sdk/codec/address" + "github.com/cosmos/cosmos-sdk/crypto/hd" + "github.com/cosmos/cosmos-sdk/crypto/keyring" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + "github.com/cosmos/cosmos-sdk/testutil/network" + sdk "github.com/cosmos/cosmos-sdk/types" + + "git.vdb.to/cerc-io/laconic2d/x/bond/client/cli" +) + +type E2ETestSuite struct { + suite.Suite + + cfg network.Config + network *network.Network + + accountName string + accountAddress string +} + +func NewE2ETestSuite(cfg network.Config) *E2ETestSuite { + return &E2ETestSuite{cfg: cfg} +} + +func (ets *E2ETestSuite) SetupSuite() { //nolint: all + sr := ets.Require() + ets.T().Log("setting up e2e test suite") + + var err error + + ets.network, err = network.New(ets.T(), ets.T().TempDir(), ets.cfg) + sr.NoError(err) + + _, err = ets.network.WaitForHeight(1) + sr.NoError(err) + + // setting up random account + ets.accountName = "accountName" + ets.createAccountWithBalance(ets.accountName, &ets.accountAddress) +} + +func (ets *E2ETestSuite) TearDownSuite() { + ets.T().Log("tearing down integration test suite") + ets.network.Cleanup() +} + +func (ets *E2ETestSuite) createAccountWithBalance(accountName string, accountAddress *string) { + val := ets.network.Validators[0] + sr := ets.Require() + + info, _, err := val.ClientCtx.Keyring.NewMnemonic(accountName, keyring.English, sdk.FullFundraiserPath, keyring.DefaultBIP39Passphrase, hd.Secp256k1) + sr.NoError(err) + + newAddr, _ := info.GetAddress() + _, err = clitestutil.MsgSendExec( + val.ClientCtx, + val.Address, + newAddr, + sdk.NewCoins(sdk.NewCoin(ets.cfg.BondDenom, math.NewInt(200000))), + addresscodec.NewBech32Codec("laconic"), + fmt.Sprintf("--%s=%s", flags.FlagFrom, accountName), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=json", flags.FlagOutput), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, sdk.NewCoins(sdk.NewCoin(ets.cfg.BondDenom, math.NewInt(10))).String()), + ) + sr.NoError(err) + *accountAddress = newAddr.String() + + // wait for tx to take effect + err = ets.network.WaitForNextBlock() + sr.NoError(err) +} + +func (ets *E2ETestSuite) createBond() { + val := ets.network.Validators[0] + sr := ets.Require() + createBondCmd := cli.NewCreateBondCmd() + args := []string{ + fmt.Sprintf("10%s", ets.cfg.BondDenom), + fmt.Sprintf("--%s=%s", flags.FlagFrom, ets.accountName), + fmt.Sprintf("--%s=json", flags.FlagOutput), + fmt.Sprintf("--%s=true", flags.FlagSkipConfirmation), + fmt.Sprintf("--%s=%s", flags.FlagBroadcastMode, flags.BroadcastSync), + fmt.Sprintf("--%s=%s", flags.FlagFees, fmt.Sprintf("3%s", ets.cfg.BondDenom)), + } + out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, createBondCmd, args) + sr.NoError(err) + var d sdk.TxResponse + err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) + sr.NoError(err) + sr.Zero(d.Code) + + // wait for tx to take effect + err = ets.network.WaitForNextBlock() + sr.NoError(err) +} diff --git a/x/bond/client/cli/tx.go b/x/bond/client/cli/tx.go new file mode 100644 index 00000000..d93f8efb --- /dev/null +++ b/x/bond/client/cli/tx.go @@ -0,0 +1,38 @@ +package cli + +import ( + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + "github.com/cosmos/cosmos-sdk/client/tx" + sdk "github.com/cosmos/cosmos-sdk/types" + "github.com/spf13/cobra" + + bondtypes "git.vdb.to/cerc-io/laconic2d/x/bond" +) + +// NewCreateBondCmd is the CLI command for creating a bond. +// Used in e2e tests +func NewCreateBondCmd() *cobra.Command { + cmd := &cobra.Command{ + Use: "create [amount]", + Short: "Create bond.", + Args: cobra.ExactArgs(1), + RunE: func(cmd *cobra.Command, args []string) error { + clientCtx, err := client.GetClientTxContext(cmd) + if err != nil { + return err + } + coin, err := sdk.ParseCoinNormalized(args[0]) + if err != nil { + return err + } + + msg := bondtypes.NewMsgCreateBond(sdk.NewCoins(coin), clientCtx.GetFromAddress()) + return tx.GenerateOrBroadcastTxCLI(clientCtx, cmd.Flags(), &msg) + }, + } + + flags.AddTxFlagsToCmd(cmd) + + return cmd +} diff --git a/x/bond/msgs.go b/x/bond/msgs.go new file mode 100644 index 00000000..c473c612 --- /dev/null +++ b/x/bond/msgs.go @@ -0,0 +1,17 @@ +package bond + +import ( + sdk "github.com/cosmos/cosmos-sdk/types" +) + +var ( + _ sdk.Msg = &MsgCreateBond{} +) + +// NewMsgCreateBond is the constructor function for MsgCreateBond. +func NewMsgCreateBond(coins sdk.Coins, signer sdk.AccAddress) MsgCreateBond { + return MsgCreateBond{ + Coins: coins, + Signer: signer.String(), + } +}