-
Notifications
You must be signed in to change notification settings - Fork 586
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Browse files
Browse the repository at this point in the history
* add empty keepers checking in ibc NewKeeper (#1284) * add empty keepers checking in ibc NewKeeper * check for empty exported keepers instead of empty sdk-defined keeper structs * Update modules/core/keeper/keeper.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * remove func checkEmptyKeepers(), check empty keepers directly within func NewKeeper() * modules/core/keeper KeeperTestSuite -> MsgServerTestSuite; creat new modules/core/keeper KeeperTestSuite for testing ibckeeper.NewKeeper() * update CHANGELOG.md * DummyStakingKeeper -> MockStakingKeeper * refactor modules/core/keeper test * Update modules/core/keeper/keeper_test.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> * Update modules/core/keeper/keeper.go Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> Co-authored-by: colin axnér <25233464+colin-axner@users.noreply.github.com> (cherry picked from commit f2577f9) # Conflicts: # CHANGELOG.md # modules/core/keeper/msg_server_test.go * fix conflict * fix conflict * fix merge * fix tests * imports formatting * Update CHANGELOG.md Co-authored-by: khanh <50263489+catShaark@users.noreply.github.com> Co-authored-by: Carlos Rodriguez <carlos@interchain.io>
- Loading branch information
1 parent
2504e42
commit 8d5fcff
Showing
4 changed files
with
157 additions
and
33 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,139 @@ | ||
package keeper_test | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/suite" | ||
|
||
sdk "github.com/cosmos/cosmos-sdk/types" | ||
capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" | ||
stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" | ||
stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" | ||
upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper" | ||
|
||
clienttypes "github.com/cosmos/ibc-go/modules/core/02-client/types" | ||
ibchost "github.com/cosmos/ibc-go/modules/core/24-host" | ||
ibckeeper "github.com/cosmos/ibc-go/modules/core/keeper" | ||
ibctesting "github.com/cosmos/ibc-go/testing" | ||
) | ||
|
||
type KeeperTestSuite struct { | ||
suite.Suite | ||
|
||
coordinator *ibctesting.Coordinator | ||
|
||
chainA *ibctesting.TestChain | ||
chainB *ibctesting.TestChain | ||
} | ||
|
||
func (suite *KeeperTestSuite) SetupTest() { | ||
suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) | ||
|
||
suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(0)) | ||
suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(1)) | ||
|
||
// TODO: remove | ||
// commit some blocks so that QueryProof returns valid proof (cannot return valid query if height <= 1) | ||
suite.coordinator.CommitNBlocks(suite.chainA, 2) | ||
suite.coordinator.CommitNBlocks(suite.chainB, 2) | ||
} | ||
|
||
func TestKeeperTestSuite(t *testing.T) { | ||
suite.Run(t, new(KeeperTestSuite)) | ||
} | ||
|
||
// MockStakingKeeper implements clienttypes.StakingKeeper used in ibckeeper.NewKeeper | ||
type MockStakingKeeper struct { | ||
mockField string | ||
} | ||
|
||
func (d MockStakingKeeper) GetHistoricalInfo(ctx sdk.Context, height int64) (stakingtypes.HistoricalInfo, bool) { | ||
return stakingtypes.HistoricalInfo{}, true | ||
} | ||
|
||
func (d MockStakingKeeper) UnbondingTime(ctx sdk.Context) time.Duration { | ||
return 0 | ||
} | ||
|
||
// Test ibckeeper.NewKeeper used to initialize IBCKeeper when creating an app instance. | ||
// It verifies if ibckeeper.NewKeeper panic when any of the keepers passed in is empty. | ||
func (suite *KeeperTestSuite) TestNewKeeper() { | ||
var ( | ||
stakingKeeper clienttypes.StakingKeeper | ||
upgradeKeeper clienttypes.UpgradeKeeper | ||
scopedKeeper capabilitykeeper.ScopedKeeper | ||
newIBCKeeper = func() { | ||
ibckeeper.NewKeeper( | ||
suite.chainA.GetSimApp().AppCodec(), | ||
suite.chainA.GetSimApp().GetKey(ibchost.StoreKey), | ||
suite.chainA.GetSimApp().GetSubspace(ibchost.ModuleName), | ||
stakingKeeper, | ||
upgradeKeeper, | ||
scopedKeeper, | ||
) | ||
} | ||
) | ||
|
||
testCases := []struct { | ||
name string | ||
malleate func() | ||
expPass bool | ||
}{ | ||
{"failure: empty staking keeper", func() { | ||
emptyStakingKeeper := stakingkeeper.Keeper{} | ||
|
||
stakingKeeper = emptyStakingKeeper | ||
|
||
}, false}, | ||
{"failure: empty mock staking keeper", func() { | ||
// use a different implementation of clienttypes.StakingKeeper | ||
emptyMockStakingKeeper := MockStakingKeeper{} | ||
|
||
stakingKeeper = emptyMockStakingKeeper | ||
|
||
}, false}, | ||
{"failure: empty upgrade keeper", func() { | ||
emptyUpgradeKeeper := upgradekeeper.Keeper{} | ||
|
||
upgradeKeeper = emptyUpgradeKeeper | ||
|
||
}, false}, | ||
{"failure: empty scoped keeper", func() { | ||
emptyScopedKeeper := capabilitykeeper.ScopedKeeper{} | ||
|
||
scopedKeeper = emptyScopedKeeper | ||
}, false}, | ||
{"success: replace stakingKeeper with non-empty MockStakingKeeper", func() { | ||
// use a different implementation of clienttypes.StakingKeeper | ||
mockStakingKeeper := MockStakingKeeper{"not empty"} | ||
|
||
stakingKeeper = mockStakingKeeper | ||
}, true}, | ||
} | ||
|
||
for _, tc := range testCases { | ||
tc := tc | ||
suite.SetupTest() | ||
|
||
suite.Run(tc.name, func() { | ||
|
||
stakingKeeper = suite.chainA.GetSimApp().StakingKeeper | ||
upgradeKeeper = suite.chainA.GetSimApp().UpgradeKeeper | ||
scopedKeeper = suite.chainA.GetSimApp().ScopedIBCKeeper | ||
|
||
tc.malleate() | ||
|
||
if tc.expPass { | ||
suite.Require().NotPanics( | ||
newIBCKeeper, | ||
) | ||
} else { | ||
suite.Require().Panics( | ||
newIBCKeeper, | ||
) | ||
} | ||
|
||
}) | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters