forked from cerc-io/laconicd
Add e2e tests for gRPC requests and CLI commands (#13)
- Add E2E tests following pattern suggested in cosmos-sdk docs: https://docs.cosmos.network/v0.50/build/building-modules/testing#end-to-end-tests - Tests for gRPC requests - Tests for manually configured CLI commands - Add a CI workflow to run these E2E tests Reviewed-on: deep-stack/laconic2d#13 Co-authored-by: Prathamesh Musale <prathamesh.musale0@gmail.com> Co-committed-by: Prathamesh Musale <prathamesh.musale0@gmail.com>
This commit is contained in:
@@ -0,0 +1,17 @@
|
||||
package auction
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/testutil/network"
|
||||
"github.com/stretchr/testify/suite"
|
||||
|
||||
"git.vdb.to/cerc-io/laconic2d/tests/e2e"
|
||||
)
|
||||
|
||||
func TestAuctionE2ETestSuite(t *testing.T) {
|
||||
cfg := network.DefaultConfig(e2e.NewTestNetworkFixture)
|
||||
cfg.NumValidators = 1
|
||||
|
||||
suite.Run(t, NewE2ETestSuite(cfg))
|
||||
}
|
||||
@@ -0,0 +1,263 @@
|
||||
package auction
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/testutil"
|
||||
|
||||
auctiontypes "git.vdb.to/cerc-io/laconic2d/x/auction"
|
||||
)
|
||||
|
||||
const (
|
||||
randomAuctionId = "randomAuctionId"
|
||||
randomBidderAddress = "randomBidderAddress"
|
||||
randomOwnerAddress = "randomOwnerAddress"
|
||||
)
|
||||
|
||||
func (ets *E2ETestSuite) TestQueryParamsGrpc() {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
reqURL := fmt.Sprintf("%s/cerc/auction/v1/params", val.APIAddress)
|
||||
|
||||
ets.Run("valid request to get auction params", func() {
|
||||
resp, err := testutil.GetRequest(reqURL)
|
||||
ets.Require().NoError(err)
|
||||
|
||||
var params auctiontypes.QueryParamsResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(resp, ¶ms)
|
||||
|
||||
sr.NoError(err)
|
||||
sr.Equal(*params.GetParams(), auctiontypes.DefaultParams())
|
||||
})
|
||||
}
|
||||
|
||||
func (ets *E2ETestSuite) TestGetAllAuctionsGrpc() {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
reqURL := fmt.Sprintf("%s/cerc/auction/v1/auctions", val.APIAddress)
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
url string
|
||||
errorMsg string
|
||||
isErrorExpected bool
|
||||
}{
|
||||
{
|
||||
"invalid request to get all auctions",
|
||||
reqURL + randomAuctionId,
|
||||
"",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"valid request to get all auctions",
|
||||
reqURL,
|
||||
"",
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
ets.Run(tc.msg, func() {
|
||||
resp, err := testutil.GetRequest(tc.url)
|
||||
if tc.isErrorExpected {
|
||||
sr.Contains(string(resp), tc.errorMsg)
|
||||
} else {
|
||||
sr.NoError(err)
|
||||
var auctions auctiontypes.QueryAuctionsResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &auctions)
|
||||
sr.NoError(err)
|
||||
sr.NotZero(len(auctions.Auctions.Auctions))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (ets *E2ETestSuite) TestGetAuctionGrpc() {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
reqURL := fmt.Sprintf("%s/cerc/auction/v1/auctions/", val.APIAddress)
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
url string
|
||||
errorMsg string
|
||||
isErrorExpected bool
|
||||
preRun func() string
|
||||
}{
|
||||
{
|
||||
"invalid request to get an auction",
|
||||
reqURL + randomAuctionId,
|
||||
"",
|
||||
true,
|
||||
func() string { return "" },
|
||||
},
|
||||
{
|
||||
"valid request to get an auction",
|
||||
reqURL,
|
||||
"",
|
||||
false,
|
||||
func() string { return ets.defaultAuctionId },
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
ets.Run(tc.msg, func() {
|
||||
auctionId := tc.preRun()
|
||||
resp, err := testutil.GetRequest(tc.url + auctionId)
|
||||
if tc.isErrorExpected {
|
||||
sr.Contains(string(resp), tc.errorMsg)
|
||||
} else {
|
||||
sr.NoError(err)
|
||||
var auction auctiontypes.QueryAuctionResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &auction)
|
||||
sr.NoError(err)
|
||||
sr.Equal(auctionId, auction.Auction.Id)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (ets *E2ETestSuite) TestGetBidsGrpc() {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
reqURL := fmt.Sprintf("%s/cerc/auction/v1/bids/", val.APIAddress)
|
||||
testCases := []struct {
|
||||
msg string
|
||||
url string
|
||||
errorMsg string
|
||||
isErrorExpected bool
|
||||
preRun func() string
|
||||
}{
|
||||
{
|
||||
"invalid request to get all bids",
|
||||
reqURL,
|
||||
"",
|
||||
true,
|
||||
func() string { return "" },
|
||||
},
|
||||
{
|
||||
"valid request to get all bids",
|
||||
reqURL,
|
||||
"",
|
||||
false,
|
||||
func() string { return ets.createAuctionAndBid(false, true) },
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range testCases {
|
||||
ets.Run(tc.msg, func() {
|
||||
auctionId := tc.preRun()
|
||||
tc.url += auctionId
|
||||
resp, err := testutil.GetRequest(tc.url)
|
||||
if tc.isErrorExpected {
|
||||
sr.Contains(string(resp), tc.errorMsg)
|
||||
} else {
|
||||
sr.NoError(err)
|
||||
var bids auctiontypes.QueryBidsResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bids)
|
||||
sr.NoError(err)
|
||||
sr.Equal(auctionId, bids.Bids[0].AuctionId)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (ets *E2ETestSuite) TestGetBidGrpc() {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
reqURL := fmt.Sprintf("%s/cerc/auction/v1/bids/", val.APIAddress)
|
||||
testCases := []struct {
|
||||
msg string
|
||||
url string
|
||||
errorMsg string
|
||||
isErrorExpected bool
|
||||
preRun func() string
|
||||
}{
|
||||
{
|
||||
"invalid request to get bid",
|
||||
reqURL,
|
||||
"",
|
||||
true,
|
||||
func() string { return randomAuctionId },
|
||||
},
|
||||
{
|
||||
"valid request to get bid",
|
||||
reqURL,
|
||||
"",
|
||||
false,
|
||||
func() string { return ets.createAuctionAndBid(false, true) },
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
ets.Run(tc.msg, func() {
|
||||
auctionId := tc.preRun()
|
||||
tc.url += auctionId + "/" + bidderAddress
|
||||
resp, err := testutil.GetRequest(tc.url)
|
||||
|
||||
if tc.isErrorExpected {
|
||||
sr.Contains(string(resp), tc.errorMsg)
|
||||
} else {
|
||||
sr.NoError(err)
|
||||
var bid auctiontypes.QueryBidResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &bid)
|
||||
sr.NoError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (ets *E2ETestSuite) TestGetAuctionsByOwnerGrpc() {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
reqURL := fmt.Sprintf("%s/cerc/auction/v1/by-owner/", val.APIAddress)
|
||||
testCases := []struct {
|
||||
msg string
|
||||
url string
|
||||
errorMsg string
|
||||
isErrorExpected bool
|
||||
}{
|
||||
{
|
||||
"invalid request to get auctions by owner",
|
||||
reqURL,
|
||||
"",
|
||||
true,
|
||||
},
|
||||
{
|
||||
"valid request to get auctions by owner",
|
||||
fmt.Sprintf("%s/%s", reqURL, ownerAddress),
|
||||
"",
|
||||
false,
|
||||
},
|
||||
}
|
||||
for _, tc := range testCases {
|
||||
ets.Run(tc.msg, func() {
|
||||
resp, err := testutil.GetRequest(tc.url)
|
||||
if tc.isErrorExpected {
|
||||
sr.Contains(string(resp), tc.errorMsg)
|
||||
} else {
|
||||
sr.NoError(err)
|
||||
var auctions auctiontypes.QueryAuctionsResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &auctions)
|
||||
sr.NoError(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (ets *E2ETestSuite) TestQueryBalanceGrpc() {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
reqURL := fmt.Sprintf("%s/cerc/auction/v1/balance", val.APIAddress)
|
||||
msg := "valid request to get the auction module balance"
|
||||
|
||||
ets.createAuctionAndBid(false, true)
|
||||
|
||||
ets.Run(msg, func() {
|
||||
resp, err := testutil.GetRequest(reqURL)
|
||||
sr.NoError(err)
|
||||
|
||||
var response auctiontypes.QueryGetAuctionModuleBalanceResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(resp, &response)
|
||||
|
||||
sr.NoError(err)
|
||||
sr.NotZero(len(response.GetBalance()))
|
||||
})
|
||||
}
|
||||
@@ -0,0 +1,45 @@
|
||||
package auction
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
|
||||
types "git.vdb.to/cerc-io/laconic2d/x/auction"
|
||||
"git.vdb.to/cerc-io/laconic2d/x/auction/client/cli"
|
||||
)
|
||||
|
||||
var queryJSONFlag = []string{fmt.Sprintf("--%s=json", flags.FlagOutput)}
|
||||
|
||||
func (ets *E2ETestSuite) TestGetCmdList() {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
|
||||
testCases := []struct {
|
||||
msg string
|
||||
createAuction bool
|
||||
}{
|
||||
{
|
||||
"list auctions when no auctions exist",
|
||||
false,
|
||||
},
|
||||
{
|
||||
"list auctions after creating an auction",
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
ets.Run(fmt.Sprintf("Case %s", test.msg), func() {
|
||||
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdList(), queryJSONFlag)
|
||||
sr.NoError(err)
|
||||
var auctions types.QueryAuctionsResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &auctions)
|
||||
sr.NoError(err)
|
||||
if test.createAuction {
|
||||
sr.NotZero(len(auctions.Auctions.Auctions))
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
@@ -0,0 +1,148 @@
|
||||
package auction
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"os"
|
||||
"path/filepath"
|
||||
|
||||
"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"
|
||||
|
||||
types "git.vdb.to/cerc-io/laconic2d/x/auction"
|
||||
"git.vdb.to/cerc-io/laconic2d/x/auction/client/cli"
|
||||
)
|
||||
|
||||
var (
|
||||
ownerAccount = "owner"
|
||||
bidderAccount = "bidder"
|
||||
ownerAddress string
|
||||
bidderAddress string
|
||||
)
|
||||
|
||||
type E2ETestSuite struct {
|
||||
suite.Suite
|
||||
|
||||
cfg network.Config
|
||||
network *network.Network
|
||||
|
||||
defaultAuctionId 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 owner and bidder accounts
|
||||
ets.createAccountWithBalance(ownerAccount, &ownerAddress)
|
||||
ets.createAccountWithBalance(bidderAccount, &bidderAddress)
|
||||
|
||||
ets.defaultAuctionId = ets.createAuctionAndBid(true, false)
|
||||
}
|
||||
|
||||
func (ets *E2ETestSuite) TearDownSuite() {
|
||||
ets.T().Log("tearing down integration test suite")
|
||||
ets.network.Cleanup()
|
||||
|
||||
ets.cleanupBidFiles()
|
||||
}
|
||||
|
||||
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) createAuctionAndBid(createAuction, createBid bool) string {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
auctionId := ""
|
||||
|
||||
if createAuction {
|
||||
auctionArgs := []string{
|
||||
sampleCommitTime, sampleRevealTime,
|
||||
fmt.Sprintf("10%s", ets.cfg.BondDenom),
|
||||
fmt.Sprintf("10%s", ets.cfg.BondDenom),
|
||||
fmt.Sprintf("100%s", ets.cfg.BondDenom),
|
||||
}
|
||||
|
||||
resp, err := ets.executeTx(cli.GetCmdCreateAuction(), auctionArgs, ownerAccount)
|
||||
sr.NoError(err)
|
||||
sr.Zero(resp.Code)
|
||||
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdList(), queryJSONFlag)
|
||||
sr.NoError(err)
|
||||
var queryResponse types.QueryAuctionsResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &queryResponse)
|
||||
sr.NoError(err)
|
||||
auctionId = queryResponse.Auctions.Auctions[0].Id
|
||||
} else {
|
||||
auctionId = ets.defaultAuctionId
|
||||
}
|
||||
|
||||
if createBid {
|
||||
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)
|
||||
}
|
||||
|
||||
return auctionId
|
||||
}
|
||||
|
||||
func (ets *E2ETestSuite) cleanupBidFiles() error {
|
||||
matches, err := filepath.Glob(fmt.Sprintf("%s-*.json", bidderAccount))
|
||||
if err != nil {
|
||||
ets.T().Errorf("Error matching bidder files: %v\n", err)
|
||||
return err
|
||||
}
|
||||
|
||||
for _, match := range matches {
|
||||
err := os.Remove(match)
|
||||
if err != nil {
|
||||
return err
|
||||
}
|
||||
}
|
||||
|
||||
return nil
|
||||
}
|
||||
@@ -0,0 +1,105 @@
|
||||
package auction
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
|
||||
"github.com/cosmos/cosmos-sdk/client/flags"
|
||||
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
||||
sdk "github.com/cosmos/cosmos-sdk/types"
|
||||
"github.com/spf13/cobra"
|
||||
|
||||
auctiontypes "git.vdb.to/cerc-io/laconic2d/x/auction"
|
||||
"git.vdb.to/cerc-io/laconic2d/x/auction/client/cli"
|
||||
)
|
||||
|
||||
const (
|
||||
sampleCommitTime = "90s"
|
||||
sampleRevealTime = "5s"
|
||||
placeholderAuctionId = "placeholder_auction_id"
|
||||
)
|
||||
|
||||
func (ets *E2ETestSuite) TestTxCommitBid() {
|
||||
val := ets.network.Validators[0]
|
||||
sr := ets.Require()
|
||||
testCases := []struct {
|
||||
msg string
|
||||
args []string
|
||||
createAuction bool
|
||||
}{
|
||||
{
|
||||
"commit bid with missing args",
|
||||
[]string{fmt.Sprintf("200%s", ets.cfg.BondDenom)},
|
||||
false,
|
||||
},
|
||||
{
|
||||
"commit bid with valid args",
|
||||
[]string{
|
||||
placeholderAuctionId,
|
||||
fmt.Sprintf("200%s", ets.cfg.BondDenom),
|
||||
},
|
||||
true,
|
||||
},
|
||||
}
|
||||
|
||||
for _, test := range testCases {
|
||||
ets.Run(fmt.Sprintf("Case %s", test.msg), func() {
|
||||
if test.createAuction {
|
||||
auctionArgs := []string{
|
||||
sampleCommitTime, sampleRevealTime,
|
||||
fmt.Sprintf("10%s", ets.cfg.BondDenom),
|
||||
fmt.Sprintf("10%s", ets.cfg.BondDenom),
|
||||
fmt.Sprintf("100%s", ets.cfg.BondDenom),
|
||||
}
|
||||
|
||||
_, err := ets.executeTx(cli.GetCmdCreateAuction(), auctionArgs, ownerAccount)
|
||||
sr.NoError(err)
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cli.GetCmdList(),
|
||||
[]string{fmt.Sprintf("--%s=json", flags.FlagOutput)})
|
||||
sr.NoError(err)
|
||||
var queryResponse auctiontypes.QueryAuctionsResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &queryResponse)
|
||||
sr.NoError(err)
|
||||
sr.NotNil(queryResponse.GetAuctions())
|
||||
test.args[0] = queryResponse.GetAuctions().Auctions[0].Id
|
||||
}
|
||||
|
||||
resp, err := ets.executeTx(cli.GetCmdCommitBid(), test.args, bidderAccount)
|
||||
if test.createAuction {
|
||||
sr.NoError(err)
|
||||
sr.Zero(resp.Code)
|
||||
} else {
|
||||
sr.Error(err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func (ets *E2ETestSuite) executeTx(cmd *cobra.Command, args []string, caller string) (sdk.TxResponse, error) {
|
||||
val := ets.network.Validators[0]
|
||||
additionalArgs := []string{
|
||||
fmt.Sprintf("--%s=%s", flags.FlagFrom, caller),
|
||||
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)),
|
||||
}
|
||||
args = append(args, additionalArgs...)
|
||||
|
||||
out, err := clitestutil.ExecTestCLICmd(val.ClientCtx, cmd, args)
|
||||
if err != nil {
|
||||
return sdk.TxResponse{}, err
|
||||
}
|
||||
|
||||
var resp sdk.TxResponse
|
||||
err = val.ClientCtx.Codec.UnmarshalJSON(out.Bytes(), &resp)
|
||||
if err != nil {
|
||||
return sdk.TxResponse{}, err
|
||||
}
|
||||
|
||||
err = ets.network.WaitForNextBlock()
|
||||
if err != nil {
|
||||
return sdk.TxResponse{}, err
|
||||
}
|
||||
|
||||
return resp, nil
|
||||
}
|
||||
Reference in New Issue
Block a user