-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathupgrade_test.go
214 lines (179 loc) · 6.48 KB
/
upgrade_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
//go:build linux
// +build linux
package cosmovisor_test
import (
"fmt"
"os"
"path/filepath"
"strings"
"testing"
"github.com/otiai10/copy"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
"cosmossdk.io/log"
"cosmossdk.io/tools/cosmovisor"
upgradetypes "cosmossdk.io/x/upgrade/types"
)
type upgradeTestSuite struct {
suite.Suite
}
func TestUpgradeTestSuite(t *testing.T) {
suite.Run(t, new(upgradeTestSuite))
}
func (s *upgradeTestSuite) TestCurrentBin() {
home := copyTestData(s.T(), "validate")
cfg := cosmovisor.Config{Home: home, Name: "dummyd"}
currentBin, err := cfg.CurrentBin()
s.Require().NoError(err)
s.Require().Equal(cfg.GenesisBin(), currentBin)
// ensure we cannot set this to an invalid value
for _, name := range []string{"missing", "nobin"} {
s.Require().Error(cfg.SetCurrentUpgrade(upgradetypes.Plan{Name: name}), name)
currentBin, err := cfg.CurrentBin()
s.Require().NoError(err)
s.Require().Equal(cfg.GenesisBin(), currentBin, name)
}
// try a few times to make sure this can be reproduced
for _, name := range []string{"chain2", "chain3", "chain2"} {
// now set it to a valid upgrade and make sure CurrentBin is now set properly
err = cfg.SetCurrentUpgrade(upgradetypes.Plan{Name: name})
s.Require().NoError(err)
// we should see current point to the new upgrade dir
currentBin, err := cfg.CurrentBin()
s.Require().NoError(err)
s.Require().Equal(cfg.UpgradeBin(name), currentBin)
}
}
func (s *upgradeTestSuite) TestCurrentAlwaysSymlinkToDirectory() {
home := copyTestData(s.T(), "validate")
cfg := cosmovisor.Config{Home: home, Name: "dummyd"}
currentBin, err := cfg.CurrentBin()
s.Require().NoError(err)
s.Require().Equal(cfg.GenesisBin(), currentBin)
s.assertCurrentLink(cfg, "genesis")
err = cfg.SetCurrentUpgrade(upgradetypes.Plan{Name: "chain2"})
s.Require().NoError(err)
currentBin, err = cfg.CurrentBin()
s.Require().NoError(err)
s.Require().Equal(cfg.UpgradeBin("chain2"), currentBin)
s.assertCurrentLink(cfg, filepath.Join("upgrades", "chain2"))
}
func (s *upgradeTestSuite) assertCurrentLink(cfg cosmovisor.Config, target string) {
link := filepath.Join(cfg.Root(), "current")
// ensure this is a symlink
info, err := os.Lstat(link)
s.Require().NoError(err)
s.Require().Equal(os.ModeSymlink, info.Mode()&os.ModeSymlink)
dest, err := os.Readlink(link)
s.Require().NoError(err)
expected := filepath.Join(cfg.Root(), target)
s.Require().Equal(expected, dest)
}
// TODO: test with download (and test all download functions)
func (s *upgradeTestSuite) TestUpgradeBinaryNoDownloadUrl() {
home := copyTestData(s.T(), "validate")
cfg := &cosmovisor.Config{Home: home, Name: "dummyd", AllowDownloadBinaries: true}
logger := log.NewLogger(os.Stdout).With(log.ModuleKey, "cosmovisor")
currentBin, err := cfg.CurrentBin()
s.Require().NoError(err)
s.Require().Equal(cfg.GenesisBin(), currentBin)
// do upgrade ignores bad files
for _, name := range []string{"missing", "nobin"} {
info := upgradetypes.Plan{Name: name}
err = cosmovisor.UpgradeBinary(logger, cfg, info)
s.Require().Error(err, name)
currentBin, err := cfg.CurrentBin()
s.Require().NoError(err)
s.Require().Equal(cfg.GenesisBin(), currentBin, name)
}
// make sure it updates a few times
for _, upgrade := range []string{"chain2", "chain3"} {
// now set it to a valid upgrade and make sure CurrentBin is now set properly
info := upgradetypes.Plan{Name: upgrade}
err = cosmovisor.UpgradeBinary(logger, cfg, info)
s.Require().NoError(err)
// we should see current point to the new upgrade dir
upgradeBin := cfg.UpgradeBin(upgrade)
currentBin, err := cfg.CurrentBin()
s.Require().NoError(err)
s.Require().Equal(upgradeBin, currentBin)
}
}
func (s *upgradeTestSuite) TestUpgradeBinary() {
logger := log.NewLogger(os.Stdout).With(log.ModuleKey, "cosmovisor")
cases := map[string]struct {
url string
canDownload bool
validBinary bool
}{
"get raw binary with checksum": {
// sha256sum ./testdata/repo/raw_binary/autod
url: "./testdata/repo/raw_binary/autod?checksum=sha256:e6bc7851600a2a9917f7bf88eb7bdee1ec162c671101485690b4deb089077b0d",
canDownload: true,
validBinary: true,
},
"get raw binary with invalid checksum": {
url: "./testdata/repo/raw_binary/autod?checksum=sha256:73e2bd6cbb99261733caf137015d5cc58e3f96248d8b01da68be8564989dd906",
canDownload: false,
},
"get zipped directory with valid checksum": {
// sha256sum ./testdata/repo/chain3-zip_dir/autod.zip
url: "./testdata/repo/chain3-zip_dir/autod.zip?checksum=sha256:8951f52a0aea8617de0ae459a20daf704c29d259c425e60d520e363df0f166b4",
canDownload: true,
validBinary: true,
},
"get zipped directory with invalid checksum": {
url: "./testdata/repo/chain3-zip_dir/autod.zip?checksum=sha256:73e2bd6cbb99261733caf137015d5cc58e3f96248d8b01da68be8564989dd906",
canDownload: false,
},
"invalid url": {
url: "./testdata/repo/bad_dir/autod?checksum=sha256:73e2bd6cbb99261733caf137015d5cc58e3f96248d8b01da68be8564989dd906",
canDownload: false,
},
"valid remote": {
url: "https://github.com/cosmos/cosmos-sdk/raw/main/tools/cosmovisor/testdata/repo/chain3-zip_dir/autod.zip?checksum=sha256:8951f52a0aea8617de0ae459a20daf704c29d259c425e60d520e363df0f166b4",
canDownload: true,
validBinary: true,
},
}
for label, tc := range cases {
s.Run(label, func() {
var err error
// make temp dir
home := copyTestData(s.T(), "download")
cfg := &cosmovisor.Config{
Home: home,
Name: "autod",
AllowDownloadBinaries: true,
}
url := tc.url
if strings.HasPrefix(url, "./") {
url, err = filepath.Abs(url)
s.Require().NoError(err)
}
plan := upgradetypes.Plan{
Name: "amazonas",
Info: fmt.Sprintf(`{"binaries":{"%s": "%s"}}`, cosmovisor.OSArch(), url),
}
err = cosmovisor.UpgradeBinary(logger, cfg, plan)
if !tc.canDownload {
s.Require().Error(err)
} else {
s.Require().NoError(err)
}
})
}
}
func (s *upgradeTestSuite) TestOsArch() {
// all download tests will fail if we are not on linux...
s.Require().Equal("linux/amd64", cosmovisor.OSArch())
}
// copyTestData will make a tempdir and then
// "cp -r" a subdirectory under testdata there
// returns the directory (which can now be used as Config.Home) and modified safely
func copyTestData(t *testing.T, subdir string) string {
t.Helper()
tmpdir := t.TempDir()
require.NoError(t, copy.Copy(filepath.Join("testdata", subdir), tmpdir))
return tmpdir
}