-
Notifications
You must be signed in to change notification settings - Fork 3.8k
/
Copy pathprocess_test.go
240 lines (205 loc) · 8.03 KB
/
process_test.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
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
238
239
240
//go:build linux
// +build linux
package cosmovisor_test
import (
"fmt"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"github.com/cosmos/cosmos-sdk/cosmovisor"
upgradetypes "github.com/cosmos/cosmos-sdk/x/upgrade/types"
)
type processTestSuite struct {
suite.Suite
}
func TestProcessTestSuite(t *testing.T) {
suite.Run(t, new(processTestSuite))
}
// TestLaunchProcess will try running the script a few times and watch upgrades work properly
// and args are passed through
func (s *processTestSuite) TestLaunchProcess() {
// binaries from testdata/validate directory
require := s.Require()
home := copyTestData(s.T(), "validate")
cfg := &cosmovisor.Config{Home: home, Name: "dummyd", PollInterval: 20, UnsafeSkipBackup: true}
logger := cosmovisor.NewLogger()
// should run the genesis binary and produce expected output
stdout, stderr := NewBuffer(), NewBuffer()
currentBin, err := cfg.CurrentBin()
require.NoError(err)
require.Equal(cfg.GenesisBin(), currentBin)
launcher, err := cosmovisor.NewLauncher(logger, cfg)
require.NoError(err)
upgradeFile := cfg.UpgradeInfoFilePath()
args := []string{"foo", "bar", "1234", upgradeFile}
doUpgrade, err := launcher.Run(args, stdout, stderr)
require.NoError(err)
require.True(doUpgrade)
require.Equal("", stderr.String())
require.Equal(fmt.Sprintf("Genesis foo bar 1234 %s\nUPGRADE \"chain2\" NEEDED at height: 49: {}\n", upgradeFile), stdout.String())
// ensure this is upgraded now and produces new output
currentBin, err = cfg.CurrentBin()
require.NoError(err)
require.Equal(cfg.UpgradeBin("chain2"), currentBin)
args = []string{"second", "run", "--verbose"}
stdout.Reset()
stderr.Reset()
doUpgrade, err = launcher.Run(args, stdout, stderr)
require.NoError(err)
require.False(doUpgrade)
require.Equal("", stderr.String())
require.Equal("Chain 2 is live!\nArgs: second run --verbose\nFinished successfully\n", stdout.String())
// ended without other upgrade
require.Equal(cfg.UpgradeBin("chain2"), currentBin)
}
func (s *processTestSuite) TestLaunchProcessWithRestartDelay() {
// binaries from testdata/validate directory
require := s.Require()
home := copyTestData(s.T(), "validate")
cfg := &cosmovisor.Config{Home: home, Name: "dummyd", RestartDelay: 5 * time.Second, PollInterval: 20, UnsafeSkipBackup: true}
logger := cosmovisor.NewLogger()
// should run the genesis binary and produce expected output
stdout, stderr := NewBuffer(), NewBuffer()
currentBin, err := cfg.CurrentBin()
require.NoError(err)
require.Equal(cfg.GenesisBin(), currentBin)
launcher, err := cosmovisor.NewLauncher(logger, cfg)
require.NoError(err)
upgradeFile := cfg.UpgradeInfoFilePath()
start := time.Now()
doUpgrade, err := launcher.Run([]string{"foo", "bar", "1234", upgradeFile}, stdout, stderr)
require.NoError(err)
require.True(doUpgrade)
// may not be the best way but the fastest way to check we meet the delay
// in addition to comparing both the runtime of this test and TestLaunchProcess in addition
if time.Since(start) < cfg.RestartDelay {
require.FailNow("restart delay not met")
}
}
// TestLaunchProcess will try running the script a few times and watch upgrades work properly
// and args are passed through
func (s *processTestSuite) TestLaunchProcessWithDownloads() {
// test case upgrade path (binaries from testdata/download directory):
// genesis -> chain2-zip_bin
// chain2-zip_bin -> ref_to_chain3-zip_dir.json = (json for the next download instructions) -> chain3-zip_dir
// chain3-zip_dir - doesn't upgrade
require := s.Require()
home := copyTestData(s.T(), "download")
cfg := &cosmovisor.Config{Home: home, Name: "autod", AllowDownloadBinaries: true, PollInterval: 100, UnsafeSkipBackup: true}
logger := cosmovisor.NewLogger()
upgradeFilename := cfg.UpgradeInfoFilePath()
// should run the genesis binary and produce expected output
currentBin, err := cfg.CurrentBin()
require.NoError(err)
require.Equal(cfg.GenesisBin(), currentBin)
launcher, err := cosmovisor.NewLauncher(logger, cfg)
require.NoError(err)
stdout, stderr := NewBuffer(), NewBuffer()
args := []string{"some", "args", upgradeFilename}
doUpgrade, err := launcher.Run(args, stdout, stderr)
require.NoError(err)
require.True(doUpgrade)
require.Equal("", stderr.String())
require.Equal("Genesis autod. Args: some args "+upgradeFilename+"\n"+`ERROR: UPGRADE "chain2" NEEDED at height: 49: zip_binary`+"\n", stdout.String())
currentBin, err = cfg.CurrentBin()
require.NoError(err)
require.Equal(cfg.UpgradeBin("chain2"), currentBin)
// start chain2
stdout.Reset()
stderr.Reset()
args = []string{"run", "--fast", upgradeFilename}
doUpgrade, err = launcher.Run(args, stdout, stderr)
require.NoError(err)
require.Equal("", stderr.String())
require.Equal("Chain 2 from zipped binary\nArgs: run --fast "+upgradeFilename+"\n"+`ERROR: UPGRADE "chain3" NEEDED at height: 936: ref_to_chain3-zip_dir.json module=main`+"\n", stdout.String())
// ended with one more upgrade
require.True(doUpgrade)
currentBin, err = cfg.CurrentBin()
require.NoError(err)
require.Equal(cfg.UpgradeBin("chain3"), currentBin)
// run the last chain
args = []string{"end", "--halt", upgradeFilename}
stdout.Reset()
stderr.Reset()
doUpgrade, err = launcher.Run(args, stdout, stderr)
require.NoError(err)
require.False(doUpgrade)
require.Equal("", stderr.String())
require.Equal("Chain 3 from zipped directory\nArgs: end --halt "+upgradeFilename+"\n", stdout.String())
// and this doesn't upgrade
currentBin, err = cfg.CurrentBin()
require.NoError(err)
require.Equal(cfg.UpgradeBin("chain3"), currentBin)
}
// TestSkipUpgrade tests heights that are identified to be skipped and return if upgrade height matches the skip heights
func TestSkipUpgrade(t *testing.T) {
cases := []struct {
args []string
upgradeInfo upgradetypes.Plan
expectRes bool
}{{
args: []string{"appb", "start", "--unsafe-skip-upgrades"},
upgradeInfo: upgradetypes.Plan{Name: "upgrade1", Info: "some info", Height: 123},
expectRes: false,
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades", "--abcd"},
upgradeInfo: upgradetypes.Plan{Name: "upgrade1", Info: "some info", Height: 123},
expectRes: false,
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades", "10", "--abcd"},
upgradeInfo: upgradetypes.Plan{Name: "upgrade1", Info: "some info", Height: 11},
expectRes: false,
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades", "10", "20", "--abcd"},
upgradeInfo: upgradetypes.Plan{Name: "upgrade1", Info: "some info", Height: 20},
expectRes: true,
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades", "10", "20", "--abcd", "34"},
upgradeInfo: upgradetypes.Plan{Name: "upgrade1", Info: "some info", Height: 34},
expectRes: false,
}}
for i := range cases {
tc := cases[i]
require := require.New(t)
h := cosmovisor.IsSkipUpgradeHeight(tc.args, tc.upgradeInfo)
require.Equal(h, tc.expectRes)
}
}
// TestUpgradeSkipHeights tests if correct skip upgrade heights are identified from the cli args
func TestUpgradeSkipHeights(t *testing.T) {
cases := []struct {
args []string
expectRes []int
}{{
args: []string{},
expectRes: nil,
}, {
args: []string{"appb", "start"},
expectRes: nil,
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades"},
expectRes: nil,
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades", "--abcd"},
expectRes: nil,
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades", "10", "--abcd"},
expectRes: []int{10},
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades", "10", "20", "--abcd"},
expectRes: []int{10, 20},
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades", "10", "20", "--abcd", "34"},
expectRes: []int{10, 20},
}, {
args: []string{"appb", "start", "--unsafe-skip-upgrades", "10", "as", "20", "--abcd"},
expectRes: []int{10, 20},
}}
for i := range cases {
tc := cases[i]
require := require.New(t)
h := cosmovisor.UpgradeSkipHeights(tc.args)
require.Equal(h, tc.expectRes)
}
}