2021-05-19 10:04:38 -07:00
|
|
|
package testutil
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
|
2021-09-20 05:02:15 -07:00
|
|
|
"github.com/stretchr/testify/suite"
|
|
|
|
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
|
|
|
|
|
2021-05-19 10:04:38 -07:00
|
|
|
"github.com/cosmos/cosmos-sdk/simapp"
|
|
|
|
clitestutil "github.com/cosmos/cosmos-sdk/testutil/cli"
|
|
|
|
"github.com/cosmos/cosmos-sdk/testutil/network"
|
|
|
|
sdk "github.com/cosmos/cosmos-sdk/types"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/upgrade/client/cli"
|
|
|
|
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
|
|
|
|
)
|
|
|
|
|
|
|
|
func NewIntegrationTestSuite(cfg network.Config) *IntegrationTestSuite {
|
|
|
|
return &IntegrationTestSuite{cfg: cfg}
|
|
|
|
}
|
|
|
|
|
|
|
|
type IntegrationTestSuite struct {
|
|
|
|
suite.Suite
|
|
|
|
|
|
|
|
app *simapp.SimApp
|
|
|
|
cfg network.Config
|
|
|
|
network *network.Network
|
|
|
|
ctx sdk.Context
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestSuite) SetupSuite() {
|
|
|
|
s.T().Log("setting up integration test suite")
|
2021-08-16 17:52:06 -07:00
|
|
|
app := simapp.Setup(s.T(), false)
|
2021-05-19 10:04:38 -07:00
|
|
|
s.app = app
|
|
|
|
s.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
|
|
|
|
|
|
|
|
cfg := network.DefaultConfig()
|
|
|
|
cfg.NumValidators = 1
|
|
|
|
|
|
|
|
s.cfg = cfg
|
2021-06-29 03:41:55 -07:00
|
|
|
|
|
|
|
var err error
|
|
|
|
s.network, err = network.New(s.T(), s.T().TempDir(), cfg)
|
|
|
|
s.Require().NoError(err)
|
2021-05-19 10:04:38 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestSuite) TearDownSuite() {
|
|
|
|
s.T().Log("tearing down integration test suite")
|
|
|
|
s.network.Cleanup()
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s *IntegrationTestSuite) TestModuleVersionsCLI() {
|
|
|
|
testCases := []struct {
|
|
|
|
msg string
|
|
|
|
req types.QueryModuleVersionsRequest
|
|
|
|
single bool
|
|
|
|
expPass bool
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
msg: "test full query",
|
|
|
|
req: types.QueryModuleVersionsRequest{ModuleName: ""},
|
|
|
|
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,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
val := s.network.Validators[0]
|
|
|
|
clientCtx := val.ClientCtx
|
|
|
|
// avoid printing as yaml from CLI command
|
|
|
|
clientCtx.OutputFormat = "JSON"
|
|
|
|
|
|
|
|
vm := s.app.UpgradeKeeper.GetModuleVersionMap(s.ctx)
|
|
|
|
mv := s.app.UpgradeKeeper.GetModuleVersions(s.ctx)
|
|
|
|
s.Require().NotEmpty(vm)
|
|
|
|
|
|
|
|
for _, tc := range testCases {
|
|
|
|
s.Run(fmt.Sprintf("Case %s", tc.msg), func() {
|
|
|
|
|
|
|
|
expect := mv
|
|
|
|
if tc.expPass {
|
|
|
|
if tc.single {
|
|
|
|
expect = []*types.ModuleVersion{{Name: tc.req.ModuleName, Version: vm[tc.req.ModuleName]}}
|
|
|
|
}
|
|
|
|
// setup expected response
|
|
|
|
pm := types.QueryModuleVersionsResponse{
|
|
|
|
ModuleVersions: expect,
|
|
|
|
}
|
2021-06-11 04:49:39 -07:00
|
|
|
jsonVM, _ := clientCtx.Codec.MarshalJSON(&pm)
|
2021-05-19 10:04:38 -07:00
|
|
|
expectedRes := string(jsonVM)
|
|
|
|
// append new line to match behaviour of PrintProto
|
|
|
|
expectedRes += "\n"
|
|
|
|
|
|
|
|
// get actual module versions list response from cli
|
|
|
|
cmd := cli.GetModuleVersionsCmd()
|
|
|
|
outVM, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{tc.req.ModuleName})
|
|
|
|
s.Require().NoError(err)
|
|
|
|
|
|
|
|
s.Require().Equal(expectedRes, outVM.String())
|
|
|
|
} else {
|
|
|
|
cmd := cli.GetModuleVersionsCmd()
|
|
|
|
_, err := clitestutil.ExecTestCLICmd(clientCtx, cmd, []string{tc.req.ModuleName})
|
|
|
|
s.Require().Error(err)
|
|
|
|
}
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|