Skip to content

Commit

Permalink
refactor: Use mocks for x/upgrade testing (#12698)
Browse files Browse the repository at this point in the history
## Description

Closes: #12500 



---

### 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
likhita-809 authored Jul 27, 2022
1 parent 105f22e commit 25929e3
Show file tree
Hide file tree
Showing 3 changed files with 68 additions and 49 deletions.
49 changes: 28 additions & 21 deletions x/upgrade/abci_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,53 +8,60 @@ import (
"time"

"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
abci "github.com/tendermint/tendermint/abci/types"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"golang.org/x/exp/maps"

"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/client/flags"
"github.com/cosmos/cosmos-sdk/codec"
"github.com/cosmos/cosmos-sdk/server"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
sdkerrors "github.com/cosmos/cosmos-sdk/types/errors"
"github.com/cosmos/cosmos-sdk/types/module"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
govtypesv1beta1 "github.com/cosmos/cosmos-sdk/x/gov/types/v1beta1"
"github.com/cosmos/cosmos-sdk/x/upgrade"
"github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/cosmos/cosmos-sdk/x/upgrade/testutil"
"github.com/tendermint/tendermint/libs/log"

"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

type TestSuite struct {
suite.Suite

module module.BeginBlockAppModule
keeper keeper.Keeper
querier sdk.Querier
handler govtypes.Handler
handler govtypesv1beta1.Handler
ctx sdk.Context
baseApp *baseapp.BaseApp
encCfg moduletestutil.TestEncodingConfig
}

var s TestSuite

func setupTest(t *testing.T, height int64, skip map[int64]bool) TestSuite {
var (
legacyAmino *codec.LegacyAmino
appOptions = make(simtestutil.AppOptionsMap, 0)
)

appOptions[server.FlagUnsafeSkipUpgrades] = maps.Keys(skip)
appOptions[flags.FlagHome] = t.TempDir()
s.encCfg = moduletestutil.MakeTestEncodingConfig(upgrade.AppModuleBasic{})
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))

appConfig := depinject.Configs(testutil.AppConfig, depinject.Supply(appOptions))
app, err := simtestutil.Setup(appConfig, &s.keeper, &legacyAmino)
s.keeper.SetVersionSetter(app.BaseApp)
require.NoError(t, err)
s.baseApp = baseapp.NewBaseApp(
"upgrade",
log.NewNopLogger(),
testCtx.DB,
s.encCfg.TxConfig.TxDecoder(),
)

s.keeper = keeper.NewKeeper(skip, key, s.encCfg.Codec, t.TempDir(), nil, authtypes.NewModuleAddress(govtypes.ModuleName).String())
s.keeper.SetVersionSetter(s.baseApp)

s.ctx = app.BaseApp.NewContext(false, tmproto.Header{Height: height, Time: time.Now()})
s.ctx = testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: time.Now(), Height: height})

s.module = upgrade.NewAppModule(s.keeper)
s.querier = s.module.LegacyQuerierHandler(legacyAmino)
s.querier = s.module.LegacyQuerierHandler(s.encCfg.Amino)
s.handler = upgrade.NewSoftwareUpgradeProposalHandler(s.keeper)
return s
}
Expand Down
27 changes: 16 additions & 11 deletions x/upgrade/keeper/grpc_query_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,17 @@ import (
"testing"

"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"github.com/cosmos/cosmos-sdk/baseapp"
codectypes "github.com/cosmos/cosmos-sdk/codec/types"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
"github.com/cosmos/cosmos-sdk/testutil"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/upgrade"

"github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/cosmos/cosmos-sdk/x/upgrade/testutil"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

Expand All @@ -26,19 +26,24 @@ type UpgradeTestSuite struct {
upgradeKeeper keeper.Keeper
ctx sdk.Context
queryClient types.QueryClient
encCfg moduletestutil.TestEncodingConfig
}

func (suite *UpgradeTestSuite) SetupTest() {
var interfaceRegistry codectypes.InterfaceRegistry

app, err := simtestutil.Setup(testutil.AppConfig, &interfaceRegistry, &suite.upgradeKeeper)
suite.NoError(err)
suite.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
suite.encCfg = moduletestutil.MakeTestEncodingConfig(upgrade.AppModuleBasic{})
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(suite.T(), key, sdk.NewTransientStoreKey("transient_test"))
suite.ctx = testCtx.Ctx

skipUpgradeHeights := make(map[int64]bool)

suite.upgradeKeeper.SetVersionSetter(app.BaseApp)
suite.upgradeKeeper.SetModuleVersionMap(suite.ctx, app.ModuleManager.GetVersionMap())
suite.upgradeKeeper = keeper.NewKeeper(skipUpgradeHeights, key, suite.encCfg.Codec, "", nil, authtypes.NewModuleAddress(govtypes.ModuleName).String())
suite.upgradeKeeper.SetModuleVersionMap(suite.ctx, module.VersionMap{
"bank": 0,
})

queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, interfaceRegistry)
queryHelper := baseapp.NewQueryServerTestHelper(testCtx.Ctx, suite.encCfg.InterfaceRegistry)
types.RegisterQueryServer(queryHelper, suite.upgradeKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
}
Expand Down
41 changes: 24 additions & 17 deletions x/upgrade/keeper/keeper_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,17 +6,19 @@ import (
"time"

"github.com/stretchr/testify/suite"
"github.com/tendermint/tendermint/libs/log"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"

"cosmossdk.io/depinject"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/testutil"
simtestutil "github.com/cosmos/cosmos-sdk/testutil/sims"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
bankkeeper "github.com/cosmos/cosmos-sdk/x/bank/keeper"
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper"
moduletestutil "github.com/cosmos/cosmos-sdk/types/module/testutil"
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
"github.com/cosmos/cosmos-sdk/x/upgrade"
"github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/cosmos/cosmos-sdk/x/upgrade/testutil"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)

Expand All @@ -29,29 +31,34 @@ type KeeperTestSuite struct {
ctx sdk.Context
msgSrvr types.MsgServer
addrs []sdk.AccAddress
encCfg moduletestutil.TestEncodingConfig
}

func (s *KeeperTestSuite) SetupTest() {
var (
bankKeeper bankkeeper.Keeper
stakingKeeper *stakingkeeper.Keeper

s.encCfg = moduletestutil.MakeTestEncodingConfig(upgrade.AppModuleBasic{})
key := sdk.NewKVStoreKey(types.StoreKey)
testCtx := testutil.DefaultContextWithDB(s.T(), key, sdk.NewTransientStoreKey("transient_test"))

s.baseApp = baseapp.NewBaseApp(
"upgrade",
log.NewNopLogger(),
testCtx.DB,
s.encCfg.TxConfig.TxDecoder(),
)

skipUpgradeHeights := make(map[int64]bool)

homeDir := filepath.Join(s.T().TempDir(), "x_upgrade_keeper_test")
appConfig := depinject.Configs(testutil.AppConfig, depinject.Supply(simtestutil.NewAppOptionsWithFlagHome(homeDir)))
app, err := simtestutil.Setup(appConfig, &s.upgradeKeeper, &bankKeeper, &stakingKeeper)
s.NoError(err)
s.upgradeKeeper.SetVersionSetter(app.BaseApp)
s.upgradeKeeper = keeper.NewKeeper(skipUpgradeHeights, key, s.encCfg.Codec, homeDir, nil, authtypes.NewModuleAddress(govtypes.ModuleName).String())
s.upgradeKeeper.SetVersionSetter(s.baseApp)

s.T().Log("home dir:", homeDir)
s.homeDir = homeDir
s.ctx = app.BaseApp.NewContext(false, tmproto.Header{
Time: time.Now(),
Height: 10,
})
s.ctx = testCtx.Ctx.WithBlockHeader(tmproto.Header{Time: time.Now(), Height: 10})

s.msgSrvr = keeper.NewMsgServerImpl(s.upgradeKeeper)
s.addrs = simtestutil.AddTestAddrsIncremental(bankKeeper, stakingKeeper, s.ctx, 1, sdk.NewInt(30000000))
s.baseApp = app.BaseApp
s.addrs = simtestutil.CreateIncrementalAccounts(1)
}

func (s *KeeperTestSuite) TestReadUpgradeInfoFromDisk() {
Expand Down

0 comments on commit 25929e3

Please sign in to comment.