2020-07-04 10:30:17 -07:00
|
|
|
package keeper_test
|
|
|
|
|
|
|
|
import (
|
|
|
|
gocontext "context"
|
2020-07-08 15:00:56 -07:00
|
|
|
"fmt"
|
2020-07-04 10:30:17 -07:00
|
|
|
"testing"
|
|
|
|
|
2020-07-08 15:00:56 -07:00
|
|
|
"github.com/stretchr/testify/suite"
|
2020-08-14 10:58:53 -07:00
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
2020-07-08 15:00:56 -07:00
|
|
|
|
2020-07-04 10:30:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/baseapp"
|
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
2021-03-19 15:01:29 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/types/module"
|
2022-03-03 07:45:02 -08:00
|
|
|
authtypes "github.com/cosmos/cosmos-sdk/x/auth/types"
|
|
|
|
govtypes "github.com/cosmos/cosmos-sdk/x/gov/types"
|
2020-07-04 10:30:17 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
|
|
|
|
)
|
|
|
|
|
2020-07-08 15:00:56 -07:00
|
|
|
type UpgradeTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
app *simapp.SimApp
|
|
|
|
ctx sdk.Context
|
|
|
|
queryClient types.QueryClient
|
|
|
|
}
|
|
|
|
|
|
|
|
func (suite *UpgradeTestSuite) SetupTest() {
|
2021-08-16 17:52:06 -07:00
|
|
|
suite.app = simapp.Setup(suite.T(), false)
|
2020-08-14 10:58:53 -07:00
|
|
|
suite.ctx = suite.app.BaseApp.NewContext(false, tmproto.Header{})
|
2020-07-08 15:00:56 -07:00
|
|
|
|
|
|
|
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() {
|
2020-07-20 06:09:57 -07:00
|
|
|
req = &types.QueryCurrentPlanRequest{}
|
2020-07-08 15:00:56 -07:00
|
|
|
expResponse = types.QueryCurrentPlanResponse{}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
"with current upgrade plan",
|
|
|
|
func() {
|
|
|
|
plan := types.Plan{Name: "test-plan", Height: 5}
|
|
|
|
suite.app.UpgradeKeeper.ScheduleUpgrade(suite.ctx, plan)
|
2020-07-04 10:30:17 -07:00
|
|
|
|
2020-07-20 06:09:57 -07:00
|
|
|
req = &types.QueryCurrentPlanRequest{}
|
2020-07-08 15:00:56 -07:00
|
|
|
expResponse = types.QueryCurrentPlanResponse{Plan: &plan}
|
|
|
|
},
|
|
|
|
true,
|
|
|
|
},
|
|
|
|
}
|
2020-07-04 10:30:17 -07:00
|
|
|
|
2020-07-08 15:00:56 -07:00
|
|
|
for _, tc := range testCases {
|
|
|
|
suite.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
suite.SetupTest() // reset
|
2020-07-04 10:30:17 -07:00
|
|
|
|
2020-07-08 15:00:56 -07:00
|
|
|
tc.malleate()
|
2020-07-04 10:30:17 -07:00
|
|
|
|
2020-07-08 15:00:56 -07:00
|
|
|
res, err := suite.queryClient.CurrentPlan(gocontext.Background(), req)
|
2020-07-04 10:30:17 -07:00
|
|
|
|
2020-07-08 15:00:56 -07:00
|
|
|
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() {
|
2020-07-20 06:09:57 -07:00
|
|
|
req = &types.QueryAppliedPlanRequest{Name: "foo"}
|
2020-07-08 15:00:56 -07:00
|
|
|
},
|
|
|
|
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)
|
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
|
|
|
|
})
|
2020-07-08 15:00:56 -07:00
|
|
|
suite.app.UpgradeKeeper.ApplyUpgrade(suite.ctx, plan)
|
|
|
|
|
2020-07-20 06:09:57 -07:00
|
|
|
req = &types.QueryAppliedPlanRequest{Name: planName}
|
2020-07-08 15:00:56 -07:00
|
|
|
},
|
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
2020-07-04 10:30:17 -07:00
|
|
|
|
2021-05-19 10:04:38 -07:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-03-03 07:45:02 -08:00
|
|
|
func (suite *UpgradeTestSuite) TestAuthority() {
|
|
|
|
res, err := suite.queryClient.Authority(gocontext.Background(), &types.QueryAuthorityRequest{})
|
|
|
|
suite.Require().NoError(err)
|
|
|
|
suite.Require().Equal(authtypes.NewModuleAddress(govtypes.ModuleName).String(), res.Address)
|
|
|
|
}
|
|
|
|
|
2020-07-08 15:00:56 -07:00
|
|
|
func TestUpgradeTestSuite(t *testing.T) {
|
|
|
|
suite.Run(t, new(UpgradeTestSuite))
|
2020-07-04 10:30:17 -07:00
|
|
|
}
|