Add e2e tests for gRPC requests and CLI commands #13

Merged
ashwin merged 14 commits from pm-cli-tests into main 2024-03-04 11:16:11 +00:00
7 changed files with 204 additions and 2 deletions
Showing only changes of commit cc6f2070b5 - Show all commits

View File

@ -9,7 +9,7 @@ import (
"git.vdb.to/cerc-io/laconic2d/tests/e2e" "git.vdb.to/cerc-io/laconic2d/tests/e2e"
) )
func TestE2ETestSuite(t *testing.T) { func TestAuctionE2ETestSuite(t *testing.T) {
cfg := network.DefaultConfig(e2e.NewTestNetworkFixture) cfg := network.DefaultConfig(e2e.NewTestNetworkFixture)
cfg.NumValidators = 1 cfg.NumValidators = 1

View File

@ -133,7 +133,7 @@ func (ets *E2ETestSuite) createAuctionAndBid(createAuction, createBid bool) stri
func (ets *E2ETestSuite) cleanupBidFiles() error { func (ets *E2ETestSuite) cleanupBidFiles() error {
matches, err := filepath.Glob(fmt.Sprintf("%s-*.json", bidderAccount)) matches, err := filepath.Glob(fmt.Sprintf("%s-*.json", bidderAccount))
if err != nil { if err != nil {
fmt.Printf("Error matching bidder files: %v\n", err) ets.T().Errorf("Error matching bidder files: %v\n", err)
return err return err
} }

View File

@ -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))
}

24
tests/e2e/bond/grpc.go Normal file
View File

@ -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, &params)
sr.NoError(err)
sr.Equal(params.GetParams().MaxBondAmount, bondtypes.DefaultParams().MaxBondAmount)
}

106
tests/e2e/bond/suite.go Normal file
View File

@ -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)
}

38
x/bond/client/cli/tx.go Normal file
View File

@ -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
}

17
x/bond/msgs.go Normal file
View File

@ -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(),
}
}