|
| 1 | +package keeper_test |
| 2 | + |
| 3 | +import ( |
| 4 | + "testing" |
| 5 | + "time" |
| 6 | + |
| 7 | + sdk "github.com/cosmos/cosmos-sdk/types" |
| 8 | + capabilitykeeper "github.com/cosmos/cosmos-sdk/x/capability/keeper" |
| 9 | + "github.com/stretchr/testify/suite" |
| 10 | + |
| 11 | + stakingkeeper "github.com/cosmos/cosmos-sdk/x/staking/keeper" |
| 12 | + stakingtypes "github.com/cosmos/cosmos-sdk/x/staking/types" |
| 13 | + upgradekeeper "github.com/cosmos/cosmos-sdk/x/upgrade/keeper" |
| 14 | + clienttypes "github.com/cosmos/ibc-go/v3/modules/core/02-client/types" |
| 15 | + ibchost "github.com/cosmos/ibc-go/v3/modules/core/24-host" |
| 16 | + ibckeeper "github.com/cosmos/ibc-go/v3/modules/core/keeper" |
| 17 | + ibctesting "github.com/cosmos/ibc-go/v3/testing" |
| 18 | +) |
| 19 | + |
| 20 | +type KeeperTestSuite struct { |
| 21 | + suite.Suite |
| 22 | + |
| 23 | + coordinator *ibctesting.Coordinator |
| 24 | + |
| 25 | + chainA *ibctesting.TestChain |
| 26 | + chainB *ibctesting.TestChain |
| 27 | +} |
| 28 | + |
| 29 | +func (suite *KeeperTestSuite) SetupTest() { |
| 30 | + suite.coordinator = ibctesting.NewCoordinator(suite.T(), 2) |
| 31 | + |
| 32 | + suite.chainA = suite.coordinator.GetChain(ibctesting.GetChainID(1)) |
| 33 | + suite.chainB = suite.coordinator.GetChain(ibctesting.GetChainID(2)) |
| 34 | + |
| 35 | + // TODO: remove |
| 36 | + // commit some blocks so that QueryProof returns valid proof (cannot return valid query if height <= 1) |
| 37 | + suite.coordinator.CommitNBlocks(suite.chainA, 2) |
| 38 | + suite.coordinator.CommitNBlocks(suite.chainB, 2) |
| 39 | +} |
| 40 | + |
| 41 | +func TestKeeperTestSuite(t *testing.T) { |
| 42 | + suite.Run(t, new(KeeperTestSuite)) |
| 43 | +} |
| 44 | + |
| 45 | +// MockStakingKeeper implements clienttypes.StakingKeeper used in ibckeeper.NewKeeper |
| 46 | +type MockStakingKeeper struct { |
| 47 | + mockField string |
| 48 | +} |
| 49 | + |
| 50 | +func (d MockStakingKeeper) GetHistoricalInfo(ctx sdk.Context, height int64) (stakingtypes.HistoricalInfo, bool) { |
| 51 | + return stakingtypes.HistoricalInfo{}, true |
| 52 | +} |
| 53 | + |
| 54 | +func (d MockStakingKeeper) UnbondingTime(ctx sdk.Context) time.Duration { |
| 55 | + return 0 |
| 56 | +} |
| 57 | + |
| 58 | +// Test ibckeeper.NewKeeper used to initialize IBCKeeper when creating an app instance. |
| 59 | +// It verifies if ibckeeper.NewKeeper panic when any of the keepers passed in is empty. |
| 60 | +func (suite *KeeperTestSuite) TestNewKeeper() { |
| 61 | + var ( |
| 62 | + stakingKeeper clienttypes.StakingKeeper |
| 63 | + upgradeKeeper clienttypes.UpgradeKeeper |
| 64 | + scopedKeeper capabilitykeeper.ScopedKeeper |
| 65 | + newIBCKeeper = func() { |
| 66 | + ibckeeper.NewKeeper( |
| 67 | + suite.chainA.GetSimApp().AppCodec(), |
| 68 | + suite.chainA.GetSimApp().GetKey(ibchost.StoreKey), |
| 69 | + suite.chainA.GetSimApp().GetSubspace(ibchost.ModuleName), |
| 70 | + stakingKeeper, |
| 71 | + upgradeKeeper, |
| 72 | + scopedKeeper, |
| 73 | + ) |
| 74 | + } |
| 75 | + ) |
| 76 | + |
| 77 | + testCases := []struct { |
| 78 | + name string |
| 79 | + malleate func() |
| 80 | + expPass bool |
| 81 | + }{ |
| 82 | + {"failure: empty staking keeper", func() { |
| 83 | + emptyStakingKeeper := stakingkeeper.Keeper{} |
| 84 | + |
| 85 | + stakingKeeper = emptyStakingKeeper |
| 86 | + |
| 87 | + }, false}, |
| 88 | + {"failure: empty mock staking keeper", func() { |
| 89 | + // use a different implementation of clienttypes.StakingKeeper |
| 90 | + emptyMockStakingKeeper := MockStakingKeeper{} |
| 91 | + |
| 92 | + stakingKeeper = emptyMockStakingKeeper |
| 93 | + |
| 94 | + }, false}, |
| 95 | + {"failure: empty upgrade keeper", func() { |
| 96 | + emptyUpgradeKeeper := upgradekeeper.Keeper{} |
| 97 | + |
| 98 | + upgradeKeeper = emptyUpgradeKeeper |
| 99 | + |
| 100 | + }, false}, |
| 101 | + {"failure: empty scoped keeper", func() { |
| 102 | + emptyScopedKeeper := capabilitykeeper.ScopedKeeper{} |
| 103 | + |
| 104 | + scopedKeeper = emptyScopedKeeper |
| 105 | + }, false}, |
| 106 | + {"success: replace stakingKeeper with non-empty MockStakingKeeper", func() { |
| 107 | + // use a different implementation of clienttypes.StakingKeeper |
| 108 | + mockStakingKeeper := MockStakingKeeper{"not empty"} |
| 109 | + |
| 110 | + stakingKeeper = mockStakingKeeper |
| 111 | + }, true}, |
| 112 | + } |
| 113 | + |
| 114 | + for _, tc := range testCases { |
| 115 | + tc := tc |
| 116 | + suite.SetupTest() |
| 117 | + |
| 118 | + suite.Run(tc.name, func() { |
| 119 | + |
| 120 | + stakingKeeper = suite.chainA.GetSimApp().StakingKeeper |
| 121 | + upgradeKeeper = suite.chainA.GetSimApp().UpgradeKeeper |
| 122 | + scopedKeeper = suite.chainA.GetSimApp().ScopedIBCKeeper |
| 123 | + |
| 124 | + tc.malleate() |
| 125 | + |
| 126 | + if tc.expPass { |
| 127 | + suite.Require().NotPanics( |
| 128 | + newIBCKeeper, |
| 129 | + ) |
| 130 | + } else { |
| 131 | + suite.Require().Panics( |
| 132 | + newIBCKeeper, |
| 133 | + ) |
| 134 | + } |
| 135 | + |
| 136 | + }) |
| 137 | + } |
| 138 | +} |
0 commit comments