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