-
Notifications
You must be signed in to change notification settings - Fork 4.2k
Expand file tree
/
Copy pathkeeper_test.go
More file actions
237 lines (210 loc) · 5.86 KB
/
Copy pathkeeper_test.go
File metadata and controls
237 lines (210 loc) · 5.86 KB
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
package keeper_test
import (
"path/filepath"
"testing"
"time"
"github.com/stretchr/testify/suite"
tmproto "github.com/tendermint/tendermint/proto/tendermint/types"
"github.com/cosmos/cosmos-sdk/simapp"
store "github.com/cosmos/cosmos-sdk/store/types"
sdk "github.com/cosmos/cosmos-sdk/types"
"github.com/cosmos/cosmos-sdk/types/module"
"github.com/cosmos/cosmos-sdk/x/upgrade/keeper"
"github.com/cosmos/cosmos-sdk/x/upgrade/types"
)
type KeeperTestSuite struct {
suite.Suite
homeDir string
app *simapp.SimApp
ctx sdk.Context
}
func (s *KeeperTestSuite) SetupTest() {
app := simapp.Setup(false)
homeDir := filepath.Join(s.T().TempDir(), "x_upgrade_keeper_test")
app.UpgradeKeeper = keeper.NewKeeper( // recreate keeper in order to use a custom home path
make(map[int64]bool), app.GetKey(types.StoreKey), app.AppCodec(), homeDir, app.BaseApp,
)
s.T().Log("home dir:", homeDir)
s.homeDir = homeDir
s.app = app
s.ctx = app.BaseApp.NewContext(false, tmproto.Header{
Time: time.Now(),
Height: 10,
})
}
func (s *KeeperTestSuite) TestReadUpgradeInfoFromDisk() {
// require no error when the upgrade info file does not exist
_, err := s.app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
s.Require().NoError(err)
expected := store.UpgradeInfo{
Name: "test_upgrade",
Height: 100,
}
// create an upgrade info file
s.Require().NoError(s.app.UpgradeKeeper.DumpUpgradeInfoToDisk(expected.Height, expected.Name))
ui, err := s.app.UpgradeKeeper.ReadUpgradeInfoFromDisk()
s.Require().NoError(err)
s.Require().Equal(expected, ui)
}
func (s *KeeperTestSuite) TestScheduleUpgrade() {
cases := []struct {
name string
plan types.Plan
setup func()
expPass bool
}{
{
name: "successful height schedule",
plan: types.Plan{
Name: "all-good",
Info: "some text here",
Height: 123450000,
},
setup: func() {},
expPass: true,
},
{
name: "successful overwrite",
plan: types.Plan{
Name: "all-good",
Info: "some text here",
Height: 123450000,
},
setup: func() {
s.app.UpgradeKeeper.ScheduleUpgrade(s.ctx, types.Plan{
Name: "alt-good",
Info: "new text here",
Height: 543210000,
})
},
expPass: true,
},
{
name: "unsuccessful schedule: invalid plan",
plan: types.Plan{
Height: 123450000,
},
setup: func() {},
expPass: false,
},
{
name: "unsuccessful height schedule: due date in past",
plan: types.Plan{
Name: "all-good",
Info: "some text here",
Height: 1,
},
setup: func() {},
expPass: false,
},
{
name: "unsuccessful schedule: schedule already executed",
plan: types.Plan{
Name: "all-good",
Info: "some text here",
Height: 123450000,
},
setup: func() {
s.app.UpgradeKeeper.SetUpgradeHandler("all-good", func(ctx sdk.Context, plan types.Plan, vm module.VersionMap) (module.VersionMap, error) {
return vm, nil
})
s.app.UpgradeKeeper.ApplyUpgrade(s.ctx, types.Plan{
Name: "all-good",
Info: "some text here",
Height: 123450000,
})
},
expPass: false,
},
}
for _, tc := range cases {
tc := tc
s.Run(tc.name, func() {
// reset suite
s.SetupTest()
// setup test case
tc.setup()
err := s.app.UpgradeKeeper.ScheduleUpgrade(s.ctx, tc.plan)
if tc.expPass {
s.Require().NoError(err, "valid test case failed")
} else {
s.Require().Error(err, "invalid test case passed")
}
})
}
}
func (s *KeeperTestSuite) TestSetUpgradedClient() {
cs := []byte("IBC client state")
cases := []struct {
name string
height int64
setup func()
exists bool
}{
{
name: "no upgraded client exists",
height: 10,
setup: func() {},
exists: false,
},
{
name: "success",
height: 10,
setup: func() {
s.app.UpgradeKeeper.SetUpgradedClient(s.ctx, 10, cs)
},
exists: true,
},
}
for _, tc := range cases {
// reset suite
s.SetupTest()
// setup test case
tc.setup()
gotCs, exists := s.app.UpgradeKeeper.GetUpgradedClient(s.ctx, tc.height)
if tc.exists {
s.Require().Equal(cs, gotCs, "valid case: %s did not retrieve correct client state", tc.name)
s.Require().True(exists, "valid case: %s did not retrieve client state", tc.name)
} else {
s.Require().Nil(gotCs, "invalid case: %s retrieved valid client state", tc.name)
s.Require().False(exists, "invalid case: %s retrieved valid client state", tc.name)
}
}
}
// Test that the protocol version successfully increments after an
// upgrade and is successfully set on BaseApp's appVersion.
func (s *KeeperTestSuite) TestIncrementProtocolVersion() {
oldProtocolVersion := s.app.BaseApp.AppVersion()
s.app.UpgradeKeeper.SetUpgradeHandler("dummy", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) { return vm, nil })
dummyPlan := types.Plan{
Name: "dummy",
Info: "some text here",
Height: 100,
}
s.app.UpgradeKeeper.ApplyUpgrade(s.ctx, dummyPlan)
upgradedProtocolVersion := s.app.BaseApp.AppVersion()
s.Require().Equal(oldProtocolVersion+1, upgradedProtocolVersion)
}
// Tests that the underlying state of x/upgrade is set correctly after
// an upgrade.
func (s *KeeperTestSuite) TestMigrations() {
initialVM := module.VersionMap{"bank": uint64(1)}
s.app.UpgradeKeeper.SetModuleVersionMap(s.ctx, initialVM)
vmBefore := s.app.UpgradeKeeper.GetModuleVersionMap(s.ctx)
s.app.UpgradeKeeper.SetUpgradeHandler("dummy", func(_ sdk.Context, _ types.Plan, vm module.VersionMap) (module.VersionMap, error) {
// simulate upgrading the bank module
vm["bank"] = vm["bank"] + 1
return vm, nil
})
dummyPlan := types.Plan{
Name: "dummy",
Info: "some text here",
Height: 123450000,
}
s.app.UpgradeKeeper.ApplyUpgrade(s.ctx, dummyPlan)
vm := s.app.UpgradeKeeper.GetModuleVersionMap(s.ctx)
s.Require().Equal(vmBefore["bank"]+1, vm["bank"])
}
func TestKeeperTestSuite(t *testing.T) {
suite.Run(t, new(KeeperTestSuite))
}