@@ -8,17 +8,32 @@ package config
8
8
9
9
import (
10
10
"fmt"
11
- "io/ioutil "
11
+ "math/big "
12
12
"os"
13
+ "io/ioutil"
13
14
"path/filepath"
14
15
"strings"
15
16
"testing"
16
17
17
- "github.com/iotexproject/go-pkgs/crypto"
18
+
18
19
"github.com/pkg/errors"
19
20
"github.com/stretchr/testify/require"
21
+
22
+ "github.com/iotexproject/go-pkgs/crypto"
20
23
)
21
24
25
+ func TestDB_SplitDBSize (t * testing.T ) {
26
+ var db = DB {SplitDBSizeMB : uint64 (1 )}
27
+ var expected = uint64 (1 * 1024 * 1024 )
28
+ require .Equal (t , expected , db .SplitDBSize ())
29
+ }
30
+
31
+ func TestStrs_String (t * testing.T ) {
32
+ ss := strs {"test" }
33
+ str := "TEST"
34
+ require .Nil (t , ss .Set (str ))
35
+ }
36
+
22
37
func TestNewDefaultConfig (t * testing.T ) {
23
38
_ , err := New ()
24
39
require .NoError (t , err )
@@ -46,6 +61,29 @@ func TestNewConfigWithWrongConfigPath(t *testing.T) {
46
61
}
47
62
}
48
63
64
+ func TestNewConfigWithPlugins (t * testing.T ) {
65
+ _plugins = strs {
66
+ "gateway" ,
67
+ }
68
+ cfg , err := New ()
69
+
70
+ require .Nil (t , cfg .Plugins [GatewayPlugin ])
71
+ require .NoError (t , err )
72
+
73
+ _plugins = strs {
74
+ "trick" ,
75
+ }
76
+
77
+ cfg , err = New ()
78
+
79
+ require .Equal (t , Config {}, cfg )
80
+ require .Error (t , err )
81
+
82
+ defer func () {
83
+ _plugins = nil
84
+ }()
85
+ }
86
+
49
87
func TestNewConfigWithOverride (t * testing.T ) {
50
88
sk , err := crypto .GenerateKey ()
51
89
require .NoError (t , err )
@@ -82,8 +120,6 @@ chain:
82
120
_overwritePath = filepath .Join (os .TempDir (), "config.yaml" )
83
121
err = ioutil .WriteFile (_overwritePath , []byte (cfgStr ), 0666 )
84
122
require .NoError (t , err )
85
- defer func () {
86
- }()
87
123
88
124
cfgStr = fmt .Sprintf (`
89
125
chain:
@@ -214,3 +250,153 @@ func TestValidateActPool(t *testing.T) {
214
250
),
215
251
)
216
252
}
253
+
254
+ func TestValidateMinGasPrice (t * testing.T ) {
255
+ ap := ActPool {MinGasPriceStr : Default .ActPool .MinGasPriceStr }
256
+ mgp := ap .MinGasPrice ()
257
+ fmt .Printf ("%T,%v" , mgp , mgp )
258
+ require .IsType (t , & big.Int {}, mgp )
259
+ }
260
+
261
+ func TestValidateProducerPrivateKey (t * testing.T ) {
262
+ cfg := Default
263
+ sk := cfg .ProducerPrivateKey ()
264
+ require .NotNil (t , sk )
265
+ }
266
+
267
+ func TestValidateProducerAddress (t * testing.T ) {
268
+ cfg := Default
269
+ addr := cfg .ProducerAddress ()
270
+ require .NotNil (t , addr )
271
+ }
272
+
273
+ func TestNewSubDefaultConfig (t * testing.T ) {
274
+ _ , err := NewSub ()
275
+ require .NoError (t , err )
276
+ }
277
+
278
+ func TestNewSubConfigWithoutValidation (t * testing.T ) {
279
+ cfg , err := NewSub (DoNotValidate )
280
+ require .NoError (t , err )
281
+ require .NotNil (t , cfg )
282
+ }
283
+
284
+ func TestNewSubConfigWithWrongConfigPath (t * testing.T ) {
285
+ _subChainPath = "wrong_path"
286
+ defer func () { _subChainPath = "" }()
287
+ cfg , err := NewSub ()
288
+ require .Error (t , err )
289
+ require .Equal (t , Config {}, cfg )
290
+ if strings .Contains (err .Error (),
291
+ "open wrong_path: The system cannot find the file specified" ) == false { // for Windows
292
+ require .Contains (t , err .Error (), "open wrong_path: no such file or directory" )
293
+ }
294
+ }
295
+
296
+ func TestNewSubConfigWithSubChainPath (t * testing.T ) {
297
+ sk , err := crypto .GenerateKey ()
298
+ require .NoError (t , err )
299
+ cfgStr := fmt .Sprintf (`
300
+ chain:
301
+ producerPrivKey: "%s"
302
+ ` ,
303
+ sk .HexString (),
304
+ )
305
+ _subChainPath = filepath .Join (os .TempDir (), "config.yaml" )
306
+ err = ioutil .WriteFile (_subChainPath , []byte (cfgStr ), 0666 )
307
+ require .NoError (t , err )
308
+ defer func () {
309
+ err = os .Remove (_subChainPath )
310
+ _subChainPath = ""
311
+ require .NoError (t , err )
312
+ }()
313
+
314
+ cfg , err := NewSub ()
315
+ require .NoError (t , err )
316
+ require .NotNil (t , cfg )
317
+ require .Equal (t , sk .HexString (), cfg .Chain .ProducerPrivKey )
318
+ }
319
+
320
+ func TestNewSubConfigWithSecret (t * testing.T ) {
321
+ sk , err := crypto .GenerateKey ()
322
+ require .NoError (t , err )
323
+ cfgStr := fmt .Sprintf (`
324
+ chain:
325
+ producerPrivKey: "%s"
326
+ ` ,
327
+ sk .HexString (),
328
+ )
329
+ _subChainPath = filepath .Join (os .TempDir (), "config.yaml" )
330
+ err = ioutil .WriteFile (_subChainPath , []byte (cfgStr ), 0666 )
331
+ require .NoError (t , err )
332
+
333
+ cfgStr = fmt .Sprintf (`
334
+ chain:
335
+ producerPrivKey: "%s"
336
+ ` ,
337
+ sk .HexString (),
338
+ )
339
+ _secretPath = filepath .Join (os .TempDir (), "secret.yaml" )
340
+ err = ioutil .WriteFile (_secretPath , []byte (cfgStr ), 0666 )
341
+ require .NoError (t , err )
342
+
343
+ defer func () {
344
+ err = os .Remove (_subChainPath )
345
+ require .NoError (t , err )
346
+ _subChainPath = ""
347
+ err = os .Remove (_secretPath )
348
+ require .NoError (t , err )
349
+ _secretPath = ""
350
+ }()
351
+
352
+ cfg , err := NewSub ()
353
+ require .NoError (t , err )
354
+ require .NotNil (t , cfg )
355
+ require .Equal (t , sk .HexString (), cfg .Chain .ProducerPrivKey )
356
+ }
357
+
358
+ func TestNewSubConfigWithLookupEnv (t * testing.T ) {
359
+ oldEnv , oldExist := os .LookupEnv ("IOTEX_TEST_NODE_TYPE" )
360
+
361
+ sk , err := crypto .GenerateKey ()
362
+ require .NoError (t , err )
363
+ cfgStr := fmt .Sprintf (`
364
+ chain:
365
+ producerPrivKey: "%s"
366
+ ` ,
367
+ sk .HexString (),
368
+ )
369
+ _subChainPath = filepath .Join (os .TempDir (), "config.yaml" )
370
+ err = ioutil .WriteFile (_subChainPath , []byte (cfgStr ), 0666 )
371
+ require .NoError (t , err )
372
+
373
+ defer func () {
374
+ err = os .Remove (_subChainPath )
375
+ require .NoError (t , err )
376
+ _subChainPath = ""
377
+ if oldExist {
378
+ err = os .Setenv ("IOTEX_TEST_NODE_TYPE" , oldEnv )
379
+ } else {
380
+ err = os .Unsetenv ("IOTEX_TEST_NODE_TYPE" )
381
+ }
382
+ require .NoError (t , err )
383
+ }()
384
+
385
+ cfg , err := NewSub ()
386
+ require .NoError (t , err )
387
+ require .NotNil (t , cfg )
388
+
389
+ err = os .Unsetenv ("IOTEX_TEST_NODE_TYPE" )
390
+ require .NoError (t , err )
391
+
392
+ cfg , err = NewSub ()
393
+ require .NoError (t , err )
394
+ require .NotNil (t , cfg )
395
+ }
396
+
397
+ func TestNewSubConfigWithoutSubChainPath (t * testing.T ) {
398
+ _subChainPath = ""
399
+ cfg , err := NewSub ()
400
+ require .Equal (t , Config {}, cfg )
401
+ require .Nil (t , err )
402
+ }
0 commit comments