From 875d5f02f9369021df40b032c197f50cc9c7e8e2 Mon Sep 17 00:00:00 2001 From: Leo Chen Date: Sat, 6 Apr 2019 05:31:13 +0000 Subject: [PATCH] [utils] add a wallet profile read function test coverage before: 78.5% test coverage after: 78.7% Signed-off-by: Leo Chen --- internal/utils/configfile.go | 66 ++++++++++++++++++ internal/utils/configfile_test.go | 112 ++++++++++++++++++++++++++++++ internal/utils/test.ini | 37 ++++++++++ 3 files changed, 215 insertions(+) create mode 100644 internal/utils/configfile.go create mode 100644 internal/utils/configfile_test.go create mode 100644 internal/utils/test.ini diff --git a/internal/utils/configfile.go b/internal/utils/configfile.go new file mode 100644 index 000000000..369fe522d --- /dev/null +++ b/internal/utils/configfile.go @@ -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 + +} diff --git a/internal/utils/configfile_test.go b/internal/utils/configfile_test.go new file mode 100644 index 000000000..27c3be493 --- /dev/null +++ b/internal/utils/configfile_test.go @@ -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]) + } +} diff --git a/internal/utils/test.ini b/internal/utils/test.ini new file mode 100644 index 000000000..eae36c213 --- /dev/null +++ b/internal/utils/test.ini @@ -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