forked from cosmos/cosmos-sdk
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathinfo_test.go
More file actions
346 lines (328 loc) · 10.6 KB
/
Copy pathinfo_test.go
File metadata and controls
346 lines (328 loc) · 10.6 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
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
package plan
import (
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/stretchr/testify/suite"
)
type InfoTestSuite struct {
suite.Suite
// Home is a temporary directory for use in these tests.
Home string
}
func (s *InfoTestSuite) SetupTest() {
s.Home = s.T().TempDir()
s.T().Logf("Home: [%s]", s.Home)
}
func TestInfoTestSuite(t *testing.T) {
suite.Run(t, new(InfoTestSuite))
}
// saveSrcTestFile saves a TestFile in this test's Home/src directory.
// The full path to the saved file is returned.
func (s *InfoTestSuite) saveTestFile(f *TestFile) string {
fullName, err := f.SaveIn(s.Home)
s.Require().NoError(err, "saving test file %s", f.Name)
return fullName
}
func (s *InfoTestSuite) TestParseInfo() {
goodJSON := `{"binaries":{"os1/arch1":"url1","os2/arch2":"url2"}}`
binariesWrongJSON := `{"binaries":["foo","bar"]}`
binariesWrongValueJSON := `{"binaries":{"os1/arch1":1,"os2/arch2":2}}`
goodJSONPath := s.saveTestFile(NewTestFile("good.json", goodJSON))
binariesWrongJSONPath := s.saveTestFile(NewTestFile("binaries-wrong.json", binariesWrongJSON))
binariesWrongValueJSONPath := s.saveTestFile(NewTestFile("binaries-wrong-value.json", binariesWrongValueJSON))
goodJSONAsInfo := &Info{
Binaries: BinaryDownloadURLMap{
"os1/arch1": "url1",
"os2/arch2": "url2",
},
}
makeInfoStrFuncString := func(val string) func(t *testing.T) string {
return func(t *testing.T) string {
t.Helper()
return val
}
}
makeInfoStrFuncURL := func(file string) func(t *testing.T) string {
return func(t *testing.T) string {
t.Helper()
return makeFileURL(t, file)
}
}
tests := []struct {
name string
infoStrMaker func(t *testing.T) string
expectedInfo *Info
expectedInError []string
}{
{
name: "json good",
infoStrMaker: makeInfoStrFuncString(goodJSON),
expectedInfo: goodJSONAsInfo,
expectedInError: nil,
},
{
name: "blank string",
infoStrMaker: makeInfoStrFuncString(" "),
expectedInfo: nil,
expectedInError: []string{"plan info must not be blank"},
},
{
name: "empty JSON",
infoStrMaker: makeInfoStrFuncString("{}"),
expectedInfo: &Info{},
expectedInError: nil,
},
{
name: "json binaries is wrong data type",
infoStrMaker: makeInfoStrFuncString(binariesWrongJSON),
expectedInfo: nil,
expectedInError: []string{"could not parse plan info", "cannot unmarshal array into Go struct field Info.binaries"},
},
{
name: "json wrong data type in binaries value",
infoStrMaker: makeInfoStrFuncString(binariesWrongValueJSON),
expectedInfo: nil,
expectedInError: []string{"could not parse plan info", "cannot unmarshal number into Go struct field Info.binaries"},
},
{
name: "url does not exist",
infoStrMaker: makeInfoStrFuncString("file:///this/file/does/not/exist?checksum=sha256:e3b0c44298fc1c149afbf4c8996fb92427ae41e4649b934ca495991b7852b855"),
expectedInfo: nil,
expectedInError: []string{"could not download url", "file:///this/file/does/not/exist"},
},
{
name: "url good",
infoStrMaker: makeInfoStrFuncURL(goodJSONPath),
expectedInfo: goodJSONAsInfo,
expectedInError: nil,
},
{
name: "url binaries is wrong data type",
infoStrMaker: makeInfoStrFuncURL(binariesWrongJSONPath),
expectedInfo: nil,
expectedInError: []string{"could not parse plan info", "cannot unmarshal array into Go struct field Info.binaries"},
},
{
name: "url wrong data type in binaries value",
infoStrMaker: makeInfoStrFuncURL(binariesWrongValueJSONPath),
expectedInfo: nil,
expectedInError: []string{"could not parse plan info", "cannot unmarshal number into Go struct field Info.binaries"},
},
}
for _, tc := range tests {
s.T().Run(tc.name, func(t *testing.T) {
infoStr := tc.infoStrMaker(t)
actualInfo, actualErr := ParseInfo(infoStr)
if len(tc.expectedInError) > 0 {
require.Error(t, actualErr)
for _, expectedErr := range tc.expectedInError {
assert.Contains(t, actualErr.Error(), expectedErr)
}
} else {
require.NoError(t, actualErr)
}
assert.Equal(t, tc.expectedInfo, actualInfo)
})
}
}
func (s *InfoTestSuite) TestInfoValidateFull() {
darwinAMD64File := NewTestFile("darwin_amd64", "#!/usr/bin\necho 'darwin/amd64'\n")
linux386File := NewTestFile("linux_386", "#!/usr/bin\necho 'darwin/amd64'\n")
darwinAMD64Path := s.saveTestFile(darwinAMD64File)
linux386Path := s.saveTestFile(linux386File)
darwinAMD64URL := makeFileURL(s.T(), darwinAMD64Path)
linux386URL := makeFileURL(s.T(), linux386Path)
tests := []struct {
name string
planInfo *Info
errs []string
}{
// Positive test case
{
name: "two good entries",
planInfo: &Info{
Binaries: BinaryDownloadURLMap{
"darwin/amd64": darwinAMD64URL,
"linux/386": linux386URL,
},
},
errs: nil,
},
// a failure from BinaryDownloadURLMap.ValidateBasic
{
name: "empty binaries",
planInfo: &Info{Binaries: BinaryDownloadURLMap{}},
errs: []string{"no \"binaries\" entries found"},
},
// a failure from BinaryDownloadURLMap.CheckURLS
{
name: "url does not exist",
planInfo: &Info{
Binaries: BinaryDownloadURLMap{
"darwin/arm64": "file:///no/such/file/exists/hopefully.zip?checksum=sha256:b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259",
},
},
errs: []string{"error downloading binary", "darwin/arm64", "no such file or directory"},
},
}
for _, tc := range tests {
s.T().Run(tc.name, func(t *testing.T) {
actualErr := tc.planInfo.ValidateFull("daemon")
if len(tc.errs) > 0 {
require.Error(t, actualErr)
for _, expectedErr := range tc.errs {
assert.Contains(t, actualErr.Error(), expectedErr)
}
} else {
require.NoError(t, actualErr)
}
})
}
}
func (s *InfoTestSuite) TestBinaryDownloadURLMapValidateBasic() {
addDummyChecksum := func(url string) string {
return url + "?checksum=sha256:b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259"
}
tests := []struct {
name string
urlMap BinaryDownloadURLMap
parseConfig ParseConfig
errs []string
}{
{
name: "empty map",
urlMap: BinaryDownloadURLMap{},
errs: []string{"no \"binaries\" entries found"},
},
{
name: "key with empty string",
urlMap: BinaryDownloadURLMap{
"": addDummyChecksum("https://v1.cosmos.network/sdk"),
},
errs: []string{"invalid os/arch", `""`},
},
{
name: "invalid key format",
urlMap: BinaryDownloadURLMap{
"badkey": addDummyChecksum("https://v1.cosmos.network/sdk"),
},
errs: []string{"invalid os/arch", "badkey"},
},
{
name: "any key is valid",
urlMap: BinaryDownloadURLMap{
"any": addDummyChecksum("https://v1.cosmos.network/sdk"),
},
errs: nil,
},
{
name: "os arch key is valid",
urlMap: BinaryDownloadURLMap{
"darwin/amd64": addDummyChecksum("https://v1.cosmos.network/sdk"),
},
errs: nil,
},
{
name: "not a url",
urlMap: BinaryDownloadURLMap{
"isa/url": addDummyChecksum("https://v1.cosmos.network/sdk"),
"nota/url": addDummyChecksum("https://v1.cosmos.network:not-a-port/sdk"),
},
errs: []string{"invalid url", "nota/url", "invalid port"},
},
{
name: "url without checksum",
urlMap: BinaryDownloadURLMap{
"darwin/amd64": "https://v1.cosmos.network/sdk",
},
parseConfig: ParseConfig{EnforceChecksum: false},
errs: nil,
},
{
name: "multiple valid entries but one bad url",
urlMap: BinaryDownloadURLMap{
"any": addDummyChecksum("https://v1.cosmos.network/sdk"),
"darwin/amd64": addDummyChecksum("https://v1.cosmos.network/sdk"),
"darwin/arm64": addDummyChecksum("https://v1.cosmos.network/sdk"),
"windows/bad": addDummyChecksum("https://v1.cosmos.network:not-a-port/sdk"),
"linux/386": addDummyChecksum("https://v1.cosmos.network/sdk"),
},
errs: []string{"invalid url", "windows/bad", "invalid port"},
},
{
name: "multiple valid entries but one bad key",
urlMap: BinaryDownloadURLMap{
"any": addDummyChecksum("https://v1.cosmos.network/sdk"),
"darwin/amd64": addDummyChecksum("https://v1.cosmos.network/sdk"),
"badkey": addDummyChecksum("https://v1.cosmos.network/sdk"),
"darwin/arm64": addDummyChecksum("https://v1.cosmos.network/sdk"),
"linux/386": addDummyChecksum("https://v1.cosmos.network/sdk"),
},
errs: []string{"invalid os/arch", "badkey"},
},
}
for _, tc := range tests {
s.T().Run(tc.name, func(t *testing.T) {
actualErr := tc.urlMap.ValidateBasic(tc.parseConfig.EnforceChecksum)
if len(tc.errs) > 0 {
require.Error(t, actualErr)
for _, expectedErr := range tc.errs {
assert.Contains(t, actualErr.Error(), expectedErr)
}
} else {
require.NoError(t, actualErr)
}
})
}
}
func (s *InfoTestSuite) TestBinaryDownloadURLMapCheckURLs() {
darwinAMD64File := NewTestFile("darwin_amd64", "#!/usr/bin\necho 'darwin/amd64'\n")
linux386File := NewTestFile("linux_386", "#!/usr/bin\necho 'darwin/amd64'\n")
darwinAMD64Path := s.saveTestFile(darwinAMD64File)
linux386Path := s.saveTestFile(linux386File)
darwinAMD64URL := makeFileURL(s.T(), darwinAMD64Path)
linux386URL := makeFileURL(s.T(), linux386Path)
tests := []struct {
name string
urlMap BinaryDownloadURLMap
parseConfig ParseConfig
errs []string
}{
{
name: "two good entries",
urlMap: BinaryDownloadURLMap{
"darwin/amd64": darwinAMD64URL,
"linux/386": linux386URL,
},
errs: nil,
},
{
name: "url does not exist",
urlMap: BinaryDownloadURLMap{
"darwin/arm64": "file:///no/such/file/exists/hopefully.zip?checksum=sha256:b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259",
},
errs: []string{"error downloading binary", "darwin/arm64", "no such file or directory"},
},
{
name: "bad checksum",
urlMap: BinaryDownloadURLMap{
"darwin/amd64": "file://" + darwinAMD64Path + "?checksum=sha256:b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259",
},
errs: []string{"error downloading binary", "darwin/amd64", "Checksums did not match", "b5a2c96250612366ea272ffac6d9744aaf4b45aacd96aa7cfcb931ee3b558259"},
},
}
for _, tc := range tests {
s.T().Run(tc.name, func(t *testing.T) {
actualErr := tc.urlMap.CheckURLs("daemon", tc.parseConfig.EnforceChecksum)
if len(tc.errs) > 0 {
require.Error(t, actualErr)
for _, expectedErr := range tc.errs {
assert.Contains(t, actualErr.Error(), expectedErr)
}
} else {
require.NoError(t, actualErr)
}
})
}
}