Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 1 | package signals |
| 2 | |
| 3 | import ( |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 4 | "bufio" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 5 | "fmt" |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 6 | "io" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 7 | "os" |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 8 | "path/filepath" |
| 9 | "runtime" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 10 | "syscall" |
| 11 | "testing" |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 12 | "time" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 13 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 14 | "v.io/core/veyron2" |
Suharsh Sivakumar | 946f64d | 2015-01-08 10:48:13 -0800 | [diff] [blame] | 15 | "v.io/core/veyron2/context" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 16 | "v.io/core/veyron2/ipc" |
| 17 | "v.io/core/veyron2/mgmt" |
| 18 | "v.io/core/veyron2/naming" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 19 | "v.io/core/veyron2/services/mgmt/appcycle" |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 20 | "v.io/core/veyron2/vtrace" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 21 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 22 | "v.io/core/veyron/lib/expect" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 23 | "v.io/core/veyron/lib/modules" |
| 24 | "v.io/core/veyron/lib/testutil" |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame^] | 25 | _ "v.io/core/veyron/profiles" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 26 | vflag "v.io/core/veyron/security/flag" |
| 27 | "v.io/core/veyron/services/mgmt/device" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 28 | ) |
| 29 | |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 30 | // TestHelperProcess is boilerplate for the modules setup. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 31 | func TestHelperProcess(t *testing.T) { |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 32 | modules.DispatchInTest() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 33 | } |
| 34 | |
| 35 | func init() { |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 36 | testutil.Init() |
Cosmos Nicolaou | 1e78ccc | 2014-10-09 08:10:26 -0700 | [diff] [blame] | 37 | modules.RegisterChild("handleDefaults", "", handleDefaults) |
| 38 | modules.RegisterChild("handleCustom", "", handleCustom) |
| 39 | modules.RegisterChild("handleCustomWithStop", "", handleCustomWithStop) |
| 40 | modules.RegisterChild("handleDefaultsIgnoreChan", "", handleDefaultsIgnoreChan) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 41 | } |
| 42 | |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame^] | 43 | func stopLoop(stop func(), stdin io.Reader, ch chan<- struct{}) { |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 44 | scanner := bufio.NewScanner(stdin) |
| 45 | for scanner.Scan() { |
| 46 | switch scanner.Text() { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 47 | case "close": |
| 48 | close(ch) |
| 49 | return |
| 50 | case "stop": |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame^] | 51 | stop() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | } |
| 55 | |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 56 | func program(stdin io.Reader, stdout io.Writer, signals ...os.Signal) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 57 | ctx, shutdown := veyron2.Init() |
Suharsh Sivakumar | 946f64d | 2015-01-08 10:48:13 -0800 | [diff] [blame] | 58 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 59 | closeStopLoop := make(chan struct{}) |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame^] | 60 | go stopLoop(veyron2.GetAppCycle(ctx).Stop, stdin, closeStopLoop) |
Suharsh Sivakumar | 946f64d | 2015-01-08 10:48:13 -0800 | [diff] [blame] | 61 | wait := ShutdownOnSignals(ctx, signals...) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 62 | fmt.Fprintf(stdout, "ready\n") |
| 63 | fmt.Fprintf(stdout, "received signal %s\n", <-wait) |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 64 | shutdown() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 65 | <-closeStopLoop |
| 66 | } |
| 67 | |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 68 | func handleDefaults(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
| 69 | program(stdin, stdout) |
| 70 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 71 | } |
| 72 | |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 73 | func handleCustom(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
| 74 | program(stdin, stdout, syscall.SIGABRT) |
| 75 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 76 | } |
| 77 | |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 78 | func handleCustomWithStop(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
| 79 | program(stdin, stdout, STOP, syscall.SIGABRT, syscall.SIGHUP) |
| 80 | return nil |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 81 | } |
| 82 | |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 83 | func handleDefaultsIgnoreChan(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 84 | ctx, shutdown := veyron2.Init() |
| 85 | defer shutdown() |
Suharsh Sivakumar | 946f64d | 2015-01-08 10:48:13 -0800 | [diff] [blame] | 86 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 87 | closeStopLoop := make(chan struct{}) |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame^] | 88 | go stopLoop(veyron2.GetAppCycle(ctx).Stop, stdin, closeStopLoop) |
Suharsh Sivakumar | 946f64d | 2015-01-08 10:48:13 -0800 | [diff] [blame] | 89 | ShutdownOnSignals(ctx) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 90 | fmt.Fprintf(stdout, "ready\n") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 91 | <-closeStopLoop |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 92 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 93 | } |
| 94 | |
| 95 | func isSignalInSet(sig os.Signal, set []os.Signal) bool { |
| 96 | for _, s := range set { |
| 97 | if sig == s { |
| 98 | return true |
| 99 | } |
| 100 | } |
| 101 | return false |
| 102 | } |
| 103 | |
| 104 | func checkSignalIsDefault(t *testing.T, sig os.Signal) { |
| 105 | if !isSignalInSet(sig, defaultSignals()) { |
| 106 | t.Errorf("signal %s not in default signal set, as expected", sig) |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | func checkSignalIsNotDefault(t *testing.T, sig os.Signal) { |
| 111 | if isSignalInSet(sig, defaultSignals()) { |
| 112 | t.Errorf("signal %s unexpectedly in default signal set", sig) |
| 113 | } |
| 114 | } |
| 115 | |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 116 | func newShell(t *testing.T, ctx *context.T, command string) (*modules.Shell, modules.Handle, *expect.Session) { |
| 117 | sh, err := modules.NewShell(ctx, nil) |
Cosmos Nicolaou | 344cc4a | 2014-11-26 15:38:43 -0800 | [diff] [blame] | 118 | if err != nil { |
| 119 | t.Fatalf("unexpected error: %s", err) |
| 120 | } |
Cosmos Nicolaou | 612ad38 | 2014-10-29 19:41:35 -0700 | [diff] [blame] | 121 | handle, err := sh.Start(command, nil) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 122 | if err != nil { |
| 123 | sh.Cleanup(os.Stderr, os.Stderr) |
| 124 | t.Fatalf("unexpected error: %s", err) |
| 125 | return nil, nil, nil |
| 126 | } |
| 127 | session := expect.NewSession(t, handle.Stdout(), time.Minute) |
| 128 | return sh, handle, session |
| 129 | } |
| 130 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 131 | // TestCleanShutdownSignal verifies that sending a signal to a child that |
| 132 | // handles it by default causes the child to shut down cleanly. |
| 133 | func TestCleanShutdownSignal(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 134 | ctx, shutdown := veyron2.Init() |
| 135 | defer shutdown() |
| 136 | |
| 137 | sh, h, s := newShell(t, ctx, "handleDefaults") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 138 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 139 | s.Expect("ready") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 140 | checkSignalIsDefault(t, syscall.SIGINT) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 141 | syscall.Kill(h.Pid(), syscall.SIGINT) |
| 142 | s.Expectf("received signal %s", syscall.SIGINT) |
| 143 | fmt.Fprintf(h.Stdin(), "close\n") |
| 144 | s.ExpectEOF() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | // TestCleanShutdownStop verifies that sending a stop comamnd to a child that |
| 148 | // handles stop commands by default causes the child to shut down cleanly. |
| 149 | func TestCleanShutdownStop(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 150 | ctx, shutdown := veyron2.Init() |
| 151 | defer shutdown() |
| 152 | |
| 153 | sh, h, s := newShell(t, ctx, "handleDefaults") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 154 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 155 | s.Expect("ready") |
| 156 | fmt.Fprintf(h.Stdin(), "stop\n") |
| 157 | s.Expectf("received signal %s", veyron2.LocalStop) |
| 158 | fmt.Fprintf(h.Stdin(), "close\n") |
| 159 | s.ExpectEOF() |
| 160 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 163 | // TestCleanShutdownStopCustom verifies that sending a stop comamnd to a child |
| 164 | // that handles stop command as part of a custom set of signals handled, causes |
| 165 | // the child to shut down cleanly. |
| 166 | func TestCleanShutdownStopCustom(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 167 | ctx, shutdown := veyron2.Init() |
| 168 | defer shutdown() |
| 169 | |
| 170 | sh, h, s := newShell(t, ctx, "handleCustomWithStop") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 171 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 172 | s.Expect("ready") |
| 173 | fmt.Fprintf(h.Stdin(), "stop\n") |
| 174 | s.Expectf("received signal %s", veyron2.LocalStop) |
| 175 | fmt.Fprintf(h.Stdin(), "close\n") |
| 176 | s.ExpectEOF() |
| 177 | } |
| 178 | |
| 179 | func testExitStatus(t *testing.T, h modules.Handle, s *expect.Session, code int) { |
| 180 | s.ExpectEOF() |
| 181 | _, file, line, _ := runtime.Caller(1) |
| 182 | file = filepath.Base(file) |
| 183 | if got, want := h.Shutdown(os.Stdout, os.Stderr), fmt.Errorf("exit status %d", code); got.Error() != want.Error() { |
| 184 | t.Errorf("%s:%d: got %q, want %q", file, line, got, want) |
| 185 | } |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 186 | } |
| 187 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 188 | // TestStopNoHandler verifies that sending a stop command to a child that does |
| 189 | // not handle stop commands causes the child to exit immediately. |
| 190 | func TestStopNoHandler(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 191 | ctx, shutdown := veyron2.Init() |
| 192 | defer shutdown() |
| 193 | |
| 194 | sh, h, s := newShell(t, ctx, "handleCustom") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 195 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 196 | s.Expect("ready") |
| 197 | fmt.Fprintf(h.Stdin(), "stop\n") |
| 198 | testExitStatus(t, h, s, veyron2.UnhandledStopExitCode) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 199 | } |
| 200 | |
| 201 | // TestDoubleSignal verifies that sending a succession of two signals to a child |
| 202 | // that handles these signals by default causes the child to exit immediately |
| 203 | // upon receiving the second signal. |
| 204 | func TestDoubleSignal(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 205 | ctx, shutdown := veyron2.Init() |
| 206 | defer shutdown() |
| 207 | |
| 208 | sh, h, s := newShell(t, ctx, "handleDefaults") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 209 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 210 | s.Expect("ready") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 211 | checkSignalIsDefault(t, syscall.SIGTERM) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 212 | syscall.Kill(h.Pid(), syscall.SIGTERM) |
| 213 | s.Expectf("received signal %s", syscall.SIGTERM) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 214 | checkSignalIsDefault(t, syscall.SIGINT) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 215 | syscall.Kill(h.Pid(), syscall.SIGINT) |
| 216 | testExitStatus(t, h, s, DoubleStopExitCode) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 217 | } |
| 218 | |
| 219 | // TestSignalAndStop verifies that sending a signal followed by a stop command |
| 220 | // to a child that handles these by default causes the child to exit immediately |
| 221 | // upon receiving the stop command. |
| 222 | func TestSignalAndStop(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 223 | ctx, shutdown := veyron2.Init() |
| 224 | defer shutdown() |
| 225 | |
| 226 | sh, h, s := newShell(t, ctx, "handleDefaults") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 227 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 228 | s.Expect("ready") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 229 | checkSignalIsDefault(t, syscall.SIGTERM) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 230 | syscall.Kill(h.Pid(), syscall.SIGTERM) |
| 231 | s.Expectf("received signal %s", syscall.SIGTERM) |
| 232 | fmt.Fprintf(h.Stdin(), "stop\n") |
| 233 | testExitStatus(t, h, s, DoubleStopExitCode) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 234 | } |
| 235 | |
| 236 | // TestDoubleStop verifies that sending a succession of stop commands to a child |
| 237 | // that handles stop commands by default causes the child to exit immediately |
| 238 | // upon receiving the second stop command. |
| 239 | func TestDoubleStop(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 240 | ctx, shutdown := veyron2.Init() |
| 241 | defer shutdown() |
| 242 | |
| 243 | sh, h, s := newShell(t, ctx, "handleDefaults") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 244 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 245 | s.Expect("ready") |
| 246 | fmt.Fprintf(h.Stdin(), "stop\n") |
| 247 | s.Expectf("received signal %s", veyron2.LocalStop) |
| 248 | fmt.Fprintf(h.Stdin(), "stop\n") |
| 249 | testExitStatus(t, h, s, DoubleStopExitCode) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | // TestSendUnhandledSignal verifies that sending a signal that the child does |
| 253 | // not handle causes the child to exit as per the signal being sent. |
| 254 | func TestSendUnhandledSignal(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 255 | ctx, shutdown := veyron2.Init() |
| 256 | defer shutdown() |
| 257 | |
| 258 | sh, h, s := newShell(t, ctx, "handleDefaults") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 259 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 260 | s.Expect("ready") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 261 | checkSignalIsNotDefault(t, syscall.SIGABRT) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 262 | syscall.Kill(h.Pid(), syscall.SIGABRT) |
| 263 | testExitStatus(t, h, s, 2) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 264 | } |
| 265 | |
| 266 | // TestDoubleSignalIgnoreChan verifies that, even if we ignore the channel that |
| 267 | // ShutdownOnSignals returns, sending two signals should still cause the |
| 268 | // process to exit (ensures that there is no dependency in ShutdownOnSignals |
| 269 | // on having a goroutine read from the returned channel). |
| 270 | func TestDoubleSignalIgnoreChan(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 271 | ctx, shutdown := veyron2.Init() |
| 272 | defer shutdown() |
| 273 | |
| 274 | sh, h, s := newShell(t, ctx, "handleDefaultsIgnoreChan") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 275 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 276 | s.Expect("ready") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 277 | // Even if we ignore the channel that ShutdownOnSignals returns, |
| 278 | // sending two signals should still cause the process to exit. |
| 279 | checkSignalIsDefault(t, syscall.SIGTERM) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 280 | syscall.Kill(h.Pid(), syscall.SIGTERM) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 281 | checkSignalIsDefault(t, syscall.SIGINT) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 282 | syscall.Kill(h.Pid(), syscall.SIGINT) |
| 283 | testExitStatus(t, h, s, DoubleStopExitCode) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 284 | } |
| 285 | |
| 286 | // TestHandlerCustomSignal verifies that sending a non-default signal to a |
| 287 | // server that listens for that signal causes the server to shut down cleanly. |
| 288 | func TestHandlerCustomSignal(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 289 | ctx, shutdown := veyron2.Init() |
| 290 | defer shutdown() |
| 291 | |
| 292 | sh, h, s := newShell(t, ctx, "handleCustom") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 293 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 294 | s.Expect("ready") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 295 | checkSignalIsNotDefault(t, syscall.SIGABRT) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 296 | syscall.Kill(h.Pid(), syscall.SIGABRT) |
| 297 | s.Expectf("received signal %s", syscall.SIGABRT) |
| 298 | fmt.Fprintf(h.Stdin(), "stop\n") |
| 299 | s.ExpectEOF() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 300 | } |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 301 | |
| 302 | // TestHandlerCustomSignalWithStop verifies that sending a custom stop signal |
| 303 | // to a server that listens for that signal causes the server to shut down |
| 304 | // cleanly, even when a STOP signal is also among the handled signals. |
| 305 | func TestHandlerCustomSignalWithStop(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 306 | rootCtx, shutdown := veyron2.Init() |
| 307 | defer shutdown() |
| 308 | |
Jiri Simsa | 7fc6e87 | 2014-06-09 09:22:53 -0700 | [diff] [blame] | 309 | for _, signal := range []syscall.Signal{syscall.SIGABRT, syscall.SIGHUP} { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 310 | ctx, _ := vtrace.SetNewTrace(rootCtx) |
| 311 | sh, h, s := newShell(t, ctx, "handleCustomWithStop") |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 312 | s.Expect("ready") |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 313 | checkSignalIsNotDefault(t, signal) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 314 | syscall.Kill(h.Pid(), signal) |
| 315 | s.Expectf("received signal %s", signal) |
| 316 | fmt.Fprintf(h.Stdin(), "close\n") |
| 317 | s.ExpectEOF() |
| 318 | sh.Cleanup(os.Stderr, os.Stderr) |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | // TestParseSignalsList verifies that ShutdownOnSignals correctly interprets |
| 323 | // the input list of signals. |
| 324 | func TestParseSignalsList(t *testing.T) { |
| 325 | list := []os.Signal{STOP, syscall.SIGTERM} |
Matt Rosencrantz | c7fecf1 | 2014-11-27 19:58:43 -0800 | [diff] [blame] | 326 | ShutdownOnSignals(nil, list...) |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 327 | if !isSignalInSet(syscall.SIGTERM, list) { |
| 328 | t.Errorf("signal %s not in signal set, as expected: %v", syscall.SIGTERM, list) |
| 329 | } |
| 330 | if !isSignalInSet(STOP, list) { |
| 331 | t.Errorf("signal %s not in signal set, as expected: %v", STOP, list) |
| 332 | } |
| 333 | } |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 334 | |
| 335 | type configServer struct { |
| 336 | ch chan<- string |
| 337 | } |
| 338 | |
| 339 | func (c *configServer) Set(_ ipc.ServerContext, key, value string) error { |
| 340 | if key != mgmt.AppCycleManagerConfigKey { |
| 341 | return fmt.Errorf("Unexpected key: %v", key) |
| 342 | } |
| 343 | c.ch <- value |
| 344 | return nil |
| 345 | |
| 346 | } |
| 347 | |
Matt Rosencrantz | f541b77 | 2015-01-13 07:58:59 -0800 | [diff] [blame] | 348 | func createConfigServer(t *testing.T, ctx *context.T) (ipc.Server, string, <-chan string) { |
| 349 | server, err := veyron2.NewServer(ctx) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 350 | if err != nil { |
| 351 | t.Fatalf("Got error: %v", err) |
| 352 | } |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 353 | ch := make(chan string) |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 354 | var ep []naming.Endpoint |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame^] | 355 | if ep, err = server.Listen(veyron2.GetListenSpec(ctx)); err != nil { |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 356 | t.Fatalf("Got error: %v", err) |
| 357 | } |
Bogdan Caprita | a456f47 | 2014-12-10 10:18:03 -0800 | [diff] [blame] | 358 | if err := server.Serve("", device.ConfigServer(&configServer{ch}), vflag.NewAuthorizerOrDie()); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 359 | t.Fatalf("Got error: %v", err) |
| 360 | } |
Cosmos Nicolaou | 8bd8e10 | 2015-01-13 21:52:53 -0800 | [diff] [blame] | 361 | return server, ep[0].Name(), ch |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 362 | |
| 363 | } |
| 364 | |
| 365 | // TestCleanRemoteShutdown verifies that remote shutdown works correctly. |
| 366 | func TestCleanRemoteShutdown(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 367 | ctx, shutdown := veyron2.Init() |
| 368 | defer shutdown() |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 369 | |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 370 | sh, err := modules.NewShell(ctx, nil) |
Cosmos Nicolaou | 344cc4a | 2014-11-26 15:38:43 -0800 | [diff] [blame] | 371 | if err != nil { |
| 372 | t.Fatalf("unexpected error: %s", err) |
| 373 | } |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 374 | defer sh.Cleanup(os.Stderr, os.Stderr) |
| 375 | |
Matt Rosencrantz | f541b77 | 2015-01-13 07:58:59 -0800 | [diff] [blame] | 376 | configServer, configServiceName, ch := createConfigServer(t, ctx) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 377 | defer configServer.Stop() |
Jiri Simsa | 3789339 | 2014-11-07 10:55:45 -0800 | [diff] [blame] | 378 | sh.SetConfigKey(mgmt.ParentNameConfigKey, configServiceName) |
| 379 | sh.SetConfigKey(mgmt.ProtocolConfigKey, "tcp") |
| 380 | sh.SetConfigKey(mgmt.AddressConfigKey, "127.0.0.1:0") |
Cosmos Nicolaou | 612ad38 | 2014-10-29 19:41:35 -0700 | [diff] [blame] | 381 | h, err := sh.Start("handleDefaults", nil) |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 382 | if err != nil { |
| 383 | t.Fatalf("unexpected error: %s", err) |
| 384 | } |
| 385 | s := expect.NewSession(t, h.Stdout(), time.Minute) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 386 | appCycleName := <-ch |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 387 | s.Expect("ready") |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 388 | appCycle := appcycle.AppCycleClient(appCycleName) |
Matt Rosencrantz | f541b77 | 2015-01-13 07:58:59 -0800 | [diff] [blame] | 389 | stream, err := appCycle.Stop(ctx) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 390 | if err != nil { |
| 391 | t.Fatalf("Got error: %v", err) |
| 392 | } |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 393 | rStream := stream.RecvStream() |
| 394 | if rStream.Advance() || rStream.Err() != nil { |
| 395 | t.Errorf("Expected EOF, got (%v, %v) instead: ", rStream.Value(), rStream.Err()) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 396 | } |
| 397 | if err := stream.Finish(); err != nil { |
| 398 | t.Fatalf("Got error: %v", err) |
| 399 | } |
Cosmos Nicolaou | cc58172 | 2014-10-07 12:45:39 -0700 | [diff] [blame] | 400 | s.Expectf("received signal %s", veyron2.RemoteStop) |
| 401 | fmt.Fprintf(h.Stdin(), "close\n") |
| 402 | s.ExpectEOF() |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 403 | } |