From b57ca602776c2e288e597610f1d7e0cec1e1db0b Mon Sep 17 00:00:00 2001 From: Prathamesh Musale Date: Fri, 1 Mar 2024 11:52:37 +0530 Subject: [PATCH] Create setup for e2e CLI tests --- Makefile | 3 ++ README.md | 6 ++- scripts/protocgen.sh | 1 - tests/Makefile | 4 +- tests/e2e/auction/cli_test.go | 16 ++++++++ tests/e2e/auction/suite.go | 76 +++++++++++++++++++++++++++++++++++ tests/e2e/common.go | 63 +++++++++++++++++++++++++++++ 7 files changed, 166 insertions(+), 3 deletions(-) create mode 100644 tests/e2e/auction/cli_test.go create mode 100644 tests/e2e/auction/suite.go create mode 100644 tests/e2e/common.go diff --git a/Makefile b/Makefile index 6ce6a9da..6617ec11 100644 --- a/Makefile +++ b/Makefile @@ -88,3 +88,6 @@ lint-fix: test-integration: $(MAKE) -C tests test-integration + +test-e2e: + $(MAKE) -C tests test-e2e diff --git a/README.md b/README.md index ab165b07..6648efc8 100644 --- a/README.md +++ b/README.md @@ -10,11 +10,15 @@ Install and run `laconic2d`: make init # start the chain - laconic2d start + laconic2d start --gql-playground --gql-server ``` Run tests: ```bash + # integration tests make test-integration + + # e2e tests + make test-e2e ``` diff --git a/scripts/protocgen.sh b/scripts/protocgen.sh index a18fcb37..6acf722a 100755 --- a/scripts/protocgen.sh +++ b/scripts/protocgen.sh @@ -16,7 +16,6 @@ for dir in $proto_dirs; do done done -# TODO: Check if required echo "Generating pulsar proto code" buf generate --template buf.gen.pulsar.yaml diff --git a/tests/Makefile b/tests/Makefile index 3ed7cfec..d992d555 100644 --- a/tests/Makefile +++ b/tests/Makefile @@ -1,2 +1,4 @@ test-integration: - go test -mod=readonly ./integration/... -test.v -timeout 10m + +test-e2e: + go test ./e2e/... -mod=readonly -test.v -timeout 10m diff --git a/tests/e2e/auction/cli_test.go b/tests/e2e/auction/cli_test.go new file mode 100644 index 00000000..6fe03c4d --- /dev/null +++ b/tests/e2e/auction/cli_test.go @@ -0,0 +1,16 @@ +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 TestE2ETestSuite(t *testing.T) { + cfg := network.DefaultConfig(e2e.NewTestNetworkFixture) + cfg.NumValidators = 2 + suite.Run(t, NewE2ETestSuite(cfg)) +} diff --git a/tests/e2e/auction/suite.go b/tests/e2e/auction/suite.go new file mode 100644 index 00000000..ef929af5 --- /dev/null +++ b/tests/e2e/auction/suite.go @@ -0,0 +1,76 @@ +package auction + +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" +) + +var ( + ownerAccount = "owner" + bidderAccount = "bidder" + ownerAddress string + bidderAddress string +) + +type E2ETestSuite struct { + suite.Suite + + cfg network.Config + network *network.Network +} + +func NewE2ETestSuite(cfg network.Config) *E2ETestSuite { + return &E2ETestSuite{cfg: cfg} +} + +func (s *E2ETestSuite) SetupSuite() { //nolint: all + s.T().Log("setting up e2e test suite") + + var err error + + s.network, err = network.New(s.T(), s.T().TempDir(), s.cfg) + s.Require().NoError(err) + + _, err = s.network.WaitForHeight(1) + s.Require().NoError(err) + + // setting up random owner and bidder accounts + s.createAccountWithBalance(ownerAccount, &ownerAddress) + s.createAccountWithBalance(bidderAccount, &bidderAddress) + + // s.defaultAuctionID = s.createAuctionAndBid(true, false) +} + +func (s *E2ETestSuite) createAccountWithBalance(accountName string, accountAddress *string) { + val := s.network.Validators[0] + sr := s.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(s.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(s.cfg.BondDenom, math.NewInt(10))).String()), + ) + sr.NoError(err) + *accountAddress = newAddr.String() +} diff --git a/tests/e2e/common.go b/tests/e2e/common.go new file mode 100644 index 00000000..5d446527 --- /dev/null +++ b/tests/e2e/common.go @@ -0,0 +1,63 @@ +package e2e + +import ( + "fmt" + "os" + + "cosmossdk.io/log" + pruningtypes "cosmossdk.io/store/pruning/types" + + dbm "github.com/cosmos/cosmos-db" + bam "github.com/cosmos/cosmos-sdk/baseapp" + "github.com/cosmos/cosmos-sdk/client/flags" + servertypes "github.com/cosmos/cosmos-sdk/server/types" + "github.com/cosmos/cosmos-sdk/testutil/network" + simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims" + "github.com/cosmos/cosmos-sdk/types/module/testutil" + "github.com/cosmos/cosmos-sdk/x/auth" + + laconicApp "git.vdb.to/cerc-io/laconic2d/app" + auctionmodule "git.vdb.to/cerc-io/laconic2d/x/auction/module" + bondmodule "git.vdb.to/cerc-io/laconic2d/x/bond/module" + registrymodule "git.vdb.to/cerc-io/laconic2d/x/registry/module" +) + +// NewTestNetworkFixture returns a new simapp AppConstructor for network simulation tests +func NewTestNetworkFixture() network.TestFixture { + dir, err := os.MkdirTemp("", "simapp") + if err != nil { + panic(fmt.Sprintf("failed creating temporary directory: %v", err)) + } + defer os.RemoveAll(dir) + + app, err := laconicApp.NewLaconicApp(log.NewNopLogger(), dbm.NewMemDB(), nil, true, simtestutil.NewAppOptionsWithFlagHome(dir)) + if err != nil { + panic(fmt.Sprintf("failed to create laconic app: %v", err)) + } + + appCtr := func(val network.ValidatorI) servertypes.Application { + app, err := laconicApp.NewLaconicApp( + val.GetCtx().Logger, dbm.NewMemDB(), nil, true, + simtestutil.NewAppOptionsWithFlagHome(val.GetCtx().Config.RootDir), + bam.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)), + bam.SetMinGasPrices(val.GetAppConfig().MinGasPrices), + bam.SetChainID(val.GetCtx().Viper.GetString(flags.FlagChainID)), + ) + if err != nil { + panic(fmt.Sprintf("failed creating temporary directory: %v", err)) + } + + return app + } + + return network.TestFixture{ + AppConstructor: appCtr, + GenesisState: app.DefaultGenesis(), + EncodingConfig: testutil.MakeTestEncodingConfig( + auth.AppModuleBasic{}, + auctionmodule.AppModule{}, + bondmodule.AppModule{}, + registrymodule.AppModule{}, + ), + } +}