Skip to content

Commit

Permalink
[FABG-788] Set PeerChannelConfig defaults properly
Browse files Browse the repository at this point in the history
When a channels->peers entry is specified with NO PeerChannelConfig in
config.yaml, make sure the defaults are filled in correctly.

Change-Id: I5032d914cc037998c93d797102ba114025d16449
Signed-off-by: Nye Liu <[email protected]>
  • Loading branch information
nyetwurk committed Dec 15, 2018
1 parent 3081d70 commit fa4badf
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
17 changes: 16 additions & 1 deletion pkg/fab/endpointconfig.go
Original file line number Diff line number Diff line change
Expand Up @@ -1749,7 +1749,22 @@ func peerChannelConfigHookFunc() mapstructure.DecodeHookFunc {
t reflect.Type,
data interface{}) (interface{}, error) {

//If target is of type 'fab.PeerChannelConfig', then only hook should work
//Run through each PeerChannelConfig, create empty config map if value is nil
if t == reflect.TypeOf(map[string]PeerChannelConfig{}) {
dataMap, ok := data.(map[string]interface{})
if ok {
for k, v := range dataMap {
if v == nil {
// Make an empty map. It will be filled in with defaults
// in other hook below
dataMap[k] = make(map[string]interface{})
}
}
return dataMap, nil
}
}

//If target is of type 'fab.PeerChannelConfig', fill in defaults if not already specified
if t == reflect.TypeOf(PeerChannelConfig{}) {
dataMap, ok := data.(map[string]interface{})
if ok {
Expand Down
11 changes: 9 additions & 2 deletions pkg/fab/endpointconfig_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1107,21 +1107,27 @@ func TestPeerChannelConfig(t *testing.T) {
assert.Equal(t, 6*time.Second, eventPolicies.PeerMonitorPeriod, "Unexpected value for PeerMonitorPeriod")

//Test if custom hook for (default=true) func is working
assert.True(t, len(networkConfig.Channels[orgChannelID].Peers) == 2)
assert.True(t, len(networkConfig.Channels[orgChannelID].Peers) == 3)
//test orgchannel peer1 (EndorsingPeer should be true as set, remaining should be default = true)
orgChannelPeer1 := networkConfig.Channels[orgChannelID].Peers["peer0.org1.example.com"]
assert.True(t, orgChannelPeer1.EndorsingPeer)
assert.True(t, orgChannelPeer1.LedgerQuery)
assert.True(t, orgChannelPeer1.EventSource)
assert.True(t, orgChannelPeer1.ChaincodeQuery)

//test orgchannel peer1 (EndorsingPeer should be false as set, remaining should be default = true)
//test orgchannel peer2 (EndorsingPeer should be false as set, remaining should be default = true)
orgChannelPeer2 := networkConfig.Channels[orgChannelID].Peers["peer0.org2.example.com"]
assert.False(t, orgChannelPeer2.EndorsingPeer)
assert.True(t, orgChannelPeer2.LedgerQuery)
assert.True(t, orgChannelPeer2.EventSource)
assert.True(t, orgChannelPeer2.ChaincodeQuery)

//test orgchannel peer3 (All should be true)
orgChannelPeer3 := networkConfig.Channels[orgChannelID].Peers["peer0.org3.example.com"]
assert.True(t, orgChannelPeer3.EndorsingPeer)
assert.True(t, orgChannelPeer3.LedgerQuery)
assert.True(t, orgChannelPeer3.EventSource)
assert.True(t, orgChannelPeer3.ChaincodeQuery)
}

func TestEndpointConfigWithMultipleBackends(t *testing.T) {
Expand Down Expand Up @@ -1281,6 +1287,7 @@ func tamperPeerChannelConfig(backend *mocks.MockConfigBackend) {
"peers": map[string]interface{}{
"peer0.org1.example.com": map[string]interface{}{"endorsingpeer": true},
"peer0.org2.example.com": map[string]interface{}{"endorsingpeer": false},
"peer0.org3.example.com": nil,
},
}
(channelsMap.(map[string]interface{}))[orgChannelID] = orgChannel
Expand Down

0 comments on commit fa4badf

Please sign in to comment.