github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/channel/joinbysnapshotstatus_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package channel 8 9 import ( 10 "testing" 11 "time" 12 13 "github.com/golang/protobuf/proto" 14 "github.com/hechain20/hechain/internal/peer/common" 15 "github.com/hechain20/hechain/protoutil" 16 pb "github.com/hyperledger/fabric-protos-go/peer" 17 "github.com/spf13/viper" 18 "github.com/stretchr/testify/require" 19 ) 20 21 func TestJoinBySnapshotStatus(t *testing.T) { 22 defer viper.Reset() 23 defer resetFlags() 24 25 InitMSP() 26 signer, err := common.GetDefaultSigner() 27 require.NoError(t, err) 28 29 joinBySnapshotStatus := &pb.JoinBySnapshotStatus{InProgress: false, BootstrappingSnapshotDir: ""} 30 mockResponse := &pb.ProposalResponse{ 31 Response: &pb.Response{Status: 200, Payload: protoutil.MarshalOrPanic(joinBySnapshotStatus)}, 32 Endorsement: &pb.Endorsement{}, 33 } 34 mockEndorserClient := common.GetMockEndorserClient(mockResponse, nil) 35 mockCF := &ChannelCmdFactory{ 36 EndorserClient: mockEndorserClient, 37 BroadcastFactory: mockBroadcastClientFactory, 38 Signer: signer, 39 } 40 41 // successful joinbysnapshotstatuscmd test 42 resetFlags() 43 cmd := joinBySnapshotStatusCmd(mockCF) 44 AddFlags(cmd) 45 require.NoError(t, cmd.Execute()) 46 47 // very joinbysnapshotstatus returns correct value 48 client := &endorserClient{mockCF} 49 status, err := client.joinBySnapshotStatus() 50 require.NoError(t, err) 51 require.True(t, proto.Equal(joinBySnapshotStatus, status)) 52 53 joinBySnapshotStatus = &pb.JoinBySnapshotStatus{InProgress: true, BootstrappingSnapshotDir: "mock_snapshot_directory"} 54 mockResponse.Response.Payload = protoutil.MarshalOrPanic(joinBySnapshotStatus) 55 status, err = client.joinBySnapshotStatus() 56 require.NoError(t, err) 57 require.True(t, proto.Equal(joinBySnapshotStatus, status)) 58 59 // negative test due to EndoserClient returning bad response 60 mockResponse.Response = &pb.Response{Status: 500, Message: "mock_bad_response"} 61 resetFlags() 62 cmd = joinBySnapshotStatusCmd(mockCF) 63 AddFlags(cmd) 64 err = cmd.Execute() 65 require.EqualError(t, err, "received bad response, status 500: mock_bad_response") 66 67 // negative test due to connection failure to endorser client 68 viper.Set("peer.client.connTimeout", 10*time.Millisecond) 69 resetFlags() 70 cmd = joinBySnapshotStatusCmd(nil) 71 AddFlags(cmd) 72 err = cmd.Execute() 73 require.Error(t, err) 74 require.Contains(t, err.Error(), "endorser client failed to connect to") 75 }