github.com/kchristidis/fabric@v1.0.4-0.20171028114726-837acd08cde1/peer/channel/list_test.go (about) 1 /* 2 Copyright IBM Corp. 2016 All Rights Reserved. 3 4 Licensed under the Apache License, Version 2.0 (the "License"); 5 you may not use this file except in compliance with the License. 6 You may obtain a copy of the License at 7 8 http://www.apache.org/licenses/LICENSE-2.0 9 10 Unless required by applicable law or agreed to in writing, software 11 distributed under the License is distributed on an "AS IS" BASIS, 12 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 See the License for the specific language governing permissions and 14 limitations under the License. 15 */ 16 17 package channel 18 19 import ( 20 "errors" 21 "testing" 22 23 "github.com/golang/protobuf/proto" 24 "github.com/hyperledger/fabric/msp" 25 "github.com/hyperledger/fabric/peer/common" 26 pb "github.com/hyperledger/fabric/protos/peer" 27 "github.com/stretchr/testify/assert" 28 ) 29 30 func TestListChannels(t *testing.T) { 31 InitMSP() 32 33 mockChannelResponse := &pb.ChannelQueryResponse{ 34 Channels: []*pb.ChannelInfo{{ 35 ChannelId: "TEST_LIST_CHANNELS", 36 }}, 37 } 38 39 mockPayload, err := proto.Marshal(mockChannelResponse) 40 assert.NoError(t, err) 41 42 mockResponse := &pb.ProposalResponse{ 43 Response: &pb.Response{ 44 Status: 200, 45 Payload: mockPayload, 46 }, 47 Endorsement: &pb.Endorsement{}, 48 } 49 50 signer, err := common.GetDefaultSigner() 51 assert.NoError(t, err) 52 53 mockCF := &ChannelCmdFactory{ 54 EndorserClient: common.GetMockEndorserClient(mockResponse, nil), 55 BroadcastFactory: mockBroadcastClientFactory, 56 Signer: signer, 57 } 58 59 cmd := listCmd(mockCF) 60 AddFlags(cmd) 61 if err := cmd.Execute(); err != nil { 62 t.Fail() 63 t.Error(err) 64 } 65 66 testListChannelsEmptyCF(t, mockCF) 67 } 68 69 func testListChannelsEmptyCF(t *testing.T, mockCF *ChannelCmdFactory) { 70 cmd := listCmd(nil) 71 AddFlags(cmd) 72 73 // Error case 1: no orderer endpoints 74 getEndorserClient := common.GetEndorserClientFnc 75 getBroadcastClient := common.GetBroadcastClientFnc 76 getDefaultSigner := common.GetDefaultSignerFnc 77 defer func() { 78 common.GetEndorserClientFnc = getEndorserClient 79 common.GetBroadcastClientFnc = getBroadcastClient 80 common.GetDefaultSignerFnc = getDefaultSigner 81 }() 82 common.GetDefaultSignerFnc = func() (msp.SigningIdentity, error) { 83 return nil, errors.New("error") 84 } 85 common.GetEndorserClientFnc = func() (pb.EndorserClient, error) { 86 return mockCF.EndorserClient, nil 87 } 88 common.GetBroadcastClientFnc = func(orderingEndpoint string, tlsEnabled bool, caFile string) (common.BroadcastClient, error) { 89 broadcastClient := common.GetMockBroadcastClient(nil) 90 return broadcastClient, nil 91 } 92 93 err := cmd.Execute() 94 assert.Error(t, err, "Error expected because GetDefaultSignerFnc returns an error") 95 96 common.GetDefaultSignerFnc = getDefaultSigner 97 common.GetEndorserClientFnc = func() (pb.EndorserClient, error) { 98 return nil, errors.New("error") 99 } 100 err = cmd.Execute() 101 assert.Error(t, err, "Error expected because GetEndorserClientFnc returns an error") 102 103 common.GetEndorserClientFnc = func() (pb.EndorserClient, error) { 104 return mockCF.EndorserClient, nil 105 } 106 err = cmd.Execute() 107 assert.NoError(t, err, "Error occurred while executing list command") 108 }