github.com/hechain20/hechain@v0.0.0-20220316014945-b544036ba106/internal/peer/snapshot/listpending_test.go (about) 1 /* 2 Copyright hechain. All Rights Reserved. 3 4 SPDX-License-Identifier: Apache-2.0 5 */ 6 7 package snapshot 8 9 import ( 10 "fmt" 11 "testing" 12 13 "github.com/hechain20/hechain/internal/peer/snapshot/mock" 14 pb "github.com/hyperledger/fabric-protos-go/peer" 15 "github.com/onsi/gomega/gbytes" 16 "github.com/stretchr/testify/require" 17 ) 18 19 func TestListPendingCmd(t *testing.T) { 20 mockSigner := &mock.Signer{} 21 mockSigner.SignReturns([]byte("snapshot-request-signature"), nil) 22 mockSnapshotClient := &mock.SnapshotClient{} 23 mockSnapshotClient.QueryPendingsReturns(&pb.QueryPendingSnapshotsResponse{BlockNumbers: []uint64{100, 200}}, nil) 24 buffer := gbytes.NewBuffer() 25 mockClient := &client{mockSnapshotClient, mockSigner, buffer} 26 27 resetFlags() 28 cmd := listPendingCmd(mockClient, nil) 29 cmd.SetArgs([]string{"-c", "mychannel"}) 30 err := cmd.Execute() 31 require.NoError(t, err) 32 require.Equal(t, []byte("Successfully got pending snapshot requests: [100 200]\n"), buffer.Contents()) 33 34 // error tests 35 mockSnapshotClient.QueryPendingsReturns(nil, fmt.Errorf("fake-querypendings-error")) 36 require.EqualError(t, cmd.Execute(), "failed to list pending requests: fake-querypendings-error") 37 38 mockSigner.SignReturns(nil, fmt.Errorf("fake-sign-error")) 39 require.EqualError(t, cmd.Execute(), "fake-sign-error") 40 41 mockSigner.SerializeReturns(nil, fmt.Errorf("fake-serialize-error")) 42 require.EqualError(t, cmd.Execute(), "fake-serialize-error") 43 44 resetFlags() 45 cmd.SetArgs([]string{}) 46 require.EqualError(t, cmd.Execute(), "the required parameter 'channelID' is empty. Rerun the command with -c flag") 47 }