github.com/schwarzm/garden-linux@v0.0.0-20150507151835-33bca2147c47/linux_backend/namespaced_signaller_test.go (about)

     1  package linux_backend_test
     2  
     3  import (
     4  	"io/ioutil"
     5  	"os"
     6  	"path/filepath"
     7  
     8  	. "github.com/onsi/ginkgo"
     9  	. "github.com/onsi/gomega"
    10  
    11  	"github.com/cloudfoundry-incubator/garden-linux/linux_backend"
    12  	"github.com/cloudfoundry/gunk/command_runner/fake_command_runner"
    13  	. "github.com/cloudfoundry/gunk/command_runner/fake_command_runner/matchers"
    14  )
    15  
    16  var _ = Describe("Namespaced Signaller", func() {
    17  	It("kills a process using ./bin/wsh based on its pid", func() {
    18  		tmp, err := ioutil.TempDir("", "namespacedsignaller")
    19  		Expect(err).ToNot(HaveOccurred())
    20  		defer os.RemoveAll(tmp)
    21  
    22  		pidFile := filepath.Join(tmp, "thepid.file")
    23  
    24  		fakeRunner := fake_command_runner.New()
    25  		signaller := &linux_backend.NamespacedSignaller{
    26  			Runner:        fakeRunner,
    27  			ContainerPath: "/fish/finger",
    28  			PidFilePath:   pidFile,
    29  		}
    30  
    31  		Expect(ioutil.WriteFile(pidFile, []byte(" 12345\n"), 0755)).To(Succeed())
    32  
    33  		Expect(signaller.Signal(os.Kill)).To(Succeed())
    34  		Expect(fakeRunner).To(HaveExecutedSerially(
    35  			fake_command_runner.CommandSpec{
    36  				Path: "/fish/finger/bin/wsh",
    37  				Args: []string{
    38  					"--socket", "/fish/finger/run/wshd.sock",
    39  					"kill", "-9", "12345",
    40  				},
    41  			}))
    42  	})
    43  
    44  	It("returns an appropriate error when the pidfile is not present", func() {
    45  		fakeRunner := fake_command_runner.New()
    46  		signaller := &linux_backend.NamespacedSignaller{
    47  			Runner:        fakeRunner,
    48  			ContainerPath: "/fish/finger",
    49  			PidFilePath:   "/does/not/exist",
    50  		}
    51  
    52  		Expect(signaller.Signal(os.Kill)).To(MatchError("linux_backend: can't open PID file: open /does/not/exist: no such file or directory"))
    53  	})
    54  
    55  	It("returns an appropriate error when the pidfile is empty", func() {
    56  		tmp, err := ioutil.TempDir("", "namespacedsignaller")
    57  		Expect(err).ToNot(HaveOccurred())
    58  		defer os.RemoveAll(tmp)
    59  
    60  		pidFile := filepath.Join(tmp, "thepid.file")
    61  
    62  		fakeRunner := fake_command_runner.New()
    63  		signaller := &linux_backend.NamespacedSignaller{
    64  			Runner:        fakeRunner,
    65  			ContainerPath: "/fish/finger",
    66  			PidFilePath:   pidFile,
    67  		}
    68  
    69  		Expect(ioutil.WriteFile(pidFile, []byte(""), 0755)).To(Succeed())
    70  
    71  		Expect(signaller.Signal(os.Kill)).To(MatchError("linux_backend: can't read PID file: is empty or non existent"))
    72  	})
    73  
    74  	It("returns an appropriate error when the pidfile does not contain a number", func() {
    75  		tmp, err := ioutil.TempDir("", "namespacedsignaller")
    76  		Expect(err).ToNot(HaveOccurred())
    77  		defer os.RemoveAll(tmp)
    78  
    79  		pidFile := filepath.Join(tmp, "thepid.file")
    80  
    81  		fakeRunner := fake_command_runner.New()
    82  		signaller := &linux_backend.NamespacedSignaller{
    83  			Runner:        fakeRunner,
    84  			ContainerPath: "/fish/finger",
    85  			PidFilePath:   pidFile,
    86  		}
    87  
    88  		Expect(ioutil.WriteFile(pidFile, []byte("not-a-pid\n"), 0755)).To(Succeed())
    89  
    90  		Expect(signaller.Signal(os.Kill)).To(MatchError("linux_backend: can't parse PID file content: expected integer"))
    91  	})
    92  })