github.com/anjalikarhana/fabric@v2.1.1+incompatible/integration/e2e/e2e_signal_test.go (about)

     1  /*
     2  Copyright IBM Corp All Rights Reserved.
     3  
     4  SPDX-License-Identifier: Apache-2.0
     5  */
     6  
     7  package e2e
     8  
     9  import (
    10  	"io/ioutil"
    11  	"os"
    12  	"syscall"
    13  
    14  	docker "github.com/fsouza/go-dockerclient"
    15  	"github.com/hyperledger/fabric/integration/nwo"
    16  	. "github.com/onsi/ginkgo"
    17  	. "github.com/onsi/gomega"
    18  	"github.com/onsi/gomega/gbytes"
    19  	"github.com/tedsuo/ifrit"
    20  	"github.com/tedsuo/ifrit/ginkgomon"
    21  )
    22  
    23  var _ = Describe("SignalHandling", func() {
    24  	var (
    25  		testDir string
    26  		client  *docker.Client
    27  		network *nwo.Network
    28  
    29  		peerRunner, ordererRunner   *ginkgomon.Runner
    30  		peerProcess, ordererProcess ifrit.Process
    31  	)
    32  
    33  	BeforeEach(func() {
    34  		var err error
    35  		testDir, err = ioutil.TempDir("", "e2e-sigs")
    36  		Expect(err).NotTo(HaveOccurred())
    37  
    38  		client, err = docker.NewClientFromEnv()
    39  		Expect(err).NotTo(HaveOccurred())
    40  
    41  		network = nwo.New(nwo.BasicSolo(), testDir, client, StartPort(), components)
    42  		network.GenerateConfigTree()
    43  		network.Bootstrap()
    44  
    45  		ordererRunner = network.OrdererRunner(network.Orderers[0])
    46  		ordererProcess = ifrit.Invoke(ordererRunner)
    47  		Eventually(ordererProcess.Ready(), network.EventuallyTimeout).Should(BeClosed())
    48  
    49  		peerRunner = network.PeerRunner(network.Peers[0])
    50  		peerProcess = ifrit.Invoke(peerRunner)
    51  		Eventually(peerProcess.Ready(), network.EventuallyTimeout).Should(BeClosed())
    52  	})
    53  
    54  	AfterEach(func() {
    55  		if peerProcess != nil {
    56  			peerProcess.Signal(syscall.SIGKILL)
    57  		}
    58  		if ordererProcess != nil {
    59  			ordererProcess.Signal(syscall.SIGKILL)
    60  		}
    61  		if network != nil {
    62  			network.Cleanup()
    63  		}
    64  		os.RemoveAll(testDir)
    65  	})
    66  
    67  	It("handles signals", func() {
    68  		By("verifying SIGUSR1 to the peer dumps go routines")
    69  		peerProcess.Signal(syscall.SIGUSR1)
    70  		Eventually(peerRunner.Err(), network.EventuallyTimeout).Should(gbytes.Say("Received signal: "))
    71  		Eventually(peerRunner.Err(), network.EventuallyTimeout).Should(gbytes.Say(`Go routines report`))
    72  
    73  		By("verifying SIGUSR1 to the orderer dumps go routines")
    74  		ordererProcess.Signal(syscall.SIGUSR1)
    75  		Eventually(ordererRunner.Err(), network.EventuallyTimeout).Should(gbytes.Say("Received signal: "))
    76  		Eventually(ordererRunner.Err(), network.EventuallyTimeout).Should(gbytes.Say(`Go routines report`))
    77  
    78  		By("verifying SIGUSR1 does not terminate processes")
    79  		Consistently(peerProcess.Wait()).ShouldNot(Receive())
    80  		Consistently(ordererProcess.Wait()).ShouldNot(Receive())
    81  
    82  		By("verifying SIGTERM to the peer stops the process")
    83  		peerProcess.Signal(syscall.SIGTERM)
    84  		Eventually(peerRunner.Err(), network.EventuallyTimeout).Should(gbytes.Say("Received signal: "))
    85  		Eventually(peerProcess.Wait(), network.EventuallyTimeout).Should(Receive())
    86  		peerProcess = nil
    87  
    88  		By("verifying SIGTERM to the orderer stops the process")
    89  		ordererProcess.Signal(syscall.SIGTERM)
    90  		Eventually(ordererRunner.Err(), network.EventuallyTimeout).Should(gbytes.Say("Received signal: "))
    91  		Eventually(ordererProcess.Wait(), network.EventuallyTimeout).Should(Receive())
    92  		ordererProcess = nil
    93  	})
    94  })