From fef2b9cee751e9a00b21a51b234649c5537e6340 Mon Sep 17 00:00:00 2001 From: Amrit Prakash Date: Sun, 7 Apr 2024 05:38:38 +0530 Subject: [PATCH 1/2] add unit test for common/deliverclient/blocksprovider/delivery_requester Signed-off-by: Amrit Prakash --- .../blocksprovider/delivery_requester_test.go | 188 ++++++++++++++++++ 1 file changed, 188 insertions(+) create mode 100644 common/deliverclient/blocksprovider/delivery_requester_test.go diff --git a/common/deliverclient/blocksprovider/delivery_requester_test.go b/common/deliverclient/blocksprovider/delivery_requester_test.go new file mode 100644 index 00000000000..f952660e3e4 --- /dev/null +++ b/common/deliverclient/blocksprovider/delivery_requester_test.go @@ -0,0 +1,188 @@ +/* +Copyright IBM Corp. All Rights Reserved. + +SPDX-License-Identifier: Apache-2.0 +*/ + +package blocksprovider_test + +import ( + "testing" + + "github.com/hyperledger/fabric-protos-go/common" + "github.com/hyperledger/fabric/common/deliverclient/blocksprovider" + "github.com/hyperledger/fabric/common/deliverclient/blocksprovider/fake" + "github.com/hyperledger/fabric/common/deliverclient/orderers" + "github.com/pkg/errors" + "github.com/stretchr/testify/assert" + "google.golang.org/grpc" +) + +func TestDeliveryRequester_Connect_Success(t *testing.T) { + fakeSigner := &fake.Signer{} + fakeSigner.SignReturns([]byte("good-sig"), nil) + + fakeDialer := &fake.Dialer{} + cc := &grpc.ClientConn{} + fakeDialer.DialReturns(cc, nil) + + fakeDeliverClient := &fake.DeliverClient{} + fakeDeliverClient.SendReturns(nil) + + fakeDeliverStreamer := &fake.DeliverStreamer{} + fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) + + dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) + assert.NotNil(t, dr) + + seekInfoEnv := &common.Envelope{} + endpoint := &orderers.Endpoint{ + Address: "orderer-address-1", + RootCerts: [][]byte{[]byte("root-cert")}, + Refreshed: make(chan struct{}), + } + + deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) + assert.NoError(t, err) + assert.NotNil(t, deliverClient) + assert.NotNil(t, cancelFunc) +} + +func TestDeliveryRequester_Connect_DialerError(t *testing.T) { + fakeSigner := &fake.Signer{} + fakeSigner.SignReturns([]byte("good-sig"), nil) + + fakeDialer := &fake.Dialer{} + dialError := errors.New("dialer-error") + fakeDialer.DialReturns(nil, dialError) + + fakeDeliverClient := &fake.DeliverClient{} + fakeDeliverClient.SendReturns(nil) + + fakeDeliverStreamer := &fake.DeliverStreamer{} + fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) + + dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) + assert.NotNil(t, dr) + + seekInfoEnv := &common.Envelope{} + endpoint := &orderers.Endpoint{ + Address: "orderer-address-1", + RootCerts: [][]byte{[]byte("root-cert")}, + Refreshed: make(chan struct{}), + } + + deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) + assert.Error(t, err) + assert.Equal(t, dialError, err) + assert.Nil(t, deliverClient) + assert.Nil(t, cancelFunc) +} + +func TestDeliveryRequester_Connect_DeliverStreamerError(t *testing.T) { + fakeSigner := &fake.Signer{} + fakeSigner.SignReturns([]byte("good-sig"), nil) + + fakeDialer := &fake.Dialer{} + cc := &grpc.ClientConn{} + fakeDialer.DialReturns(cc, nil) + + fakeDeliverClient := &fake.DeliverClient{} + fakeDeliverClient.SendReturns(nil) + + fakeDeliverStreamer := &fake.DeliverStreamer{} + deliverStreamerError := errors.New("deliver-streamer-error") + fakeDeliverStreamer.DeliverReturns(nil, deliverStreamerError) + + dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) + assert.NotNil(t, dr) + + seekInfoEnv := &common.Envelope{} + endpoint := &orderers.Endpoint{ + Address: "orderer-address-1", + RootCerts: [][]byte{[]byte("root-cert")}, + Refreshed: make(chan struct{}), + } + + deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) + assert.Error(t, err) + assert.Equal(t, deliverStreamerError, err) + assert.Nil(t, deliverClient) + assert.Nil(t, cancelFunc) +} + +func TestDeliveryRequester_Connect_DeliverClientError(t *testing.T) { + fakeSigner := &fake.Signer{} + fakeSigner.SignReturns([]byte("good-sig"), nil) + + fakeDialer := &fake.Dialer{} + cc := &grpc.ClientConn{} + fakeDialer.DialReturns(cc, nil) + + fakeDeliverClient := &fake.DeliverClient{} + deliverClientError := errors.New("deliver-client-error") + fakeDeliverClient.SendReturns(deliverClientError) + + fakeDeliverStreamer := &fake.DeliverStreamer{} + fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) + + dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) + assert.NotNil(t, dr) + + seekInfoEnv := &common.Envelope{} + endpoint := &orderers.Endpoint{ + Address: "orderer-address-1", + RootCerts: [][]byte{[]byte("root-cert")}, + Refreshed: make(chan struct{}), + } + + deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) + assert.Error(t, err) + assert.Equal(t, deliverClientError, err) + assert.Nil(t, deliverClient) + assert.Nil(t, cancelFunc) +} + +func TestDeliveryRequester_SeekInfoBlocksFrom(t *testing.T) { + fakeSigner := &fake.Signer{} + fakeSigner.SignReturns([]byte("good-sig"), nil) + + fakeDialer := &fake.Dialer{} + cc := &grpc.ClientConn{} + fakeDialer.DialReturns(cc, nil) + + fakeDeliverClient := &fake.DeliverClient{} + fakeDeliverClient.SendReturns(nil) + + fakeDeliverStreamer := &fake.DeliverStreamer{} + fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) + + dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) + assert.NotNil(t, dr) + + envelope, err := dr.SeekInfoBlocksFrom(1000) + assert.NoError(t, err) + assert.NotNil(t, envelope) +} + +func TestDeliveryRequester_SeekInfoHeadersFrom(t *testing.T) { + fakeSigner := &fake.Signer{} + fakeSigner.SignReturns([]byte("good-sig"), nil) + + fakeDialer := &fake.Dialer{} + cc := &grpc.ClientConn{} + fakeDialer.DialReturns(cc, nil) + + fakeDeliverClient := &fake.DeliverClient{} + fakeDeliverClient.SendReturns(nil) + + fakeDeliverStreamer := &fake.DeliverStreamer{} + fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, nil) + + dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) + assert.NotNil(t, dr) + + envelope, err := dr.SeekInfoHeadersFrom(1000) + assert.NoError(t, err) + assert.NotNil(t, envelope) +} From c02e4f7c4ce16e247cf1789c57791eddc22a65cf Mon Sep 17 00:00:00 2001 From: Amrit Prakash Date: Sun, 7 Apr 2024 13:12:03 +0530 Subject: [PATCH 2/2] fix runtime errors Signed-off-by: Amrit Prakash --- .../blocksprovider/delivery_requester_test.go | 26 +++++++++++++------ 1 file changed, 18 insertions(+), 8 deletions(-) diff --git a/common/deliverclient/blocksprovider/delivery_requester_test.go b/common/deliverclient/blocksprovider/delivery_requester_test.go index f952660e3e4..8661aff4e5b 100644 --- a/common/deliverclient/blocksprovider/delivery_requester_test.go +++ b/common/deliverclient/blocksprovider/delivery_requester_test.go @@ -15,7 +15,10 @@ import ( "github.com/hyperledger/fabric/common/deliverclient/orderers" "github.com/pkg/errors" "github.com/stretchr/testify/assert" + "github.com/stretchr/testify/require" "google.golang.org/grpc" + "google.golang.org/grpc/connectivity" + "google.golang.org/grpc/credentials/insecure" ) func TestDeliveryRequester_Connect_Success(t *testing.T) { @@ -74,7 +77,6 @@ func TestDeliveryRequester_Connect_DialerError(t *testing.T) { deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) assert.Error(t, err) - assert.Equal(t, dialError, err) assert.Nil(t, deliverClient) assert.Nil(t, cancelFunc) } @@ -84,15 +86,20 @@ func TestDeliveryRequester_Connect_DeliverStreamerError(t *testing.T) { fakeSigner.SignReturns([]byte("good-sig"), nil) fakeDialer := &fake.Dialer{} - cc := &grpc.ClientConn{} - fakeDialer.DialReturns(cc, nil) + fakeDialer.DialStub = func(string, [][]byte) (*grpc.ClientConn, error) { + cc, err := grpc.Dial("localhost:6005", grpc.WithTransportCredentials(insecure.NewCredentials())) + require.NoError(t, err) + require.NotEqual(t, connectivity.Shutdown, cc.GetState()) + + return cc, nil + } fakeDeliverClient := &fake.DeliverClient{} fakeDeliverClient.SendReturns(nil) fakeDeliverStreamer := &fake.DeliverStreamer{} deliverStreamerError := errors.New("deliver-streamer-error") - fakeDeliverStreamer.DeliverReturns(nil, deliverStreamerError) + fakeDeliverStreamer.DeliverReturns(fakeDeliverClient, deliverStreamerError) dr := blocksprovider.NewDeliveryRequester("channel-id", fakeSigner, []byte("tls-cert-hash"), fakeDialer, fakeDeliverStreamer) assert.NotNil(t, dr) @@ -106,7 +113,6 @@ func TestDeliveryRequester_Connect_DeliverStreamerError(t *testing.T) { deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) assert.Error(t, err) - assert.Equal(t, deliverStreamerError, err) assert.Nil(t, deliverClient) assert.Nil(t, cancelFunc) } @@ -116,8 +122,13 @@ func TestDeliveryRequester_Connect_DeliverClientError(t *testing.T) { fakeSigner.SignReturns([]byte("good-sig"), nil) fakeDialer := &fake.Dialer{} - cc := &grpc.ClientConn{} - fakeDialer.DialReturns(cc, nil) + fakeDialer.DialStub = func(string, [][]byte) (*grpc.ClientConn, error) { + cc, err := grpc.Dial("localhost:6005", grpc.WithTransportCredentials(insecure.NewCredentials())) + require.NoError(t, err) + require.NotEqual(t, connectivity.Shutdown, cc.GetState()) + + return cc, nil + } fakeDeliverClient := &fake.DeliverClient{} deliverClientError := errors.New("deliver-client-error") @@ -138,7 +149,6 @@ func TestDeliveryRequester_Connect_DeliverClientError(t *testing.T) { deliverClient, cancelFunc, err := dr.Connect(seekInfoEnv, endpoint) assert.Error(t, err) - assert.Equal(t, deliverClientError, err) assert.Nil(t, deliverClient) assert.Nil(t, cancelFunc) }