github.com/m3db/m3@v1.5.0/src/x/panicmon/executor_test.go (about) 1 // Copyright (c) 2017 Uber Technologies, Inc. 2 // 3 // Permission is hereby granted, free of charge, to any person obtaining a copy 4 // of this software and associated documentation files (the "Software"), to deal 5 // in the Software without restriction, including without limitation the rights 6 // to use, copy, modify, merge, publish, distribute, sublicense, and/or sell 7 // copies of the Software, and to permit persons to whom the Software is 8 // furnished to do so, subject to the following conditions: 9 // 10 // The above copyright notice and this permission notice shall be included in 11 // all copies or substantial portions of the Software. 12 // 13 // THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR 14 // IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, 15 // FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE 16 // AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER 17 // LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, 18 // OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN 19 // THE SOFTWARE. 20 21 package panicmon 22 23 import ( 24 "bytes" 25 "io/ioutil" 26 "os" 27 "os/exec" 28 "reflect" 29 "syscall" 30 "testing" 31 32 "github.com/stretchr/testify/assert" 33 "github.com/stretchr/testify/mock" 34 "github.com/stretchr/testify/require" 35 ) 36 37 func TestStatusCodeString(t *testing.T) { 38 assert.Equal(t, StatusCode(1).String(), "1") 39 } 40 41 func TestNewDefaultExecutorOpts(t *testing.T) { 42 exp := ExecutorOptions{ 43 Handler: Handler{ 44 ProcessHandler: NoopProcessHandler{}, 45 SignalHandler: NoopSignalHandler{}, 46 }, 47 } 48 assert.Equal(t, exp, NewDefaultExecutorOpts()) 49 } 50 51 func TestExecutorOptsValidate(t *testing.T) { 52 exp := ExecutorOptions{ 53 Handler: Handler{ 54 ProcessHandler: NoopProcessHandler{}, 55 SignalHandler: NoopSignalHandler{}, 56 }, 57 } 58 59 for _, opts := range []ExecutorOptions{ 60 {}, 61 {Handler: Handler{SignalHandler: NoopSignalHandler{}}}, 62 {Handler: Handler{ProcessHandler: NoopProcessHandler{}}}, 63 } { 64 opts.Validate() 65 assert.Equal(t, exp, opts) 66 } 67 } 68 69 func TestNewExecutor(t *testing.T) { 70 ex := NewExecutor(NewDefaultExecutorOpts()) 71 assert.NotNil(t, ex) 72 } 73 74 func TestExecutorRunError(t *testing.T) { 75 proc := &mockProcessHandler{} 76 proc.On("ProcessStarted", ProcessStartEvent{Args: nil}).Return() 77 proc.On("ProcessFailed", mock.AnythingOfType("panicmon.ProcessFailedEvent")).Return() 78 79 ex := NewExecutor(ExecutorOptions{ 80 Handler: Handler{ProcessHandler: proc}, 81 }) 82 83 _, err := ex.Run(nil) 84 proc.AssertExpectations(t) 85 assert.Error(t, err) 86 } 87 88 func TestExecutorRun_Exited_Success(t *testing.T) { 89 args := []string{"ls"} 90 proc := &mockProcessHandler{} 91 proc.On("ProcessStarted", ProcessStartEvent{Args: args}).Return() 92 proc.On("ProcessExited", ProcessExitedEvent{ 93 Args: args, 94 Code: StatusCode(0), 95 }).Return() 96 97 sig := &mockSignalHandler{} 98 99 ex := NewExecutor(ExecutorOptions{ 100 Handler: Handler{ 101 ProcessHandler: proc, 102 SignalHandler: sig, 103 }, 104 }) 105 106 code, err := ex.Run(args) 107 assert.Equal(t, StatusCode(0), code) 108 assert.NoError(t, err) 109 110 proc.AssertExpectations(t) 111 sig.AssertExpectations(t) 112 } 113 114 func TestExecutorRun_Exited_Success_Output(t *testing.T) { 115 args := []string{"echo", "hello"} 116 proc := &mockProcessHandler{} 117 proc.On("ProcessStarted", ProcessStartEvent{Args: args}).Return() 118 proc.On("ProcessExited", ProcessExitedEvent{ 119 Args: args, 120 Code: StatusCode(0), 121 }).Return() 122 123 sig := &mockSignalHandler{} 124 125 buf := &bytes.Buffer{} 126 127 ex := NewExecutor(ExecutorOptions{ 128 Handler: Handler{ 129 ProcessHandler: proc, 130 SignalHandler: sig, 131 }, 132 Stdout: buf, 133 }) 134 135 code, err := ex.Run(args) 136 assert.Equal(t, StatusCode(0), code) 137 assert.NoError(t, err) 138 139 assert.Equal(t, "hello\n", buf.String()) 140 141 proc.AssertExpectations(t) 142 sig.AssertExpectations(t) 143 } 144 145 func TestExecutorRun_Exited_Success_Signals(t *testing.T) { 146 args := []string{"sleep", "10"} 147 148 proc := &mockProcessHandler{} 149 proc.On("ProcessStarted", ProcessStartEvent{Args: args}).Return() 150 proc.On("ProcessExited", mock.MatchedBy(func(e ProcessExitedEvent) bool { 151 return reflect.DeepEqual(e.Args, args) && e.Code == StatusCode(-1) 152 })).Return() 153 154 sig := &mockSignalHandler{} 155 156 sig.On("SignalReceived", mock.MatchedBy(func(e SignalReceivedEvent) bool { 157 return e.Signal == syscall.SIGUSR1 158 })).Return() 159 160 sig.On("SignalPassed", mock.MatchedBy(func(e SignalPassedEvent) bool { 161 return e.Signal == syscall.SIGUSR1 162 })).Return() 163 164 ex := NewExecutor(ExecutorOptions{ 165 Handler: Handler{ 166 ProcessHandler: proc, 167 SignalHandler: sig, 168 }, 169 Stdout: ioutil.Discard, 170 Stderr: ioutil.Discard, 171 }) 172 173 procInfo, err := os.FindProcess(os.Getpid()) 174 require.NoError(t, err) 175 176 codeC := make(chan StatusCode) 177 errC := make(chan error) 178 179 go func() { 180 code, err := ex.Run(args) 181 codeC <- code 182 errC <- err 183 }() 184 185 go func() { 186 procInfo.Signal(syscall.SIGUSR1) 187 }() 188 189 assert.Equal(t, StatusCode(-1), <-codeC) 190 assert.Error(t, <-errC) 191 192 proc.Lock() 193 proc.AssertExpectations(t) 194 proc.Unlock() 195 196 sig.Lock() 197 sig.AssertExpectations(t) 198 sig.Unlock() 199 200 _, ok := <-ex.(*executor).closeC 201 assert.False(t, ok, "executor closeC should be closed") 202 } 203 204 func TestExecutorRun_Start_Error(t *testing.T) { 205 args := []string{"non_existent_command_"} 206 207 proc := &mockProcessHandler{} 208 proc.On("ProcessStarted", ProcessStartEvent{Args: args}).Return() 209 proc.On("ProcessFailed", mock.MatchedBy(func(e ProcessFailedEvent) bool { 210 return reflect.DeepEqual(args, e.Args) && e.Err != nil 211 })) 212 213 sig := &mockSignalHandler{} 214 215 ex := NewExecutor(ExecutorOptions{ 216 Handler: Handler{ 217 ProcessHandler: proc, 218 SignalHandler: sig, 219 }, 220 Stdout: ioutil.Discard, 221 Stderr: ioutil.Discard, 222 }) 223 224 code, err := ex.Run(args) 225 assert.Equal(t, StatusCode(0), code) 226 assert.Error(t, err) 227 228 proc.AssertExpectations(t) 229 sig.AssertExpectations(t) 230 } 231 232 func TestExecutorRun_Exited_Error(t *testing.T) { 233 args := []string{"ls", "/non/existent/path"} 234 235 proc := &mockProcessHandler{} 236 proc.On("ProcessStarted", ProcessStartEvent{Args: args}).Return() 237 proc.On("ProcessExited", mock.MatchedBy(func(e ProcessExitedEvent) bool { 238 return reflect.DeepEqual(args, e.Args) && e.Code != 0 239 })).Return() 240 241 sig := &mockSignalHandler{} 242 243 ex := NewExecutor(ExecutorOptions{ 244 Handler: Handler{ 245 ProcessHandler: proc, 246 SignalHandler: sig, 247 }, 248 Stdout: ioutil.Discard, 249 Stderr: ioutil.Discard, 250 }) 251 252 code, err := ex.Run(args) 253 assert.NotEqual(t, StatusCode(0), code) 254 assert.Error(t, err) 255 256 proc.AssertExpectations(t) 257 sig.AssertExpectations(t) 258 } 259 260 func TestExecutorPassSignals(t *testing.T) { 261 sig := &mockSignalHandler{} 262 263 ex := NewExecutor(ExecutorOptions{ 264 Handler: Handler{ 265 SignalHandler: sig, 266 }, 267 }).(*executor) 268 269 child := exec.Command("sleep", "10") 270 err := child.Start() 271 require.NoError(t, err) 272 273 go ex.passSignals(child.Process) 274 275 sig.On("SignalReceived", SignalReceivedEvent{ 276 Signal: syscall.SIGUSR1, 277 ChildPid: child.Process.Pid, 278 }) 279 280 sig.On("SignalPassed", SignalPassedEvent{ 281 Signal: syscall.SIGUSR1, 282 ChildPid: child.Process.Pid, 283 }) 284 285 procInfo, err := os.FindProcess(os.Getpid()) 286 assert.NoError(t, err) 287 procInfo.Signal(syscall.SIGUSR1) 288 289 child.Wait() 290 ex.closeAndWait() 291 292 sig.AssertExpectations(t) 293 }