[utils] add a wallet profile read function

test coverage before: 78.5%
test coverage after: 78.7%

Signed-off-by: Leo Chen <leo@harmony.one>
pull/689/head
Leo Chen 6 years ago
parent d2db9f493a
commit 875d5f02f9
  1. 66
      internal/utils/configfile.go
  2. 112
      internal/utils/configfile_test.go
  3. 37
      internal/utils/test.ini

@ -0,0 +1,66 @@
package utils
// this module in utils handles the ini file read/write
import (
"fmt"
"strings"
"github.com/harmony-one/harmony/p2p"
ini "gopkg.in/ini.v1"
)
// WalletProfile contains a section and key value pair map
type WalletProfile struct {
Profile string
Bootnodes []string
Shards int
RPCServer [][]p2p.Peer
}
// ReadWalletProfile reads an ini file and return WalletProfile
func ReadWalletProfile(fn string, profile string) (*WalletProfile, error) {
cfg, err := ini.ShadowLoad(fn)
if err != nil {
return nil, err
}
config := new(WalletProfile)
config.Profile = profile
// get the profile section
sec, err := cfg.GetSection(profile)
if err != nil {
return nil, err
}
if sec.HasKey("bootnode") {
config.Bootnodes = sec.Key("bootnode").ValueWithShadows()
} else {
return nil, fmt.Errorf("can't find bootnode key")
}
if sec.HasKey("shards") {
config.Shards = sec.Key("shards").MustInt()
config.RPCServer = make([][]p2p.Peer, config.Shards)
} else {
return nil, fmt.Errorf("can't find shards key")
}
for i := 0; i < config.Shards; i++ {
rpcSec, err := cfg.GetSection(fmt.Sprintf("%s.shard%v.rpc", profile, i))
if err != nil {
return nil, err
}
rpcKey := rpcSec.Key("rpc").ValueWithShadows()
for _, key := range rpcKey {
v := strings.Split(key, ":")
rpc := p2p.Peer{
IP: v[0],
Port: v[1],
}
config.RPCServer[i] = append(config.RPCServer[i], rpc)
}
}
return config, nil
}

@ -0,0 +1,112 @@
package utils
import (
"reflect"
"testing"
"github.com/harmony-one/harmony/p2p"
)
func TestReadWalletProfile(t *testing.T) {
config := []*WalletProfile{
&WalletProfile{
Profile: "default",
Bootnodes: []string{"127.0.0.1:9000/abcd", "127.0.0.1:9999/daeg"},
Shards: 4,
RPCServer: [][]p2p.Peer{
[]p2p.Peer{
p2p.Peer{
IP: "127.0.0.4",
Port: "8888",
},
p2p.Peer{
IP: "192.168.0.4",
Port: "9876",
},
},
[]p2p.Peer{
p2p.Peer{
IP: "127.0.0.1",
Port: "8888",
},
p2p.Peer{
IP: "192.168.0.1",
Port: "9876",
},
},
[]p2p.Peer{
p2p.Peer{
IP: "127.0.0.2",
Port: "8888",
},
p2p.Peer{
IP: "192.168.0.2",
Port: "9876",
},
},
[]p2p.Peer{
p2p.Peer{
IP: "127.0.0.3",
Port: "8888",
},
p2p.Peer{
IP: "192.168.0.3",
Port: "9876",
},
},
},
},
&WalletProfile{
Profile: "testnet",
Bootnodes: []string{"192.168.0.1:9990/abcd", "127.0.0.1:8888/daeg"},
Shards: 3,
RPCServer: [][]p2p.Peer{
[]p2p.Peer{
p2p.Peer{
IP: "192.168.2.3",
Port: "8888",
},
p2p.Peer{
IP: "192.168.192.3",
Port: "9877",
},
},
[]p2p.Peer{
p2p.Peer{
IP: "192.168.2.1",
Port: "8888",
},
p2p.Peer{
IP: "192.168.192.1",
Port: "9877",
},
},
[]p2p.Peer{
p2p.Peer{
IP: "192.168.2.2",
Port: "8888",
},
p2p.Peer{
IP: "192.168.192.2",
Port: "9877",
},
},
},
},
}
config1, err := ReadWalletProfile("test.ini", "default")
if err != nil {
t.Fatalf("ReadWalletProfile Error: %v", err)
}
if !reflect.DeepEqual(config[0], config1) {
t.Errorf("Got: %v\nExpect: %v\n", config1, config[0])
}
config2, err := ReadWalletProfile("test.ini", "testnet")
if err != nil {
t.Fatalf("ReadWalletProfile Error: %v", err)
}
if !reflect.DeepEqual(config[1], config2) {
t.Errorf("Got: %v\nExpect: %v\n", config2, config[1])
}
}

@ -0,0 +1,37 @@
[default]
bootnode = 127.0.0.1:9000/abcd
bootnode = 127.0.0.1:9999/daeg
shards = 4
[default.shard0.rpc]
rpc = 127.0.0.4:8888
rpc = 192.168.0.4:9876
[default.shard1.rpc]
rpc = 127.0.0.1:8888
rpc = 192.168.0.1:9876
[default.shard2.rpc]
rpc = 127.0.0.2:8888
rpc = 192.168.0.2:9876
[default.shard3.rpc]
rpc = 127.0.0.3:8888
rpc = 192.168.0.3:9876
[testnet]
bootnode = 192.168.0.1:9990/abcd
bootnode = 127.0.0.1:8888/daeg
shards = 3
[testnet.shard0.rpc]
rpc = 192.168.2.3:8888
rpc = 192.168.192.3:9877
[testnet.shard1.rpc]
rpc = 192.168.2.1:8888
rpc = 192.168.192.1:9877
[testnet.shard2.rpc]
rpc = 192.168.2.2:8888
rpc = 192.168.192.2:9877
Loading…
Cancel
Save