github.com/kaituanwang/hyperledger@v2.0.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 operationsListener, err := net.Listen("tcp", "localhost:0") 42 gt.Expect(err).NotTo(HaveOccurred()) 43 operationsListenAddress := operationsListener.Addr() 44 45 err = peerListener.Close() 46 gt.Expect(err).NotTo(HaveOccurred()) 47 err = operationsListener.Close() 48 gt.Expect(err).NotTo(HaveOccurred()) 49 50 for _, plugin := range []string{ 51 "ENDORSERS_ESCC", 52 "VALIDATORS_VSCC", 53 } { 54 plugin := plugin 55 t.Run(plugin, func(t *testing.T) { 56 cmd := exec.Command(peer, "node", "start") 57 cmd.Env = []string{ 58 fmt.Sprintf("CORE_PEER_FILESYSTEMPATH=%s", tempDir), 59 fmt.Sprintf("CORE_PEER_HANDLERS_%s_LIBRARY=%s", plugin, filepath.Join(parentDir, "internal/peer/testdata/invalid_plugins/invalidplugin.so")), 60 fmt.Sprintf("CORE_PEER_LISTENADDRESS=%s", peerListenAddress), 61 fmt.Sprintf("CORE_PEER_MSPCONFIGPATH=%s", "msp"), 62 fmt.Sprintf("CORE_OPERATIONS_LISTENADDRESS=%s", operationsListenAddress), 63 "CORE_OPERATIONS_TLS_ENABLED=false", 64 fmt.Sprintf("FABRIC_CFG_PATH=%s", filepath.Join(parentDir, "sampleconfig")), 65 } 66 sess, err := gexec.Start(cmd, nil, nil) 67 gt.Expect(err).NotTo(HaveOccurred()) 68 gt.Eventually(sess, time.Minute).Should(gexec.Exit(2)) 69 70 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")))) 71 gt.Expect(sess.Err).To(gbytes.Say("plugin.Open")) 72 }) 73 } 74 }