Skip to content

Commit

Permalink
test: export e2e tests as a package (#85)
Browse files Browse the repository at this point in the history
* nice

* fmt
  • Loading branch information
Alex Johnson authored May 29, 2024
1 parent cf0c02f commit 57d7094
Show file tree
Hide file tree
Showing 4 changed files with 214 additions and 42 deletions.
35 changes: 21 additions & 14 deletions tests/e2e/e2e_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,10 +4,15 @@ import (
"fmt"
"testing"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/x/auth"
"github.com/cosmos/cosmos-sdk/x/bank"
"github.com/cosmos/cosmos-sdk/x/gov"

"github.com/skip-mev/feemarket/x/feemarket"

sdkmath "cosmossdk.io/math"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
interchaintest "github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/stretchr/testify/suite"
Expand All @@ -21,7 +26,7 @@ var (
baseGasPrice = sdkmath.LegacyNewDec(1000000)

// config params
numValidators = 3
numValidators = 4
numFullNodes = 1
denom = "stake"

Expand All @@ -30,9 +35,14 @@ var (
Version: "latest",
UidGid: "1000:1000",
}
encodingConfig = MakeEncodingConfig()
noHostMount = false
gasAdjustment = 10.0
encodingConfig = testutil.MakeTestEncodingConfig(
bank.AppModuleBasic{},
gov.AppModuleBasic{},
auth.AppModuleBasic{},
feemarket.AppModuleBasic{},
)
noHostMount = false
gasAdjustment = 10.0

genesisKV = []cosmos.GenesisKV{
{
Expand Down Expand Up @@ -73,7 +83,7 @@ var (
Version: "latest",
NoHostMount: &noHostMount,
ChainConfig: ibc.ChainConfig{
EncodingConfig: encodingConfig,
EncodingConfig: &encodingConfig,
Images: []ibc.DockerImage{
image,
},
Expand All @@ -93,13 +103,10 @@ var (
}
)

func MakeEncodingConfig() *testutil.TestEncodingConfig {
cfg := cosmos.DefaultEncoding()
feemarkettypes.RegisterInterfaces(cfg.InterfaceRegistry)
return &cfg
}

func TestE2ETestSuite(t *testing.T) {
s := e2e.NewE2ETestSuiteFromSpec(spec)
s := e2e.NewIntegrationSuite(
spec,
)

suite.Run(t, s)
}
77 changes: 77 additions & 0 deletions tests/e2e/lib.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package e2e

import (
"context"
"testing"
"time"

interchaintest "github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
"github.com/strangelove-ventures/interchaintest/v8/testreporter"
"github.com/stretchr/testify/require"
"go.uber.org/zap/zaptest"
)

// ChainConstructor returns the chain that will be using slinky, as well as any additional chains
// that are needed for the test. The first chain returned will be the chain that is used in the
// slinky integration tests.
type ChainConstructor func(t *testing.T, spec *interchaintest.ChainSpec) []*cosmos.CosmosChain

// Interchain is an interface representing the set of chains that are used in the slinky e2e tests, as well
// as any additional relayer / ibc-path information
type Interchain interface {
Relayer() ibc.Relayer
Reporter() *testreporter.RelayerExecReporter
IBCPath() string
}

// InterchainConstructor returns an interchain that will be used in the feemarket integration tests.
// The chains used in the interchain constructor should be the chains constructed via the ChainConstructor
type InterchainConstructor func(ctx context.Context, t *testing.T, chains []*cosmos.CosmosChain) Interchain

// DefaultChainConstructor is the default construct of a chan that will be used in the feemarket
// integration tests. There is only a single chain that is created.
func DefaultChainConstructor(t *testing.T, spec *interchaintest.ChainSpec) []*cosmos.CosmosChain {
// require that NumFullNodes == NumValidators == 4
require.Equal(t, 4, *spec.NumValidators)

cf := interchaintest.NewBuiltinChainFactory(zaptest.NewLogger(t), []*interchaintest.ChainSpec{spec})

chains, err := cf.Chains(t.Name())
require.NoError(t, err)

// require that the chain is a cosmos chain
require.Len(t, chains, 1)
chain := chains[0]

cosmosChain, ok := chain.(*cosmos.CosmosChain)
require.True(t, ok)

return []*cosmos.CosmosChain{cosmosChain}
}

// DefaultInterchainConstructor is the default constructor of an interchain that will be used in the feemarket integration tests.
func DefaultInterchainConstructor(ctx context.Context, t *testing.T, chains []*cosmos.CosmosChain) Interchain {
require.Len(t, chains, 1)

ic := interchaintest.NewInterchain()
ic.AddChain(chains[0])

// create docker network
client, networkID := interchaintest.DockerSetup(t)

ctx, cancel := context.WithTimeout(ctx, 2*time.Minute)
defer cancel()

// build the interchain
err := ic.Build(ctx, nil, interchaintest.InterchainBuildOptions{
SkipPathCreation: true,
Client: client,
NetworkID: networkID,
TestName: t.Name(),
})
require.NoError(t, err)

return nil
}
143 changes: 115 additions & 28 deletions tests/e2e/suite.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,13 +3,18 @@ package e2e
import (
"context"
"math/rand"
"os"
"os/signal"
"sync"
"syscall"
"time"

"cosmossdk.io/math"

"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
interchaintest "github.com/strangelove-ventures/interchaintest/v8"
"github.com/strangelove-ventures/interchaintest/v8/chain/cosmos"
"github.com/strangelove-ventures/interchaintest/v8/ibc"
Expand All @@ -18,7 +23,15 @@ import (
)

const (
initBalance = 30000000000000
envKeepAlive = "FEEMARKET_INTEGRATION_KEEPALIVE"
initBalance = 30000000000000
genesisAmount = 1000000000
defaultDenom = "stake"
validatorKey = "validator"
yes = "yes"
deposit = 1000000
userMnemonic = "foster poverty abstract scorpion short shrimp tilt edge romance adapt only benefit moral another where host egg echo ability wisdom lizard lazy pool roast"
userAccountAddressHex = "877E307618AB73E009A978AC32E0264791F6D40A"
)

var r *rand.Rand
Expand All @@ -36,12 +49,8 @@ type TestSuite struct {
spec *interchaintest.ChainSpec
// chain
chain ibc.Chain
// interchain
ic *interchaintest.Interchain
// users
user1, user2, user3 ibc.Wallet
// denom
denom string

// overrides for key-ring configuration of the broadcaster
broadcasterOverrides *KeyringOverride
Expand All @@ -50,23 +59,78 @@ type TestSuite struct {
bc *cosmos.Broadcaster

cdc codec.Codec

// default token denom
denom string

// authority address
authority sdk.AccAddress

// block time
blockTime time.Duration

// interchain constructor
icc InterchainConstructor

// interchain
ic Interchain

// chain constructor
cc ChainConstructor
}

func NewE2ETestSuiteFromSpec(spec *interchaintest.ChainSpec) *TestSuite {
return &TestSuite{
spec: spec,
denom: "stake",
// Option is a function that modifies the TestSuite
type Option func(*TestSuite)

// WithDenom sets the token denom
func WithDenom(denom string) Option {
return func(s *TestSuite) {
s.denom = denom
}
}

func (s *TestSuite) WithDenom(denom string) *TestSuite {
s.denom = denom
// WithAuthority sets the authority address
func WithAuthority(addr sdk.AccAddress) Option {
return func(s *TestSuite) {
s.authority = addr
}
}

// update the bech32 prefixes
sdk.GetConfig().SetBech32PrefixForAccount(s.denom, s.denom+sdk.PrefixPublic)
sdk.GetConfig().SetBech32PrefixForValidator(s.denom+sdk.PrefixValidator, s.denom+sdk.PrefixValidator+sdk.PrefixPublic)
sdk.GetConfig().Seal()
return s
// WithBlockTime sets the block time
func WithBlockTime(t time.Duration) Option {
return func(s *TestSuite) {
s.blockTime = t
}
}

// WithInterchainConstructor sets the interchain constructor
func WithInterchainConstructor(ic InterchainConstructor) Option {
return func(s *TestSuite) {
s.icc = ic
}
}

// WithChainConstructor sets the chain constructor
func WithChainConstructor(cc ChainConstructor) Option {
return func(s *TestSuite) {
s.cc = cc
}
}

func NewIntegrationSuite(spec *interchaintest.ChainSpec, opts ...Option) *TestSuite {
suite := &TestSuite{
spec: spec,
denom: defaultDenom,
authority: authtypes.NewModuleAddress(govtypes.ModuleName),
icc: DefaultInterchainConstructor,
cc: DefaultChainConstructor,
}

for _, opt := range opts {
opt(suite)
}

return suite
}

func (s *TestSuite) WithKeyringOptions(cdc codec.Codec, opts keyring.Option) {
Expand All @@ -79,17 +143,15 @@ func (s *TestSuite) WithKeyringOptions(cdc codec.Codec, opts keyring.Option) {
func (s *TestSuite) SetupSuite() {
// build the chain
s.T().Log("building chain with spec", s.spec)
s.chain = ChainBuilderFromChainSpec(s.T(), s.spec)
chains := s.cc(s.T(), s.spec)

// build the interchain
s.T().Log("building interchain")
ctx := context.Background()
s.ic = BuildInterchain(s.T(), ctx, s.chain)
// start the chain
s.ic = s.icc(context.Background(), s.T(), chains)

cc, ok := s.chain.(*cosmos.CosmosChain)
if !ok {
panic("unable to assert ibc.Chain as CosmosChain")
}
s.chain = chains[0]

// create the broadcaster
s.T().Log("creating broadcaster")
Expand All @@ -98,18 +160,43 @@ func (s *TestSuite) SetupSuite() {
s.cdc = s.chain.Config().EncodingConfig.Codec

// get the users
s.user1 = s.GetAndFundTestUsers(ctx, s.T().Name(), initBalance, cc)
s.user2 = s.GetAndFundTestUsers(ctx, s.T().Name(), initBalance, cc)
s.user3 = s.GetAndFundTestUsers(ctx, s.T().Name(), initBalance, cc)
s.user1 = s.GetAndFundTestUsers(ctx, s.T().Name(), initBalance, chains[0])
s.user2 = s.GetAndFundTestUsers(ctx, s.T().Name(), initBalance, chains[0])
s.user3 = s.GetAndFundTestUsers(ctx, s.T().Name(), initBalance, chains[0])

// create the broadcaster
s.T().Log("creating broadcaster")
s.setupBroadcaster()
}

func (s *TestSuite) TearDownSuite() {
// close the interchain
s.Require().NoError(s.ic.Close())
defer s.Teardown()
// get the oracle integration-test suite keep alive env
if ok := os.Getenv(envKeepAlive); ok == "" {
return
}

// await on a signal to keep the chain running
sig := make(chan os.Signal, 1)
signal.Notify(sig, syscall.SIGINT, syscall.SIGTERM)
s.T().Log("Keeping the chain running")
<-sig
}

func (s *TestSuite) Teardown() {
// stop all nodes + sidecars in the chain
ctx := context.Background()
if s.chain == nil {
return
}

cc, ok := s.chain.(*cosmos.CosmosChain)
if !ok {
panic("unable to assert ibc.Chain as CosmosChain")
}

_ = cc.StopAllNodes(ctx)
_ = cc.StopAllSidecars(ctx)
}

func (s *TestSuite) SetupSubTest() {
Expand Down
1 change: 1 addition & 0 deletions x/feemarket/keeper/query_server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package keeper_test

import (
"cosmossdk.io/math"

"github.com/skip-mev/feemarket/x/feemarket/types"
)

Expand Down

0 comments on commit 57d7094

Please sign in to comment.