github.com/true-sqn/fabric@v2.1.1+incompatible/core/deliverservice/deliveryclient_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package deliverservice
     8  
     9  import (
    10  	"fmt"
    11  	"testing"
    12  	"time"
    13  
    14  	"github.com/hyperledger/fabric/core/deliverservice/fake"
    15  	"github.com/hyperledger/fabric/internal/pkg/comm"
    16  	"github.com/hyperledger/fabric/internal/pkg/peer/blocksprovider"
    17  
    18  	"github.com/stretchr/testify/assert"
    19  	"github.com/stretchr/testify/require"
    20  )
    21  
    22  //go:generate counterfeiter -o fake/ledger_info.go --fake-name LedgerInfo . ledgerInfo
    23  type ledgerInfo interface {
    24  	blocksprovider.LedgerInfo
    25  }
    26  
    27  func TestStartDeliverForChannel(t *testing.T) {
    28  	fakeLedgerInfo := &fake.LedgerInfo{}
    29  	fakeLedgerInfo.LedgerHeightReturns(0, fmt.Errorf("fake-ledger-error"))
    30  
    31  	grpcClient, err := comm.NewGRPCClient(comm.ClientConfig{
    32  		SecOpts: comm.SecureOptions{
    33  			UseTLS:            true,
    34  			RequireClientCert: true,
    35  			// The below certificates were taken from the peer TLS
    36  			// dir as output by cryptogen.
    37  			// They are server.crt and server.key respectively.
    38  			Certificate: []byte(`-----BEGIN CERTIFICATE-----
    39  MIIChTCCAiygAwIBAgIQOrr7/tDzKhhCba04E6QVWzAKBggqhkjOPQQDAjB2MQsw
    40  CQYDVQQGEwJVUzETMBEGA1UECBMKQ2FsaWZvcm5pYTEWMBQGA1UEBxMNU2FuIEZy
    41  YW5jaXNjbzEZMBcGA1UEChMQb3JnMS5leGFtcGxlLmNvbTEfMB0GA1UEAxMWdGxz
    42  Y2Eub3JnMS5leGFtcGxlLmNvbTAeFw0xOTA4MjcyMDA2MDBaFw0yOTA4MjQyMDA2
    43  MDBaMFsxCzAJBgNVBAYTAlVTMRMwEQYDVQQIEwpDYWxpZm9ybmlhMRYwFAYDVQQH
    44  Ew1TYW4gRnJhbmNpc2NvMR8wHQYDVQQDExZwZWVyMC5vcmcxLmV4YW1wbGUuY29t
    45  MFkwEwYHKoZIzj0CAQYIKoZIzj0DAQcDQgAExglppLxiAYSasrdFsrZJDxRULGBb
    46  wHlArrap9SmAzGIeeIuqe9t3F23Q5Jry9lAnIh8h3UlkvZZpClXcjRiCeqOBtjCB
    47  szAOBgNVHQ8BAf8EBAMCBaAwHQYDVR0lBBYwFAYIKwYBBQUHAwEGCCsGAQUFBwMC
    48  MAwGA1UdEwEB/wQCMAAwKwYDVR0jBCQwIoAgL35aqafj6SNnWdI4aMLh+oaFJvsA
    49  aoHgYMkcPvvkiWcwRwYDVR0RBEAwPoIWcGVlcjAub3JnMS5leGFtcGxlLmNvbYIF
    50  cGVlcjCCFnBlZXIwLm9yZzEuZXhhbXBsZS5jb22CBXBlZXIwMAoGCCqGSM49BAMC
    51  A0cAMEQCIAiAGoYeKPMd3bqtixZji8q2zGzLmIzq83xdTJoZqm50AiAKleso2EVi
    52  2TwsekWGpMaCOI6JV1+ZONyti6vBChhUYg==
    53  -----END CERTIFICATE-----`),
    54  			Key: []byte(`-----BEGIN PRIVATE KEY-----
    55  MIGHAgEAMBMGByqGSM49AgEGCCqGSM49AwEHBG0wawIBAQQgxiyAFyD0Eg1NxjbS
    56  U2EKDLoTQr3WPK8z7WyeOSzr+GGhRANCAATGCWmkvGIBhJqyt0WytkkPFFQsYFvA
    57  eUCutqn1KYDMYh54i6p723cXbdDkmvL2UCciHyHdSWS9lmkKVdyNGIJ6
    58  -----END PRIVATE KEY-----`,
    59  			),
    60  		},
    61  	})
    62  	require.NoError(t, err)
    63  
    64  	t.Run("Green Path With Mutual TLS", func(t *testing.T) {
    65  		ds := NewDeliverService(&Config{
    66  			DeliverGRPCClient:    grpcClient,
    67  			DeliverServiceConfig: &DeliverServiceConfig{},
    68  		}).(*deliverServiceImpl)
    69  
    70  		finalized := make(chan struct{})
    71  		err := ds.StartDeliverForChannel("channel-id", fakeLedgerInfo, func() {
    72  			close(finalized)
    73  		})
    74  		require.NoError(t, err)
    75  
    76  		select {
    77  		case <-finalized:
    78  		case <-time.After(time.Second):
    79  			assert.FailNow(t, "finalizer should have executed")
    80  		}
    81  
    82  		bp, ok := ds.blockProviders["channel-id"]
    83  		require.True(t, ok, "map entry must exist")
    84  		assert.Equal(t, "76f7a03f8dfdb0ef7c4b28b3901fe163c730e906c70e4cdf887054ad5f608bed", fmt.Sprintf("%x", bp.TLSCertHash))
    85  	})
    86  
    87  	t.Run("Green Path without mutual TLS", func(t *testing.T) {
    88  		grpcClient, err := comm.NewGRPCClient(comm.ClientConfig{
    89  			SecOpts: comm.SecureOptions{
    90  				UseTLS: true,
    91  			},
    92  		})
    93  		require.NoError(t, err)
    94  
    95  		ds := NewDeliverService(&Config{
    96  			DeliverGRPCClient:    grpcClient,
    97  			DeliverServiceConfig: &DeliverServiceConfig{},
    98  		}).(*deliverServiceImpl)
    99  
   100  		finalized := make(chan struct{})
   101  		err = ds.StartDeliverForChannel("channel-id", fakeLedgerInfo, func() {
   102  			close(finalized)
   103  		})
   104  		require.NoError(t, err)
   105  
   106  		select {
   107  		case <-finalized:
   108  		case <-time.After(time.Second):
   109  			assert.FailNow(t, "finalizer should have executed")
   110  		}
   111  
   112  		bp, ok := ds.blockProviders["channel-id"]
   113  		require.True(t, ok, "map entry must exist")
   114  		assert.Nil(t, bp.TLSCertHash)
   115  	})
   116  
   117  	t.Run("Exists", func(t *testing.T) {
   118  		ds := NewDeliverService(&Config{
   119  			DeliverGRPCClient:    grpcClient,
   120  			DeliverServiceConfig: &DeliverServiceConfig{},
   121  		}).(*deliverServiceImpl)
   122  
   123  		err = ds.StartDeliverForChannel("channel-id", fakeLedgerInfo, func() {})
   124  		require.NoError(t, err)
   125  
   126  		err = ds.StartDeliverForChannel("channel-id", fakeLedgerInfo, func() {})
   127  		assert.EqualError(t, err, "Delivery service - block provider already exists for channel-id found, can't start delivery")
   128  	})
   129  
   130  	t.Run("Stopping", func(t *testing.T) {
   131  		ds := NewDeliverService(&Config{
   132  			DeliverGRPCClient:    grpcClient,
   133  			DeliverServiceConfig: &DeliverServiceConfig{},
   134  		}).(*deliverServiceImpl)
   135  
   136  		ds.Stop()
   137  
   138  		err = ds.StartDeliverForChannel("channel-id", fakeLedgerInfo, func() {})
   139  		assert.EqualError(t, err, "Delivery service is stopping cannot join a new channel channel-id")
   140  	})
   141  }
   142  
   143  func TestStopDeliverForChannel(t *testing.T) {
   144  	t.Run("Green path", func(t *testing.T) {
   145  		ds := NewDeliverService(&Config{}).(*deliverServiceImpl)
   146  		doneA := make(chan struct{})
   147  		ds.blockProviders = map[string]*blocksprovider.Deliverer{
   148  			"a": {
   149  				DoneC: doneA,
   150  			},
   151  			"b": {
   152  				DoneC: make(chan struct{}),
   153  			},
   154  		}
   155  		err := ds.StopDeliverForChannel("a")
   156  		assert.NoError(t, err)
   157  		assert.Len(t, ds.blockProviders, 1)
   158  		_, ok := ds.blockProviders["a"]
   159  		assert.False(t, ok)
   160  		select {
   161  		case <-doneA:
   162  		default:
   163  			assert.Fail(t, "should have stopped the blocksprovider")
   164  		}
   165  	})
   166  
   167  	t.Run("Already stopping", func(t *testing.T) {
   168  		ds := NewDeliverService(&Config{}).(*deliverServiceImpl)
   169  		ds.blockProviders = map[string]*blocksprovider.Deliverer{
   170  			"a": {
   171  				DoneC: make(chan struct{}),
   172  			},
   173  			"b": {
   174  				DoneC: make(chan struct{}),
   175  			},
   176  		}
   177  
   178  		ds.Stop()
   179  		err := ds.StopDeliverForChannel("a")
   180  		assert.EqualError(t, err, "Delivery service is stopping, cannot stop delivery for channel a")
   181  	})
   182  
   183  	t.Run("Non-existent", func(t *testing.T) {
   184  		ds := NewDeliverService(&Config{}).(*deliverServiceImpl)
   185  		ds.blockProviders = map[string]*blocksprovider.Deliverer{
   186  			"a": {
   187  				DoneC: make(chan struct{}),
   188  			},
   189  			"b": {
   190  				DoneC: make(chan struct{}),
   191  			},
   192  		}
   193  
   194  		err := ds.StopDeliverForChannel("c")
   195  		assert.EqualError(t, err, "Delivery service - no block provider for c found, can't stop delivery")
   196  	})
   197  }
   198  
   199  func TestStop(t *testing.T) {
   200  	ds := NewDeliverService(&Config{}).(*deliverServiceImpl)
   201  	ds.blockProviders = map[string]*blocksprovider.Deliverer{
   202  		"a": {
   203  			DoneC: make(chan struct{}),
   204  		},
   205  		"b": {
   206  			DoneC: make(chan struct{}),
   207  		},
   208  	}
   209  	assert.False(t, ds.stopping)
   210  	for _, bp := range ds.blockProviders {
   211  		select {
   212  		case <-bp.DoneC:
   213  			assert.Fail(t, "block providers should not be closed")
   214  		default:
   215  		}
   216  	}
   217  
   218  	ds.Stop()
   219  	assert.True(t, ds.stopping)
   220  	assert.Len(t, ds.blockProviders, 2)
   221  	for _, bp := range ds.blockProviders {
   222  		select {
   223  		case <-bp.DoneC:
   224  		default:
   225  			assert.Fail(t, "block providers should te closed")
   226  		}
   227  	}
   228  
   229  }