github.com/palisadeinc/bor@v0.0.0-20230615125219-ab7196213d15/internal/cli/server/config_test.go (about)

     1  package server
     2  
     3  import (
     4  	"testing"
     5  	"time"
     6  
     7  	"github.com/stretchr/testify/assert"
     8  )
     9  
    10  func TestConfigDefault(t *testing.T) {
    11  	// the default config should work out of the box
    12  	config := DefaultConfig()
    13  	assert.NoError(t, config.loadChain())
    14  
    15  	_, err := config.buildNode()
    16  	assert.NoError(t, err)
    17  
    18  	_, err = config.buildEth(nil, nil)
    19  	assert.NoError(t, err)
    20  }
    21  
    22  func TestConfigMerge(t *testing.T) {
    23  	c0 := &Config{
    24  		Chain:    "0",
    25  		Snapshot: true,
    26  		RequiredBlocks: map[string]string{
    27  			"a": "b",
    28  		},
    29  		TxPool: &TxPoolConfig{
    30  			LifeTime: 5 * time.Second,
    31  		},
    32  		P2P: &P2PConfig{
    33  			Discovery: &P2PDiscovery{
    34  				StaticNodes: []string{
    35  					"a",
    36  				},
    37  			},
    38  		},
    39  	}
    40  	c1 := &Config{
    41  		Chain: "1",
    42  		RequiredBlocks: map[string]string{
    43  			"b": "c",
    44  		},
    45  		P2P: &P2PConfig{
    46  			MaxPeers: 10,
    47  			Discovery: &P2PDiscovery{
    48  				StaticNodes: []string{
    49  					"b",
    50  				},
    51  			},
    52  		},
    53  	}
    54  
    55  	expected := &Config{
    56  		Chain:    "1",
    57  		Snapshot: false,
    58  		RequiredBlocks: map[string]string{
    59  			"a": "b",
    60  			"b": "c",
    61  		},
    62  		P2P: &P2PConfig{
    63  			MaxPeers: 10,
    64  			Discovery: &P2PDiscovery{
    65  				StaticNodes: []string{
    66  					"b",
    67  				},
    68  			},
    69  		},
    70  	}
    71  
    72  	assert.NoError(t, c0.Merge(c1))
    73  	assert.Equal(t, c0, expected)
    74  }
    75  
    76  func TestDefaultDatatypeOverride(t *testing.T) {
    77  	t.Parallel()
    78  
    79  	// This test is specific to `maxpeers` flag (for now) to check
    80  	// if default datatype value (0 in case of uint64) is overridden.
    81  	c0 := &Config{
    82  		P2P: &P2PConfig{
    83  			MaxPeers: 30,
    84  		},
    85  	}
    86  
    87  	c1 := &Config{
    88  		P2P: &P2PConfig{
    89  			MaxPeers: 0,
    90  		},
    91  	}
    92  
    93  	expected := &Config{
    94  		P2P: &P2PConfig{
    95  			MaxPeers: 0,
    96  		},
    97  	}
    98  
    99  	assert.NoError(t, c0.Merge(c1))
   100  	assert.Equal(t, c0, expected)
   101  }
   102  
   103  var dummyEnodeAddr = "enode://0cb82b395094ee4a2915e9714894627de9ed8498fb881cec6db7c65e8b9a5bd7f2f25cc84e71e89d0947e51c76e85d0847de848c7782b13c0255247a6758178c@44.232.55.71:30303"
   104  
   105  func TestConfigBootnodesDefault(t *testing.T) {
   106  	t.Run("EmptyBootnodes", func(t *testing.T) {
   107  		// if no bootnodes are specific, we use the ones from the genesis chain
   108  		config := DefaultConfig()
   109  		assert.NoError(t, config.loadChain())
   110  
   111  		cfg, err := config.buildNode()
   112  		assert.NoError(t, err)
   113  		assert.NotEmpty(t, cfg.P2P.BootstrapNodes)
   114  	})
   115  	t.Run("NotEmptyBootnodes", func(t *testing.T) {
   116  		// if bootnodes specific, DO NOT load the genesis bootnodes
   117  		config := DefaultConfig()
   118  		config.P2P.Discovery.Bootnodes = []string{dummyEnodeAddr}
   119  
   120  		cfg, err := config.buildNode()
   121  		assert.NoError(t, err)
   122  		assert.Len(t, cfg.P2P.BootstrapNodes, 1)
   123  	})
   124  }
   125  
   126  func TestMakePasswordListFromFile(t *testing.T) {
   127  	t.Parallel()
   128  
   129  	t.Run("ReadPasswordFile", func(t *testing.T) {
   130  		t.Parallel()
   131  
   132  		result, _ := MakePasswordListFromFile("./testdata/password.txt")
   133  		assert.Equal(t, []string{"test1", "test2"}, result)
   134  	})
   135  }