Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 1 | package namespace_test |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 2 | |
| 3 | import ( |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 4 | "runtime" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 5 | "runtime/debug" |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 6 | "sync" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 7 | "testing" |
| 8 | "time" |
| 9 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 10 | "v.io/core/veyron2" |
| 11 | "v.io/core/veyron2/context" |
| 12 | "v.io/core/veyron2/ipc" |
| 13 | "v.io/core/veyron2/naming" |
| 14 | "v.io/core/veyron2/options" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 15 | "v.io/core/veyron2/security" |
| 16 | verror "v.io/core/veyron2/verror2" |
| 17 | "v.io/core/veyron2/vlog" |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 18 | |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 19 | "v.io/core/veyron/lib/testutil" |
Ankur | fb9255a | 2015-01-21 15:29:38 -0800 | [diff] [blame] | 20 | tsecurity "v.io/core/veyron/lib/testutil/security" |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame] | 21 | _ "v.io/core/veyron/profiles" |
Jiri Simsa | 764efb7 | 2014-12-25 20:57:03 -0800 | [diff] [blame] | 22 | "v.io/core/veyron/runtimes/google/naming/namespace" |
| 23 | vsecurity "v.io/core/veyron/security" |
| 24 | service "v.io/core/veyron/services/mounttable/lib" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 25 | ) |
| 26 | |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 27 | func init() { |
| 28 | testutil.Init() |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 29 | } |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 30 | |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 31 | func createContexts(t *testing.T) (sc, c *context.T, cleanup func()) { |
Ankur | f416ac5 | 2015-01-29 13:58:24 -0800 | [diff] [blame] | 32 | ctx, shutdown := testutil.InitForTest() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 33 | var err error |
| 34 | if sc, err = veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("test-blessing")); err != nil { |
Ankur | fb9255a | 2015-01-21 15:29:38 -0800 | [diff] [blame] | 35 | t.Fatal(err) |
| 36 | } |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 37 | if c, err = veyron2.SetPrincipal(ctx, tsecurity.NewPrincipal("test-blessing")); err != nil { |
Ankur | fb9255a | 2015-01-21 15:29:38 -0800 | [diff] [blame] | 38 | t.Fatal(err) |
| 39 | } |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 40 | return sc, c, shutdown |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 41 | } |
| 42 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 43 | func boom(t *testing.T, f string, v ...interface{}) { |
| 44 | t.Logf(f, v...) |
| 45 | t.Fatal(string(debug.Stack())) |
| 46 | } |
| 47 | |
David Why Use Two When One Will Do Presotto | cce8f4f | 2014-09-30 14:50:44 -0700 | [diff] [blame] | 48 | // N squared but who cares, this is a little test. |
| 49 | // Ignores dups. |
| 50 | func contains(container, contained []string) bool { |
| 51 | L: |
| 52 | for _, d := range contained { |
| 53 | for _, r := range container { |
| 54 | if r == d { |
| 55 | continue L |
| 56 | } |
| 57 | } |
| 58 | return false |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 59 | } |
David Why Use Two When One Will Do Presotto | cce8f4f | 2014-09-30 14:50:44 -0700 | [diff] [blame] | 60 | return true |
| 61 | } |
| 62 | |
| 63 | func compare(t *testing.T, caller, name string, got, want []string) { |
| 64 | // Compare ignoring dups. |
| 65 | if !contains(got, want) || !contains(want, got) { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 66 | boom(t, "%s: %q: got %v, want %v", caller, name, got, want) |
| 67 | } |
| 68 | } |
| 69 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 70 | func doGlob(t *testing.T, ctx *context.T, ns naming.Namespace, pattern string, limit int) []string { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 71 | var replies []string |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 72 | rc, err := ns.Glob(ctx, pattern) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 73 | if err != nil { |
| 74 | boom(t, "Glob(%s): %s", pattern, err) |
| 75 | } |
| 76 | for s := range rc { |
| 77 | replies = append(replies, s.Name) |
David Why Use Two When One Will Do Presotto | 2db32f9 | 2014-07-18 12:20:35 -0700 | [diff] [blame] | 78 | if limit > 0 && len(replies) > limit { |
| 79 | boom(t, "Glob returns too many results, perhaps not limiting recursion") |
| 80 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 81 | } |
| 82 | return replies |
| 83 | } |
| 84 | |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 85 | type testServer struct { |
| 86 | suffix string |
| 87 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 88 | |
Todd Wang | 1fe7cdd | 2014-11-12 12:51:49 -0800 | [diff] [blame] | 89 | func (testServer) KnockKnock(ctx ipc.ServerContext) string { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 90 | return "Who's there?" |
| 91 | } |
| 92 | |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 93 | // testServer has the following namespace: |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 94 | // "" -> {level1} -> {level2} |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 95 | func (t *testServer) GlobChildren__(ipc.ServerContext) (<-chan string, error) { |
| 96 | ch := make(chan string, 1) |
| 97 | switch t.suffix { |
| 98 | case "": |
| 99 | ch <- "level1" |
| 100 | case "level1": |
| 101 | ch <- "level2" |
| 102 | default: |
| 103 | return nil, nil |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 104 | } |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 105 | close(ch) |
| 106 | return ch, nil |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 107 | } |
| 108 | |
Asim Shankar | 98d6596 | 2014-10-24 16:55:30 -0700 | [diff] [blame] | 109 | type allowEveryoneAuthorizer struct{} |
| 110 | |
| 111 | func (allowEveryoneAuthorizer) Authorize(security.Context) error { return nil } |
| 112 | |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 113 | type dispatcher struct{} |
| 114 | |
Robin Thellend | a02fe8f | 2014-11-19 09:58:29 -0800 | [diff] [blame] | 115 | func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) { |
Cosmos Nicolaou | 710daa2 | 2014-11-11 19:39:18 -0800 | [diff] [blame] | 116 | return &testServer{suffix}, allowEveryoneAuthorizer{}, nil |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 117 | } |
| 118 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 119 | func knockKnock(t *testing.T, ctx *context.T, name string) { |
| 120 | client := veyron2.GetClient(ctx) |
Matt Rosencrantz | f5afcaf | 2014-06-02 11:31:22 -0700 | [diff] [blame] | 121 | call, err := client.StartCall(ctx, name, "KnockKnock", nil) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 122 | if err != nil { |
| 123 | boom(t, "StartCall failed: %s", err) |
| 124 | } |
| 125 | var result string |
| 126 | if err := call.Finish(&result); err != nil { |
| 127 | boom(t, "Finish returned an error: %s", err) |
| 128 | } |
| 129 | if result != "Who's there?" { |
| 130 | boom(t, "Wrong result: %v", result) |
| 131 | } |
| 132 | } |
| 133 | |
Cosmos Nicolaou | 8bd8e10 | 2015-01-13 21:52:53 -0800 | [diff] [blame] | 134 | func doResolveTest(t *testing.T, fname string, f func(*context.T, string, ...naming.ResolveOpt) (*naming.MountEntry, error), ctx *context.T, name string, want []string, opts ...naming.ResolveOpt) { |
| 135 | me, err := f(ctx, name, opts...) |
Ryan Brown | bc2c87c | 2014-11-17 18:55:25 +0000 | [diff] [blame] | 136 | if err != nil { |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 137 | boom(t, "Failed to %s %s: %s", fname, name, err) |
Ryan Brown | bc2c87c | 2014-11-17 18:55:25 +0000 | [diff] [blame] | 138 | } |
Cosmos Nicolaou | 8bd8e10 | 2015-01-13 21:52:53 -0800 | [diff] [blame] | 139 | compare(t, fname, name, me.Names(), want) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 140 | } |
| 141 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 142 | func testResolveToMountTable(t *testing.T, ctx *context.T, ns naming.Namespace, name string, want ...string) { |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 143 | doResolveTest(t, "ResolveToMountTable", ns.ResolveToMountTable, ctx, name, want) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 144 | } |
| 145 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 146 | func testResolveToMountTableWithPattern(t *testing.T, ctx *context.T, ns naming.Namespace, name string, pattern naming.ResolveOpt, want ...string) { |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 147 | doResolveTest(t, "ResolveToMountTable", ns.ResolveToMountTable, ctx, name, want, pattern) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 148 | } |
| 149 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 150 | func testResolve(t *testing.T, ctx *context.T, ns naming.Namespace, name string, want ...string) { |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 151 | doResolveTest(t, "Resolve", ns.Resolve, ctx, name, want) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 152 | } |
| 153 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 154 | func testResolveWithPattern(t *testing.T, ctx *context.T, ns naming.Namespace, name string, pattern naming.ResolveOpt, want ...string) { |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 155 | doResolveTest(t, "Resolve", ns.Resolve, ctx, name, want, pattern) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 156 | } |
| 157 | |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 158 | type serverEntry struct { |
| 159 | mountPoint string |
| 160 | server ipc.Server |
| 161 | endpoint naming.Endpoint |
| 162 | name string |
| 163 | } |
| 164 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 165 | func runServer(t *testing.T, ctx *context.T, disp ipc.Dispatcher, mountPoint string) *serverEntry { |
| 166 | return run(t, ctx, disp, mountPoint, false) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 167 | } |
| 168 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 169 | func runMT(t *testing.T, ctx *context.T, mountPoint string) *serverEntry { |
Bogdan Caprita | e96cd04 | 2015-02-03 17:32:57 -0800 | [diff] [blame^] | 170 | mtd, err := service.NewMountTableDispatcher("") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 171 | if err != nil { |
Bogdan Caprita | e96cd04 | 2015-02-03 17:32:57 -0800 | [diff] [blame^] | 172 | boom(t, "NewMountTableDispatcher returned error: %v", err) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 173 | } |
Bogdan Caprita | e96cd04 | 2015-02-03 17:32:57 -0800 | [diff] [blame^] | 174 | return run(t, ctx, mtd, mountPoint, true) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 175 | } |
| 176 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 177 | func run(t *testing.T, ctx *context.T, disp ipc.Dispatcher, mountPoint string, mt bool) *serverEntry { |
| 178 | s, err := veyron2.NewServer(ctx, options.ServesMountTable(mt)) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 179 | if err != nil { |
| 180 | boom(t, "r.NewServer: %s", err) |
| 181 | } |
| 182 | // Add a mount table server. |
| 183 | // Start serving on a loopback address. |
Suharsh Sivakumar | d68949c | 2015-01-26 10:32:23 -0800 | [diff] [blame] | 184 | eps, err := s.Listen(veyron2.GetListenSpec(ctx)) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 185 | if err != nil { |
| 186 | boom(t, "Failed to Listen: %s", err) |
| 187 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 188 | if err := s.ServeDispatcher(mountPoint, disp); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 189 | boom(t, "Failed to serve mount table at %s: %s", mountPoint, err) |
| 190 | } |
Cosmos Nicolaou | 8bd8e10 | 2015-01-13 21:52:53 -0800 | [diff] [blame] | 191 | return &serverEntry{mountPoint: mountPoint, server: s, endpoint: eps[0], name: eps[0].Name()} |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | const ( |
| 195 | mt1MP = "mt1" |
| 196 | mt2MP = "mt2" |
| 197 | mt3MP = "mt3" |
| 198 | mt4MP = "mt4" |
| 199 | mt5MP = "mt5" |
| 200 | j1MP = "joke1" |
| 201 | j2MP = "joke2" |
| 202 | j3MP = "joke3" |
| 203 | |
| 204 | ttl = 100 * time.Second |
| 205 | ) |
| 206 | |
| 207 | // runMountTables creates a root mountable with some mount tables mounted |
| 208 | // in it: mt{1,2,3,4,5} |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 209 | func runMountTables(t *testing.T, ctx *context.T) (*serverEntry, map[string]*serverEntry) { |
| 210 | root := runMT(t, ctx, "") |
| 211 | veyron2.GetNamespace(ctx).SetRoots(root.name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 212 | t.Logf("mountTable %q -> %s", root.mountPoint, root.endpoint) |
| 213 | |
| 214 | mps := make(map[string]*serverEntry) |
| 215 | for _, mp := range []string{mt1MP, mt2MP, mt3MP, mt4MP, mt5MP} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 216 | m := runMT(t, ctx, mp) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 217 | t.Logf("mountTable %q -> %s", mp, m.endpoint) |
| 218 | mps[mp] = m |
| 219 | } |
| 220 | return root, mps |
| 221 | } |
| 222 | |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 223 | // createNamespace creates a hierarchy of mounttables and servers |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 224 | // as follows: |
| 225 | // /mt1, /mt2, /mt3, /mt4, /mt5, /joke1, /joke2, /joke3. |
| 226 | // That is, mt1 is a mount table mounted in the root mount table, |
| 227 | // joke1 is a server mounted in the root mount table. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 228 | func createNamespace(t *testing.T, ctx *context.T) (*serverEntry, map[string]*serverEntry, map[string]*serverEntry, func()) { |
| 229 | root, mts := runMountTables(t, ctx) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 230 | jokes := make(map[string]*serverEntry) |
| 231 | // Let's run some non-mount table services. |
| 232 | for _, j := range []string{j1MP, j2MP, j3MP} { |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 233 | disp := &dispatcher{} |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 234 | jokes[j] = runServer(t, ctx, disp, j) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 235 | } |
| 236 | return root, mts, jokes, func() { |
| 237 | for _, s := range jokes { |
| 238 | s.server.Stop() |
| 239 | } |
| 240 | for _, s := range mts { |
| 241 | s.server.Stop() |
| 242 | } |
| 243 | root.server.Stop() |
| 244 | } |
| 245 | } |
| 246 | |
| 247 | // runNestedMountTables creates some nested mount tables in the hierarchy |
| 248 | // created by createNamespace as follows: |
| 249 | // /mt4/foo, /mt4/foo/bar and /mt4/baz where foo, bar and baz are mount tables. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 250 | func runNestedMountTables(t *testing.T, ctx *context.T, mts map[string]*serverEntry) { |
| 251 | ns := veyron2.GetNamespace(ctx) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 252 | // Set up some nested mounts and verify resolution. |
| 253 | for _, m := range []string{"mt4/foo", "mt4/foo/bar"} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 254 | mts[m] = runMT(t, ctx, m) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 255 | } |
| 256 | |
| 257 | // Use a global name for a mount, rather than a relative one. |
| 258 | // We directly mount baz into the mt4/foo mount table. |
| 259 | globalMP := naming.JoinAddressName(mts["mt4/foo"].name, "baz") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 260 | mts["baz"] = runMT(t, ctx, "baz") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 261 | if err := ns.Mount(ctx, globalMP, mts["baz"].name, ttl); err != nil { |
| 262 | boom(t, "Failed to Mount %s: %s", globalMP, err) |
| 263 | } |
| 264 | } |
| 265 | |
| 266 | // TestNamespaceCommon tests common use of the Namespace library |
| 267 | // against a root mount table and some mount tables mounted on it. |
| 268 | func TestNamespaceCommon(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 269 | _, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 270 | defer cleanup() |
| 271 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 272 | root, mts, jokes, stopper := createNamespace(t, c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 273 | defer stopper() |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 274 | ns := veyron2.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 275 | |
| 276 | // All of the initial mounts are served by the root mounttable |
| 277 | // and hence ResolveToMountTable should return the root mountable |
| 278 | // as the address portion of the terminal name for those mounttables. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 279 | testResolveToMountTable(t, c, ns, "", root.name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 280 | for _, m := range []string{mt2MP, mt3MP, mt5MP} { |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 281 | rootMT := naming.Join(root.name, m) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 282 | // All of these mount tables are hosted by the root mount table |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 283 | testResolveToMountTable(t, c, ns, m, rootMT) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 284 | |
| 285 | // The server registered for each mount point is a mount table |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 286 | testResolve(t, c, ns, m, mts[m].name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 287 | |
| 288 | // ResolveToMountTable will walk through to the sub MountTables |
| 289 | mtbar := naming.Join(m, "bar") |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 290 | subMT := naming.Join(mts[m].name, "bar") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 291 | testResolveToMountTable(t, c, ns, mtbar, subMT) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 292 | } |
| 293 | |
| 294 | for _, j := range []string{j1MP, j2MP, j3MP} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 295 | testResolve(t, c, ns, j, jokes[j].name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 296 | } |
| 297 | } |
| 298 | |
Cosmos Nicolaou | 8bd8e10 | 2015-01-13 21:52:53 -0800 | [diff] [blame] | 299 | // TestNamespaceDetails tests more detailed use of the Namespace library. |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 300 | func TestNamespaceDetails(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 301 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 302 | defer cleanup() |
| 303 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 304 | root, mts, _, stopper := createNamespace(t, sc) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 305 | defer stopper() |
| 306 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 307 | ns := veyron2.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 308 | ns.SetRoots(root.name) |
| 309 | |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 310 | // /mt2 is not an endpoint. Thus, the example below will fail. |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 311 | mt3Server := mts[mt3MP].name |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 312 | mt2a := "/mt2/a" |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 313 | if err := ns.Mount(c, mt2a, mt3Server, ttl); verror.Is(err, naming.ErrNoSuchName.ID) { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 314 | boom(t, "Successfully mounted %s - expected an err %v, not %v", mt2a, naming.ErrNoSuchName, err) |
| 315 | } |
| 316 | |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 317 | // Mount using the relative name. |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 318 | // This means walk through mt2 if it already exists and mount within |
| 319 | // the lower level mount table, if the name doesn't exist we'll create |
| 320 | // a new name for it. |
| 321 | mt2a = "mt2/a" |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 322 | if err := ns.Mount(c, mt2a, mt3Server, ttl); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 323 | boom(t, "Failed to Mount %s: %s", mt2a, err) |
| 324 | } |
| 325 | |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 326 | mt2mt := naming.Join(mts[mt2MP].name, "a") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 327 | // The mt2/a is served by the mt2 mount table |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 328 | testResolveToMountTable(t, c, ns, mt2a, mt2mt) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 329 | // The server for mt2a is mt3server from the second mount above. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 330 | testResolve(t, c, ns, mt2a, mt3Server) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 331 | |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 332 | // Add two more mounts. The // should be stripped off of the |
| 333 | // second. |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 334 | for _, mp := range []struct{ name, server string }{ |
| 335 | {"mt2", mts[mt4MP].name}, |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 336 | {"mt2//", mts[mt5MP].name}, |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 337 | } { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 338 | if err := ns.Mount(c, mp.name, mp.server, ttl, naming.ServesMountTableOpt(true)); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 339 | boom(t, "Failed to Mount %s: %s", mp.name, err) |
| 340 | } |
| 341 | } |
| 342 | |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 343 | names := []string{naming.JoinAddressName(mts[mt4MP].name, "a"), |
| 344 | naming.JoinAddressName(mts[mt5MP].name, "a")} |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 345 | names = append(names, naming.JoinAddressName(mts[mt2MP].name, "a")) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 346 | // We now have 3 mount tables prepared to serve mt2/a |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 347 | testResolveToMountTable(t, c, ns, "mt2/a", names...) |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 348 | names = []string{mts[mt4MP].name, mts[mt5MP].name} |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 349 | names = append(names, mts[mt2MP].name) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 350 | testResolve(t, c, ns, "mt2", names...) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 351 | } |
| 352 | |
| 353 | // TestNestedMounts tests some more deeply nested mounts |
| 354 | func TestNestedMounts(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 355 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 356 | defer cleanup() |
| 357 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 358 | root, mts, _, stopper := createNamespace(t, sc) |
| 359 | runNestedMountTables(t, sc, mts) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 360 | defer stopper() |
| 361 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 362 | ns := veyron2.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 363 | ns.SetRoots(root.name) |
| 364 | |
| 365 | // Set up some nested mounts and verify resolution. |
| 366 | for _, m := range []string{"mt4/foo", "mt4/foo/bar"} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 367 | testResolve(t, c, ns, m, mts[m].name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 368 | } |
| 369 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 370 | testResolveToMountTable(t, c, ns, "mt4/foo", |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 371 | naming.JoinAddressName(mts[mt4MP].name, "foo")) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 372 | testResolveToMountTable(t, c, ns, "mt4/foo/bar", |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 373 | naming.JoinAddressName(mts["mt4/foo"].name, "bar")) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 374 | testResolveToMountTable(t, c, ns, "mt4/foo/baz", |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 375 | naming.JoinAddressName(mts["mt4/foo"].name, "baz")) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 376 | } |
| 377 | |
| 378 | // TestServers tests invoking RPCs on simple servers |
| 379 | func TestServers(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 380 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 381 | defer cleanup() |
| 382 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 383 | root, mts, jokes, stopper := createNamespace(t, sc) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 384 | defer stopper() |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 385 | ns := veyron2.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 386 | ns.SetRoots(root.name) |
| 387 | |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 388 | // Let's run some non-mount table services |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 389 | for _, j := range []string{j1MP, j2MP, j3MP} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 390 | testResolve(t, c, ns, j, jokes[j].name) |
| 391 | knockKnock(t, c, j) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 392 | globalName := naming.JoinAddressName(mts["mt4"].name, j) |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 393 | disp := &dispatcher{} |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 394 | gj := "g_" + j |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 395 | jokes[gj] = runServer(t, c, disp, globalName) |
| 396 | testResolve(t, c, ns, "mt4/"+j, jokes[gj].name) |
| 397 | knockKnock(t, c, "mt4/"+j) |
| 398 | testResolveToMountTable(t, c, ns, "mt4/"+j, globalName) |
| 399 | testResolveToMountTable(t, c, ns, "mt4/"+j+"/garbage", globalName+"/garbage") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 400 | } |
| 401 | } |
| 402 | |
| 403 | // TestGlob tests some glob patterns. |
| 404 | func TestGlob(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 405 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 406 | defer cleanup() |
| 407 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 408 | root, mts, _, stopper := createNamespace(t, sc) |
| 409 | runNestedMountTables(t, sc, mts) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 410 | defer stopper() |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 411 | ns := veyron2.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 412 | ns.SetRoots(root.name) |
| 413 | |
| 414 | tln := []string{"baz", "mt1", "mt2", "mt3", "mt4", "mt5", "joke1", "joke2", "joke3"} |
| 415 | barbaz := []string{"mt4/foo/bar", "mt4/foo/baz"} |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 416 | level12 := []string{"joke1/level1", "joke1/level1/level2", "joke2/level1", "joke2/level1/level2", "joke3/level1", "joke3/level1/level2"} |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 417 | foo := append([]string{"mt4/foo"}, barbaz...) |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 418 | foo = append(foo, level12...) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 419 | // Try various globs. |
| 420 | globTests := []struct { |
| 421 | pattern string |
| 422 | expected []string |
| 423 | }{ |
| 424 | {"*", tln}, |
David Why Use Two When One Will Do Presotto | cce8f4f | 2014-09-30 14:50:44 -0700 | [diff] [blame] | 425 | {"x", []string{}}, |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 426 | {"m*", []string{"mt1", "mt2", "mt3", "mt4", "mt5"}}, |
| 427 | {"mt[2,3]", []string{"mt2", "mt3"}}, |
| 428 | {"*z", []string{"baz"}}, |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 429 | {"joke1/*", []string{"joke1/level1"}}, |
| 430 | {"j?ke1/level1/*", []string{"joke1/level1/level2"}}, |
| 431 | {"joke1/level1/*", []string{"joke1/level1/level2"}}, |
| 432 | {"joke1/level1/level2/...", []string{"joke1/level1/level2"}}, |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 433 | {"...", append(append(tln, foo...), "")}, |
| 434 | {"*/...", append(tln, foo...)}, |
| 435 | {"*/foo/*", barbaz}, |
| 436 | {"*/*/*z", []string{"mt4/foo/baz"}}, |
| 437 | {"*/f??/*z", []string{"mt4/foo/baz"}}, |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 438 | {"mt4/foo/baz", []string{"mt4/foo/baz"}}, |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 439 | } |
| 440 | for _, test := range globTests { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 441 | out := doGlob(t, c, ns, test.pattern, 0) |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 442 | compare(t, "Glob", test.pattern, out, test.expected) |
Robin Thellend | 5f83857 | 2014-07-10 13:28:56 -0700 | [diff] [blame] | 443 | // Do the same with a full rooted name. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 444 | out = doGlob(t, c, ns, naming.JoinAddressName(root.name, test.pattern), 0) |
Robin Thellend | 5f83857 | 2014-07-10 13:28:56 -0700 | [diff] [blame] | 445 | var expectedWithRoot []string |
| 446 | for _, s := range test.expected { |
| 447 | expectedWithRoot = append(expectedWithRoot, naming.JoinAddressName(root.name, s)) |
| 448 | } |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 449 | compare(t, "Glob", test.pattern, out, expectedWithRoot) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 450 | } |
| 451 | } |
| 452 | |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 453 | type GlobbableServer struct { |
| 454 | callCount int |
| 455 | mu sync.Mutex |
| 456 | } |
| 457 | |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 458 | func (g *GlobbableServer) Glob__(ipc.ServerContext, string) (<-chan naming.VDLMountEntry, error) { |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 459 | g.mu.Lock() |
| 460 | defer g.mu.Unlock() |
| 461 | g.callCount++ |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 462 | return nil, nil |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 463 | } |
| 464 | |
| 465 | func (g *GlobbableServer) GetAndResetCount() int { |
| 466 | g.mu.Lock() |
| 467 | defer g.mu.Unlock() |
| 468 | cnt := g.callCount |
| 469 | g.callCount = 0 |
| 470 | |
| 471 | return cnt |
| 472 | } |
| 473 | |
| 474 | // TestGlobEarlyStop tests that Glob doesn't query terminal servers with finished patterns. |
| 475 | func TestGlobEarlyStop(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 476 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 477 | defer cleanup() |
| 478 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 479 | root, mts, _, stopper := createNamespace(t, sc) |
| 480 | runNestedMountTables(t, sc, mts) |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 481 | defer stopper() |
| 482 | |
| 483 | globServer := &GlobbableServer{} |
| 484 | name := naming.JoinAddressName(mts["mt4/foo/bar"].name, "glob") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 485 | runningGlobServer := runServer(t, c, testutil.LeafDispatcher(globServer, nil), name) |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 486 | defer runningGlobServer.server.Stop() |
| 487 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 488 | ns := veyron2.GetNamespace(c) |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 489 | ns.SetRoots(root.name) |
| 490 | |
| 491 | tests := []struct { |
| 492 | pattern string |
| 493 | expectedCalls int |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 494 | expected []string |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 495 | }{ |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 496 | {"mt4/foo/bar/glob", 0, []string{"mt4/foo/bar/glob"}}, |
| 497 | {"mt4/foo/bar/glob/...", 1, []string{"mt4/foo/bar/glob"}}, |
| 498 | {"mt4/foo/bar/glob/*", 1, nil}, |
| 499 | {"mt4/foo/bar/***", 0, []string{"mt4/foo/bar", "mt4/foo/bar/glob"}}, |
| 500 | {"mt4/foo/bar/...", 1, []string{"mt4/foo/bar", "mt4/foo/bar/glob"}}, |
| 501 | {"mt4/foo/bar/*", 0, []string{"mt4/foo/bar/glob"}}, |
| 502 | {"mt4/***/bar/***", 0, []string{"mt4/foo/bar", "mt4/foo/bar/glob"}}, |
| 503 | {"mt4/*/bar/***", 0, []string{"mt4/foo/bar", "mt4/foo/bar/glob"}}, |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 504 | } |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 505 | // Test allowing the tests to descend into leaves. |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 506 | for _, test := range tests { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 507 | out := doGlob(t, c, ns, test.pattern, 0) |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 508 | compare(t, "Glob", test.pattern, out, test.expected) |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 509 | if calls := globServer.GetAndResetCount(); calls != test.expectedCalls { |
| 510 | boom(t, "Wrong number of Glob calls to terminal server got: %d want: %d.", calls, test.expectedCalls) |
| 511 | } |
| 512 | } |
| 513 | } |
| 514 | |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 515 | func TestCycles(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 516 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 517 | defer cleanup() |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 518 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 519 | root, _, _, stopper := createNamespace(t, sc) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 520 | defer stopper() |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 521 | ns := veyron2.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 522 | ns.SetRoots(root.name) |
| 523 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 524 | c1 := runMT(t, c, "c1") |
| 525 | c2 := runMT(t, c, "c2") |
| 526 | c3 := runMT(t, c, "c3") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 527 | defer c1.server.Stop() |
| 528 | defer c2.server.Stop() |
| 529 | defer c3.server.Stop() |
| 530 | |
| 531 | m := "c1/c2" |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 532 | if err := ns.Mount(c, m, c1.name, ttl, naming.ServesMountTableOpt(true)); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 533 | boom(t, "Failed to Mount %s: %s", "c1/c2", err) |
| 534 | } |
| 535 | |
| 536 | m = "c1/c2/c3" |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 537 | if err := ns.Mount(c, m, c3.name, ttl, naming.ServesMountTableOpt(true)); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 538 | boom(t, "Failed to Mount %s: %s", m, err) |
| 539 | } |
| 540 | |
| 541 | m = "c1/c3/c4" |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 542 | if err := ns.Mount(c, m, c1.name, ttl, naming.ServesMountTableOpt(true)); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 543 | boom(t, "Failed to Mount %s: %s", m, err) |
| 544 | } |
| 545 | |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 546 | // Since c1 was mounted with the Serve call, it will have both the tcp and ws endpoints. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 547 | testResolve(t, c, ns, "c1", c1.name) |
| 548 | testResolve(t, c, ns, "c1/c2", c1.name) |
| 549 | testResolve(t, c, ns, "c1/c3", c3.name) |
| 550 | testResolve(t, c, ns, "c1/c3/c4", c1.name) |
| 551 | testResolve(t, c, ns, "c1/c3/c4/c3/c4", c1.name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 552 | cycle := "c3/c4" |
| 553 | for i := 0; i < 40; i++ { |
| 554 | cycle += "/c3/c4" |
| 555 | } |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 556 | if _, err := ns.Resolve(c, "c1/"+cycle); !verror.Is(err, naming.ErrResolutionDepthExceeded.ID) { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 557 | boom(t, "Failed to detect cycle") |
| 558 | } |
| 559 | |
David Why Use Two When One Will Do Presotto | 2db32f9 | 2014-07-18 12:20:35 -0700 | [diff] [blame] | 560 | // Perform the glob with a response length limit. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 561 | doGlob(t, c, ns, "c1/...", 1000) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 562 | } |
| 563 | |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 564 | // TestGoroutineLeaks tests for leaking goroutines - we have many:-( |
| 565 | func TestGoroutineLeaks(t *testing.T) { |
| 566 | t.Skip() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 567 | sc, _, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 568 | defer cleanup() |
| 569 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 570 | _, _, _, stopper := createNamespace(t, sc) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 571 | defer func() { |
| 572 | vlog.Infof("%d goroutines:", runtime.NumGoroutine()) |
| 573 | }() |
| 574 | defer stopper() |
| 575 | defer func() { |
| 576 | vlog.Infof("%d goroutines:", runtime.NumGoroutine()) |
| 577 | }() |
| 578 | //panic("this will show up lots of goroutine+channel leaks!!!!") |
| 579 | } |
| 580 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 581 | func TestBadRoots(t *testing.T) { |
Matt Rosencrantz | afe4964 | 2014-12-05 15:10:56 -0800 | [diff] [blame] | 582 | if _, err := namespace.New(); err != nil { |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 583 | t.Errorf("namespace.New should not have failed with no roots") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 584 | } |
Matt Rosencrantz | afe4964 | 2014-12-05 15:10:56 -0800 | [diff] [blame] | 585 | if _, err := namespace.New("not a rooted name"); err == nil { |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 586 | t.Errorf("namespace.New should have failed with an unrooted name") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 587 | } |
| 588 | } |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 589 | |
| 590 | func bless(blesser, delegate security.Principal, extension string) { |
| 591 | b, err := blesser.Bless(delegate.PublicKey(), blesser.BlessingStore().Default(), extension, security.UnconstrainedUse()) |
| 592 | if err != nil { |
| 593 | panic(err) |
| 594 | } |
| 595 | delegate.BlessingStore().SetDefault(b) |
| 596 | } |
| 597 | |
| 598 | func TestRootBlessing(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 599 | c, cc, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 600 | defer cleanup() |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 601 | |
| 602 | proot, err := vsecurity.NewPrincipal() |
| 603 | if err != nil { |
| 604 | panic(err) |
| 605 | } |
| 606 | b, err := proot.BlessSelf("root") |
| 607 | if err != nil { |
| 608 | panic(err) |
| 609 | } |
| 610 | proot.BlessingStore().SetDefault(b) |
| 611 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 612 | sprincipal := veyron2.GetPrincipal(c) |
| 613 | cprincipal := veyron2.GetPrincipal(cc) |
| 614 | bless(proot, sprincipal, "server") |
| 615 | bless(proot, cprincipal, "client") |
| 616 | cprincipal.AddToRoots(proot.BlessingStore().Default()) |
| 617 | sprincipal.AddToRoots(proot.BlessingStore().Default()) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 618 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 619 | root, mts, _, stopper := createNamespace(t, c) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 620 | defer stopper() |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 621 | ns := veyron2.GetNamespace(c) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 622 | |
| 623 | name := naming.Join(root.name, mt2MP) |
| 624 | // First check with a non-matching blessing pattern. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 625 | _, err = ns.Resolve(c, name, naming.RootBlessingPatternOpt("root/foobar")) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 626 | if !verror.Is(err, verror.NotTrusted.ID) { |
| 627 | t.Errorf("Resolve expected NotTrusted error, got %v", err) |
| 628 | } |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 629 | _, err = ns.ResolveToMountTable(c, name, naming.RootBlessingPatternOpt("root/foobar")) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 630 | if !verror.Is(err, verror.NotTrusted.ID) { |
| 631 | t.Errorf("ResolveToMountTable expected NotTrusted error, got %v", err) |
| 632 | } |
| 633 | |
| 634 | // Now check a matching pattern. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 635 | testResolveWithPattern(t, c, ns, name, naming.RootBlessingPatternOpt("root/server"), mts[mt2MP].name) |
| 636 | testResolveToMountTableWithPattern(t, c, ns, name, naming.RootBlessingPatternOpt("root/server"), name) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 637 | |
| 638 | // After successful lookup it should be cached, so the pattern doesn't matter. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 639 | testResolveWithPattern(t, c, ns, name, naming.RootBlessingPatternOpt("root/foobar"), mts[mt2MP].name) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 640 | } |