cosmos-sdk/x/upgrade/keeper/grpc_query_test.go

209 lines
5.0 KiB
Go
Raw Normal View History

package keeper_test
import (
gocontext "context"
"fmt"
"testing"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/baseapp"
"github.com/cosmos/cosmos-sdk/simapp"
sdk "github.com/cosmos/cosmos-sdk/types"
x/upgrade: added consensus version tracking (part of ADR-041) (#8743) * -added consensus version tracking to x/upgrade * -added interface to module manager -added e2e test for migrations using consensus version store in x/upgrade -cleaned up x/upgrade Keeper -handler in apply upgrade now handles errors and setting consensus versions -cleaned up migration map keys -removed init chainer method -simapp now implements GetConsensusVersions to assist with testing * Changed MigrationMap identifier to VersionMap removed module_test * updated docs * forgot this * added line to changelog for this PR * Change set consensus version function to match adr 041 spec * add documentation * remove newline from changelog unnecessary newline removed * updated example in simapp for RunMigrations, SetCurrentConsensusVersions now returns an error * switch TestMigrations to use Require instead of t.Fatal * Update CHANGELOG.md Co-authored-by: Aaron Craelius <aaron@regen.network> * docs for SetVersionManager * -init genesis method added -removed panics/fails from setting consensus versions * update identifiers to be more go-like * update docs and UpgradeHandler fnc sig * Upgrade Keeper now takes a VersionMap instead of a VersionManager interface * upgrade keeper transition to Version Map * cleanup, added versionmap return to RunMigrations * quick fix * Update docs/architecture/adr-041-in-place-store-migrations.md Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> * remove support for versionmap field on upgrade keeper * cleanup * rename get/set version map keeper functions * update adr doc to match name changes * remove redudant line Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com> Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2021-03-19 15:01:29 -07:00
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)
type UpgradeTestSuite struct {
suite.Suite
app *simapp.SimApp
ctx sdk.Context
queryClient types.QueryClient
}
func (suite *UpgradeTestSuite) SetupTest() {
refactor(test)!: refactor `simapp.Setup` function (#9938) <!-- The default pull request template is for types feat, fix, or refactor. For other templates, add one of the following parameters to the url: - template=docs.md - template=other.md --> ## Description ref: #8961 <!-- Add a description of the changes that this PR introduces and the files that are the most critical to review. --> Following up on [#9697](https://github.com/cosmos/cosmos-sdk/pull/9697#pullrequestreview-727295733), this PR is the first step for the #8961. --- ### 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... - [x] included the correct [type prefix](https://github.com/commitizen/conventional-commit-types/blob/v3.0.0/index.json) in the PR title - [x] added `!` to the type prefix if API or client breaking change - [x] targeted the correct branch (see [PR Targeting](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#pr-targeting)) - [x] provided a link to the relevant issue or specification - [ ] followed the guidelines for [building modules](https://github.com/cosmos/cosmos-sdk/blob/master/docs/building-modules) - [ ] included the necessary unit and integration [tests](https://github.com/cosmos/cosmos-sdk/blob/master/CONTRIBUTING.md#testing) - [x] 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)
2021-08-16 17:52:06 -07:00
suite.app = simapp.Setup(suite.T(), false)
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{})
queryHelper := baseapp.NewQueryServerTestHelper(suite.ctx, suite.app.InterfaceRegistry())
types.RegisterQueryServer(queryHelper, suite.app.UpgradeKeeper)
suite.queryClient = types.NewQueryClient(queryHelper)
}
func (suite *UpgradeTestSuite) TestQueryCurrentPlan() {
var (
req *types.QueryCurrentPlanRequest
expResponse types.QueryCurrentPlanResponse
)
testCases := []struct {
msg string
malleate func()
expPass bool
}{
{
"without current upgrade plan",
func() {
req = &types.QueryCurrentPlanRequest{}
expResponse = types.QueryCurrentPlanResponse{}
},
true,
},
{
"with current upgrade plan",
func() {
plan := types.Plan{Name: "test-plan", Height: 5}
suite.app.UpgradeKeeper.ScheduleUpgrade(suite.ctx, plan)
req = &types.QueryCurrentPlanRequest{}
expResponse = types.QueryCurrentPlanResponse{Plan: &plan}
},
true,
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset
tc.malleate()
res, err := suite.queryClient.CurrentPlan(gocontext.Background(), req)
if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(&expResponse, res)
} else {
suite.Require().Error(err)
}
})
}
}
func (suite *UpgradeTestSuite) TestAppliedCurrentPlan() {
var (
req *types.QueryAppliedPlanRequest
expHeight int64
)
testCases := []struct {
msg string
malleate func()
expPass bool
}{
{
"with non-existent upgrade plan",
func() {
req = &types.QueryAppliedPlanRequest{Name: "foo"}
},
true,
},
{
"with applied upgrade plan",
func() {
expHeight = 5
planName := "test-plan"
plan := types.Plan{Name: planName, Height: expHeight}
suite.app.UpgradeKeeper.ScheduleUpgrade(suite.ctx, plan)
suite.ctx = suite.ctx.WithBlockHeight(expHeight)
x/upgrade: added consensus version tracking (part of ADR-041) (#8743) * -added consensus version tracking to x/upgrade * -added interface to module manager -added e2e test for migrations using consensus version store in x/upgrade -cleaned up x/upgrade Keeper -handler in apply upgrade now handles errors and setting consensus versions -cleaned up migration map keys -removed init chainer method -simapp now implements GetConsensusVersions to assist with testing * Changed MigrationMap identifier to VersionMap removed module_test * updated docs * forgot this * added line to changelog for this PR * Change set consensus version function to match adr 041 spec * add documentation * remove newline from changelog unnecessary newline removed * updated example in simapp for RunMigrations, SetCurrentConsensusVersions now returns an error * switch TestMigrations to use Require instead of t.Fatal * Update CHANGELOG.md Co-authored-by: Aaron Craelius <aaron@regen.network> * docs for SetVersionManager * -init genesis method added -removed panics/fails from setting consensus versions * update identifiers to be more go-like * update docs and UpgradeHandler fnc sig * Upgrade Keeper now takes a VersionMap instead of a VersionManager interface * upgrade keeper transition to Version Map * cleanup, added versionmap return to RunMigrations * quick fix * Update docs/architecture/adr-041-in-place-store-migrations.md Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> * remove support for versionmap field on upgrade keeper * cleanup * rename get/set version map keeper functions * update adr doc to match name changes * remove redudant line Co-authored-by: technicallyty <48813565+tytech3@users.noreply.github.com> Co-authored-by: Aaron Craelius <aaron@regen.network> Co-authored-by: Amaury <1293565+amaurym@users.noreply.github.com> Co-authored-by: mergify[bot] <37929162+mergify[bot]@users.noreply.github.com>
2021-03-19 15:01:29 -07:00
suite.app.UpgradeKeeper.SetUpgradeHandler(planName, func(ctx sdk.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) {
return vm, nil
})
suite.app.UpgradeKeeper.ApplyUpgrade(suite.ctx, plan)
req = &types.QueryAppliedPlanRequest{Name: planName}
},
true,
},
}
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset
tc.malleate()
res, err := suite.queryClient.AppliedPlan(gocontext.Background(), req)
if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
suite.Require().Equal(expHeight, res.Height)
} else {
suite.Require().Error(err)
}
})
}
}
func (suite *UpgradeTestSuite) TestModuleVersions() {
testCases := []struct {
msg string
req types.QueryModuleVersionsRequest
single bool
expPass bool
}{
{
msg: "test full query",
req: types.QueryModuleVersionsRequest{},
single: false,
expPass: true,
},
{
msg: "test single module",
req: types.QueryModuleVersionsRequest{ModuleName: "bank"},
single: true,
expPass: true,
},
{
msg: "test non-existent module",
req: types.QueryModuleVersionsRequest{ModuleName: "abcdefg"},
single: true,
expPass: false,
},
}
vm := suite.app.UpgradeKeeper.GetModuleVersionMap(suite.ctx)
mv := suite.app.UpgradeKeeper.GetModuleVersions(suite.ctx)
for _, tc := range testCases {
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
suite.SetupTest() // reset
res, err := suite.queryClient.ModuleVersions(gocontext.Background(), &tc.req)
if tc.expPass {
suite.Require().NoError(err)
suite.Require().NotNil(res)
if tc.single {
// test that the single module response is valid
suite.Require().Len(res.ModuleVersions, 1)
// make sure we got the right values
suite.Require().Equal(vm[tc.req.ModuleName], res.ModuleVersions[0].Version)
suite.Require().Equal(tc.req.ModuleName, res.ModuleVersions[0].Name)
} else {
// check that the full response is valid
suite.Require().NotEmpty(res.ModuleVersions)
suite.Require().Equal(len(mv), len(res.ModuleVersions))
for i, v := range res.ModuleVersions {
suite.Require().Equal(mv[i].Version, v.Version)
suite.Require().Equal(mv[i].Name, v.Name)
}
}
} else {
suite.Require().Error(err)
}
})
}
}
func TestUpgradeTestSuite(t *testing.T) {
suite.Run(t, new(UpgradeTestSuite))
}