github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/lifecycle/chaincode/client_connections_test.go (about) 1 /* 2 Copyright hechain. 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/hechain20/hechain/bccsp/sw" 13 "github.com/stretchr/testify/require" 14 ) 15 16 func TestNewClientConnections(t *testing.T) { 17 require := require.New(t) 18 cryptoProvider, err := sw.NewDefaultSecurityLevelWithKeystore(sw.NewDummyKeyStore()) 19 require.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 require.Nil(c) 30 require.Error(err) 31 require.Contains(err.Error(), "failed to validate peer connection parameters: error unmarshalling YAML") 32 }) 33 34 t.Run("uneven connection profile", func(t *testing.T) { 35 input := &ClientConnectionsInput{ 36 CommandName: "commit", 37 ChannelID: "mychannel", 38 EndorserRequired: true, 39 ConnectionProfilePath: "testdata/connectionprofile-uneven.yaml", 40 } 41 42 c, err := NewClientConnections(input, cryptoProvider) 43 require.Nil(c) 44 require.Error(err) 45 require.EqualError(err, "failed to validate peer connection parameters: peer 'peer0.org2.example.com' 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 require.Nil(c) 58 require.Error(err) 59 require.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 require.Nil(c) 71 require.Error(err) 72 require.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 require.Nil(c) 86 require.Error(err) 87 require.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 require.Nil(c) 101 require.Error(err) 102 require.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 require.Nil(c) 116 require.Error(err) 117 require.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 require.Nil(c) 128 require.Error(err) 129 require.Contains(err.Error(), "no endorser clients retrieved") 130 }) 131 132 t.Run("install using connection profile", func(t *testing.T) { 133 input := &ClientConnectionsInput{ 134 CommandName: "install", 135 EndorserRequired: true, 136 ConnectionProfilePath: "testdata/connectionprofile.yaml", 137 TargetPeer: "peer0.org2.example.com", 138 } 139 140 c, err := NewClientConnections(input, cryptoProvider) 141 require.Nil(c) 142 require.Error(err) 143 require.Contains(err.Error(), "failed to retrieve endorser client") 144 }) 145 146 t.Run("install using connection profile - no target peer specified", func(t *testing.T) { 147 input := &ClientConnectionsInput{ 148 CommandName: "install", 149 EndorserRequired: true, 150 ConnectionProfilePath: "testdata/connectionprofile.yaml", 151 TargetPeer: "", 152 } 153 154 c, err := NewClientConnections(input, cryptoProvider) 155 require.Nil(c) 156 require.Error(err) 157 require.Contains(err.Error(), "failed to validate peer connection parameters: --targetPeer must be specified for channel-less operation using connection profile") 158 }) 159 160 t.Run("install using connection profile - target peer doesn't exist", func(t *testing.T) { 161 input := &ClientConnectionsInput{ 162 CommandName: "install", 163 EndorserRequired: true, 164 ConnectionProfilePath: "testdata/connectionprofile.yaml", 165 TargetPeer: "not-a-peer", 166 } 167 168 c, err := NewClientConnections(input, cryptoProvider) 169 require.Nil(c) 170 require.Error(err) 171 require.Contains(err.Error(), "failed to validate peer connection parameters: peer 'not-a-peer' doesn't have associated peer config") 172 }) 173 174 t.Run("failure connecting to orderer", func(t *testing.T) { 175 input := &ClientConnectionsInput{ 176 OrdererRequired: true, 177 OrderingEndpoint: "testing", 178 PeerAddresses: []string{"testing123"}, 179 TLSRootCertFiles: []string{"123testing"}, 180 } 181 182 c, err := NewClientConnections(input, cryptoProvider) 183 require.Nil(c) 184 require.Error(err) 185 require.Contains(err.Error(), "cannot obtain orderer endpoint, empty endorser list") 186 }) 187 }