Skip to content

Commit

Permalink
[FAB-11063] IgnoreEndpoint minor fixes
Browse files Browse the repository at this point in the history
- addressed code review commets from previous push


Change-Id: Ifb164b27fac502939eb154d120aed0866810b4b0
Signed-off-by: Sudesh Shetty <sudesh.shetty@securekey.com>
  • Loading branch information
sudeshrshetty committed Jul 16, 2018
1 parent 3701453 commit 227d4ed
Show file tree
Hide file tree
Showing 3 changed files with 90 additions and 63 deletions.
67 changes: 26 additions & 41 deletions pkg/fab/endpointconfig.go
Expand Up @@ -128,15 +128,7 @@ func (c *EndpointConfig) OrderersConfig() []fab.OrdererConfig {

// OrdererConfig returns the requested orderer
func (c *EndpointConfig) OrdererConfig(nameOrURL string) (*fab.OrdererConfig, bool) {

matchingOrdererConfig := c.tryMatchingOrdererConfig(nameOrURL, true)

if matchingOrdererConfig == nil {
logger.Debugf("Could not find Orderer for [%s] ", nameOrURL)
return nil, false
}

return matchingOrdererConfig, true
return c.tryMatchingOrdererConfig(nameOrURL, true)
}

// PeersConfig Retrieves the fabric peers for the specified org from the
Expand All @@ -148,14 +140,7 @@ func (c *EndpointConfig) PeersConfig(org string) ([]fab.PeerConfig, bool) {

// PeerConfig Retrieves a specific peer from the configuration by name or url
func (c *EndpointConfig) PeerConfig(nameOrURL string) (*fab.PeerConfig, bool) {

matchPeerConfig := c.tryMatchingPeerConfig(nameOrURL, true)
if matchPeerConfig == nil {
logger.Debugf("Could not find Peer for [%s] ", nameOrURL)
return nil, false
}

return matchPeerConfig, true
return c.tryMatchingPeerConfig(nameOrURL, true)
}

// NetworkConfig returns the network configuration defined in the config file
Expand Down Expand Up @@ -914,8 +899,8 @@ func (c *EndpointConfig) loadPeerConfigsByOrg() {
peers := []fab.PeerConfig{}

for _, peerName := range orgPeers {
p := c.tryMatchingPeerConfig(peerName, false)
if p == nil {
p, ok := c.tryMatchingPeerConfig(peerName, false)
if !ok {
continue
}

Expand Down Expand Up @@ -953,8 +938,8 @@ func (c *EndpointConfig) loadOrdererConfigs() error {
ordererConfigs := []fab.OrdererConfig{}
for name := range c.networkConfig.Orderers {

matchedOrderer := c.tryMatchingOrdererConfig(name, false)
if matchedOrderer == nil {
matchedOrderer, ok := c.tryMatchingOrdererConfig(name, false)
if !ok {
continue
}

Expand All @@ -979,8 +964,8 @@ func (c *EndpointConfig) loadChannelPeers() error {
for channelID, channelConfig := range c.networkConfig.Channels {
peers := []fab.ChannelPeer{}
for peerName, chPeerConfig := range channelConfig.Peers {
p := c.tryMatchingPeerConfig(strings.ToLower(peerName), false)
if p == nil {
p, ok := c.tryMatchingPeerConfig(strings.ToLower(peerName), false)
if !ok {
continue
}

Expand Down Expand Up @@ -1016,8 +1001,8 @@ func (c *EndpointConfig) loadChannelOrderers() error {
orderers := []fab.OrdererConfig{}
for _, ordererName := range channelConfig.Orderers {

orderer := c.tryMatchingOrdererConfig(strings.ToLower(ordererName), false)
if orderer == nil {
orderer, ok := c.tryMatchingOrdererConfig(strings.ToLower(ordererName), false)
if !ok {
return errors.Errorf("Could not find Orderer Config for channel orderer [%s]", ordererName)
}
orderers = append(orderers, *orderer)
Expand Down Expand Up @@ -1102,7 +1087,7 @@ func (c *EndpointConfig) isOrdererToBeIgnored(ordererName string) bool {
return false
}

func (c *EndpointConfig) tryMatchingPeerConfig(peerSearchKey string, searchByURL bool) *fab.PeerConfig {
func (c *EndpointConfig) tryMatchingPeerConfig(peerSearchKey string, searchByURL bool) (*fab.PeerConfig, bool) {

//loop over peer entity matchers to find the matching peer
for _, matcher := range c.peerMatchers {
Expand All @@ -1115,14 +1100,14 @@ func (c *EndpointConfig) tryMatchingPeerConfig(peerSearchKey string, searchByURL
//direct lookup if peer matchers are not configured or no matchers matched
peerConfig, ok := c.networkConfig.Peers[strings.ToLower(peerSearchKey)]
if ok {
return &peerConfig
return &peerConfig, true
}

if searchByURL {
//lookup by URL
for _, staticPeerConfig := range c.networkConfig.Peers {
if strings.EqualFold(staticPeerConfig.URL, peerSearchKey) {
return &staticPeerConfig
return &staticPeerConfig, true
}
}
}
Expand All @@ -1136,22 +1121,22 @@ func (c *EndpointConfig) tryMatchingPeerConfig(peerSearchKey string, searchByURL
// }
//}

return nil
return nil, false
}

func (c *EndpointConfig) matchPeer(peerSearchKey string, matcher matcherEntry) *fab.PeerConfig {
func (c *EndpointConfig) matchPeer(peerSearchKey string, matcher matcherEntry) (*fab.PeerConfig, bool) {

if matcher.matchConfig.IgnoreEndpoint {
logger.Debugf(" Ignoring peer `%s` since entity matcher IgnoreEndpoint flag is on", peerSearchKey)
return nil
return nil, false
}

mappedHost := c.regexMatchAndReplace(matcher.regex, peerSearchKey, matcher.matchConfig.MappedHost)

matchedPeer := c.getMappedPeer(mappedHost)
if matchedPeer == nil {
logger.Debugf("Could not find mapped host [%s] for peer [%s]", matcher.matchConfig.MappedHost, peerSearchKey)
return nil
return nil, false
}

//URLSubstitutionExp if found use from entity matcher otherwise use from mapped host
Expand All @@ -1174,7 +1159,7 @@ func (c *EndpointConfig) matchPeer(peerSearchKey string, matcher matcherEntry) *
matchedPeer.URL = c.getDefaultMatchingURL(peerSearchKey)
}

return matchedPeer
return matchedPeer, true
}

//getDefaultMatchingURL if search key is a URL then returns search key as URL otherwise returns empty
Expand Down Expand Up @@ -1206,7 +1191,7 @@ func (c *EndpointConfig) getMappedPeer(host string) *fab.PeerConfig {
return &mappedConfig
}

func (c *EndpointConfig) tryMatchingOrdererConfig(ordererSearchKey string, searchByURL bool) *fab.OrdererConfig {
func (c *EndpointConfig) tryMatchingOrdererConfig(ordererSearchKey string, searchByURL bool) (*fab.OrdererConfig, bool) {

//loop over orderer entity matchers to find the matching orderer
for _, matcher := range c.ordererMatchers {
Expand All @@ -1219,14 +1204,14 @@ func (c *EndpointConfig) tryMatchingOrdererConfig(ordererSearchKey string, searc
//direct lookup if orderer matchers are not configured or no matchers matched
orderer, ok := c.networkConfig.Orderers[strings.ToLower(ordererSearchKey)]
if ok {
return &orderer
return &orderer, true
}

if searchByURL {
//lookup by URL
for _, ordererCfg := range c.OrderersConfig() {
if strings.EqualFold(ordererCfg.URL, ordererSearchKey) {
return &ordererCfg
return &ordererCfg, true
}
}
}
Expand All @@ -1240,14 +1225,14 @@ func (c *EndpointConfig) tryMatchingOrdererConfig(ordererSearchKey string, searc
// }
//}

return nil
return nil, false
}

func (c *EndpointConfig) matchOrderer(ordererSearchKey string, matcher matcherEntry) *fab.OrdererConfig {
func (c *EndpointConfig) matchOrderer(ordererSearchKey string, matcher matcherEntry) (*fab.OrdererConfig, bool) {

if matcher.matchConfig.IgnoreEndpoint {
logger.Debugf(" Ignoring peer `%s` since entity matcher IgnoreEndpoint flag is on", ordererSearchKey)
return nil
return nil, false
}

mappedHost := c.regexMatchAndReplace(matcher.regex, ordererSearchKey, matcher.matchConfig.MappedHost)
Expand All @@ -1256,7 +1241,7 @@ func (c *EndpointConfig) matchOrderer(ordererSearchKey string, matcher matcherEn
matchedOrderer := c.getMappedOrderer(mappedHost)
if matchedOrderer == nil {
logger.Debugf("Could not find mapped host [%s] for orderer [%s]", matcher.matchConfig.MappedHost, ordererSearchKey)
return nil
return nil, false
}

//URLSubstitutionExp if found use from entity matcher otherwise use from mapped host
Expand All @@ -1274,7 +1259,7 @@ func (c *EndpointConfig) matchOrderer(ordererSearchKey string, matcher matcherEn
matchedOrderer.URL = c.getDefaultMatchingURL(ordererSearchKey)
}

return matchedOrderer
return matchedOrderer, true
}

func (c *EndpointConfig) getMappedOrderer(host string) *fab.OrdererConfig {
Expand Down
66 changes: 54 additions & 12 deletions pkg/fab/matchers_test.go
Expand Up @@ -405,29 +405,62 @@ func getBackendsFromFiles(files ...string) ([]core.ConfigBackend, error) {
// orderer excluded in orderer search by URL
// peer/orderer excluded in networkconfig
func TestMatchersIgnoreEndpoint(t *testing.T) {

//prepare backends for test
backends, err := getBackendsFromFiles(sampleMatchersIgnoreEndpoint, configTestFilePath)
assert.Nil(t, err, "not supposed to get error")
assert.Equal(t, 2, len(backends))

//get config from backend
config, err := ConfigFromBackend(backends...)
assert.Nil(t, err, "not supposed to get error")
assert.NotNil(t, config)

//Test if orderer excluded in channel orderers
testIgnoreEndpointChannelOrderers(t, config)

//Test if peer excluded in channel peers
testIgnoreEndpointChannelPeers(t, config)

//Test if orderer/peer excluded in channel config
testIgnoreEndpointChannelConfig(t, config)

//Test if peer excluded in org peers
testIgnoreEndpointOrgPeers(t, config)

//Test if peer excluded in network peers
testIgnoreEndpointNetworkPeers(t, config)

//Test if peer excluded in peer search by URL
testIgnoreEndpointPeerSearch(t, config)

//Test if orderer excluded in all orderers
testIgnoreEndpointAllOrderers(t, config)

//Test if orderer excluded in orderer search by name/URL
testIgnoreEndpointOrdererSearch(t, config)

//test NetworkConfig
testIgnoreEndpointNetworkConfig(t, config)
}

func testIgnoreEndpointChannelOrderers(t *testing.T, config fab.EndpointConfig) {
orderers, ok := config.ChannelOrderers(testChannelID)
assert.True(t, ok)
assert.NotEmpty(t, orderers)
assert.Equal(t, 1, len(orderers))
checkOrdererConfigExcluded(orderers, "orderer.exclude.example.com", t)
}

//Test if peer excluded in channel peers
func testIgnoreEndpointChannelPeers(t *testing.T, config fab.EndpointConfig) {
channelPeers, ok := config.ChannelPeers(testChannelID)
assert.True(t, ok)
assert.NotEmpty(t, channelPeers)
assert.Equal(t, 2, len(channelPeers))
checkChannelPeerExcluded(channelPeers, "peer1.org", t)
}

//Test if orderer/peer excluded in channel config
func testIgnoreEndpointChannelConfig(t *testing.T, config fab.EndpointConfig) {
chNwConfig, ok := config.ChannelConfig(testChannelID)
assert.True(t, ok)
assert.NotNil(t, chNwConfig)
Expand All @@ -437,10 +470,11 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
_, ok = chNwConfig.Peers["peer1.org2.example.com"]
assert.False(t, ok, "should not have excluded peer's entry in channel network config")
assert.NotEmpty(t, chNwConfig.Orderers)
assert.Equal(t, 1, len(orderers))
assert.NotEqual(t, "orderer.exclude.example.com", orderers[0])
assert.Equal(t, 1, len(chNwConfig.Orderers))
assert.NotEqual(t, "orderer.exclude.example.com", chNwConfig.Orderers[0])
}

//Test if peer excluded in org peers
func testIgnoreEndpointOrgPeers(t *testing.T, config fab.EndpointConfig) {
// test org 1 peers
orgPeers, ok := config.PeersConfig("org1")
assert.True(t, ok)
Expand All @@ -452,11 +486,15 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
assert.True(t, ok)
assert.NotEmpty(t, orgPeers)
checkPeerConfigExcluded(orgPeers, "peer1.org2", t)
}

//Test if peer excluded in network peers
func testIgnoreEndpointNetworkPeers(t *testing.T, config fab.EndpointConfig) {
nwPeers := config.NetworkPeers()
assert.NotEmpty(t, nwPeers)
checkNetworkPeerExcluded(nwPeers, "peer1.org", t)
}

func testIgnoreEndpointPeerSearch(t *testing.T, config fab.EndpointConfig) {

//Test if peer excluded in peer search by URL
peerConfig, ok := config.PeerConfig("peer1.org1.example.com:7151")
Expand Down Expand Up @@ -492,15 +530,18 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
peerConfig, ok = config.PeerConfig("peer0.org2.example.com")
assert.True(t, ok)
assert.NotNil(t, peerConfig)
}

//Test if orderer excluded in all orderers

func testIgnoreEndpointAllOrderers(t *testing.T, config fab.EndpointConfig) {
ordererConfigs := config.OrderersConfig()
assert.True(t, ok)
assert.NotEmpty(t, ordererConfigs)
checkOrdererConfigExcluded(ordererConfigs, "orderer.exclude.", t)
}

func testIgnoreEndpointOrdererSearch(t *testing.T, config fab.EndpointConfig) {

//Test if orderer excluded in orderer search by name

ordererConfig, ok := config.OrdererConfig("orderer.exclude.example.com")
assert.False(t, ok)
assert.Nil(t, ordererConfig)
Expand Down Expand Up @@ -536,12 +577,14 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
assert.False(t, ok)
assert.Nil(t, ordererConfig)

//test NetworkConfig
}

func testIgnoreEndpointNetworkConfig(t *testing.T, config fab.EndpointConfig) {
networkConfig := config.NetworkConfig()
assert.NotNil(t, networkConfig)
assert.Equal(t, 2, len(networkConfig.Peers))
assert.Equal(t, 1, len(networkConfig.Orderers))
_, ok = networkConfig.Peers["peer1.org1.example.com"]
_, ok := networkConfig.Peers["peer1.org1.example.com"]
assert.False(t, ok)
_, ok = networkConfig.Peers["peer1.org2.example.com"]
assert.False(t, ok)
Expand All @@ -553,7 +596,6 @@ func TestMatchersIgnoreEndpoint(t *testing.T) {
assert.False(t, ok)
_, ok = networkConfig.Orderers["orderer.example.com"]
assert.True(t, ok)

}

func checkOrdererConfigExcluded(ordererConfigs []fab.OrdererConfig, excluded string, t *testing.T) {
Expand Down

0 comments on commit 227d4ed

Please sign in to comment.