From 36a26eef54c3e3413060db6e2bd6eba61e86e900 Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Tue, 12 Mar 2024 17:08:49 +0530 Subject: [PATCH] Verify write transactions in E2E tests --- tests/e2e/auction/suite.go | 18 ++++++++-------- tests/e2e/auction/tx.go | 11 ++++------ tests/e2e/bond/suite.go | 18 ++++++++-------- tests/e2e/common.go | 2 ++ tests/e2e/registry/suite.go | 43 +++++++++++-------------------------- testutil/cli/tx.go | 41 +++++++++++++++++++++++++++++++++++ 6 files changed, 78 insertions(+), 55 deletions(-) create mode 100644 testutil/cli/tx.go diff --git a/tests/e2e/auction/suite.go b/tests/e2e/auction/suite.go index 13a3f19c..03fb3091 100644 --- a/tests/e2e/auction/suite.go +++ b/tests/e2e/auction/suite.go @@ -15,6 +15,7 @@ import ( clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" + laconictestcli "git.vdb.to/cerc-io/laconic2d/testutil/cli" "git.vdb.to/cerc-io/laconic2d/testutil/network" types "git.vdb.to/cerc-io/laconic2d/x/auction" "git.vdb.to/cerc-io/laconic2d/x/auction/client/cli" @@ -74,7 +75,7 @@ func (ets *E2ETestSuite) createAccountWithBalance(accountName string, accountAdd sr.NoError(err) newAddr, _ := info.GetAddress() - _, err = clitestutil.MsgSendExec( + out, err := clitestutil.MsgSendExec( val.ClientCtx, val.Address, newAddr, @@ -87,11 +88,12 @@ func (ets *E2ETestSuite) createAccountWithBalance(accountName string, accountAdd 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) + var response sdk.TxResponse + sr.NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &response), out.String()) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, response.TxHash, 0)) + + *accountAddress = newAddr.String() } func (ets *E2ETestSuite) createAuctionAndBid(createAuction, createBid bool) string { @@ -109,9 +111,7 @@ func (ets *E2ETestSuite) createAuctionAndBid(createAuction, createBid bool) stri resp, err := ets.executeTx(cli.GetCmdCreateAuction(), auctionArgs, ownerAccount) sr.NoError(err) - sr.Zero(resp.Code) - err = ets.network.WaitForNextBlock() - sr.NoError(err) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, resp.TxHash, 0)) out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdList(), queryJSONFlag) sr.NoError(err) @@ -127,7 +127,7 @@ func (ets *E2ETestSuite) createAuctionAndBid(createAuction, createBid bool) stri bidArgs := []string{auctionId, fmt.Sprintf("200%s", ets.cfg.BondDenom)} resp, err := ets.executeTx(cli.GetCmdCommitBid(), bidArgs, bidderAccount) sr.NoError(err) - sr.Zero(resp.Code) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, resp.TxHash, 0)) } return auctionId diff --git a/tests/e2e/auction/tx.go b/tests/e2e/auction/tx.go index 1675d25e..7180309b 100644 --- a/tests/e2e/auction/tx.go +++ b/tests/e2e/auction/tx.go @@ -8,6 +8,7 @@ import ( sdk "github.com/cosmos/cosmos-sdk/types" "github.com/spf13/cobra" + laconictestcli "git.vdb.to/cerc-io/laconic2d/testutil/cli" auctiontypes "git.vdb.to/cerc-io/laconic2d/x/auction" "git.vdb.to/cerc-io/laconic2d/x/auction/client/cli" ) @@ -51,8 +52,9 @@ func (ets *E2ETestSuite) TestTxCommitBid() { fmt.Sprintf("100%s", ets.cfg.BondDenom), } - _, err := ets.executeTx(cli.GetCmdCreateAuction(), auctionArgs, ownerAccount) + resp, err := ets.executeTx(cli.GetCmdCreateAuction(), auctionArgs, ownerAccount) sr.NoError(err) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, resp.TxHash, 0)) out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdList(), []string{fmt.Sprintf("--%s=json", flags.FlagOutput)}) @@ -67,7 +69,7 @@ func (ets *E2ETestSuite) TestTxCommitBid() { resp, err := ets.executeTx(cli.GetCmdCommitBid(), test.args, bidderAccount) if test.createAuction { sr.NoError(err) - sr.Zero(resp.Code) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, resp.TxHash, 0)) } else { sr.Error(err) } @@ -96,10 +98,5 @@ func (ets *E2ETestSuite) executeTx(cmd *cobra.Command, args []string, caller str return sdk.TxResponse{}, err } - err = ets.network.WaitForNextBlock() - if err != nil { - return sdk.TxResponse{}, err - } - return resp, nil } diff --git a/tests/e2e/bond/suite.go b/tests/e2e/bond/suite.go index 6da8cbbb..0aaeedcb 100644 --- a/tests/e2e/bond/suite.go +++ b/tests/e2e/bond/suite.go @@ -13,6 +13,7 @@ import ( clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" + laconictestcli "git.vdb.to/cerc-io/laconic2d/testutil/cli" "git.vdb.to/cerc-io/laconic2d/testutil/network" bondtypes "git.vdb.to/cerc-io/laconic2d/x/bond" "git.vdb.to/cerc-io/laconic2d/x/bond/client/cli" @@ -62,7 +63,7 @@ func (ets *E2ETestSuite) createAccountWithBalance(accountName string, accountAdd sr.NoError(err) newAddr, _ := info.GetAddress() - _, err = clitestutil.MsgSendExec( + out, err := clitestutil.MsgSendExec( val.ClientCtx, val.Address, newAddr, @@ -75,11 +76,12 @@ func (ets *E2ETestSuite) createAccountWithBalance(accountName string, accountAdd 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) + var response sdk.TxResponse + sr.NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &response), out.String()) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, response.TxHash, 0)) + + *accountAddress = newAddr.String() } func (ets *E2ETestSuite) createBond() string { @@ -96,14 +98,12 @@ func (ets *E2ETestSuite) createBond() string { } 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) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, d.TxHash, 0)) // getting the bonds list and returning the bond-id clientCtx := val.ClientCtx diff --git a/tests/e2e/common.go b/tests/e2e/common.go index 0a59e1ed..118dd386 100644 --- a/tests/e2e/common.go +++ b/tests/e2e/common.go @@ -14,6 +14,7 @@ import ( simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" "github.com/cosmos/cosmos-sdk/types/module/testutil" "github.com/cosmos/cosmos-sdk/x/auth" + "github.com/cosmos/cosmos-sdk/x/bank" "github.com/cosmos/cosmos-sdk/x/staking" laconicApp "git.vdb.to/cerc-io/laconic2d/app" @@ -58,6 +59,7 @@ func NewTestNetworkFixture() network.TestFixture { GenesisState: app.DefaultGenesis(), EncodingConfig: testutil.MakeTestEncodingConfig( auth.AppModuleBasic{}, + bank.AppModuleBasic{}, staking.AppModuleBasic{}, auctionmodule.AppModule{}, bondmodule.AppModule{}, diff --git a/tests/e2e/registry/suite.go b/tests/e2e/registry/suite.go index 4d605715..77a84256 100644 --- a/tests/e2e/registry/suite.go +++ b/tests/e2e/registry/suite.go @@ -15,6 +15,7 @@ import ( clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" sdk "github.com/cosmos/cosmos-sdk/types" + laconictestcli "git.vdb.to/cerc-io/laconic2d/testutil/cli" "git.vdb.to/cerc-io/laconic2d/testutil/network" bondtypes "git.vdb.to/cerc-io/laconic2d/x/bond" bondcli "git.vdb.to/cerc-io/laconic2d/x/bond/client/cli" @@ -81,7 +82,7 @@ func (ets *E2ETestSuite) createAccountWithBalance(accountName string, accountAdd sr.NoError(err) newAddr, _ := info.GetAddress() - _, err = clitestutil.MsgSendExec( + out, err := clitestutil.MsgSendExec( val.ClientCtx, val.Address, newAddr, @@ -94,11 +95,12 @@ func (ets *E2ETestSuite) createAccountWithBalance(accountName string, accountAdd 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) + var response sdk.TxResponse + sr.NoError(val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &response), out.String()) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, response.TxHash, 0)) + + *accountAddress = newAddr.String() } func (ets *E2ETestSuite) createBond() string { @@ -118,11 +120,7 @@ func (ets *E2ETestSuite) createBond() string { 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) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, d.TxHash, 0)) // getting the bonds list and returning the bond-id clientCtx := val.ClientCtx @@ -162,10 +160,7 @@ func (ets *E2ETestSuite) reserveName(authorityName string) { var d sdk.TxResponse err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) sr.NoError(err) - sr.Zero(d.Code) - - err = ets.network.WaitForNextBlock() - sr.NoError(err) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, d.TxHash, 0)) } func (ets *E2ETestSuite) createNameRecord(authorityName string) { @@ -189,10 +184,7 @@ func (ets *E2ETestSuite) createNameRecord(authorityName string) { var d sdk.TxResponse err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) sr.NoError(err) - sr.Zero(d.Code) - - err = ets.network.WaitForNextBlock() - sr.NoError(err) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, d.TxHash, 0)) // Get the bond-id bondId := ets.bondId @@ -212,10 +204,7 @@ func (ets *E2ETestSuite) createNameRecord(authorityName string) { sr.NoError(err) err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) sr.NoError(err) - sr.Zero(d.Code) - - err = ets.network.WaitForNextBlock() - sr.NoError(err) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, d.TxHash, 0)) args = []string{ fmt.Sprintf("lrn://%s/", authorityName), @@ -233,10 +222,7 @@ func (ets *E2ETestSuite) createNameRecord(authorityName string) { sr.NoError(err) err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) sr.NoError(err) - sr.Zero(d.Code) - - err = ets.network.WaitForNextBlock() - sr.NoError(err) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, d.TxHash, 0)) } func (ets *E2ETestSuite) createRecord(bondId string) { @@ -263,10 +249,7 @@ func (ets *E2ETestSuite) createRecord(bondId string) { var d sdk.TxResponse err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &d) sr.NoError(err) - sr.Zero(d.Code, d.RawLog) - - err = ets.network.WaitForNextBlock() - sr.NoError(err) + sr.NoError(laconictestcli.CheckTxCode(ets.network, val.ClientCtx, d.TxHash, 0)) } func (ets *E2ETestSuite) updateParams(params *registrytypes.Params) { diff --git a/testutil/cli/tx.go b/testutil/cli/tx.go new file mode 100644 index 00000000..e84d6c25 --- /dev/null +++ b/testutil/cli/tx.go @@ -0,0 +1,41 @@ +package cli + +import ( + "fmt" + + "github.com/cosmos/cosmos-sdk/client" + "github.com/cosmos/cosmos-sdk/client/flags" + clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli" + sdk "github.com/cosmos/cosmos-sdk/types" + authcli "github.com/cosmos/cosmos-sdk/x/auth/client/cli" + + "git.vdb.to/cerc-io/laconic2d/testutil/network" +) + +// CheckTxCode verifies that the transaction result returns a specific code +// Takes a network, wait for two blocks and fetch the transaction from its hash +func CheckTxCode(network *network.Network, clientCtx client.Context, txHash string, expectedCode uint32) error { + // wait for 2 blocks + for i := 0; i < 2; i++ { + if err := network.WaitForNextBlock(); err != nil { + return fmt.Errorf("failed to wait for next block: %w", err) + } + } + + cmd := authcli.QueryTxCmd() + out, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{txHash, fmt.Sprintf("--%s=json", flags.FlagOutput)}) + if err != nil { + return err + } + + var response sdk.TxResponse + if err := clientCtx.Codec.UnmarshalJSON(out.Bytes(), &response); err != nil { + return err + } + + if response.Code != expectedCode { + return fmt.Errorf("expected code %d, got %d", expectedCode, response.Code) + } + + return nil +}