go-ethereum/internal/cli/server/config_test.go
Pratik Patil 71980bf5b1
internal/cli: use config file for flags in new-cli, builder/files: update defaults (#457)
* updated simple.json and simple.hcl

* added annotations for developer and grpc block

* added toml tags and simple.toml file

* added support for toml config files

* updated simple files toml, hcl, json

* added config.toml in builder/files and updated bor.service

* add dumpconfig command in cli for exporting configs

* update docs

* updated .goreleaser.yml (POS-651)

* changed --config to -config

* added test config for tests and fixed lint errors

* made fields of type big.int and time.Duration private, removed merge from dumpconfig, setting up default values to the Raw fields in dumpconfig, and fixed one lint error

* fixed lint errors (strange)

* lint fix

* private no-more, using '-' in name tags to ignore

* updated name tags, made c.configFile as a stringFlag (only one config file supported) and updated the merge logic in command.go -> Run()

* fix: set method for big.Int flags, added a TODO

* handeled bigInt and timeDuration type, bug fix in config_legacy, lint fix

* updated flags, consistent across flags.go and config.go

* fixed config test and updated test hcl, json config files

* updated config legacy test

* added test to check values of cmd flags, restructured Run in command.go, linter fix

* fix linters

* lint again

* changed 2 flags and assert -> require

* changed the 2 flags back

* updated correct congig.toml path and made mainnet default

* updated config.toml with new flags

* added sample config (toml) file

* removed sample-config.toml and added it in docs/config.md

Co-authored-by: Manav Darji <manavdarji.india@gmail.com>
2022-07-27 12:28:26 +05:30

171 lines
3.4 KiB
Go

package server
import (
"math/big"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
func TestConfigDefault(t *testing.T) {
// the default config should work out of the box
config := DefaultConfig()
assert.NoError(t, config.loadChain())
_, err := config.buildNode()
assert.NoError(t, err)
_, err = config.buildEth(nil, nil)
assert.NoError(t, err)
}
func TestConfigMerge(t *testing.T) {
c0 := &Config{
Chain: "0",
Snapshot: true,
RequiredBlocks: map[string]string{
"a": "b",
},
TxPool: &TxPoolConfig{
LifeTime: 5 * time.Second,
},
P2P: &P2PConfig{
Discovery: &P2PDiscovery{
StaticNodes: []string{
"a",
},
},
},
}
c1 := &Config{
Chain: "1",
RequiredBlocks: map[string]string{
"b": "c",
},
P2P: &P2PConfig{
MaxPeers: 10,
Discovery: &P2PDiscovery{
StaticNodes: []string{
"b",
},
},
},
}
expected := &Config{
Chain: "1",
Snapshot: false,
RequiredBlocks: map[string]string{
"a": "b",
"b": "c",
},
P2P: &P2PConfig{
MaxPeers: 10,
Discovery: &P2PDiscovery{
StaticNodes: []string{
"b",
},
},
},
}
assert.NoError(t, c0.Merge(c1))
assert.Equal(t, c0, expected)
}
func TestDefaultDatatypeOverride(t *testing.T) {
t.Parallel()
// This test is specific to `maxpeers` flag (for now) to check
// if default datatype value (0 in case of uint64) is overridden.
c0 := &Config{
P2P: &P2PConfig{
MaxPeers: 30,
},
}
c1 := &Config{
P2P: &P2PConfig{
MaxPeers: 0,
},
}
expected := &Config{
P2P: &P2PConfig{
MaxPeers: 0,
},
}
assert.NoError(t, c0.Merge(c1))
assert.Equal(t, c0, expected)
}
func TestConfigLoadFile(t *testing.T) {
readFile := func(path string) {
config, err := readConfigFile(path)
assert.NoError(t, err)
assert.Equal(t, config, &Config{
DataDir: "./data",
RequiredBlocks: map[string]string{
"a": "b",
},
P2P: &P2PConfig{
MaxPeers: 30,
},
TxPool: &TxPoolConfig{
LifeTime: 1 * time.Second,
},
Gpo: &GpoConfig{
MaxPrice: big.NewInt(100),
},
Sealer: &SealerConfig{},
Cache: &CacheConfig{},
})
}
// read file in hcl format
t.Run("hcl", func(t *testing.T) {
readFile("./testdata/test.hcl")
})
// read file in json format
t.Run("json", func(t *testing.T) {
readFile("./testdata/test.json")
})
}
var dummyEnodeAddr = "enode://0cb82b395094ee4a2915e9714894627de9ed8498fb881cec6db7c65e8b9a5bd7f2f25cc84e71e89d0947e51c76e85d0847de848c7782b13c0255247a6758178c@44.232.55.71:30303"
func TestConfigBootnodesDefault(t *testing.T) {
t.Run("EmptyBootnodes", func(t *testing.T) {
// if no bootnodes are specific, we use the ones from the genesis chain
config := DefaultConfig()
assert.NoError(t, config.loadChain())
cfg, err := config.buildNode()
assert.NoError(t, err)
assert.NotEmpty(t, cfg.P2P.BootstrapNodes)
})
t.Run("NotEmptyBootnodes", func(t *testing.T) {
// if bootnodes specific, DO NOT load the genesis bootnodes
config := DefaultConfig()
config.P2P.Discovery.Bootnodes = []string{dummyEnodeAddr}
cfg, err := config.buildNode()
assert.NoError(t, err)
assert.Len(t, cfg.P2P.BootstrapNodes, 1)
})
}
func TestMakePasswordListFromFile(t *testing.T) {
t.Parallel()
t.Run("ReadPasswordFile", func(t *testing.T) {
t.Parallel()
result, _ := MakePasswordListFromFile("./testdata/password.txt")
assert.Equal(t, []string{"test1", "test2"}, result)
})
}