/
suite.go
116 lines (99 loc) · 2.97 KB
/
suite.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
package testutil
import (
"fmt"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"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")
app := simapp.Setup(s.T(), false)
s.app = app
s.ctx = app.BaseApp.NewContext(false, tmproto.Header{})
cfg := network.DefaultConfig()
cfg.NumValidators = 1
s.cfg = cfg
var err error
s.network, err = network.New(s.T(), s.T().TempDir(), cfg)
s.Require().NoError(err)
}
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,
}
jsonVM, _ := clientCtx.Codec.MarshalJSON(&pm)
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)
}
})
}
}