Skip to content

Commit

Permalink
refactor(testutil): remove dependency on simapp (#12624)
Browse files Browse the repository at this point in the history
## 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)
  • Loading branch information
kocubinski authored Jul 20, 2022
1 parent 3523c26 commit d4a9d4d
Show file tree
Hide file tree
Showing 22 changed files with 117 additions and 56 deletions.
5 changes: 3 additions & 2 deletions client/context_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -17,6 +17,7 @@ import (
"github.com/cosmos/cosmos-sdk/codec/types"
"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/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
)
Expand Down Expand Up @@ -145,7 +146,7 @@ x: "10"
}

func TestCLIQueryConn(t *testing.T) {
cfg := network.DefaultConfig()
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
cfg.NumValidators = 1

n, err := network.New(t, t.TempDir(), cfg)
Expand All @@ -159,7 +160,7 @@ func TestCLIQueryConn(t *testing.T) {
}

func TestGetFromFields(t *testing.T) {
cfg := network.DefaultConfig()
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
path := hd.CreateHDPath(118, 0, 0).String()

testCases := []struct {
Expand Down
3 changes: 2 additions & 1 deletion client/grpc/tmservice/service_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/rest"
"github.com/cosmos/cosmos-sdk/types"
Expand All @@ -34,7 +35,7 @@ func TestIntegrationTestSuite(t *testing.T) {
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

cfg := network.DefaultConfig()
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
cfg.NumValidators = 1

s.cfg = cfg
Expand Down
3 changes: 2 additions & 1 deletion client/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import (
"google.golang.org/grpc"
"google.golang.org/grpc/metadata"

"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
"github.com/cosmos/cosmos-sdk/testutil/testdata"
sdk "github.com/cosmos/cosmos-sdk/types"
Expand All @@ -29,7 +30,7 @@ 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.network, err = network.New(s.T(), s.T().TempDir(), network.DefaultConfig(simapp.NewTestNetworkFixture))
s.Require().NoError(err)

_, err = s.network.WaitForHeight(2)
Expand Down
3 changes: 2 additions & 1 deletion client/rpc/rpc_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"github.com/stretchr/testify/suite"

"github.com/cosmos/cosmos-sdk/client/rpc"
"github.com/cosmos/cosmos-sdk/simapp"
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
"github.com/cosmos/cosmos-sdk/testutil/network"
)
Expand All @@ -21,7 +22,7 @@ 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.network, err = network.New(s.T(), s.T().TempDir(), network.DefaultConfig(simapp.NewTestNetworkFixture))
s.Require().NoError(err)

s.Require().NoError(s.network.WaitForNextBlock())
Expand Down
3 changes: 2 additions & 1 deletion server/grpc/grpc_web_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"github.com/cosmos/cosmos-sdk/client/grpc/tmservice"
"github.com/cosmos/cosmos-sdk/codec"
cryptotypes "github.com/cosmos/cosmos-sdk/crypto/types"
"github.com/cosmos/cosmos-sdk/simapp"
"github.com/cosmos/cosmos-sdk/testutil/network"
banktypes "github.com/cosmos/cosmos-sdk/x/bank/types"
)
Expand All @@ -41,7 +42,7 @@ type GRPCWebTestSuite struct {
func (s *GRPCWebTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")

cfg := network.DefaultConfig()
cfg := network.DefaultConfig(simapp.NewTestNetworkFixture)
cfg.NumValidators = 1
s.cfg = cfg

Expand Down
2 changes: 1 addition & 1 deletion server/grpc/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ type IntegrationTestSuite struct {
func (s *IntegrationTestSuite) SetupSuite() {
s.T().Log("setting up integration test suite")
s.app = simapp.Setup(s.T(), false)
s.cfg = network.DefaultConfig()
s.cfg = network.DefaultConfig(simapp.NewTestNetworkFixture)
s.cfg.NumValidators = 1

var err error
Expand Down
3 changes: 2 additions & 1 deletion simapp/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 simapp/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,
}
}
Original file line number Diff line number Diff line change
@@ -1,14 +1,15 @@
//go:build norace
// +build norace

package network_test
package simapp_test

import (
"testing"
"time"

"github.com/stretchr/testify/suite"

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

Expand All @@ -22,7 +23,7 @@ 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.network, err = network.New(s.T(), s.T().TempDir(), network.DefaultConfig(simapp.NewTestNetworkFixture))
s.Require().NoError(err)

h, err := s.network.WaitForHeight(1)
Expand Down
8 changes: 4 additions & 4 deletions testutil/key_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand All @@ -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)

Expand All @@ -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)

Expand Down
66 changes: 34 additions & 32 deletions testutil/network/network.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand All @@ -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"
Expand All @@ -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

// 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 AppConstructor = func(val moduletestutil.Validator) servertypes.Application
type TestFixtureFactory = func() TestFixture

type TestFixture struct {
AppConstructor AppConstructor
GenesisState map[string]json.RawMessage
EncodingConfig moduletestutil.TestEncodingConfig
}

// Config defines the necessary configuration used to bootstrap and start an
Expand Down Expand Up @@ -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,
Expand All @@ -133,8 +125,6 @@ func DefaultConfig() Config {
}

func DefaultConfigWithAppConfig(appConfig depinject.Config) (Config, error) {
cfg := DefaultConfig()

var (
appBuilder *runtime.AppBuilder
txConfig client.TxConfig
Expand All @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
12 changes: 12 additions & 0 deletions types/module/testutil/network.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
package testutil

import (
"github.com/cosmos/cosmos-sdk/server"
srvconfig "github.com/cosmos/cosmos-sdk/server/config"
)

// TODO rename
type Validator interface {
GetCtx() *server.Context
GetAppConfig() *srvconfig.Config
}
Loading

0 comments on commit d4a9d4d

Please sign in to comment.