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