github.com/cloudfoundry-attic/garden-linux@v0.333.2-candidate/process_tracker/namespaced_signaller_test.go (about) 1 package process_tracker_test 2 3 import ( 4 "fmt" 5 "io/ioutil" 6 "os" 7 "path/filepath" 8 "syscall" 9 "time" 10 11 . "github.com/onsi/ginkgo" 12 . "github.com/onsi/gomega" 13 "github.com/pivotal-golang/lager/lagertest" 14 15 "github.com/cloudfoundry-incubator/garden-linux/process_tracker" 16 "github.com/cloudfoundry/gunk/command_runner/fake_command_runner" 17 . "github.com/cloudfoundry/gunk/command_runner/fake_command_runner/matchers" 18 ) 19 20 var _ = Describe("Namespaced Signaller", func() { 21 var ( 22 containerPath string 23 processId string 24 processPidFileContent string 25 signaller *process_tracker.NamespacedSignaller 26 fakeRunner *fake_command_runner.FakeCommandRunner 27 request *process_tracker.SignalRequest 28 ) 29 30 BeforeEach(func() { 31 var err error 32 containerPath, err = ioutil.TempDir("", "namespacedsignaller") 33 Expect(err).ToNot(HaveOccurred()) 34 35 err = os.Mkdir(filepath.Join(containerPath, "processes"), 0755) 36 Expect(err).ToNot(HaveOccurred()) 37 38 fakeRunner = fake_command_runner.New() 39 signaller = &process_tracker.NamespacedSignaller{ 40 Runner: fakeRunner, 41 ContainerPath: containerPath, 42 Logger: lagertest.NewTestLogger("test"), 43 Timeout: time.Millisecond * 100, 44 } 45 46 processId = "12345" 47 request = &process_tracker.SignalRequest{ 48 Pid: processId, 49 Link: nil, 50 Signal: syscall.SIGKILL, 51 } 52 }) 53 54 AfterEach(func() { 55 os.RemoveAll(containerPath) 56 }) 57 58 JustBeforeEach(func() { 59 processPidFile := filepath.Join(containerPath, "processes", fmt.Sprintf("%d.pid", processId)) 60 Expect(ioutil.WriteFile(processPidFile, []byte(processPidFileContent), 0755)).To(Succeed()) 61 }) 62 63 Context("when the pidfile exists", func() { 64 BeforeEach(func() { 65 processPidFileContent = " 12345\n" 66 }) 67 68 It("kills a process using ./bin/wsh based on its pid", func() { 69 Expect(signaller.Signal(request)).To(Succeed()) 70 71 Expect(fakeRunner).To(HaveExecutedSerially( 72 fake_command_runner.CommandSpec{ 73 Path: filepath.Join(containerPath, "bin/wsh"), 74 Args: []string{ 75 "--socket", filepath.Join(containerPath, "run/wshd.sock"), 76 "--user", "root", 77 "kill", "-9", "12345", 78 }, 79 })) 80 }) 81 }) 82 83 Context("when the pidfile is not present", func() { 84 JustBeforeEach(func() { 85 os.RemoveAll(containerPath) 86 }) 87 88 It("returns an appropriate error", func() { 89 errMsg := fmt.Sprintf("linux_backend: can't open PID file: open %s/processes/%d.pid: no such file or directory", containerPath, processId) 90 Expect(signaller.Signal(request)).To(MatchError(errMsg)) 91 }) 92 }) 93 94 Context("when the pidfile is empty", func() { 95 BeforeEach(func() { 96 processPidFileContent = "" 97 }) 98 99 It("returns an appropriate error", func() { 100 Expect(signaller.Signal(request)).To(MatchError("linux_backend: can't read PID file: is empty or non existent")) 101 }) 102 }) 103 104 Context("when the pidfile does not contain a number", func() { 105 BeforeEach(func() { 106 processPidFileContent = "not-a-pid\n" 107 }) 108 109 It("returns an appropriate error", func() { 110 Expect(signaller.Signal(request)).To(MatchError("linux_backend: can't parse PID file content: expected integer")) 111 }) 112 }) 113 })