github.com/geofffranks/garden-linux@v0.0.0-20160715111146-26c893169cfa/container_daemon/signaller_test.go (about)

     1  package container_daemon_test
     2  
     3  import (
     4  	"fmt"
     5  	"io"
     6  	"os/exec"
     7  	"syscall"
     8  
     9  	"code.cloudfoundry.org/garden-linux/container_daemon"
    10  	. "github.com/onsi/ginkgo"
    11  	. "github.com/onsi/gomega"
    12  	"github.com/onsi/gomega/gbytes"
    13  	"code.cloudfoundry.org/lager/lagertest"
    14  )
    15  
    16  var _ = Describe("Signalling a running process", func() {
    17  	var pid int
    18  	var signaller *container_daemon.ProcessSignaller
    19  	var stdout *gbytes.Buffer
    20  
    21  	BeforeEach(func() {
    22  		stdout = gbytes.NewBuffer()
    23  		cmd := exec.Command("bash", "-c", `
    24  		trap "echo TERMed; exit" TERM
    25  		echo "pid = $$"
    26  		sleep 2
    27  	`)
    28  		cmd.Stdout = io.MultiWriter(stdout, GinkgoWriter)
    29  		cmd.Stderr = GinkgoWriter
    30  
    31  		err := cmd.Start()
    32  		Expect(err).NotTo(HaveOccurred())
    33  
    34  		Eventually(stdout).Should(gbytes.Say("pid"))
    35  		_, err = fmt.Sscanf(string(stdout.Contents()), "pid = %d\n", &pid)
    36  		Expect(err).ToNot(HaveOccurred())
    37  
    38  		signaller = &container_daemon.ProcessSignaller{
    39  			Logger: lagertest.NewTestLogger("test"),
    40  		}
    41  	})
    42  
    43  	Context("when a process with the given pid exists", func() {
    44  		It("sends the signal to the process", func() {
    45  			Expect(signaller.Signal(pid, syscall.SIGTERM)).To(Succeed())
    46  			Eventually(stdout, "5s").Should(gbytes.Say("TERMed"))
    47  		})
    48  	})
    49  
    50  	Context("when a process with the given pid does not exist", func() {
    51  		It("returns an error", func() {
    52  			err := signaller.Signal(123123123, syscall.SIGTERM)
    53  			Expect(err).To(MatchError(ContainSubstring("container_daemon: signaller: signal process: pid:")))
    54  		})
    55  	})
    56  })