forked from ava-labs/avalanchego
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathconfig_test.go
623 lines (570 loc) · 19.5 KB
/
config_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
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
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
// Copyright (C) 2019-2021, Ava Labs, Inc. All rights reserved.
// See the file LICENSE for licensing terms.
package config
import (
"encoding/base64"
"encoding/json"
"fmt"
"log"
"os"
"path/filepath"
"testing"
"github.com/spf13/pflag"
"github.com/spf13/viper"
"github.com/stretchr/testify/assert"
"github.com/ava-labs/avalanchego/chains"
"github.com/ava-labs/avalanchego/ids"
)
func TestGetChainConfigsFromFiles(t *testing.T) {
tests := map[string]struct {
configs map[string]string
upgrades map[string]string
errMessage string
expected map[string]chains.ChainConfig
}{
"no chain configs": {
configs: map[string]string{},
upgrades: map[string]string{},
expected: map[string]chains.ChainConfig{},
},
"valid chain-id": {
configs: map[string]string{"yH8D7ThNJkxmtkuv2jgBa4P1Rn3Qpr4pPr7QYNfcdoS6k6HWp": "hello", "2JVSBoinj9C2J33VntvzYtVJNZdN2NKiwwKjcumHUWEb5DbBrm": "world"},
upgrades: map[string]string{"yH8D7ThNJkxmtkuv2jgBa4P1Rn3Qpr4pPr7QYNfcdoS6k6HWp": "helloUpgrades"},
expected: func() map[string]chains.ChainConfig {
m := map[string]chains.ChainConfig{}
id1, err := ids.FromString("yH8D7ThNJkxmtkuv2jgBa4P1Rn3Qpr4pPr7QYNfcdoS6k6HWp")
assert.NoError(t, err)
m[id1.String()] = chains.ChainConfig{Config: []byte("hello"), Upgrade: []byte("helloUpgrades")}
id2, err := ids.FromString("2JVSBoinj9C2J33VntvzYtVJNZdN2NKiwwKjcumHUWEb5DbBrm")
assert.NoError(t, err)
m[id2.String()] = chains.ChainConfig{Config: []byte("world"), Upgrade: []byte(nil)}
return m
}(),
},
"valid alias": {
configs: map[string]string{"C": "hello", "X": "world"},
upgrades: map[string]string{"C": "upgradess"},
expected: func() map[string]chains.ChainConfig {
m := map[string]chains.ChainConfig{}
m["C"] = chains.ChainConfig{Config: []byte("hello"), Upgrade: []byte("upgradess")}
m["X"] = chains.ChainConfig{Config: []byte("world"), Upgrade: []byte(nil)}
return m
}(),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
root := t.TempDir()
configJSON := fmt.Sprintf(`{%q: %q}`, ChainConfigDirKey, root)
configFile := setupConfigJSON(t, root, configJSON)
chainsDir := root
// Create custom configs
for key, value := range test.configs {
chainDir := filepath.Join(chainsDir, key)
setupFile(t, chainDir, chainConfigFileName+".ex", value)
}
for key, value := range test.upgrades {
chainDir := filepath.Join(chainsDir, key)
setupFile(t, chainDir, chainUpgradeFileName+".ex", value)
}
v := setupViper(configFile)
// Parse config
assert.Equal(root, v.GetString(ChainConfigDirKey))
chainConfigs, err := getChainConfigs(v)
if len(test.errMessage) > 0 {
assert.Error(err)
if err != nil {
assert.Contains(err.Error(), test.errMessage)
}
} else {
assert.NoError(err)
}
assert.Equal(test.expected, chainConfigs)
})
}
}
func TestGetChainConfigsDirNotExist(t *testing.T) {
tests := map[string]struct {
structure string
file map[string]string
errMessage string
expected map[string]chains.ChainConfig
}{
"cdir not exist": {
structure: "/",
file: map[string]string{"config.ex": "noeffect"},
errMessage: "cannot read directory",
expected: nil,
},
"cdir is file ": {
structure: "/",
file: map[string]string{"cdir": "noeffect"},
errMessage: "cannot read directory",
expected: nil,
},
"chain subdir not exist": {
structure: "/cdir/",
file: map[string]string{"config.ex": "noeffect"},
expected: map[string]chains.ChainConfig{},
},
"full structure": {
structure: "/cdir/C/",
file: map[string]string{"config.ex": "hello"},
expected: map[string]chains.ChainConfig{"C": {Config: []byte("hello"), Upgrade: []byte(nil)}},
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
root := t.TempDir()
chainConfigDir := filepath.Join(root, "cdir")
configJSON := fmt.Sprintf(`{%q: %q}`, ChainConfigDirKey, chainConfigDir)
configFile := setupConfigJSON(t, root, configJSON)
dirToCreate := filepath.Join(root, test.structure)
assert.NoError(os.MkdirAll(dirToCreate, 0o700))
for key, value := range test.file {
setupFile(t, dirToCreate, key, value)
}
v := setupViper(configFile)
// Parse config
assert.Equal(chainConfigDir, v.GetString(ChainConfigDirKey))
// don't read with getConfigFromViper since it's very slow.
chainConfigs, err := getChainConfigs(v)
switch {
case len(test.errMessage) > 0:
assert.Error(err)
assert.Contains(err.Error(), test.errMessage)
default:
assert.NoError(err)
assert.Equal(test.expected, chainConfigs)
}
})
}
}
func TestSetChainConfigDefaultDir(t *testing.T) {
assert := assert.New(t)
root := t.TempDir()
// changes internal package variable, since using defaultDir (under user home) is risky.
defaultChainConfigDir = filepath.Join(root, "cdir")
configFilePath := setupConfigJSON(t, root, "{}")
v := setupViper(configFilePath)
assert.Equal(defaultChainConfigDir, v.GetString(ChainConfigDirKey))
chainsDir := filepath.Join(defaultChainConfigDir, "C")
setupFile(t, chainsDir, chainConfigFileName+".ex", "helloworld")
chainConfigs, err := getChainConfigs(v)
assert.NoError(err)
expected := map[string]chains.ChainConfig{"C": {Config: []byte("helloworld"), Upgrade: []byte(nil)}}
assert.Equal(expected, chainConfigs)
}
func TestGetChainConfigsFromFlags(t *testing.T) {
tests := map[string]struct {
fullConfigs map[string]chains.ChainConfig
errMessage string
expected map[string]chains.ChainConfig
}{
"no chain configs": {
fullConfigs: map[string]chains.ChainConfig{},
expected: map[string]chains.ChainConfig{},
},
"valid chain-id": {
fullConfigs: func() map[string]chains.ChainConfig {
m := map[string]chains.ChainConfig{}
id1, err := ids.FromString("yH8D7ThNJkxmtkuv2jgBa4P1Rn3Qpr4pPr7QYNfcdoS6k6HWp")
assert.NoError(t, err)
m[id1.String()] = chains.ChainConfig{Config: []byte("hello"), Upgrade: []byte("helloUpgrades")}
id2, err := ids.FromString("2JVSBoinj9C2J33VntvzYtVJNZdN2NKiwwKjcumHUWEb5DbBrm")
assert.NoError(t, err)
m[id2.String()] = chains.ChainConfig{Config: []byte("world"), Upgrade: []byte(nil)}
return m
}(),
expected: func() map[string]chains.ChainConfig {
m := map[string]chains.ChainConfig{}
id1, err := ids.FromString("yH8D7ThNJkxmtkuv2jgBa4P1Rn3Qpr4pPr7QYNfcdoS6k6HWp")
assert.NoError(t, err)
m[id1.String()] = chains.ChainConfig{Config: []byte("hello"), Upgrade: []byte("helloUpgrades")}
id2, err := ids.FromString("2JVSBoinj9C2J33VntvzYtVJNZdN2NKiwwKjcumHUWEb5DbBrm")
assert.NoError(t, err)
m[id2.String()] = chains.ChainConfig{Config: []byte("world"), Upgrade: []byte(nil)}
return m
}(),
},
"valid alias": {
fullConfigs: map[string]chains.ChainConfig{
"C": {Config: []byte("hello"), Upgrade: []byte("upgradess")},
"X": {Config: []byte("world"), Upgrade: []byte(nil)},
},
expected: func() map[string]chains.ChainConfig {
m := map[string]chains.ChainConfig{}
m["C"] = chains.ChainConfig{Config: []byte("hello"), Upgrade: []byte("upgradess")}
m["X"] = chains.ChainConfig{Config: []byte("world"), Upgrade: []byte(nil)}
return m
}(),
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
jsonMaps, err := json.Marshal(test.fullConfigs)
assert.NoError(err)
encodedFileContent := base64.StdEncoding.EncodeToString(jsonMaps)
// build viper config
v := setupViperFlags()
v.Set(ChainConfigContentKey, encodedFileContent)
// Parse config
chainConfigs, err := getChainConfigs(v)
if len(test.errMessage) > 0 {
assert.Error(err)
if err != nil {
assert.Contains(err.Error(), test.errMessage)
}
} else {
assert.NoError(err)
}
assert.Equal(test.expected, chainConfigs)
})
}
}
func TestGetVMAliasesFromFile(t *testing.T) {
tests := map[string]struct {
givenJSON string
expected map[ids.ID][]string
errMessage string
}{
"wrong vm id": {
givenJSON: `{"wrongVmId": ["vm1","vm2"]}`,
expected: nil,
errMessage: "problem unmarshaling vmAliases",
},
"vm id": {
givenJSON: `{"2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i": ["vm1","vm2"],
"Gmt4fuNsGJAd2PX86LBvycGaBpgCYKbuULdCLZs3SEs1Jx1LU": ["vm3", "vm4"] }`,
expected: func() map[ids.ID][]string {
m := map[ids.ID][]string{}
id1, _ := ids.FromString("2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i")
id2, _ := ids.FromString("Gmt4fuNsGJAd2PX86LBvycGaBpgCYKbuULdCLZs3SEs1Jx1LU")
m[id1] = []string{"vm1", "vm2"}
m[id2] = []string{"vm3", "vm4"}
return m
}(),
errMessage: "",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
root := t.TempDir()
aliasPath := filepath.Join(root, "aliases.json")
configJSON := fmt.Sprintf(`{%q: %q}`, VMAliasesFileKey, aliasPath)
configFilePath := setupConfigJSON(t, root, configJSON)
setupFile(t, root, "aliases.json", test.givenJSON)
v := setupViper(configFilePath)
vmAliases, err := getVMAliases(v)
if len(test.errMessage) > 0 {
assert.Error(err)
assert.Contains(err.Error(), test.errMessage)
} else {
assert.NoError(err)
assert.Equal(test.expected, vmAliases)
}
})
}
}
func TestGetVMAliasesFromFlag(t *testing.T) {
tests := map[string]struct {
givenJSON string
expected map[ids.ID][]string
errMessage string
}{
"wrong vm id": {
givenJSON: `{"wrongVmId": ["vm1","vm2"]}`,
expected: nil,
errMessage: "problem unmarshaling vmAliases",
},
"vm id": {
givenJSON: `{"2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i": ["vm1","vm2"],
"Gmt4fuNsGJAd2PX86LBvycGaBpgCYKbuULdCLZs3SEs1Jx1LU": ["vm3", "vm4"] }`,
expected: func() map[ids.ID][]string {
m := map[ids.ID][]string{}
id1, _ := ids.FromString("2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i")
id2, _ := ids.FromString("Gmt4fuNsGJAd2PX86LBvycGaBpgCYKbuULdCLZs3SEs1Jx1LU")
m[id1] = []string{"vm1", "vm2"}
m[id2] = []string{"vm3", "vm4"}
return m
}(),
errMessage: "",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
encodedFileContent := base64.StdEncoding.EncodeToString([]byte(test.givenJSON))
// build viper config
v := setupViperFlags()
v.Set(VMAliasesContentKey, encodedFileContent)
vmAliases, err := getVMAliases(v)
if len(test.errMessage) > 0 {
assert.Error(err)
assert.Contains(err.Error(), test.errMessage)
} else {
assert.NoError(err)
assert.Equal(test.expected, vmAliases)
}
})
}
}
func TestGetVMAliasesDefaultDir(t *testing.T) {
assert := assert.New(t)
root := t.TempDir()
// changes internal package variable, since using defaultDir (under user home) is risky.
defaultVMAliasFilePath = filepath.Join(root, "aliases.json")
configFilePath := setupConfigJSON(t, root, "{}")
v := setupViper(configFilePath)
assert.Equal(defaultVMAliasFilePath, v.GetString(VMAliasesFileKey))
setupFile(t, root, "aliases.json", `{"2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i": ["vm1","vm2"]}`)
vmAliases, err := getVMAliases(v)
assert.NoError(err)
expected := map[ids.ID][]string{}
id, _ := ids.FromString("2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i")
expected[id] = []string{"vm1", "vm2"}
assert.Equal(expected, vmAliases)
}
func TestGetVMAliasesDirNotExists(t *testing.T) {
assert := assert.New(t)
root := t.TempDir()
aliasPath := "/not/exists"
// set it explicitly
configJSON := fmt.Sprintf(`{%q: %q}`, VMAliasesFileKey, aliasPath)
configFilePath := setupConfigJSON(t, root, configJSON)
v := setupViper(configFilePath)
vmAliases, err := getVMAliases(v)
assert.Nil(vmAliases)
assert.Error(err)
assert.Contains(err.Error(), "vm alias file does not exist")
// do not set it explicitly
configJSON = "{}"
configFilePath = setupConfigJSON(t, root, configJSON)
v = setupViper(configFilePath)
vmAliases, err = getVMAliases(v)
assert.Nil(vmAliases)
assert.NoError(err)
}
func TestGetSubnetConfigsFromFile(t *testing.T) {
tests := map[string]struct {
givenJSON string
testF func(*assert.Assertions, map[ids.ID]chains.SubnetConfig)
errMessage string
fileName string
}{
"wrong config": {
fileName: "2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i.json",
givenJSON: `thisisnotjson`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
assert.Nil(given)
},
errMessage: "couldn't read subnet configs",
},
"subnet is not whitelisted": {
fileName: "Gmt4fuNsGJAd2PX86LBvycGaBpgCYKbuULdCLZs3SEs1Jx1LU.json",
givenJSON: `{"validatorOnly": true}`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
assert.Empty(given)
},
},
"wrong extension": {
fileName: "2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i.yaml",
givenJSON: `{"validatorOnly": true}`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
assert.Empty(given)
},
},
"invalid consensus parameters": {
fileName: "2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i.json",
givenJSON: `{"consensusParameters":{"k": 111, "alpha":1234} }`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
assert.Nil(given)
},
errMessage: "fails the condition that: alpha <= k",
},
"correct config": {
fileName: "2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i.json",
givenJSON: `{"validatorOnly": true, "consensusParameters":{"parents": 111, "alpha":16} }`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
id, _ := ids.FromString("2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i")
config, ok := given[id]
assert.True(ok)
assert.Equal(true, config.ValidatorOnly)
assert.Equal(111, config.ConsensusParameters.Parents)
assert.Equal(16, config.ConsensusParameters.Alpha)
// must still respect defaults
assert.Equal(20, config.ConsensusParameters.K)
},
errMessage: "",
},
"gossip config": {
fileName: "2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i.json",
givenJSON: `{"appGossipNonValidatorSize": 100 }`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
id, _ := ids.FromString("2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i")
config, ok := given[id]
assert.True(ok)
assert.Equal(uint(100), config.AppGossipNonValidatorSize)
// must still respect defaults
assert.Equal(20, config.ConsensusParameters.K)
assert.Equal(uint(10), config.AppGossipValidatorSize)
},
errMessage: "",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
root := t.TempDir()
subnetPath := filepath.Join(root, "subnets")
configJSON := fmt.Sprintf(`{%q: %q}`, SubnetConfigDirKey, subnetPath)
configFilePath := setupConfigJSON(t, root, configJSON)
subnetID, err := ids.FromString("2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i")
assert.NoError(err)
setupFile(t, subnetPath, test.fileName, test.givenJSON)
v := setupViper(configFilePath)
subnetConfigs, err := getSubnetConfigs(v, []ids.ID{subnetID})
if len(test.errMessage) > 0 {
assert.Error(err)
assert.Contains(err.Error(), test.errMessage)
} else {
assert.NoError(err)
test.testF(assert, subnetConfigs)
}
})
}
}
func TestGetSubnetConfigsFromFlags(t *testing.T) {
tests := map[string]struct {
givenJSON string
testF func(*assert.Assertions, map[ids.ID]chains.SubnetConfig)
errMessage string
}{
"no configs": {
givenJSON: `{}`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
assert.Empty(given)
},
errMessage: "",
},
"entry with no config": {
givenJSON: `{"2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i":{}}`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
assert.True(len(given) == 1)
id, _ := ids.FromString("2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i")
config, ok := given[id]
assert.True(ok)
// should respect defaults
assert.Equal(20, config.ConsensusParameters.K)
},
},
"subnet is not whitelisted": {
givenJSON: `{"Gmt4fuNsGJAd2PX86LBvycGaBpgCYKbuULdCLZs3SEs1Jx1LU":{"validatorOnly":true}}`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
assert.Empty(given)
},
},
"invalid consensus parameters": {
givenJSON: `{
"2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i": {
"consensusParameters": {
"k": 111,
"alpha": 1234
}
}
}`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
assert.Empty(given)
},
errMessage: "fails the condition that: alpha <= k",
},
"correct config": {
givenJSON: `{
"2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i": {
"consensusParameters": {
"k": 30,
"alpha": 20,
"parents": 111
},
"validatorOnly": true
}
}`,
testF: func(assert *assert.Assertions, given map[ids.ID]chains.SubnetConfig) {
id, _ := ids.FromString("2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i")
config, ok := given[id]
assert.True(ok)
assert.Equal(true, config.ValidatorOnly)
assert.Equal(111, config.ConsensusParameters.Parents)
assert.Equal(20, config.ConsensusParameters.Alpha)
assert.Equal(30, config.ConsensusParameters.K)
// must still respect defaults
assert.Equal(uint(10), config.AppGossipValidatorSize)
assert.Equal(1024, config.ConsensusParameters.MaxOutstandingItems)
},
errMessage: "",
},
}
for name, test := range tests {
t.Run(name, func(t *testing.T) {
assert := assert.New(t)
subnetID, err := ids.FromString("2Ctt6eGAeo4MLqTmGa7AdRecuVMPGWEX9wSsCLBYrLhX4a394i")
assert.NoError(err)
encodedFileContent := base64.StdEncoding.EncodeToString([]byte(test.givenJSON))
// build viper config
v := setupViperFlags()
v.Set(SubnetConfigContentKey, encodedFileContent)
subnetConfigs, err := getSubnetConfigs(v, []ids.ID{subnetID})
if len(test.errMessage) > 0 {
assert.Error(err)
assert.Contains(err.Error(), test.errMessage)
} else {
assert.NoError(err)
test.testF(assert, subnetConfigs)
}
})
}
}
// setups config json file and writes content
func setupConfigJSON(t *testing.T, rootPath string, value string) string {
configFilePath := filepath.Join(rootPath, "config.json")
assert.NoError(t, os.WriteFile(configFilePath, []byte(value), 0o600))
return configFilePath
}
// setups file creates necessary path and writes value to it.
func setupFile(t *testing.T, path string, fileName string, value string) {
assert.NoError(t, os.MkdirAll(path, 0o700))
filePath := filepath.Join(path, fileName)
assert.NoError(t, os.WriteFile(filePath, []byte(value), 0o600))
}
func setupViperFlags() *viper.Viper {
v := viper.New()
fs := BuildFlagSet()
pflag.CommandLine = pflag.NewFlagSet(os.Args[0], pflag.PanicOnError) // flags are now reset
pflag.CommandLine.AddGoFlagSet(fs)
pflag.Parse()
if err := v.BindPFlags(pflag.CommandLine); err != nil {
log.Fatal(err)
}
return v
}
func setupViper(configFilePath string) *viper.Viper {
v := setupViperFlags()
// need to set it since in tests executable dir is somewhere /var/tmp/ (or wherever is designated by go)
// thus it searches buildDir in /var/tmp/
// but actual buildDir resides under project_root/build
currentPath, err := os.Getwd()
if err != nil {
log.Fatal(err)
}
v.Set(BuildDirKey, filepath.Join(currentPath, "..", "build"))
v.SetConfigFile(configFilePath)
err = v.ReadInConfig()
if err != nil {
log.Fatal(err)
}
return v
}