Skip to content

Commit

Permalink
add more tests for coverage
Browse files Browse the repository at this point in the history
  • Loading branch information
manav2401 committed Sep 18, 2023
1 parent defb84f commit 2e7ea69
Show file tree
Hide file tree
Showing 4 changed files with 145 additions and 51 deletions.
2 changes: 1 addition & 1 deletion internal/cli/flagset/flagset.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func (f *Flagset) UpdateValue(names []string, values []string) {
case reflect.TypeOf(map[string]string{}):
newValue = GetMapString(value)
default:
log.Trace("Unable to parse the type while overriding flag, skipping", "flag", name, "got type", oldType)
log.Info("Unable to parse the type while overriding flag, skipping", "flag", name, "got type", oldType)
continue
}
}
Expand Down
140 changes: 116 additions & 24 deletions internal/cli/server/command_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,43 +8,135 @@ import (
"github.com/stretchr/testify/require"
)

func TestFlags(t *testing.T) {
// TestFlagsWithoutConfig tests all types of flags passed only
// via cli args.
func TestFlagsWithoutConfig(t *testing.T) {
t.Parallel()

var c Command

args := []string{
"--txpool.rejournal", "30m0s",
"--txpool.lifetime", "30m0s",
"--miner.gasprice", "20000000000",
"--gpo.maxprice", "70000000000",
"--gpo.ignoreprice", "1",
"--cache.trie.rejournal", "40m0s",
"--dev",
"--dev.period", "2",
"--identity", "",
"--datadir", "./data",
"--maxpeers", "30",
"--verbosity", "3",
"--rpc.batchlimit", "0",
"--snapshot",
"--bor.logs=false",
"--eth.requiredblocks", "a=b",
"--http.api", "eth,web3,bor",
"--miner.gasprice", "30000000000",
"--miner.recommit", "20s",
"--rpc.evmtimeout", "5s",
"--rpc.txfeecap", "6.0",
"--http.api", "eth,bor",
"--ws.api", "",
"--gpo.maxprice", "5000000000000",
}

err := c.extractFlags(args)

require.NoError(t, err)

txRe, _ := time.ParseDuration("30m0s")
txLt, _ := time.ParseDuration("30m0s")
caRe, _ := time.ParseDuration("40m0s")
recommit, _ := time.ParseDuration("20s")
evmTimeout, _ := time.ParseDuration("5s")

require.Equal(t, c.config.Identity, "")
require.Equal(t, c.config.DataDir, "./data")
require.Equal(t, c.config.Developer.Enabled, true)
require.Equal(t, c.config.Developer.Period, uint64(2))
require.Equal(t, c.config.TxPool.Rejournal, txRe)
require.Equal(t, c.config.TxPool.LifeTime, txLt)
require.Equal(t, c.config.Sealer.GasPrice, big.NewInt(20000000000))
require.Equal(t, c.config.Gpo.MaxPrice, big.NewInt(70000000000))
require.Equal(t, c.config.Gpo.IgnorePrice, big.NewInt(1))
require.Equal(t, c.config.Cache.Rejournal, caRe)
require.Equal(t, c.config.P2P.MaxPeers, uint64(30))
require.Equal(t, c.config.Verbosity, 3)
require.Equal(t, c.config.RPCBatchLimit, uint64(0))
require.Equal(t, c.config.Snapshot, true)
require.Equal(t, c.config.BorLogs, false)
require.Equal(t, c.config.RequiredBlocks, map[string]string{"a": "b"})
require.Equal(t, c.config.JsonRPC.Http.API, []string{"eth", "web3", "bor"})
require.Equal(t, c.config.Sealer.GasPrice, big.NewInt(30000000000))
require.Equal(t, c.config.Sealer.Recommit, recommit)
require.Equal(t, c.config.JsonRPC.RPCEVMTimeout, evmTimeout)
require.Equal(t, c.config.JsonRPC.Http.API, []string{"eth", "bor"})
require.Equal(t, c.config.JsonRPC.Ws.API, []string(nil))
require.Equal(t, c.config.Gpo.MaxPrice, big.NewInt(5000000000000))
}

// TestFlagsWithoutConfig tests all types of flags passed only
// via config file.
func TestFlagsWithConfig(t *testing.T) {
t.Parallel()

var c Command

args := []string{
"--config", "./testdata/test.toml",
}

err := c.extractFlags(args)

require.NoError(t, err)

recommit, _ := time.ParseDuration("20s")
evmTimeout, _ := time.ParseDuration("5s")

require.Equal(t, c.config.Identity, "")
require.Equal(t, c.config.DataDir, "./data")
require.Equal(t, c.config.Verbosity, 3)
require.Equal(t, c.config.RPCBatchLimit, uint64(0))
require.Equal(t, c.config.Snapshot, true)
require.Equal(t, c.config.BorLogs, false)
require.Equal(t, c.config.RequiredBlocks,
map[string]string{
"31000000": "0x2087b9e2b353209c2c21e370c82daa12278efd0fe5f0febe6c29035352cf050e",
"32000000": "0x875500011e5eecc0c554f95d07b31cf59df4ca2505f4dbbfffa7d4e4da917c68",
},
)
require.Equal(t, c.config.Sealer.GasPrice, big.NewInt(30000000000))
require.Equal(t, c.config.Sealer.Recommit, recommit)
require.Equal(t, c.config.JsonRPC.RPCEVMTimeout, evmTimeout)
require.Equal(t, c.config.JsonRPC.Http.API, []string{"eth", "bor"})
require.Equal(t, c.config.JsonRPC.Ws.API, []string{""})
require.Equal(t, c.config.Gpo.MaxPrice, big.NewInt(5000000000000))
}

// TestFlagsWithConfig tests all types of flags passed via both
// config file and cli args. The cli args should overwrite the
// value of flag.
func TestFlagsWithConfigAndFlags(t *testing.T) {
t.Parallel()

var c Command

// Set the config and also override
args := []string{
"--config", "./testdata/test.toml",
"--identity", "Anon",
"--datadir", "",
"--verbosity", "0",
"--rpc.batchlimit", "5",
"--snapshot=false",
"--bor.logs=true",
"--eth.requiredblocks", "x=y",
"--miner.gasprice", "60000000000",
"--miner.recommit", "30s",
"--rpc.evmtimeout", "0s",
"--rpc.txfeecap", "0",
"--http.api", "",
"--ws.api", "eth,bor,web3",
"--gpo.maxprice", "0",
}

err := c.extractFlags(args)

require.NoError(t, err)

recommit, _ := time.ParseDuration("30s")
evmTimeout, _ := time.ParseDuration("0s")

require.Equal(t, c.config.Identity, "Anon")
require.Equal(t, c.config.DataDir, "")
require.Equal(t, c.config.Verbosity, 0)
require.Equal(t, c.config.RPCBatchLimit, uint64(5))
require.Equal(t, c.config.Snapshot, false)
require.Equal(t, c.config.BorLogs, true)
require.Equal(t, c.config.RequiredBlocks, map[string]string{"x": "y"})
require.Equal(t, c.config.Sealer.GasPrice, big.NewInt(60000000000))
require.Equal(t, c.config.Sealer.Recommit, recommit)
require.Equal(t, c.config.JsonRPC.RPCEVMTimeout, evmTimeout)
require.Equal(t, c.config.JsonRPC.Http.API, []string(nil))
require.Equal(t, c.config.JsonRPC.Ws.API, []string{"eth", "bor", "web3"})
require.Equal(t, c.config.Gpo.MaxPrice, big.NewInt(0))
}
22 changes: 12 additions & 10 deletions internal/cli/server/config_legacy_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,21 +15,23 @@ func TestConfigLegacy(t *testing.T) {

testConfig := DefaultConfig()

testConfig.Identity = ""
testConfig.DataDir = "./data"
testConfig.Snapshot = false
testConfig.Verbosity = 3
testConfig.RPCBatchLimit = 0
testConfig.Snapshot = true
testConfig.BorLogs = false
testConfig.RequiredBlocks = map[string]string{
"31000000": "0x2087b9e2b353209c2c21e370c82daa12278efd0fe5f0febe6c29035352cf050e",
"32000000": "0x875500011e5eecc0c554f95d07b31cf59df4ca2505f4dbbfffa7d4e4da917c68",
}
testConfig.P2P.MaxPeers = 30
testConfig.TxPool.Locals = []string{}
testConfig.TxPool.LifeTime = time.Second
testConfig.Sealer.Enabled = true
testConfig.Sealer.GasCeil = 30000000
testConfig.Sealer.GasPrice = big.NewInt(1000000000)
testConfig.Gpo.IgnorePrice = big.NewInt(4)
testConfig.Cache.Cache = 1024
testConfig.Cache.Rejournal = time.Second
testConfig.Sealer.GasPrice = big.NewInt(30000000000)
testConfig.Sealer.Recommit = 20 * time.Second
testConfig.JsonRPC.RPCEVMTimeout = 5 * time.Second
testConfig.JsonRPC.TxFeeCap = 6.0
testConfig.JsonRPC.Http.API = []string{"eth", "bor"}
testConfig.JsonRPC.Ws.API = []string{""}
testConfig.Gpo.MaxPrice = big.NewInt(5000000000000)

assert.Equal(t, expectedConfig, testConfig)
}
Expand Down
32 changes: 16 additions & 16 deletions internal/cli/server/testdata/test.toml
Original file line number Diff line number Diff line change
@@ -1,25 +1,25 @@
identity = ""
datadir = "./data"
snapshot = false
verbosity = 3
"rpc.batchlimit" = 0
snapshot = true
"bor.logs" = false

["eth.requiredblocks"]
"31000000" = "0x2087b9e2b353209c2c21e370c82daa12278efd0fe5f0febe6c29035352cf050e"
"32000000" = "0x875500011e5eecc0c554f95d07b31cf59df4ca2505f4dbbfffa7d4e4da917c68"

[p2p]
maxpeers = 30

[txpool]
locals = []
lifetime = "1s"

[miner]
mine = true
gaslimit = 30000000
gasprice = "1000000000"
gasprice = "30000000000"
recommit = "20s"

[gpo]
ignoreprice = "4"
[jsonrpc]
evmtimeout = "5s"
txfeecap = 6.0
[jsonrpc.http]
api = ["eth", "bor"]
[jsonrpc.ws]
api = [""]

[cache]
cache = 1024
rejournal = "1s"
[gpo]
maxprice = "5000000000000"

0 comments on commit 2e7ea69

Please sign in to comment.