github.com/true-sqn/fabric@v2.1.1+incompatible/internal/peer/node/start_test.go (about)

     1  /*
     2  Copyright Hitachi America, Ltd.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package node
     8  
     9  import (
    10  	"bytes"
    11  	"io/ioutil"
    12  	"os"
    13  	"strconv"
    14  	"testing"
    15  	"time"
    16  
    17  	"github.com/hyperledger/fabric-protos-go/common"
    18  	"github.com/hyperledger/fabric/core/handlers/library"
    19  	"github.com/hyperledger/fabric/core/testutil"
    20  	"github.com/hyperledger/fabric/internal/peer/node/mock"
    21  	msptesttools "github.com/hyperledger/fabric/msp/mgmt/testtools"
    22  	"github.com/mitchellh/mapstructure"
    23  	. "github.com/onsi/gomega"
    24  	"github.com/spf13/viper"
    25  	"github.com/stretchr/testify/assert"
    26  	"google.golang.org/grpc"
    27  )
    28  
    29  func TestStartCmd(t *testing.T) {
    30  	defer viper.Reset()
    31  	g := NewGomegaWithT(t)
    32  
    33  	tempDir, err := ioutil.TempDir("", "startcmd")
    34  	g.Expect(err).NotTo(HaveOccurred())
    35  	defer os.RemoveAll(tempDir)
    36  
    37  	viper.Set("peer.address", "localhost:6051")
    38  	viper.Set("peer.listenAddress", "0.0.0.0:6051")
    39  	viper.Set("peer.chaincodeListenAddress", "0.0.0.0:6052")
    40  	viper.Set("peer.fileSystemPath", tempDir)
    41  	viper.Set("chaincode.executetimeout", "30s")
    42  	viper.Set("chaincode.mode", "dev")
    43  	viper.Set("vm.endpoint", "unix:///var/run/docker.sock")
    44  
    45  	msptesttools.LoadMSPSetupForTesting()
    46  
    47  	go func() {
    48  		cmd := startCmd()
    49  		assert.NoError(t, cmd.Execute(), "expected to successfully start command")
    50  	}()
    51  
    52  	grpcProbe := func(addr string) bool {
    53  		c, err := grpc.Dial(addr, grpc.WithBlock(), grpc.WithInsecure())
    54  		if err == nil {
    55  			c.Close()
    56  			return true
    57  		}
    58  		return false
    59  	}
    60  	g.Eventually(grpcProbe("localhost:6051")).Should(BeTrue())
    61  }
    62  
    63  func TestHandlerMap(t *testing.T) {
    64  	config1 := `
    65    peer:
    66      handlers:
    67        authFilters:
    68          - name: filter1
    69            library: /opt/lib/filter1.so
    70          - name: filter2
    71    `
    72  	viper.SetConfigType("yaml")
    73  	err := viper.ReadConfig(bytes.NewBuffer([]byte(config1)))
    74  	assert.NoError(t, err)
    75  
    76  	var libConf library.Config
    77  	err = mapstructure.Decode(viper.Get("peer.handlers"), &libConf)
    78  	assert.NoError(t, err)
    79  	assert.Len(t, libConf.AuthFilters, 2, "expected two filters")
    80  	assert.Equal(t, "/opt/lib/filter1.so", libConf.AuthFilters[0].Library)
    81  	assert.Equal(t, "filter2", libConf.AuthFilters[1].Name)
    82  }
    83  
    84  func TestComputeChaincodeEndpoint(t *testing.T) {
    85  	var tests = []struct {
    86  		peerAddress            string
    87  		chaincodeAddress       string
    88  		chaincodeListenAddress string
    89  		expectedError          string
    90  		expectedEndpoint       string
    91  	}{
    92  		{
    93  			peerAddress:   "0.0.0.0",
    94  			expectedError: "invalid endpoint for chaincode to connect",
    95  		},
    96  		{
    97  			peerAddress:      "127.0.0.1",
    98  			expectedEndpoint: "127.0.0.1:7052",
    99  		},
   100  		{
   101  			peerAddress:            "0.0.0.0",
   102  			chaincodeListenAddress: "0.0.0.0:8052",
   103  			expectedError:          "invalid endpoint for chaincode to connect",
   104  		},
   105  		{
   106  			peerAddress:            "127.0.0.1",
   107  			chaincodeListenAddress: "0.0.0.0:8052",
   108  			expectedEndpoint:       "127.0.0.1:8052",
   109  		},
   110  		{
   111  			peerAddress:            "127.0.0.1",
   112  			chaincodeListenAddress: "127.0.0.1:8052",
   113  			expectedEndpoint:       "127.0.0.1:8052",
   114  		},
   115  		{
   116  			peerAddress:            "127.0.0.1",
   117  			chaincodeListenAddress: "abc",
   118  			expectedError:          "address abc: missing port in address",
   119  		},
   120  		{
   121  			peerAddress:      "127.0.0.1",
   122  			chaincodeAddress: "0.0.0.0:9052",
   123  			expectedError:    "invalid endpoint for chaincode to connect",
   124  		},
   125  		{
   126  			peerAddress:      "127.0.0.1",
   127  			chaincodeAddress: "127.0.0.2:9052",
   128  			expectedEndpoint: "127.0.0.2:9052",
   129  		},
   130  		{
   131  			peerAddress:            "127.0.0.1",
   132  			chaincodeAddress:       "bcd",
   133  			chaincodeListenAddress: "ignored",
   134  			expectedError:          "address bcd: missing port in address",
   135  		},
   136  		{
   137  			peerAddress:            "127.0.0.1",
   138  			chaincodeAddress:       "127.0.0.2:9052",
   139  			chaincodeListenAddress: "ignored",
   140  			expectedEndpoint:       "127.0.0.2:9052",
   141  		},
   142  	}
   143  
   144  	for i, tt := range tests {
   145  		t.Run(strconv.Itoa(i), func(t *testing.T) {
   146  			ccEndpoint, err := computeChaincodeEndpoint(tt.chaincodeAddress, tt.chaincodeListenAddress, tt.peerAddress)
   147  			if tt.expectedError != "" {
   148  				assert.EqualErrorf(t, err, tt.expectedError, "peerAddress: %q, ccListenAddr: %q, ccAddr: %q", tt.peerAddress, tt.chaincodeListenAddress, tt.chaincodeAddress)
   149  				return
   150  			}
   151  			assert.NoErrorf(t, err, "peerAddress: %q, ccListenAddr: %q, ccAddr: %q", tt.peerAddress, tt.chaincodeListenAddress, tt.chaincodeAddress)
   152  			assert.Equalf(t, tt.expectedEndpoint, ccEndpoint, "peerAddress: %q, ccListenAddr: %q, ccAddr: %q", tt.peerAddress, tt.chaincodeListenAddress, tt.chaincodeAddress)
   153  		})
   154  	}
   155  }
   156  
   157  func TestGetDockerHostConfig(t *testing.T) {
   158  	testutil.SetupTestConfig()
   159  	hostConfig := getDockerHostConfig()
   160  	assert.NotNil(t, hostConfig)
   161  	assert.Equal(t, "host", hostConfig.NetworkMode)
   162  	assert.Equal(t, "json-file", hostConfig.LogConfig.Type)
   163  	assert.Equal(t, "50m", hostConfig.LogConfig.Config["max-size"])
   164  	assert.Equal(t, "5", hostConfig.LogConfig.Config["max-file"])
   165  	assert.Equal(t, int64(1024*1024*1024*2), hostConfig.Memory)
   166  	assert.Equal(t, int64(0), hostConfig.CPUShares)
   167  }
   168  
   169  func TestResetLoop(t *testing.T) {
   170  	peerLedger := &mock.PeerLedger{}
   171  	peerLedger.GetBlockchainInfoReturnsOnCall(
   172  		0,
   173  		&common.BlockchainInfo{
   174  			Height: uint64(1),
   175  		},
   176  		nil,
   177  	)
   178  
   179  	peerLedger.GetBlockchainInfoReturnsOnCall(
   180  		1,
   181  		&common.BlockchainInfo{
   182  			Height: uint64(5),
   183  		},
   184  		nil,
   185  	)
   186  
   187  	peerLedger.GetBlockchainInfoReturnsOnCall(
   188  		2,
   189  		&common.BlockchainInfo{
   190  			Height: uint64(11),
   191  		},
   192  		nil,
   193  	)
   194  
   195  	peerLedger.GetBlockchainInfoReturnsOnCall(
   196  		3,
   197  		&common.BlockchainInfo{
   198  			Height: uint64(11),
   199  		},
   200  		nil,
   201  	)
   202  
   203  	getLedger := &mock.GetLedger{}
   204  	getLedger.Returns(peerLedger)
   205  	resetFilter := &reset{
   206  		reject: true,
   207  	}
   208  
   209  	ledgerIDs := []string{"testchannel", "testchannel2"}
   210  	heights := map[string]uint64{
   211  		"testchannel":  uint64(10),
   212  		"testchannel2": uint64(10),
   213  	}
   214  
   215  	resetLoop(resetFilter, heights, ledgerIDs, getLedger.Spy, 1*time.Second)
   216  	assert.False(t, resetFilter.reject)
   217  	assert.Equal(t, 4, peerLedger.GetBlockchainInfoCallCount())
   218  }