@@ -21,6 +21,82 @@ import (
21
21
"github.com/iotexproject/go-pkgs/crypto"
22
22
)
23
23
24
+ const (
25
+ overwritePath = "_overwritePath"
26
+ secretPath = "_secretPath"
27
+ subChainPath = "_subChainPath"
28
+ )
29
+
30
+ func makePathAndWriteFile (cfgStr , flagForPath string ) (err error ) {
31
+ switch flagForPath {
32
+ case overwritePath :
33
+ _overwritePath = filepath .Join (os .TempDir (), "config.yaml" )
34
+ err = ioutil .WriteFile (_overwritePath , []byte (cfgStr ), 0666 )
35
+ case secretPath :
36
+ _secretPath = filepath .Join (os .TempDir (), "secret.yaml" )
37
+ err = ioutil .WriteFile (_secretPath , []byte (cfgStr ), 0666 )
38
+ case subChainPath :
39
+ _subChainPath = filepath .Join (os .TempDir (), "config.yaml" )
40
+ err = ioutil .WriteFile (_subChainPath , []byte (cfgStr ), 0666 )
41
+ }
42
+ return err
43
+ }
44
+
45
+ func resetPathValues (t * testing.T , flagForPath []string ) {
46
+ for _ , pathValue := range flagForPath {
47
+ switch pathValue {
48
+ case overwritePath :
49
+ err := os .Remove (_overwritePath )
50
+ _overwritePath = ""
51
+ require .NoError (t , err )
52
+ case secretPath :
53
+ err := os .Remove (_secretPath )
54
+ _secretPath = ""
55
+ require .NoError (t , err )
56
+ case subChainPath :
57
+ err := os .Remove (_subChainPath )
58
+ _subChainPath = ""
59
+ require .NoError (t , err )
60
+ }
61
+ }
62
+ }
63
+
64
+ func resetPathValuesWithLookupEnv (t * testing.T , oldEnv string , oldExist bool , flagForPath string ) {
65
+ switch flagForPath {
66
+ case overwritePath :
67
+ err := os .Remove (_overwritePath )
68
+ require .NoError (t , err )
69
+ _overwritePath = ""
70
+ if oldExist {
71
+ err = os .Setenv ("IOTEX_TEST_NODE_TYPE" , oldEnv )
72
+ } else {
73
+ err = os .Unsetenv ("IOTEX_TEST_NODE_TYPE" )
74
+ }
75
+ require .NoError (t , err )
76
+ case subChainPath :
77
+ err := os .Remove (_subChainPath )
78
+ require .NoError (t , err )
79
+ _subChainPath = ""
80
+ if oldExist {
81
+ err = os .Setenv ("IOTEX_TEST_NODE_TYPE" , oldEnv )
82
+ } else {
83
+ err = os .Unsetenv ("IOTEX_TEST_NODE_TYPE" )
84
+ }
85
+ require .NoError (t , err )
86
+ }
87
+ }
88
+
89
+ func generateProducerPrivKey () (crypto.PrivateKey , string , error ) {
90
+ sk , err := crypto .GenerateKey ()
91
+ cfgStr := fmt .Sprintf (`
92
+ chain:
93
+ producerPrivKey: "%s"
94
+ ` ,
95
+ sk .HexString (),
96
+ )
97
+ return sk , cfgStr , err
98
+ }
99
+
24
100
func TestDB_SplitDBSize (t * testing.T ) {
25
101
var db = DB {SplitDBSizeMB : uint64 (1 )}
26
102
var expected = uint64 (1 * 1024 * 1024 )
@@ -84,22 +160,12 @@ func TestNewConfigWithPlugins(t *testing.T) {
84
160
}
85
161
86
162
func TestNewConfigWithOverride (t * testing.T ) {
87
- sk , err := crypto .GenerateKey ()
88
- require .NoError (t , err )
89
- cfgStr := fmt .Sprintf (`
90
- chain:
91
- producerPrivKey: "%s"
92
- ` ,
93
- sk .HexString (),
94
- )
95
- _overwritePath = filepath .Join (os .TempDir (), "config.yaml" )
96
- err = ioutil .WriteFile (_overwritePath , []byte (cfgStr ), 0666 )
163
+ sk , cfgStr , err := generateProducerPrivKey ()
97
164
require .NoError (t , err )
98
- defer func () {
99
- err := os .Remove (_overwritePath )
100
- _overwritePath = ""
101
- require .NoError (t , err )
102
- }()
165
+
166
+ require .NoError (t , makePathAndWriteFile (cfgStr , "_overwritePath" ))
167
+
168
+ defer resetPathValues (t , []string {"_overwritePath" })
103
169
104
170
cfg , err := New ()
105
171
require .NoError (t , err )
@@ -108,36 +174,14 @@ chain:
108
174
}
109
175
110
176
func TestNewConfigWithSecret (t * testing.T ) {
111
- sk , err := crypto .GenerateKey ()
112
- require .NoError (t , err )
113
- cfgStr := fmt .Sprintf (`
114
- chain:
115
- producerPrivKey: "%s"
116
- ` ,
117
- sk .HexString (),
118
- )
119
- _overwritePath = filepath .Join (os .TempDir (), "config.yaml" )
120
- err = ioutil .WriteFile (_overwritePath , []byte (cfgStr ), 0666 )
177
+ sk , cfgStr , err := generateProducerPrivKey ()
121
178
require .NoError (t , err )
122
179
123
- cfgStr = fmt .Sprintf (`
124
- chain:
125
- producerPrivKey: "%s"
126
- ` ,
127
- sk .HexString (),
128
- )
129
- _secretPath = filepath .Join (os .TempDir (), "secret.yaml" )
130
- err = ioutil .WriteFile (_secretPath , []byte (cfgStr ), 0666 )
131
- require .NoError (t , err )
180
+ require .NoError (t , makePathAndWriteFile (cfgStr , "_overwritePath" ))
132
181
133
- defer func () {
134
- err := os .Remove (_overwritePath )
135
- require .NoError (t , err )
136
- _overwritePath = ""
137
- err = os .Remove (_secretPath )
138
- require .NoError (t , err )
139
- _secretPath = ""
140
- }()
182
+ require .NoError (t , makePathAndWriteFile (cfgStr , "_secretPath" ))
183
+
184
+ defer resetPathValues (t , []string {"_overwritePath" , "_secretPath" })
141
185
142
186
cfg , err := New ()
143
187
require .NoError (t , err )
@@ -148,29 +192,11 @@ chain:
148
192
func TestNewConfigWithLookupEnv (t * testing.T ) {
149
193
oldEnv , oldExist := os .LookupEnv ("IOTEX_TEST_NODE_TYPE" )
150
194
151
- sk , err := crypto .GenerateKey ()
152
- require .NoError (t , err )
153
- cfgStr := fmt .Sprintf (`
154
- chain:
155
- producerPrivKey: "%s"
156
- ` ,
157
- sk .HexString (),
158
- )
159
- _overwritePath = filepath .Join (os .TempDir (), "config.yaml" )
160
- err = ioutil .WriteFile (_overwritePath , []byte (cfgStr ), 0666 )
195
+ _ , cfgStr , err := generateProducerPrivKey ()
161
196
require .NoError (t , err )
197
+ require .NoError (t , makePathAndWriteFile (cfgStr , "_overwritePath" ))
162
198
163
- defer func () {
164
- err := os .Remove (_overwritePath )
165
- require .NoError (t , err )
166
- _overwritePath = ""
167
- if oldExist {
168
- err = os .Setenv ("IOTEX_TEST_NODE_TYPE" , oldEnv )
169
- } else {
170
- err = os .Unsetenv ("IOTEX_TEST_NODE_TYPE" )
171
- }
172
- require .NoError (t , err )
173
- }()
199
+ defer resetPathValuesWithLookupEnv (t , oldEnv , oldExist , "_overwritePath" )
174
200
175
201
cfg , err := New ()
176
202
require .NoError (t , err )
@@ -293,60 +319,25 @@ func TestNewSubConfigWithWrongConfigPath(t *testing.T) {
293
319
}
294
320
295
321
func TestNewSubConfigWithSubChainPath (t * testing.T ) {
296
- sk , err := crypto .GenerateKey ()
297
- require .NoError (t , err )
298
- cfgStr := fmt .Sprintf (`
299
- chain:
300
- producerPrivKey: "%s"
301
- ` ,
302
- sk .HexString (),
303
- )
304
- _subChainPath = filepath .Join (os .TempDir (), "config.yaml" )
305
- err = ioutil .WriteFile (_subChainPath , []byte (cfgStr ), 0666 )
322
+ sk , cfgStr , err := generateProducerPrivKey ()
306
323
require .NoError (t , err )
307
- defer func () {
308
- err = os .Remove (_subChainPath )
309
- _subChainPath = ""
310
- require .NoError (t , err )
311
- }()
324
+ require .NoError (t , makePathAndWriteFile (cfgStr , "_subChainPath" ))
312
325
326
+ defer resetPathValues (t , []string {"_subChainPath" })
313
327
cfg , err := NewSub ()
314
328
require .NoError (t , err )
315
329
require .NotNil (t , cfg )
316
330
require .Equal (t , sk .HexString (), cfg .Chain .ProducerPrivKey )
317
331
}
318
332
319
333
func TestNewSubConfigWithSecret (t * testing.T ) {
320
- sk , err := crypto .GenerateKey ()
321
- require .NoError (t , err )
322
- cfgStr := fmt .Sprintf (`
323
- chain:
324
- producerPrivKey: "%s"
325
- ` ,
326
- sk .HexString (),
327
- )
328
- _subChainPath = filepath .Join (os .TempDir (), "config.yaml" )
329
- err = ioutil .WriteFile (_subChainPath , []byte (cfgStr ), 0666 )
334
+ sk , cfgStr , err := generateProducerPrivKey ()
330
335
require .NoError (t , err )
336
+ require .NoError (t , makePathAndWriteFile (cfgStr , "_subChainPath" ))
331
337
332
- cfgStr = fmt .Sprintf (`
333
- chain:
334
- producerPrivKey: "%s"
335
- ` ,
336
- sk .HexString (),
337
- )
338
- _secretPath = filepath .Join (os .TempDir (), "secret.yaml" )
339
- err = ioutil .WriteFile (_secretPath , []byte (cfgStr ), 0666 )
340
- require .NoError (t , err )
338
+ require .NoError (t , makePathAndWriteFile (cfgStr , "_secretPath" ))
341
339
342
- defer func () {
343
- err = os .Remove (_subChainPath )
344
- require .NoError (t , err )
345
- _subChainPath = ""
346
- err = os .Remove (_secretPath )
347
- require .NoError (t , err )
348
- _secretPath = ""
349
- }()
340
+ defer resetPathValues (t , []string {"_subChainPath" , "_secretPath" })
350
341
351
342
cfg , err := NewSub ()
352
343
require .NoError (t , err )
@@ -357,29 +348,12 @@ chain:
357
348
func TestNewSubConfigWithLookupEnv (t * testing.T ) {
358
349
oldEnv , oldExist := os .LookupEnv ("IOTEX_TEST_NODE_TYPE" )
359
350
360
- sk , err := crypto .GenerateKey ()
361
- require .NoError (t , err )
362
- cfgStr := fmt .Sprintf (`
363
- chain:
364
- producerPrivKey: "%s"
365
- ` ,
366
- sk .HexString (),
367
- )
368
- _subChainPath = filepath .Join (os .TempDir (), "config.yaml" )
369
- err = ioutil .WriteFile (_subChainPath , []byte (cfgStr ), 0666 )
351
+ _ , cfgStr , err := generateProducerPrivKey ()
370
352
require .NoError (t , err )
371
353
372
- defer func () {
373
- err = os .Remove (_subChainPath )
374
- require .NoError (t , err )
375
- _subChainPath = ""
376
- if oldExist {
377
- err = os .Setenv ("IOTEX_TEST_NODE_TYPE" , oldEnv )
378
- } else {
379
- err = os .Unsetenv ("IOTEX_TEST_NODE_TYPE" )
380
- }
381
- require .NoError (t , err )
382
- }()
354
+ require .NoError (t , makePathAndWriteFile (cfgStr , "_subChainPath" ))
355
+
356
+ defer resetPathValuesWithLookupEnv (t , oldEnv , oldExist , "_subChainPath" )
383
357
384
358
cfg , err := NewSub ()
385
359
require .NoError (t , err )
0 commit comments