github.com/lzy4123/fabric@v2.1.1+incompatible/internal/peer/lifecycle/chaincode/client_connections_test.go (about) 1 /* 2 Copyright IBM Corp. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package chaincode 8 9 import ( 10 "testing" 11 12 "github.com/hyperledger/fabric/bccsp/sw" 13 "github.com/stretchr/testify/assert" 14 ) 15 16 func TestNewClientConnections(t *testing.T) { 17 assert := assert.New(t) 18 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 19 assert.Nil(err) 20 21 t.Run("bad connection profile", func(t *testing.T) { 22 input := &ClientConnectionsInput{ 23 CommandName: "install", 24 EndorserRequired: true, 25 ConnectionProfilePath: "testdata/connectionprofile-bad.yaml", 26 } 27 28 c, err := NewClientConnections(input, cryptoProvider) 29 assert.Nil(c) 30 assert.Error(err) 31 assert.Contains(err.Error(), "failed to validate peer connection parameters: error unmarshaling YAML") 32 }) 33 34 t.Run("uneven connection profile", func(t *testing.T) { 35 input := &ClientConnectionsInput{ 36 CommandName: "install", 37 ChannelID: "mychannel", 38 EndorserRequired: true, 39 ConnectionProfilePath: "testdata/connectionprofile-uneven.yaml", 40 } 41 42 c, err := NewClientConnections(input, cryptoProvider) 43 assert.Nil(c) 44 assert.Error(err) 45 assert.EqualError(err, "failed to validate peer connection parameters: peer 'peer0.org2.example.com' is defined in the channel config but doesn't have associated peer config") 46 }) 47 48 t.Run("good connection profile - two peers", func(t *testing.T) { 49 input := &ClientConnectionsInput{ 50 CommandName: "approveformyorg", 51 ChannelID: "mychannel", 52 EndorserRequired: true, 53 ConnectionProfilePath: "testdata/connectionprofile.yaml", 54 } 55 56 c, err := NewClientConnections(input, cryptoProvider) 57 assert.Nil(c) 58 assert.Error(err) 59 assert.Contains(err.Error(), "failed to retrieve endorser client") 60 }) 61 62 t.Run("more than one peer not allowed", func(t *testing.T) { 63 input := &ClientConnectionsInput{ 64 CommandName: "install", 65 EndorserRequired: true, 66 PeerAddresses: []string{"testing123", "testing321"}, 67 } 68 69 c, err := NewClientConnections(input, cryptoProvider) 70 assert.Nil(c) 71 assert.Error(err) 72 assert.EqualError(err, "failed to validate peer connection parameters: 'install' command supports one peer. 2 peers provided") 73 }) 74 75 t.Run("more TLS root cert files than peer addresses and TLS enabled", func(t *testing.T) { 76 input := &ClientConnectionsInput{ 77 CommandName: "install", 78 EndorserRequired: true, 79 PeerAddresses: []string{"testing123"}, 80 TLSRootCertFiles: []string{"123testing", "321testing"}, 81 TLSEnabled: true, 82 } 83 84 c, err := NewClientConnections(input, cryptoProvider) 85 assert.Nil(c) 86 assert.Error(err) 87 assert.EqualError(err, "failed to validate peer connection parameters: number of peer addresses (1) does not match the number of TLS root cert files (2)") 88 }) 89 90 t.Run("failure connecting to endorser - TLS enabled", func(t *testing.T) { 91 input := &ClientConnectionsInput{ 92 CommandName: "install", 93 EndorserRequired: true, 94 PeerAddresses: []string{"testing123"}, 95 TLSRootCertFiles: []string{"123testing"}, 96 TLSEnabled: true, 97 } 98 99 c, err := NewClientConnections(input, cryptoProvider) 100 assert.Nil(c) 101 assert.Error(err) 102 assert.Contains(err.Error(), "failed to retrieve endorser client") 103 }) 104 105 t.Run("failure connecting to endorser - TLS disabled", func(t *testing.T) { 106 input := &ClientConnectionsInput{ 107 CommandName: "install", 108 EndorserRequired: true, 109 PeerAddresses: []string{"testing123"}, 110 TLSRootCertFiles: []string{"123testing"}, 111 TLSEnabled: false, 112 } 113 114 c, err := NewClientConnections(input, cryptoProvider) 115 assert.Nil(c) 116 assert.Error(err) 117 assert.Contains(err.Error(), "failed to retrieve endorser client") 118 }) 119 120 t.Run("no endorser clients - programming bug", func(t *testing.T) { 121 input := &ClientConnectionsInput{ 122 CommandName: "install", 123 EndorserRequired: true, 124 } 125 126 c, err := NewClientConnections(input, cryptoProvider) 127 assert.Nil(c) 128 assert.Error(err) 129 assert.Contains(err.Error(), "no endorser clients retrieved") 130 }) 131 132 t.Run("failure connecting to orderer", func(t *testing.T) { 133 input := &ClientConnectionsInput{ 134 OrdererRequired: true, 135 OrderingEndpoint: "testing", 136 PeerAddresses: []string{"testing123"}, 137 TLSRootCertFiles: []string{"123testing"}, 138 } 139 140 c, err := NewClientConnections(input, cryptoProvider) 141 assert.Nil(c) 142 assert.Error(err) 143 assert.Contains(err.Error(), "cannot obtain orderer endpoint, empty endorser list") 144 }) 145 }