github.com/ewagmig/fabric@v2.1.1+incompatible/cmd/peer/main_test.go (about)

     1  /*
     2  Copyright IBM Corp. All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package main
     8  
     9  import (
    10  	"fmt"
    11  	"io/ioutil"
    12  	"net"
    13  	"os"
    14  	"os/exec"
    15  	"path/filepath"
    16  	"testing"
    17  	"time"
    18  
    19  	. "github.com/onsi/gomega"
    20  	"github.com/onsi/gomega/gbytes"
    21  	"github.com/onsi/gomega/gexec"
    22  )
    23  
    24  func TestPluginLoadingFailure(t *testing.T) {
    25  	gt := NewGomegaWithT(t)
    26  	peer, err := gexec.Build("github.com/hyperledger/fabric/cmd/peer")
    27  	gt.Expect(err).NotTo(HaveOccurred())
    28  	defer gexec.CleanupBuildArtifacts()
    29  
    30  	parentDir, err := filepath.Abs("../..")
    31  	gt.Expect(err).NotTo(HaveOccurred())
    32  
    33  	tempDir, err := ioutil.TempDir("", "plugin-failure")
    34  	gt.Expect(err).NotTo(HaveOccurred())
    35  	defer os.RemoveAll(tempDir)
    36  
    37  	peerListener, err := net.Listen("tcp", "localhost:0")
    38  	gt.Expect(err).NotTo(HaveOccurred())
    39  	peerListenAddress := peerListener.Addr()
    40  
    41  	chaincodeListener, err := net.Listen("tcp", "localhost:0")
    42  	gt.Expect(err).NotTo(HaveOccurred())
    43  	chaincodeListenAddress := chaincodeListener.Addr()
    44  
    45  	operationsListener, err := net.Listen("tcp", "localhost:0")
    46  	gt.Expect(err).NotTo(HaveOccurred())
    47  	operationsListenAddress := operationsListener.Addr()
    48  
    49  	err = peerListener.Close()
    50  	gt.Expect(err).NotTo(HaveOccurred())
    51  	err = chaincodeListener.Close()
    52  	gt.Expect(err).NotTo(HaveOccurred())
    53  	err = operationsListener.Close()
    54  	gt.Expect(err).NotTo(HaveOccurred())
    55  
    56  	for _, plugin := range []string{
    57  		"ENDORSERS_ESCC",
    58  		"VALIDATORS_VSCC",
    59  	} {
    60  		plugin := plugin
    61  		t.Run(plugin, func(t *testing.T) {
    62  			cmd := exec.Command(peer, "node", "start")
    63  			cmd.Env = []string{
    64  				fmt.Sprintf("CORE_PEER_FILESYSTEMPATH=%s", tempDir),
    65  				fmt.Sprintf("CORE_PEER_HANDLERS_%s_LIBRARY=%s", plugin, filepath.Join(parentDir, "internal/peer/testdata/invalid_plugins/invalidplugin.so")),
    66  				fmt.Sprintf("CORE_PEER_LISTENADDRESS=%s", peerListenAddress),
    67  				fmt.Sprintf("CORE_PEER_CHAINCODELISTENADDRESS=%s", chaincodeListenAddress),
    68  				fmt.Sprintf("CORE_PEER_MSPCONFIGPATH=%s", "msp"),
    69  				fmt.Sprintf("CORE_OPERATIONS_LISTENADDRESS=%s", operationsListenAddress),
    70  				"CORE_OPERATIONS_TLS_ENABLED=false",
    71  				fmt.Sprintf("FABRIC_CFG_PATH=%s", filepath.Join(parentDir, "sampleconfig")),
    72  			}
    73  			sess, err := gexec.Start(cmd, nil, nil)
    74  			gt.Expect(err).NotTo(HaveOccurred())
    75  			gt.Eventually(sess, time.Minute).Should(gexec.Exit(2))
    76  
    77  			gt.Expect(sess.Err).To(gbytes.Say(fmt.Sprintf("panic: Error opening plugin at path %s", filepath.Join(parentDir, "internal/peer/testdata/invalid_plugins/invalidplugin.so"))))
    78  			gt.Expect(sess.Err).To(gbytes.Say("plugin.Open"))
    79  		})
    80  	}
    81  }