github.com/myafeier/fabric@v1.0.1-0.20170722181825-3a4b1f2bce86/gossip/service/join_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package service 8 9 import ( 10 "sync" 11 "testing" 12 "time" 13 14 "github.com/hyperledger/fabric/common/config" 15 "github.com/hyperledger/fabric/gossip/api" 16 "github.com/hyperledger/fabric/gossip/comm" 17 "github.com/hyperledger/fabric/gossip/common" 18 "github.com/hyperledger/fabric/gossip/discovery" 19 "github.com/hyperledger/fabric/gossip/util" 20 proto "github.com/hyperledger/fabric/protos/gossip" 21 "github.com/hyperledger/fabric/protos/peer" 22 "github.com/stretchr/testify/assert" 23 "github.com/stretchr/testify/mock" 24 ) 25 26 type secAdvMock struct { 27 } 28 29 func init() { 30 util.SetupTestLogging() 31 } 32 33 func (s *secAdvMock) OrgByPeerIdentity(identity api.PeerIdentityType) api.OrgIdentityType { 34 return api.OrgIdentityType(identity) 35 } 36 37 type gossipMock struct { 38 mock.Mock 39 } 40 41 func (*gossipMock) SuspectPeers(s api.PeerSuspector) { 42 panic("implement me") 43 } 44 45 func (*gossipMock) Send(msg *proto.GossipMessage, peers ...*comm.RemotePeer) { 46 panic("implement me") 47 } 48 49 func (*gossipMock) Peers() []discovery.NetworkMember { 50 panic("implement me") 51 } 52 53 func (*gossipMock) PeersOfChannel(common.ChainID) []discovery.NetworkMember { 54 panic("implement me") 55 } 56 57 func (*gossipMock) UpdateMetadata(metadata []byte) { 58 panic("implement me") 59 } 60 61 func (*gossipMock) UpdateChannelMetadata(metadata []byte, chainID common.ChainID) { 62 panic("implement me") 63 } 64 65 func (*gossipMock) Gossip(msg *proto.GossipMessage) { 66 panic("implement me") 67 } 68 69 func (*gossipMock) Accept(acceptor common.MessageAcceptor, passThrough bool) (<-chan *proto.GossipMessage, <-chan proto.ReceivedMessage) { 70 panic("implement me") 71 } 72 73 func (g *gossipMock) JoinChan(joinMsg api.JoinChannelMessage, chainID common.ChainID) { 74 g.Called(joinMsg, chainID) 75 } 76 77 func (*gossipMock) Stop() { 78 panic("implement me") 79 } 80 81 type appOrgMock struct { 82 id string 83 } 84 85 func (*appOrgMock) Name() string { 86 panic("implement me") 87 } 88 89 func (ao *appOrgMock) MSPID() string { 90 return ao.id 91 } 92 93 func (ao *appOrgMock) AnchorPeers() []*peer.AnchorPeer { 94 return []*peer.AnchorPeer{} 95 } 96 97 type configMock struct { 98 orgs2AppOrgs map[string]config.ApplicationOrg 99 } 100 101 func (*configMock) ChainID() string { 102 return "A" 103 } 104 105 func (c *configMock) Organizations() map[string]config.ApplicationOrg { 106 return c.orgs2AppOrgs 107 } 108 109 func (*configMock) Sequence() uint64 { 110 return 0 111 } 112 113 func TestJoinChannelConfig(t *testing.T) { 114 // Scenarios: The channel we're joining has a single org - Org0 115 // but our org ID is actually Org0MSP in the negative path 116 // and Org0 in the positive path 117 118 failChan := make(chan struct{}, 1) 119 g1SvcMock := &gossipMock{} 120 g1SvcMock.On("JoinChan", mock.Anything, mock.Anything).Run(func(_ mock.Arguments) { 121 failChan <- struct{}{} 122 }) 123 g1 := &gossipServiceImpl{secAdv: &secAdvMock{}, peerIdentity: api.PeerIdentityType("OrgMSP0"), gossipSvc: g1SvcMock} 124 g1.configUpdated(&configMock{ 125 orgs2AppOrgs: map[string]config.ApplicationOrg{ 126 "Org0": &appOrgMock{id: "Org0"}, 127 }, 128 }) 129 select { 130 case <-time.After(time.Second): 131 case <-failChan: 132 assert.Fail(t, "Joined a badly configured channel") 133 } 134 135 succChan := make(chan struct{}, 1) 136 g2SvcMock := &gossipMock{} 137 g2SvcMock.On("JoinChan", mock.Anything, mock.Anything).Run(func(_ mock.Arguments) { 138 succChan <- struct{}{} 139 }) 140 g2 := &gossipServiceImpl{secAdv: &secAdvMock{}, peerIdentity: api.PeerIdentityType("Org0"), gossipSvc: g2SvcMock} 141 g2.configUpdated(&configMock{ 142 orgs2AppOrgs: map[string]config.ApplicationOrg{ 143 "Org0": &appOrgMock{id: "Org0"}, 144 }, 145 }) 146 select { 147 case <-time.After(time.Second): 148 assert.Fail(t, "Didn't join a channel (should have done so within the time period)") 149 case <-succChan: 150 151 } 152 } 153 154 func TestJoinChannelNoAnchorPeers(t *testing.T) { 155 // Scenario: The channel we're joining has 2 orgs but no anchor peers 156 // The test ensures that JoinChan is called with a JoinChannelMessage with Members 157 // that consist of the organizations of the configuration given. 158 159 var joinChanCalled sync.WaitGroup 160 joinChanCalled.Add(1) 161 gMock := &gossipMock{} 162 gMock.On("JoinChan", mock.Anything, mock.Anything).Run(func(args mock.Arguments) { 163 defer joinChanCalled.Done() 164 jcm := args.Get(0).(api.JoinChannelMessage) 165 channel := args.Get(1).(common.ChainID) 166 assert.Len(t, jcm.Members(), 2) 167 assert.Contains(t, jcm.Members(), api.OrgIdentityType("Org0")) 168 assert.Contains(t, jcm.Members(), api.OrgIdentityType("Org1")) 169 assert.Equal(t, "A", string(channel)) 170 }) 171 172 g := &gossipServiceImpl{secAdv: &secAdvMock{}, peerIdentity: api.PeerIdentityType("Org0"), gossipSvc: gMock} 173 174 appOrg0 := &appOrgMock{id: "Org0"} 175 appOrg1 := &appOrgMock{id: "Org1"} 176 177 // Make sure the ApplicationOrgs really have no anchor peers 178 assert.Empty(t, appOrg0.AnchorPeers()) 179 assert.Empty(t, appOrg1.AnchorPeers()) 180 181 g.configUpdated(&configMock{ 182 orgs2AppOrgs: map[string]config.ApplicationOrg{ 183 "Org0": appOrg0, 184 "Org1": appOrg1, 185 }, 186 }) 187 joinChanCalled.Wait() 188 }