Cosmos Nicolaou | b07fa77 | 2014-05-16 08:07:02 -0700 | [diff] [blame] | 1 | package rt_test |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
| 4 | "fmt" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 5 | "io" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | "os" |
Bogdan Caprita | 0a9f998 | 2014-06-11 15:50:08 -0700 | [diff] [blame] | 7 | "reflect" |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 8 | "strings" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 9 | "testing" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 10 | "time" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 11 | |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 12 | "veyron.io/veyron/veyron2" |
| 13 | "veyron.io/veyron/veyron2/ipc" |
| 14 | "veyron.io/veyron/veyron2/mgmt" |
| 15 | "veyron.io/veyron/veyron2/naming" |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 16 | "veyron.io/veyron/veyron2/options" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 17 | "veyron.io/veyron/veyron2/services/mgmt/appcycle" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 18 | |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 19 | "veyron.io/veyron/veyron/lib/expect" |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 20 | "veyron.io/veyron/veyron/lib/flags/consts" |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 21 | "veyron.io/veyron/veyron/lib/modules" |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 22 | "veyron.io/veyron/veyron/lib/testutil" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 23 | "veyron.io/veyron/veyron/lib/testutil/security" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 24 | "veyron.io/veyron/veyron/profiles" |
Jiri Simsa | 519c507 | 2014-09-17 21:37:57 -0700 | [diff] [blame] | 25 | "veyron.io/veyron/veyron/runtimes/google/rt" |
| 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 | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 30 | var profileOpt = options.Profile{profiles.New()} |
| 31 | |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 32 | const ( |
| 33 | noWaitersCmd = "noWaiters" |
| 34 | forceStopCmd = "forceStop" |
| 35 | appCmd = "app" |
| 36 | ) |
| 37 | |
| 38 | func init() { |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 39 | testutil.Init() |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 40 | modules.RegisterChild(noWaitersCmd, "", noWaiters) |
| 41 | modules.RegisterChild(forceStopCmd, "", forceStop) |
| 42 | modules.RegisterChild(appCmd, "", app) |
| 43 | } |
| 44 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 45 | // TestBasic verifies that the basic plumbing works: LocalStop calls result in |
| 46 | // stop messages being sent on the channel passed to WaitForStop. |
| 47 | func TestBasic(t *testing.T) { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 48 | m, _ := rt.New(profileOpt) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 49 | ch := make(chan string, 1) |
| 50 | m.WaitForStop(ch) |
| 51 | for i := 0; i < 10; i++ { |
| 52 | m.Stop() |
Cosmos Nicolaou | b07fa77 | 2014-05-16 08:07:02 -0700 | [diff] [blame] | 53 | if want, got := veyron2.LocalStop, <-ch; want != got { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 54 | t.Errorf("WaitForStop want %q got %q", want, got) |
| 55 | } |
| 56 | select { |
| 57 | case s := <-ch: |
| 58 | t.Errorf("channel expected to be empty, got %q instead", s) |
| 59 | default: |
| 60 | } |
| 61 | } |
| 62 | } |
| 63 | |
| 64 | // TestMultipleWaiters verifies that the plumbing works with more than one |
| 65 | // registered wait channel. |
| 66 | func TestMultipleWaiters(t *testing.T) { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 67 | m, _ := rt.New(profileOpt) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 68 | ch1 := make(chan string, 1) |
| 69 | m.WaitForStop(ch1) |
| 70 | ch2 := make(chan string, 1) |
| 71 | m.WaitForStop(ch2) |
| 72 | for i := 0; i < 10; i++ { |
| 73 | m.Stop() |
Cosmos Nicolaou | b07fa77 | 2014-05-16 08:07:02 -0700 | [diff] [blame] | 74 | if want, got := veyron2.LocalStop, <-ch1; want != got { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 75 | t.Errorf("WaitForStop want %q got %q", want, got) |
| 76 | } |
Cosmos Nicolaou | b07fa77 | 2014-05-16 08:07:02 -0700 | [diff] [blame] | 77 | if want, got := veyron2.LocalStop, <-ch2; want != got { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 78 | t.Errorf("WaitForStop want %q got %q", want, got) |
| 79 | } |
| 80 | } |
| 81 | } |
| 82 | |
| 83 | // TestMultipleStops verifies that LocalStop does not block even if the wait |
| 84 | // channel is not being drained: once the channel's buffer fills up, future |
| 85 | // Stops become no-ops. |
| 86 | func TestMultipleStops(t *testing.T) { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 87 | m, _ := rt.New(profileOpt) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 88 | ch := make(chan string, 1) |
| 89 | m.WaitForStop(ch) |
| 90 | for i := 0; i < 10; i++ { |
| 91 | m.Stop() |
| 92 | } |
Cosmos Nicolaou | b07fa77 | 2014-05-16 08:07:02 -0700 | [diff] [blame] | 93 | if want, got := veyron2.LocalStop, <-ch; want != got { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 94 | t.Errorf("WaitForStop want %q got %q", want, got) |
| 95 | } |
| 96 | select { |
| 97 | case s := <-ch: |
| 98 | t.Errorf("channel expected to be empty, got %q instead", s) |
| 99 | default: |
| 100 | } |
| 101 | } |
| 102 | |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 103 | func noWaiters(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 104 | m, _ := rt.New(profileOpt) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 105 | fmt.Fprintf(stdout, "ready\n") |
| 106 | modules.WaitForEOF(stdin) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 107 | m.Stop() |
| 108 | os.Exit(42) // This should not be reached. |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 109 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 110 | } |
| 111 | |
Bogdan Caprita | 1002ba4 | 2014-06-06 19:24:40 -0700 | [diff] [blame] | 112 | // TestNoWaiters verifies that the child process exits in the absence of any |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 113 | // wait channel being registered with its runtime. |
| 114 | func TestNoWaiters(t *testing.T) { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 115 | sh := modules.NewShell(noWaitersCmd) |
| 116 | defer sh.Cleanup(os.Stderr, os.Stderr) |
Cosmos Nicolaou | 612ad38 | 2014-10-29 19:41:35 -0700 | [diff] [blame] | 117 | h, err := sh.Start(noWaitersCmd, nil) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 118 | if err != nil { |
| 119 | t.Fatalf("unexpected error: %s", err) |
| 120 | } |
| 121 | expect.NewSession(t, h.Stdout(), time.Minute).Expect("ready") |
| 122 | want := fmt.Sprintf("exit status %d", veyron2.UnhandledStopExitCode) |
| 123 | if err = h.Shutdown(os.Stderr, os.Stderr); err == nil || err.Error() != want { |
| 124 | t.Errorf("got %v, want %s", err, want) |
| 125 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 126 | } |
| 127 | |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 128 | func forceStop(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 129 | m, _ := rt.New(profileOpt) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 130 | fmt.Fprintf(stdout, "ready\n") |
| 131 | modules.WaitForEOF(stdin) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 132 | m.WaitForStop(make(chan string, 1)) |
| 133 | m.ForceStop() |
| 134 | os.Exit(42) // This should not be reached. |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 135 | return nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 136 | } |
| 137 | |
| 138 | // TestForceStop verifies that ForceStop causes the child process to exit |
| 139 | // immediately. |
| 140 | func TestForceStop(t *testing.T) { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 141 | sh := modules.NewShell(forceStopCmd) |
| 142 | defer sh.Cleanup(os.Stderr, os.Stderr) |
Cosmos Nicolaou | 612ad38 | 2014-10-29 19:41:35 -0700 | [diff] [blame] | 143 | h, err := sh.Start(forceStopCmd, nil) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 144 | if err != nil { |
| 145 | t.Fatalf("unexpected error: %s", err) |
| 146 | } |
| 147 | s := expect.NewSession(t, h.Stdout(), time.Minute) |
| 148 | s.Expect("ready") |
| 149 | err = h.Shutdown(os.Stderr, os.Stderr) |
| 150 | want := fmt.Sprintf("exit status %d", veyron2.UnhandledStopExitCode) |
| 151 | if err == nil || err.Error() != want { |
| 152 | t.Errorf("got %v, want %s", err, want) |
| 153 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 154 | } |
Bogdan Caprita | 0a9f998 | 2014-06-11 15:50:08 -0700 | [diff] [blame] | 155 | |
| 156 | func checkProgress(t *testing.T, ch <-chan veyron2.Task, progress, goal int) { |
| 157 | if want, got := (veyron2.Task{progress, goal}), <-ch; !reflect.DeepEqual(want, got) { |
| 158 | t.Errorf("Unexpected progress: want %+v, got %+v", want, got) |
| 159 | } |
| 160 | } |
| 161 | |
| 162 | func checkNoProgress(t *testing.T, ch <-chan veyron2.Task) { |
| 163 | select { |
| 164 | case p := <-ch: |
| 165 | t.Errorf("channel expected to be empty, got %+v instead", p) |
| 166 | default: |
| 167 | } |
| 168 | } |
| 169 | |
| 170 | // TestProgress verifies that the ticker update/track logic works for a single |
| 171 | // tracker. |
| 172 | func TestProgress(t *testing.T) { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 173 | m, _ := rt.New(profileOpt) |
Bogdan Caprita | 0a9f998 | 2014-06-11 15:50:08 -0700 | [diff] [blame] | 174 | m.AdvanceGoal(50) |
| 175 | ch := make(chan veyron2.Task, 1) |
| 176 | m.TrackTask(ch) |
| 177 | checkNoProgress(t, ch) |
| 178 | m.AdvanceProgress(10) |
| 179 | checkProgress(t, ch, 10, 50) |
| 180 | checkNoProgress(t, ch) |
| 181 | m.AdvanceProgress(5) |
| 182 | checkProgress(t, ch, 15, 50) |
| 183 | m.AdvanceGoal(50) |
| 184 | checkProgress(t, ch, 15, 100) |
| 185 | m.AdvanceProgress(1) |
| 186 | checkProgress(t, ch, 16, 100) |
| 187 | m.AdvanceGoal(10) |
| 188 | checkProgress(t, ch, 16, 110) |
| 189 | m.AdvanceProgress(-13) |
| 190 | checkNoProgress(t, ch) |
| 191 | m.AdvanceGoal(0) |
| 192 | checkNoProgress(t, ch) |
Bogdan Caprita | 4258d88 | 2014-07-02 09:15:22 -0700 | [diff] [blame] | 193 | m.Cleanup() |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 194 | if _, ok := <-ch; ok { |
| 195 | t.Errorf("Expected channel to be closed") |
| 196 | } |
Bogdan Caprita | 0a9f998 | 2014-06-11 15:50:08 -0700 | [diff] [blame] | 197 | } |
| 198 | |
| 199 | // TestProgressMultipleTrackers verifies that the ticker update/track logic |
| 200 | // works for more than one tracker. It also ensures that the runtime doesn't |
| 201 | // block when the tracker channels are full. |
| 202 | func TestProgressMultipleTrackers(t *testing.T) { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 203 | m, _ := rt.New(profileOpt) |
Bogdan Caprita | 0a9f998 | 2014-06-11 15:50:08 -0700 | [diff] [blame] | 204 | // ch1 is 1-buffered, ch2 is 2-buffered. |
| 205 | ch1, ch2 := make(chan veyron2.Task, 1), make(chan veyron2.Task, 2) |
| 206 | m.TrackTask(ch1) |
| 207 | m.TrackTask(ch2) |
| 208 | checkNoProgress(t, ch1) |
| 209 | checkNoProgress(t, ch2) |
| 210 | m.AdvanceProgress(1) |
| 211 | checkProgress(t, ch1, 1, 0) |
| 212 | checkNoProgress(t, ch1) |
| 213 | checkProgress(t, ch2, 1, 0) |
| 214 | checkNoProgress(t, ch2) |
| 215 | for i := 0; i < 10; i++ { |
| 216 | m.AdvanceProgress(1) |
| 217 | } |
| 218 | checkProgress(t, ch1, 2, 0) |
| 219 | checkNoProgress(t, ch1) |
| 220 | checkProgress(t, ch2, 2, 0) |
| 221 | checkProgress(t, ch2, 3, 0) |
| 222 | checkNoProgress(t, ch2) |
| 223 | m.AdvanceGoal(4) |
| 224 | checkProgress(t, ch1, 11, 4) |
| 225 | checkProgress(t, ch2, 11, 4) |
Bogdan Caprita | 4258d88 | 2014-07-02 09:15:22 -0700 | [diff] [blame] | 226 | m.Cleanup() |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 227 | if _, ok := <-ch1; ok { |
| 228 | t.Errorf("Expected channel to be closed") |
| 229 | } |
| 230 | if _, ok := <-ch2; ok { |
| 231 | t.Errorf("Expected channel to be closed") |
| 232 | } |
| 233 | } |
| 234 | |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 235 | func app(stdin io.Reader, stdout, stderr io.Writer, env map[string]string, args ...string) error { |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 236 | r, err := rt.New(profileOpt) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 237 | if err != nil { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 238 | return err |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 239 | } |
Bogdan Caprita | 4258d88 | 2014-07-02 09:15:22 -0700 | [diff] [blame] | 240 | defer r.Cleanup() |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 241 | ch := make(chan string, 1) |
| 242 | r.WaitForStop(ch) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 243 | fmt.Fprintf(stdout, "Got %s\n", <-ch) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 244 | r.AdvanceGoal(10) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 245 | fmt.Fprintf(stdout, "Doing some work\n") |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 246 | r.AdvanceProgress(2) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 247 | fmt.Fprintf(stdout, "Doing some more work\n") |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 248 | r.AdvanceProgress(5) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 249 | return nil |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 250 | } |
| 251 | |
| 252 | type configServer struct { |
| 253 | ch chan<- string |
| 254 | } |
| 255 | |
| 256 | func (c *configServer) Set(_ ipc.ServerContext, key, value string) error { |
| 257 | if key != mgmt.AppCycleManagerConfigKey { |
| 258 | return fmt.Errorf("Unexpected key: %v", key) |
| 259 | } |
| 260 | c.ch <- value |
| 261 | return nil |
| 262 | |
| 263 | } |
| 264 | |
Cosmos Nicolaou | 39a00e0 | 2014-08-14 11:04:14 -0700 | [diff] [blame] | 265 | func createConfigServer(t *testing.T, r veyron2.Runtime) (ipc.Server, string, <-chan string) { |
| 266 | server, err := r.NewServer() |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 267 | if err != nil { |
| 268 | t.Fatalf("Got error: %v", err) |
| 269 | } |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 270 | ch := make(chan string) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 271 | |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 272 | var ep naming.Endpoint |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 273 | if ep, err = server.Listen(profiles.LocalListenSpec); err != nil { |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 274 | t.Fatalf("Got error: %v", err) |
| 275 | } |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 276 | if err := server.Serve("", node.ConfigServer(&configServer{ch}), vflag.NewAuthorizerOrDie()); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 277 | t.Fatalf("Got error: %v", err) |
| 278 | } |
| 279 | return server, naming.JoinAddressName(ep.String(), ""), ch |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 280 | |
| 281 | } |
| 282 | |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 283 | func setupRemoteAppCycleMgr(t *testing.T) (veyron2.Runtime, modules.Handle, appcycle.AppCycleClientMethods, func()) { |
Cosmos Nicolaou | 39a00e0 | 2014-08-14 11:04:14 -0700 | [diff] [blame] | 284 | // We need to use the public API since stubs are used below (and they |
| 285 | // refer to the global rt.R() function), but we take care to make sure |
| 286 | // that the "google" runtime we are trying to test in this package is |
| 287 | // the one being used. |
Cosmos Nicolaou | aadfd4d | 2014-10-31 18:51:25 -0700 | [diff] [blame] | 288 | r, _ := rt.New(profileOpt) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 289 | |
Cosmos Nicolaou | bc22fa1 | 2014-10-14 20:51:55 -0700 | [diff] [blame] | 290 | childcreds := security.NewVeyronCredentials(r.Principal(), appCmd) |
Cosmos Nicolaou | 39a00e0 | 2014-08-14 11:04:14 -0700 | [diff] [blame] | 291 | configServer, configServiceName, ch := createConfigServer(t, r) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 292 | sh := modules.NewShell(appCmd) |
Asim Shankar | 95910b6 | 2014-10-31 22:02:29 -0700 | [diff] [blame] | 293 | sh.SetVar(consts.VeyronCredentials, childcreds) |
Jiri Simsa | 3789339 | 2014-11-07 10:55:45 -0800 | [diff] [blame^] | 294 | sh.SetConfigKey(mgmt.ParentNameConfigKey, configServiceName) |
| 295 | sh.SetConfigKey(mgmt.ProtocolConfigKey, "tcp") |
| 296 | sh.SetConfigKey(mgmt.AddressConfigKey, "127.0.0.1:0") |
Cosmos Nicolaou | 612ad38 | 2014-10-29 19:41:35 -0700 | [diff] [blame] | 297 | h, err := sh.Start("app", nil) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 298 | if err != nil { |
| 299 | t.Fatalf("unexpected error: %s", err) |
| 300 | } |
| 301 | |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 302 | appCycleName := <-ch |
Todd Wang | 702385a | 2014-11-07 01:54:08 -0800 | [diff] [blame] | 303 | appCycle := appcycle.AppCycleClient(appCycleName) |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 304 | return r, h, appCycle, func() { |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 305 | configServer.Stop() |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 306 | sh.Cleanup(os.Stderr, os.Stderr) |
Cosmos Nicolaou | bc22fa1 | 2014-10-14 20:51:55 -0700 | [diff] [blame] | 307 | os.RemoveAll(childcreds) |
Bogdan Caprita | 4258d88 | 2014-07-02 09:15:22 -0700 | [diff] [blame] | 308 | // Don't do r.Cleanup() since the runtime needs to be used by |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 309 | // more than one test case. |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | // TestRemoteForceStop verifies that the child process exits when sending it |
| 314 | // a remote ForceStop rpc. |
| 315 | func TestRemoteForceStop(t *testing.T) { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 316 | r, h, appCycle, cleanup := setupRemoteAppCycleMgr(t) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 317 | defer cleanup() |
Cosmos Nicolaou | 39a00e0 | 2014-08-14 11:04:14 -0700 | [diff] [blame] | 318 | if err := appCycle.ForceStop(r.NewContext()); err == nil || !strings.Contains(err.Error(), "EOF") { |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 319 | t.Fatalf("Expected EOF error, got %v instead", err) |
| 320 | } |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 321 | s := expect.NewSession(t, h.Stdout(), time.Minute) |
| 322 | s.ExpectEOF() |
| 323 | err := h.Shutdown(os.Stderr, os.Stderr) |
| 324 | want := fmt.Sprintf("exit status %d", veyron2.ForceStopExitCode) |
| 325 | if err == nil || err.Error() != want { |
| 326 | t.Errorf("got %v, want %s", err, want) |
| 327 | } |
| 328 | |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 329 | } |
| 330 | |
| 331 | // TestRemoteStop verifies that the child shuts down cleanly when sending it |
| 332 | // a remote Stop rpc. |
| 333 | func TestRemoteStop(t *testing.T) { |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 334 | r, h, appCycle, cleanup := setupRemoteAppCycleMgr(t) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 335 | defer cleanup() |
Cosmos Nicolaou | 39a00e0 | 2014-08-14 11:04:14 -0700 | [diff] [blame] | 336 | stream, err := appCycle.Stop(r.NewContext()) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 337 | if err != nil { |
| 338 | t.Fatalf("Got error: %v", err) |
| 339 | } |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 340 | rStream := stream.RecvStream() |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 341 | expectTask := func(progress, goal int32) { |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 342 | if !rStream.Advance() { |
| 343 | t.Fatalf("unexpected streaming error: %q", rStream.Err()) |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 344 | } |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 345 | task := rStream.Value() |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 346 | if task.Progress != progress || task.Goal != goal { |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 347 | t.Errorf("Got (%d, %d), want (%d, %d)", task.Progress, task.Goal, progress, goal) |
| 348 | } |
| 349 | } |
| 350 | expectTask(0, 10) |
| 351 | expectTask(2, 10) |
| 352 | expectTask(7, 10) |
Shyam Jayaraman | 97b9dca | 2014-07-31 13:30:46 -0700 | [diff] [blame] | 353 | if rStream.Advance() || rStream.Err() != nil { |
| 354 | t.Errorf("Expected EOF, got (%v, %v) instead", rStream.Value(), rStream.Err()) |
Bogdan Caprita | a4d9ee4 | 2014-06-20 16:42:53 -0700 | [diff] [blame] | 355 | } |
| 356 | if err := stream.Finish(); err != nil { |
| 357 | t.Errorf("Got error %v", err) |
| 358 | } |
Cosmos Nicolaou | 59496fe | 2014-10-14 11:21:05 -0700 | [diff] [blame] | 359 | s := expect.NewSession(t, h.Stdout(), time.Minute) |
| 360 | s.Expect(fmt.Sprintf("Got %s", veyron2.RemoteStop)) |
| 361 | s.Expect("Doing some work") |
| 362 | s.Expect("Doing some more work") |
| 363 | s.ExpectEOF() |
| 364 | if err := h.Shutdown(os.Stderr, os.Stderr); err != nil { |
| 365 | t.Fatalf("unexpected error: %s", err) |
| 366 | } |
Bogdan Caprita | 0a9f998 | 2014-06-11 15:50:08 -0700 | [diff] [blame] | 367 | } |