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