Skip to content

Commit

Permalink
refactor(testutil): remove dependency on simapp (cosmos#12624)
Browse files Browse the repository at this point in the history
## Description

Closes: cosmos#12546 

Removes `simapp` as a dependency from `testutil/network` by abstracting `simapp` construction behind a factory fn (now in `simapp`), then requiring callers of `network.DefaultConfig` to pass in the factory fn directly.

This patch is an intermediate step to removing all dependencies on `simapp`.  Follow up work may include moving all of the tests using `network.DefaultConfig` updated here up to `simapp`, or possibly just the runners.



---

### Author Checklist

*All items are required. Please add a note to the item if the item is not applicable and
please add links to any relevant follow up issues.*

I have...

- [ ] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] added `!` to the type prefix if API or client breaking change
- [ ] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#pr-targeting))
- [ ] provided a link to the relevant issue or specification
- [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/main/docs/building-modules)
- [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/main/CONTRIBUTING.md#testing)
- [ ] added a changelog entry to `CHANGELOG.md`
- [ ] included comments for [documenting Go code](https://blog.golang.org/godoc)
- [ ] updated the relevant documentation or specification
- [ ] reviewed "Files changed" and left comments if necessary
- [ ] confirmed all CI checks have passed

### Reviewers Checklist

*All items are required. Please add a note if the item is not applicable and please add
your handle next to the items reviewed if you only reviewed selected items.*

I have...

- [ ] confirmed the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title
- [ ] confirmed `!` in the type prefix if API or client breaking change
- [ ] confirmed all author checklist items have been addressed 
- [ ] reviewed state machine logic
- [ ] reviewed API design and naming
- [ ] reviewed documentation is accurate
- [ ] reviewed tests and test coverage
- [ ] manually tested (if applicable)
  • Loading branch information
kocubinski authored Jul 20, 2022
1 parent 08d6023 commit c9565fb
Show file tree
Hide file tree
Showing 3 changed files with 79 additions and 3 deletions.
3 changes: 2 additions & 1 deletion simd/cmd/testnet.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import (
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil"
"github.com/cosmos/cosmos-sdk/testutil/network"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand Down Expand Up @@ -484,7 +485,7 @@ func writeFile(name string, dir string, contents []byte) error {

// startTestnet starts an in-process testnet
func startTestnet(cmd *cobra.Command, args startArgs) error {
networkConfig := network.DefaultConfig()
networkConfig := network.DefaultConfig(simapp.NewTestNetworkFixture)

// Default networkConfig.ChainID is random, and we should only override it if chainID provided
// is non-empty
Expand Down
34 changes: 32 additions & 2 deletions test_helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,16 @@ import (
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/crypto/keys/secp256k1"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
pruningtypes "github.com/cosmos/cosmos-sdk/pruning/types"
"github.com/cosmos/cosmos-sdk/server"
"github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/simapp/params"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
simappparams "github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/testutil/mock"
"github.com/cosmos/cosmos-sdk/testutil/network"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
Expand All @@ -38,7 +42,7 @@ import (
type SetupOptions struct {
Logger log.Logger
DB *dbm.MemDB
EncConfig params.EncodingConfig
EncConfig simappparams.EncodingConfig
AppOpts types.AppOptions
}

Expand Down Expand Up @@ -352,3 +356,29 @@ func ModuleAccountAddrs() map[string]bool {
}
return bk.GetBlockedAddresses()
}

// NewTestNetworkFixture returns a new simapp AppConstructor for network simulation tests
func NewTestNetworkFixture() network.TestFixture {
encodingCfg := MakeTestEncodingConfig()
cfg := testutil.TestEncodingConfig{
TxConfig: encodingCfg.TxConfig,
Codec: encodingCfg.Codec,
Amino: encodingCfg.Amino,
InterfaceRegistry: encodingCfg.InterfaceRegistry,
}
appCtr := func(val testutil.Validator) servertypes.Application {
return NewSimApp(
val.GetCtx().Logger, dbm.NewMemDB(), nil, true,
encodingCfg,
simtestutil.NewAppOptionsWithFlagHome(val.GetCtx().Config.RootDir),
bam.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
bam.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
)
}

return network.TestFixture{
AppConstructor: appCtr,
GenesisState: ModuleBasics.DefaultGenesis(cfg.Codec),
EncodingConfig: cfg,
}
}
45 changes: 45 additions & 0 deletions testutil_network_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
//go:build norace
// +build norace

package simapp_test

import (
"testing"
"time"

"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
)

type IntegrationTestSuite struct {
suite.Suite

network *network.Network
}

func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

var err error
s.network, err = network.New(s.T(), s.T().TempDir(), network.DefaultConfig(simapp.NewTestNetworkFixture))
s.Require().NoError(err)

h, err := s.network.WaitForHeight(1)
s.Require().NoError(err, "stalled at height %d", h)
}

func (s *IntegrationTestSuite) TearDownSuite() {
s.T().Log("tearing down integration test suite")
s.network.Cleanup()
}

func (s *IntegrationTestSuite) TestNetwork_Liveness() {
h, err := s.network.WaitForHeightWithTimeout(10, time.Minute)
s.Require().NoError(err, "expected to reach 10 blocks; got %d", h)
}

func TestIntegrationTestSuite(t *testing.T) {
suite.Run(t, new(IntegrationTestSuite))
}

0 comments on commit c9565fb

Please sign in to comment.