refactor(testutil): remove dependency on simapp (#12624)

## Description

Closes: #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)
This commit is contained in:
Matt Kocubinski
2022-07-20 07:34:58 +00:00
committed by GitHub
parent 3523c2669f
commit d4a9d4d321
22 changed files with 116 additions and 55 deletions
+4 -4
View File
@@ -7,13 +7,13 @@ import (
"github.com/cosmos/cosmos-sdk/crypto/hd"
"github.com/cosmos/cosmos-sdk/crypto/keyring"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module/testutil"
)
func TestGenerateCoinKey(t *testing.T) {
t.Parallel()
cdc := simapp.MakeTestEncodingConfig().Codec
cdc := testutil.MakeTestEncodingConfig().Codec
addr, mnemonic, err := GenerateCoinKey(hd.Secp256k1, cdc)
require.NoError(t, err)
@@ -28,7 +28,7 @@ func TestGenerateCoinKey(t *testing.T) {
func TestGenerateSaveCoinKey(t *testing.T) {
t.Parallel()
encCfg := simapp.MakeTestEncodingConfig()
encCfg := testutil.MakeTestEncodingConfig()
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, encCfg.Codec)
require.NoError(t, err)
@@ -53,7 +53,7 @@ func TestGenerateSaveCoinKey(t *testing.T) {
func TestGenerateSaveCoinKeyOverwriteFlag(t *testing.T) {
t.Parallel()
encCfg := simapp.MakeTestEncodingConfig()
encCfg := testutil.MakeTestEncodingConfig()
kb, err := keyring.New(t.Name(), "test", t.TempDir(), nil, encCfg.Codec)
require.NoError(t, err)
+33 -31
View File
@@ -25,6 +25,7 @@ import (
"google.golang.org/grpc"
"cosmossdk.io/math"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/baseapp"
@@ -41,10 +42,7 @@ import (
"github.com/cosmos/cosmos-sdk/server/api"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
servertypes "github.com/cosmos/cosmos-sdk/server/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/simapp/params"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
@@ -57,19 +55,13 @@ var lock = new(sync.Mutex)
// AppConstructor defines a function which accepts a network configuration and
// creates an ABCI Application to provide to Tendermint.
type AppConstructor = func(val Validator) servertypes.Application
type AppConstructor = func(val moduletestutil.Validator) servertypes.Application
type TestFixtureFactory = func() TestFixture
// NewAppConstructor returns a new simapp AppConstructor
func NewAppConstructor(encodingCfg params.EncodingConfig) AppConstructor {
return func(val Validator) servertypes.Application {
return simapp.NewSimApp(
val.Ctx.Logger, dbm.NewMemDB(), nil, true,
encodingCfg,
simtestutil.NewAppOptionsWithFlagHome(val.Ctx.Config.RootDir),
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.AppConfig.Pruning)),
baseapp.SetMinGasPrices(val.AppConfig.MinGasPrices),
)
}
type TestFixture struct {
AppConstructor AppConstructor
GenesisState map[string]json.RawMessage
EncodingConfig moduletestutil.TestEncodingConfig
}
// Config defines the necessary configuration used to bootstrap and start an
@@ -105,17 +97,17 @@ type Config struct {
// DefaultConfig returns a sane default configuration suitable for nearly all
// testing requirements.
func DefaultConfig() Config {
encCfg := simapp.MakeTestEncodingConfig()
func DefaultConfig(factory TestFixtureFactory) Config {
fixture := factory()
return Config{
Codec: encCfg.Codec,
TxConfig: encCfg.TxConfig,
LegacyAmino: encCfg.Amino,
InterfaceRegistry: encCfg.InterfaceRegistry,
Codec: fixture.EncodingConfig.Codec,
TxConfig: fixture.EncodingConfig.TxConfig,
LegacyAmino: fixture.EncodingConfig.Amino,
InterfaceRegistry: fixture.EncodingConfig.InterfaceRegistry,
AccountRetriever: authtypes.AccountRetriever{},
AppConstructor: NewAppConstructor(encCfg),
GenesisState: simapp.ModuleBasics.DefaultGenesis(encCfg.Codec),
AppConstructor: fixture.AppConstructor,
GenesisState: fixture.GenesisState,
TimeoutCommit: 2 * time.Second,
ChainID: "chain-" + tmrand.Str(6),
NumValidators: 4,
@@ -133,8 +125,6 @@ func DefaultConfig() Config {
}
func DefaultConfigWithAppConfig(appConfig depinject.Config) (Config, error) {
cfg := DefaultConfig()
var (
appBuilder *runtime.AppBuilder
txConfig client.TxConfig
@@ -153,23 +143,26 @@ func DefaultConfigWithAppConfig(appConfig depinject.Config) (Config, error) {
return Config{}, err
}
cfg := DefaultConfig(func() TestFixture {
return TestFixture{}
})
cfg.Codec = cdc
cfg.TxConfig = txConfig
cfg.LegacyAmino = legacyAmino
cfg.InterfaceRegistry = interfaceRegistry
cfg.GenesisState = appBuilder.DefaultGenesis()
cfg.AppConstructor = func(val Validator) servertypes.Application {
cfg.AppConstructor = func(val moduletestutil.Validator) servertypes.Application {
// we build a unique app instance for every validator here
var appBuilder *runtime.AppBuilder
if err := depinject.Inject(appConfig, &appBuilder); err != nil {
panic(err)
}
app := appBuilder.Build(
val.Ctx.Logger,
val.GetCtx().Logger,
dbm.NewMemDB(),
nil,
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.AppConfig.Pruning)),
baseapp.SetMinGasPrices(val.AppConfig.MinGasPrices),
baseapp.SetPruning(pruningtypes.NewPruningOptionsFromString(val.GetAppConfig().Pruning)),
baseapp.SetMinGasPrices(val.GetAppConfig().MinGasPrices),
)
if err := app.Load(true); err != nil {
@@ -234,10 +227,19 @@ type Logger interface {
}
var (
_ Logger = (*testing.T)(nil)
_ Logger = (*CLILogger)(nil)
_ Logger = (*testing.T)(nil)
_ Logger = (*CLILogger)(nil)
_ moduletestutil.Validator = Validator{}
)
func (v Validator) GetCtx() *server.Context {
return v.Ctx
}
func (v Validator) GetAppConfig() *srvconfig.Config {
return v.AppConfig
}
// CLILogger wraps a cobra.Command and provides command logging methods.
type CLILogger struct {
cmd *cobra.Command
-44
View File
@@ -1,44 +0,0 @@
//go:build norace
// +build norace
package network_test
import (
"testing"
"time"
"github.com/stretchr/testify/suite"
"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())
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))
}