Jiri Simsa | d7616c9 | 2015-03-24 23:44:30 -0700 | [diff] [blame] | 1 | // Copyright 2015 The Vanadium Authors. All rights reserved. |
| 2 | // Use of this source code is governed by a BSD-style |
| 3 | // license that can be found in the LICENSE file. |
| 4 | |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 5 | package namespace_test |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 8 | "fmt" |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 9 | "runtime" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 10 | "runtime/debug" |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 11 | "sync" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | "testing" |
| 13 | "time" |
| 14 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 15 | "v.io/v23" |
| 16 | "v.io/v23/context" |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 17 | "v.io/v23/namespace" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 18 | "v.io/v23/naming" |
| 19 | "v.io/v23/options" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 20 | "v.io/v23/rpc" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 21 | "v.io/v23/security" |
| 22 | "v.io/v23/verror" |
Jiri Simsa | 337af23 | 2015-02-27 14:36:46 -0800 | [diff] [blame] | 23 | "v.io/x/lib/vlog" |
Cosmos Nicolaou | f889c73 | 2014-10-16 20:46:54 -0700 | [diff] [blame] | 24 | |
Jiri Simsa | ffceefa | 2015-02-28 11:03:34 -0800 | [diff] [blame] | 25 | _ "v.io/x/ref/profiles" |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 26 | inamespace "v.io/x/ref/profiles/internal/naming/namespace" |
Todd Wang | 5987a94 | 2015-04-06 11:06:17 -0700 | [diff] [blame] | 27 | "v.io/x/ref/services/mounttable/mounttablelib" |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 28 | "v.io/x/ref/test" |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 29 | "v.io/x/ref/test/testutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 30 | ) |
| 31 | |
Suharsh Sivakumar | d19c95d | 2015-02-19 14:44:50 -0800 | [diff] [blame] | 32 | //go:generate v23 test generate |
Asim Shankar | c920db3 | 2014-10-16 19:18:21 -0700 | [diff] [blame] | 33 | |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 34 | func createContexts(t *testing.T) (sc, c *context.T, cleanup func()) { |
Cosmos Nicolaou | a18a1eb | 2015-03-12 13:15:01 -0700 | [diff] [blame] | 35 | ctx, shutdown := test.InitForTest() |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 36 | var ( |
| 37 | err error |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 38 | psc = testutil.NewPrincipal("sc") |
| 39 | pc = testutil.NewPrincipal("c") |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 40 | ) |
| 41 | // Setup the principals so that they recognize each other. |
| 42 | if err := psc.AddToRoots(pc.BlessingStore().Default()); err != nil { |
Ankur | fb9255a | 2015-01-21 15:29:38 -0800 | [diff] [blame] | 43 | t.Fatal(err) |
| 44 | } |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 45 | if err := pc.AddToRoots(psc.BlessingStore().Default()); err != nil { |
| 46 | t.Fatal(err) |
| 47 | } |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 48 | if sc, err = v23.SetPrincipal(ctx, psc); err != nil { |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 49 | t.Fatal(err) |
| 50 | } |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 51 | if c, err = v23.SetPrincipal(ctx, pc); err != nil { |
Ankur | fb9255a | 2015-01-21 15:29:38 -0800 | [diff] [blame] | 52 | t.Fatal(err) |
| 53 | } |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 54 | return sc, c, shutdown |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 55 | } |
| 56 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 57 | func boom(t *testing.T, f string, v ...interface{}) { |
| 58 | t.Logf(f, v...) |
| 59 | t.Fatal(string(debug.Stack())) |
| 60 | } |
| 61 | |
David Why Use Two When One Will Do Presotto | cce8f4f | 2014-09-30 14:50:44 -0700 | [diff] [blame] | 62 | // N squared but who cares, this is a little test. |
| 63 | // Ignores dups. |
| 64 | func contains(container, contained []string) bool { |
| 65 | L: |
| 66 | for _, d := range contained { |
| 67 | for _, r := range container { |
| 68 | if r == d { |
| 69 | continue L |
| 70 | } |
| 71 | } |
| 72 | return false |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 73 | } |
David Why Use Two When One Will Do Presotto | cce8f4f | 2014-09-30 14:50:44 -0700 | [diff] [blame] | 74 | return true |
| 75 | } |
| 76 | |
| 77 | func compare(t *testing.T, caller, name string, got, want []string) { |
| 78 | // Compare ignoring dups. |
| 79 | if !contains(got, want) || !contains(want, got) { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 80 | boom(t, "%s: %q: got %v, want %v", caller, name, got, want) |
| 81 | } |
| 82 | } |
| 83 | |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 84 | func doGlob(t *testing.T, ctx *context.T, ns namespace.T, pattern string, limit int) []string { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 85 | var replies []string |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 86 | rc, err := ns.Glob(ctx, pattern) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 87 | if err != nil { |
| 88 | boom(t, "Glob(%s): %s", pattern, err) |
| 89 | } |
| 90 | for s := range rc { |
David Why Use Two When One Will Do Presotto | 03c34d6 | 2015-02-10 01:38:58 -0800 | [diff] [blame] | 91 | switch v := s.(type) { |
James Ring | c971dca | 2015-04-09 09:51:20 -0700 | [diff] [blame] | 92 | case *naming.GlobReplyEntry: |
| 93 | replies = append(replies, v.Value.Name) |
David Why Use Two When One Will Do Presotto | 03c34d6 | 2015-02-10 01:38:58 -0800 | [diff] [blame] | 94 | if limit > 0 && len(replies) > limit { |
| 95 | boom(t, "Glob returns too many results, perhaps not limiting recursion") |
| 96 | } |
David Why Use Two When One Will Do Presotto | 2db32f9 | 2014-07-18 12:20:35 -0700 | [diff] [blame] | 97 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 98 | } |
| 99 | return replies |
| 100 | } |
| 101 | |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 102 | type testServer struct { |
| 103 | suffix string |
| 104 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 105 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 106 | func (testServer) KnockKnock(call rpc.ServerCall) (string, error) { |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 107 | return "Who's there?", nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 108 | } |
| 109 | |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 110 | // testServer has the following namespace: |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 111 | // "" -> {level1} -> {level2} |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 112 | func (t *testServer) GlobChildren__(rpc.ServerCall) (<-chan string, error) { |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 113 | ch := make(chan string, 1) |
| 114 | switch t.suffix { |
| 115 | case "": |
| 116 | ch <- "level1" |
| 117 | case "level1": |
| 118 | ch <- "level2" |
| 119 | default: |
| 120 | return nil, nil |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 121 | } |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 122 | close(ch) |
| 123 | return ch, nil |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 124 | } |
| 125 | |
Asim Shankar | 98d6596 | 2014-10-24 16:55:30 -0700 | [diff] [blame] | 126 | type allowEveryoneAuthorizer struct{} |
| 127 | |
Matt Rosencrantz | 250558f | 2015-03-17 11:37:31 -0700 | [diff] [blame] | 128 | func (allowEveryoneAuthorizer) Authorize(*context.T) error { return nil } |
Asim Shankar | 98d6596 | 2014-10-24 16:55:30 -0700 | [diff] [blame] | 129 | |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 130 | type dispatcher struct{} |
| 131 | |
Robin Thellend | a02fe8f | 2014-11-19 09:58:29 -0800 | [diff] [blame] | 132 | func (d *dispatcher) Lookup(suffix string) (interface{}, security.Authorizer, error) { |
Cosmos Nicolaou | 710daa2 | 2014-11-11 19:39:18 -0800 | [diff] [blame] | 133 | return &testServer{suffix}, allowEveryoneAuthorizer{}, nil |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 136 | func knockKnock(t *testing.T, ctx *context.T, name string) { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 137 | client := v23.GetClient(ctx) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 138 | var result string |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame^] | 139 | if err := client.Call(ctx, name, "KnockKnock", nil, []interface{}{&result}); err != nil { |
| 140 | boom(t, "Call failed: %s", err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 141 | } |
| 142 | if result != "Who's there?" { |
| 143 | boom(t, "Wrong result: %v", result) |
| 144 | } |
| 145 | } |
| 146 | |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 147 | func doResolveTest(t *testing.T, fname string, f func(*context.T, string, ...naming.NamespaceOpt) (*naming.MountEntry, error), ctx *context.T, name string, want []string, opts ...naming.NamespaceOpt) { |
Cosmos Nicolaou | 8bd8e10 | 2015-01-13 21:52:53 -0800 | [diff] [blame] | 148 | me, err := f(ctx, name, opts...) |
Ryan Brown | bc2c87c | 2014-11-17 18:55:25 +0000 | [diff] [blame] | 149 | if err != nil { |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 150 | boom(t, "Failed to %s %s: %s", fname, name, err) |
Ryan Brown | bc2c87c | 2014-11-17 18:55:25 +0000 | [diff] [blame] | 151 | } |
Cosmos Nicolaou | 8bd8e10 | 2015-01-13 21:52:53 -0800 | [diff] [blame] | 152 | compare(t, fname, name, me.Names(), want) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 153 | } |
| 154 | |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 155 | func testResolveToMountTable(t *testing.T, ctx *context.T, ns namespace.T, name string, want ...string) { |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 156 | doResolveTest(t, "ResolveToMountTable", ns.ResolveToMountTable, ctx, name, want) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 157 | } |
| 158 | |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 159 | func testResolveToMountTableWithPattern(t *testing.T, ctx *context.T, ns namespace.T, name string, pattern naming.NamespaceOpt, want ...string) { |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 160 | doResolveTest(t, "ResolveToMountTable", ns.ResolveToMountTable, ctx, name, want, pattern) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 161 | } |
| 162 | |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 163 | func testResolve(t *testing.T, ctx *context.T, ns namespace.T, name string, want ...string) { |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 164 | doResolveTest(t, "Resolve", ns.Resolve, ctx, name, want) |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 165 | } |
| 166 | |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 167 | func testResolveWithPattern(t *testing.T, ctx *context.T, ns namespace.T, name string, pattern naming.NamespaceOpt, want ...string) { |
David Why Use Two When One Will Do Presotto | 8de8585 | 2015-01-21 11:05:09 -0800 | [diff] [blame] | 168 | doResolveTest(t, "Resolve", ns.Resolve, ctx, name, want, pattern) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 169 | } |
| 170 | |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 171 | type serverEntry struct { |
| 172 | mountPoint string |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 173 | server rpc.Server |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 174 | endpoint naming.Endpoint |
| 175 | name string |
| 176 | } |
| 177 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 178 | func runServer(t *testing.T, ctx *context.T, disp rpc.Dispatcher, mountPoint string) *serverEntry { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 179 | return run(t, ctx, disp, mountPoint, false) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 180 | } |
| 181 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 182 | func runMT(t *testing.T, ctx *context.T, mountPoint string) *serverEntry { |
Todd Wang | 5987a94 | 2015-04-06 11:06:17 -0700 | [diff] [blame] | 183 | mtd, err := mounttablelib.NewMountTableDispatcher("") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 184 | if err != nil { |
Bogdan Caprita | e96cd04 | 2015-02-03 17:32:57 -0800 | [diff] [blame] | 185 | boom(t, "NewMountTableDispatcher returned error: %v", err) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 186 | } |
Bogdan Caprita | e96cd04 | 2015-02-03 17:32:57 -0800 | [diff] [blame] | 187 | return run(t, ctx, mtd, mountPoint, true) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 188 | } |
| 189 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 190 | func run(t *testing.T, ctx *context.T, disp rpc.Dispatcher, mountPoint string, mt bool) *serverEntry { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 191 | s, err := v23.NewServer(ctx, options.ServesMountTable(mt)) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 192 | if err != nil { |
| 193 | boom(t, "r.NewServer: %s", err) |
| 194 | } |
| 195 | // Add a mount table server. |
| 196 | // Start serving on a loopback address. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 197 | eps, err := s.Listen(v23.GetListenSpec(ctx)) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 198 | if err != nil { |
| 199 | boom(t, "Failed to Listen: %s", err) |
| 200 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 201 | if err := s.ServeDispatcher(mountPoint, disp); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 202 | boom(t, "Failed to serve mount table at %s: %s", mountPoint, err) |
| 203 | } |
Cosmos Nicolaou | 8bd8e10 | 2015-01-13 21:52:53 -0800 | [diff] [blame] | 204 | 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] | 205 | } |
| 206 | |
| 207 | const ( |
| 208 | mt1MP = "mt1" |
| 209 | mt2MP = "mt2" |
| 210 | mt3MP = "mt3" |
| 211 | mt4MP = "mt4" |
| 212 | mt5MP = "mt5" |
| 213 | j1MP = "joke1" |
| 214 | j2MP = "joke2" |
| 215 | j3MP = "joke3" |
| 216 | |
| 217 | ttl = 100 * time.Second |
| 218 | ) |
| 219 | |
| 220 | // runMountTables creates a root mountable with some mount tables mounted |
| 221 | // in it: mt{1,2,3,4,5} |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 222 | func runMountTables(t *testing.T, ctx *context.T) (*serverEntry, map[string]*serverEntry) { |
| 223 | root := runMT(t, ctx, "") |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 224 | v23.GetNamespace(ctx).SetRoots(root.name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 225 | t.Logf("mountTable %q -> %s", root.mountPoint, root.endpoint) |
| 226 | |
| 227 | mps := make(map[string]*serverEntry) |
| 228 | for _, mp := range []string{mt1MP, mt2MP, mt3MP, mt4MP, mt5MP} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 229 | m := runMT(t, ctx, mp) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 230 | t.Logf("mountTable %q -> %s", mp, m.endpoint) |
| 231 | mps[mp] = m |
| 232 | } |
| 233 | return root, mps |
| 234 | } |
| 235 | |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 236 | // createNamespace creates a hierarchy of mounttables and servers |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 237 | // as follows: |
| 238 | // /mt1, /mt2, /mt3, /mt4, /mt5, /joke1, /joke2, /joke3. |
| 239 | // That is, mt1 is a mount table mounted in the root mount table, |
| 240 | // joke1 is a server mounted in the root mount table. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 241 | func createNamespace(t *testing.T, ctx *context.T) (*serverEntry, map[string]*serverEntry, map[string]*serverEntry, func()) { |
| 242 | root, mts := runMountTables(t, ctx) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 243 | jokes := make(map[string]*serverEntry) |
| 244 | // Let's run some non-mount table services. |
| 245 | for _, j := range []string{j1MP, j2MP, j3MP} { |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 246 | disp := &dispatcher{} |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 247 | jokes[j] = runServer(t, ctx, disp, j) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 248 | } |
| 249 | return root, mts, jokes, func() { |
| 250 | for _, s := range jokes { |
| 251 | s.server.Stop() |
| 252 | } |
| 253 | for _, s := range mts { |
| 254 | s.server.Stop() |
| 255 | } |
| 256 | root.server.Stop() |
| 257 | } |
| 258 | } |
| 259 | |
| 260 | // runNestedMountTables creates some nested mount tables in the hierarchy |
| 261 | // created by createNamespace as follows: |
| 262 | // /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] | 263 | func runNestedMountTables(t *testing.T, ctx *context.T, mts map[string]*serverEntry) { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 264 | ns := v23.GetNamespace(ctx) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 265 | // Set up some nested mounts and verify resolution. |
| 266 | for _, m := range []string{"mt4/foo", "mt4/foo/bar"} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 267 | mts[m] = runMT(t, ctx, m) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 268 | } |
| 269 | |
| 270 | // Use a global name for a mount, rather than a relative one. |
| 271 | // We directly mount baz into the mt4/foo mount table. |
| 272 | globalMP := naming.JoinAddressName(mts["mt4/foo"].name, "baz") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 273 | mts["baz"] = runMT(t, ctx, "baz") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 274 | if err := ns.Mount(ctx, globalMP, mts["baz"].name, ttl); err != nil { |
| 275 | boom(t, "Failed to Mount %s: %s", globalMP, err) |
| 276 | } |
| 277 | } |
| 278 | |
| 279 | // TestNamespaceCommon tests common use of the Namespace library |
| 280 | // against a root mount table and some mount tables mounted on it. |
| 281 | func TestNamespaceCommon(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 282 | _, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 283 | defer cleanup() |
| 284 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 285 | root, mts, jokes, stopper := createNamespace(t, c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 286 | defer stopper() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 287 | ns := v23.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 288 | |
| 289 | // All of the initial mounts are served by the root mounttable |
| 290 | // and hence ResolveToMountTable should return the root mountable |
| 291 | // as the address portion of the terminal name for those mounttables. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 292 | testResolveToMountTable(t, c, ns, "", root.name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 293 | 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] | 294 | rootMT := naming.Join(root.name, m) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 295 | // All of these mount tables are hosted by the root mount table |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 296 | testResolveToMountTable(t, c, ns, m, rootMT) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 297 | |
| 298 | // The server registered for each mount point is a mount table |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 299 | testResolve(t, c, ns, m, mts[m].name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 300 | |
| 301 | // ResolveToMountTable will walk through to the sub MountTables |
| 302 | mtbar := naming.Join(m, "bar") |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 303 | subMT := naming.Join(mts[m].name, "bar") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 304 | testResolveToMountTable(t, c, ns, mtbar, subMT) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 305 | } |
| 306 | |
| 307 | for _, j := range []string{j1MP, j2MP, j3MP} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 308 | testResolve(t, c, ns, j, jokes[j].name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 309 | } |
| 310 | } |
| 311 | |
Cosmos Nicolaou | 8bd8e10 | 2015-01-13 21:52:53 -0800 | [diff] [blame] | 312 | // TestNamespaceDetails tests more detailed use of the Namespace library. |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 313 | func TestNamespaceDetails(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 314 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 315 | defer cleanup() |
| 316 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 317 | root, mts, _, stopper := createNamespace(t, sc) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 318 | defer stopper() |
| 319 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 320 | ns := v23.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 321 | ns.SetRoots(root.name) |
| 322 | |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 323 | // /mt2 is not an endpoint. Thus, the example below will fail. |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 324 | mt3Server := mts[mt3MP].name |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 325 | mt2a := "/mt2/a" |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 326 | if err := ns.Mount(c, mt2a, mt3Server, ttl); verror.ErrorID(err) == naming.ErrNoSuchName.ID { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 327 | boom(t, "Successfully mounted %s - expected an err %v, not %v", mt2a, naming.ErrNoSuchName, err) |
| 328 | } |
| 329 | |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 330 | // Mount using the relative name. |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 331 | // This means walk through mt2 if it already exists and mount within |
| 332 | // the lower level mount table, if the name doesn't exist we'll create |
| 333 | // a new name for it. |
| 334 | mt2a = "mt2/a" |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 335 | if err := ns.Mount(c, mt2a, mt3Server, ttl); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 336 | boom(t, "Failed to Mount %s: %s", mt2a, err) |
| 337 | } |
| 338 | |
David Why Use Two When One Will Do Presotto | 8b4dbbf | 2014-11-06 10:50:14 -0800 | [diff] [blame] | 339 | mt2mt := naming.Join(mts[mt2MP].name, "a") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 340 | // The mt2/a is served by the mt2 mount table |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 341 | testResolveToMountTable(t, c, ns, mt2a, mt2mt) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 342 | // The server for mt2a is mt3server from the second mount above. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 343 | testResolve(t, c, ns, mt2a, mt3Server) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 344 | |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 345 | // Add two more mounts. The // should be stripped off of the |
| 346 | // second. |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 347 | for _, mp := range []struct{ name, server string }{ |
| 348 | {"mt2", mts[mt4MP].name}, |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 349 | {"mt2//", mts[mt5MP].name}, |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 350 | } { |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 351 | if err := ns.Mount(c, mp.name, mp.server, ttl, naming.ServesMountTable(true)); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 352 | boom(t, "Failed to Mount %s: %s", mp.name, err) |
| 353 | } |
| 354 | } |
| 355 | |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 356 | names := []string{naming.JoinAddressName(mts[mt4MP].name, "a"), |
| 357 | naming.JoinAddressName(mts[mt5MP].name, "a")} |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 358 | names = append(names, naming.JoinAddressName(mts[mt2MP].name, "a")) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 359 | // We now have 3 mount tables prepared to serve mt2/a |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 360 | testResolveToMountTable(t, c, ns, "mt2/a", names...) |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 361 | names = []string{mts[mt4MP].name, mts[mt5MP].name} |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 362 | names = append(names, mts[mt2MP].name) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 363 | testResolve(t, c, ns, "mt2", names...) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 364 | } |
| 365 | |
| 366 | // TestNestedMounts tests some more deeply nested mounts |
| 367 | func TestNestedMounts(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 368 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 369 | defer cleanup() |
| 370 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 371 | root, mts, _, stopper := createNamespace(t, sc) |
| 372 | runNestedMountTables(t, sc, mts) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 373 | defer stopper() |
| 374 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 375 | ns := v23.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 376 | ns.SetRoots(root.name) |
| 377 | |
| 378 | // Set up some nested mounts and verify resolution. |
| 379 | for _, m := range []string{"mt4/foo", "mt4/foo/bar"} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 380 | testResolve(t, c, ns, m, mts[m].name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 381 | } |
| 382 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 383 | testResolveToMountTable(t, c, ns, "mt4/foo", |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 384 | naming.JoinAddressName(mts[mt4MP].name, "foo")) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 385 | testResolveToMountTable(t, c, ns, "mt4/foo/bar", |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 386 | naming.JoinAddressName(mts["mt4/foo"].name, "bar")) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 387 | testResolveToMountTable(t, c, ns, "mt4/foo/baz", |
Cosmos Nicolaou | ae8dd21 | 2014-12-13 23:43:08 -0800 | [diff] [blame] | 388 | naming.JoinAddressName(mts["mt4/foo"].name, "baz")) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 389 | } |
| 390 | |
| 391 | // TestServers tests invoking RPCs on simple servers |
| 392 | func TestServers(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 393 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 394 | defer cleanup() |
| 395 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 396 | root, mts, jokes, stopper := createNamespace(t, sc) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 397 | defer stopper() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 398 | ns := v23.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 399 | ns.SetRoots(root.name) |
| 400 | |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 401 | // Let's run some non-mount table services |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 402 | for _, j := range []string{j1MP, j2MP, j3MP} { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 403 | testResolve(t, c, ns, j, jokes[j].name) |
| 404 | knockKnock(t, c, j) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 405 | globalName := naming.JoinAddressName(mts["mt4"].name, j) |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 406 | disp := &dispatcher{} |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 407 | gj := "g_" + j |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 408 | jokes[gj] = runServer(t, c, disp, globalName) |
| 409 | testResolve(t, c, ns, "mt4/"+j, jokes[gj].name) |
| 410 | knockKnock(t, c, "mt4/"+j) |
| 411 | testResolveToMountTable(t, c, ns, "mt4/"+j, globalName) |
| 412 | testResolveToMountTable(t, c, ns, "mt4/"+j+"/garbage", globalName+"/garbage") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 413 | } |
| 414 | } |
| 415 | |
| 416 | // TestGlob tests some glob patterns. |
| 417 | func TestGlob(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 418 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 419 | defer cleanup() |
| 420 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 421 | root, mts, _, stopper := createNamespace(t, sc) |
| 422 | runNestedMountTables(t, sc, mts) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 423 | defer stopper() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 424 | ns := v23.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 425 | ns.SetRoots(root.name) |
| 426 | |
| 427 | tln := []string{"baz", "mt1", "mt2", "mt3", "mt4", "mt5", "joke1", "joke2", "joke3"} |
| 428 | barbaz := []string{"mt4/foo/bar", "mt4/foo/baz"} |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 429 | 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] | 430 | foo := append([]string{"mt4/foo"}, barbaz...) |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 431 | foo = append(foo, level12...) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 432 | // Try various globs. |
| 433 | globTests := []struct { |
| 434 | pattern string |
| 435 | expected []string |
| 436 | }{ |
| 437 | {"*", tln}, |
David Why Use Two When One Will Do Presotto | cce8f4f | 2014-09-30 14:50:44 -0700 | [diff] [blame] | 438 | {"x", []string{}}, |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 439 | {"m*", []string{"mt1", "mt2", "mt3", "mt4", "mt5"}}, |
| 440 | {"mt[2,3]", []string{"mt2", "mt3"}}, |
| 441 | {"*z", []string{"baz"}}, |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 442 | {"joke1/*", []string{"joke1/level1"}}, |
| 443 | {"j?ke1/level1/*", []string{"joke1/level1/level2"}}, |
| 444 | {"joke1/level1/*", []string{"joke1/level1/level2"}}, |
| 445 | {"joke1/level1/level2/...", []string{"joke1/level1/level2"}}, |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 446 | {"...", append(append(tln, foo...), "")}, |
| 447 | {"*/...", append(tln, foo...)}, |
| 448 | {"*/foo/*", barbaz}, |
| 449 | {"*/*/*z", []string{"mt4/foo/baz"}}, |
| 450 | {"*/f??/*z", []string{"mt4/foo/baz"}}, |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 451 | {"mt4/foo/baz", []string{"mt4/foo/baz"}}, |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 452 | } |
| 453 | for _, test := range globTests { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 454 | out := doGlob(t, c, ns, test.pattern, 0) |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 455 | compare(t, "Glob", test.pattern, out, test.expected) |
Robin Thellend | 5f83857 | 2014-07-10 13:28:56 -0700 | [diff] [blame] | 456 | // Do the same with a full rooted name. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 457 | out = doGlob(t, c, ns, naming.JoinAddressName(root.name, test.pattern), 0) |
Robin Thellend | 5f83857 | 2014-07-10 13:28:56 -0700 | [diff] [blame] | 458 | var expectedWithRoot []string |
| 459 | for _, s := range test.expected { |
| 460 | expectedWithRoot = append(expectedWithRoot, naming.JoinAddressName(root.name, s)) |
| 461 | } |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 462 | compare(t, "Glob", test.pattern, out, expectedWithRoot) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 463 | } |
| 464 | } |
| 465 | |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 466 | type GlobbableServer struct { |
| 467 | callCount int |
| 468 | mu sync.Mutex |
| 469 | } |
| 470 | |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 471 | func (g *GlobbableServer) Glob__(rpc.ServerCall, string) (<-chan naming.GlobReply, error) { |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 472 | g.mu.Lock() |
| 473 | defer g.mu.Unlock() |
| 474 | g.callCount++ |
Robin Thellend | 39ac323 | 2014-12-02 09:50:41 -0800 | [diff] [blame] | 475 | return nil, nil |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 476 | } |
| 477 | |
| 478 | func (g *GlobbableServer) GetAndResetCount() int { |
| 479 | g.mu.Lock() |
| 480 | defer g.mu.Unlock() |
| 481 | cnt := g.callCount |
| 482 | g.callCount = 0 |
| 483 | |
| 484 | return cnt |
| 485 | } |
| 486 | |
| 487 | // TestGlobEarlyStop tests that Glob doesn't query terminal servers with finished patterns. |
| 488 | func TestGlobEarlyStop(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 489 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 490 | defer cleanup() |
| 491 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 492 | root, mts, _, stopper := createNamespace(t, sc) |
| 493 | runNestedMountTables(t, sc, mts) |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 494 | defer stopper() |
| 495 | |
| 496 | globServer := &GlobbableServer{} |
| 497 | name := naming.JoinAddressName(mts["mt4/foo/bar"].name, "glob") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 498 | runningGlobServer := runServer(t, c, testutil.LeafDispatcher(globServer, nil), name) |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 499 | defer runningGlobServer.server.Stop() |
| 500 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 501 | ns := v23.GetNamespace(c) |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 502 | ns.SetRoots(root.name) |
| 503 | |
| 504 | tests := []struct { |
| 505 | pattern string |
| 506 | expectedCalls int |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 507 | expected []string |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 508 | }{ |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 509 | {"mt4/foo/bar/glob", 0, []string{"mt4/foo/bar/glob"}}, |
| 510 | {"mt4/foo/bar/glob/...", 1, []string{"mt4/foo/bar/glob"}}, |
| 511 | {"mt4/foo/bar/glob/*", 1, nil}, |
| 512 | {"mt4/foo/bar/***", 0, []string{"mt4/foo/bar", "mt4/foo/bar/glob"}}, |
| 513 | {"mt4/foo/bar/...", 1, []string{"mt4/foo/bar", "mt4/foo/bar/glob"}}, |
| 514 | {"mt4/foo/bar/*", 0, []string{"mt4/foo/bar/glob"}}, |
| 515 | {"mt4/***/bar/***", 0, []string{"mt4/foo/bar", "mt4/foo/bar/glob"}}, |
| 516 | {"mt4/*/bar/***", 0, []string{"mt4/foo/bar", "mt4/foo/bar/glob"}}, |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 517 | } |
David Why Use Two When One Will Do Presotto | c28686e | 2014-11-05 11:19:29 -0800 | [diff] [blame] | 518 | // Test allowing the tests to descend into leaves. |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 519 | for _, test := range tests { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 520 | 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] | 521 | compare(t, "Glob", test.pattern, out, test.expected) |
Matt Rosencrantz | 50c2bb8 | 2014-07-24 09:10:33 -0700 | [diff] [blame] | 522 | if calls := globServer.GetAndResetCount(); calls != test.expectedCalls { |
| 523 | boom(t, "Wrong number of Glob calls to terminal server got: %d want: %d.", calls, test.expectedCalls) |
| 524 | } |
| 525 | } |
| 526 | } |
| 527 | |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 528 | func TestCycles(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 529 | sc, c, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 530 | defer cleanup() |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 531 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 532 | root, _, _, stopper := createNamespace(t, sc) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 533 | defer stopper() |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 534 | ns := v23.GetNamespace(c) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 535 | ns.SetRoots(root.name) |
| 536 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 537 | c1 := runMT(t, c, "c1") |
| 538 | c2 := runMT(t, c, "c2") |
| 539 | c3 := runMT(t, c, "c3") |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 540 | defer c1.server.Stop() |
| 541 | defer c2.server.Stop() |
| 542 | defer c3.server.Stop() |
| 543 | |
| 544 | m := "c1/c2" |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 545 | if err := ns.Mount(c, m, c1.name, ttl, naming.ServesMountTable(true)); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 546 | boom(t, "Failed to Mount %s: %s", "c1/c2", err) |
| 547 | } |
| 548 | |
| 549 | m = "c1/c2/c3" |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 550 | if err := ns.Mount(c, m, c3.name, ttl, naming.ServesMountTable(true)); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 551 | boom(t, "Failed to Mount %s: %s", m, err) |
| 552 | } |
| 553 | |
| 554 | m = "c1/c3/c4" |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 555 | if err := ns.Mount(c, m, c1.name, ttl, naming.ServesMountTable(true)); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 556 | boom(t, "Failed to Mount %s: %s", m, err) |
| 557 | } |
| 558 | |
Shyam Jayaraman | dbae76b | 2014-11-17 12:51:29 -0800 | [diff] [blame] | 559 | // 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] | 560 | testResolve(t, c, ns, "c1", c1.name) |
| 561 | testResolve(t, c, ns, "c1/c2", c1.name) |
| 562 | testResolve(t, c, ns, "c1/c3", c3.name) |
| 563 | testResolve(t, c, ns, "c1/c3/c4", c1.name) |
| 564 | testResolve(t, c, ns, "c1/c3/c4/c3/c4", c1.name) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 565 | cycle := "c3/c4" |
| 566 | for i := 0; i < 40; i++ { |
| 567 | cycle += "/c3/c4" |
| 568 | } |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 569 | if _, err := ns.Resolve(c, "c1/"+cycle); verror.ErrorID(err) != naming.ErrResolutionDepthExceeded.ID { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 570 | boom(t, "Failed to detect cycle") |
| 571 | } |
| 572 | |
David Why Use Two When One Will Do Presotto | 3fc13f4 | 2015-03-30 09:56:26 -0700 | [diff] [blame] | 573 | // Perform the glob with a response length limit and dup suppression. The dup supression |
| 574 | // should win. |
| 575 | r := doGlob(t, c, ns, "c1/...", 1000) |
| 576 | if len(r) != 6 { |
| 577 | t.Fatal("expected 6 replies, got %v", r) |
| 578 | } |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 579 | } |
| 580 | |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 581 | // TestGoroutineLeaks tests for leaking goroutines - we have many:-( |
| 582 | func TestGoroutineLeaks(t *testing.T) { |
| 583 | t.Skip() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 584 | sc, _, cleanup := createContexts(t) |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 585 | defer cleanup() |
| 586 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 587 | _, _, _, stopper := createNamespace(t, sc) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 588 | defer func() { |
| 589 | vlog.Infof("%d goroutines:", runtime.NumGoroutine()) |
| 590 | }() |
| 591 | defer stopper() |
| 592 | defer func() { |
| 593 | vlog.Infof("%d goroutines:", runtime.NumGoroutine()) |
| 594 | }() |
| 595 | //panic("this will show up lots of goroutine+channel leaks!!!!") |
| 596 | } |
| 597 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 598 | func TestBadRoots(t *testing.T) { |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 599 | if _, err := inamespace.New(); err != nil { |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 600 | t.Errorf("namespace.New should not have failed with no roots") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 601 | } |
Todd Wang | 5082a55 | 2015-04-02 10:56:11 -0700 | [diff] [blame] | 602 | if _, err := inamespace.New("not a rooted name"); err == nil { |
Cosmos Nicolaou | 4e02997 | 2014-06-13 14:53:08 -0700 | [diff] [blame] | 603 | t.Errorf("namespace.New should have failed with an unrooted name") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 604 | } |
| 605 | } |
Ryan Brown | fc29014 | 2014-11-24 15:47:24 -0800 | [diff] [blame] | 606 | |
| 607 | func bless(blesser, delegate security.Principal, extension string) { |
| 608 | b, err := blesser.Bless(delegate.PublicKey(), blesser.BlessingStore().Default(), extension, security.UnconstrainedUse()) |
| 609 | if err != nil { |
| 610 | panic(err) |
| 611 | } |
| 612 | delegate.BlessingStore().SetDefault(b) |
| 613 | } |
| 614 | |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 615 | func TestAuthorizationDuringResolve(t *testing.T) { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 616 | ctx, shutdown := v23.Init() |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 617 | defer shutdown() |
| 618 | |
| 619 | var ( |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 620 | rootMtCtx, _ = v23.SetPrincipal(ctx, testutil.NewPrincipal()) // root mounttable |
| 621 | mtCtx, _ = v23.SetPrincipal(ctx, testutil.NewPrincipal()) // intermediate mounttable |
| 622 | serverCtx, _ = v23.SetPrincipal(ctx, testutil.NewPrincipal()) // end server |
| 623 | clientCtx, _ = v23.SetPrincipal(ctx, testutil.NewPrincipal()) // client process (doing Resolves). |
| 624 | idp = testutil.NewIDProvider("idp") // identity provider |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 625 | serverEndpoint = naming.FormatEndpoint("tcp", "127.0.0.1:14141") |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 626 | |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 627 | resolve = func(name string, opts ...naming.NamespaceOpt) (*naming.MountEntry, error) { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 628 | return v23.GetNamespace(clientCtx).Resolve(clientCtx, name, opts...) |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 629 | } |
| 630 | |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 631 | mount = func(name, server string, ttl time.Duration, opts ...naming.NamespaceOpt) error { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 632 | return v23.GetNamespace(serverCtx).Mount(serverCtx, name, server, ttl, opts...) |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 633 | } |
| 634 | ) |
| 635 | // Setup default blessings for the processes. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 636 | idp.Bless(v23.GetPrincipal(rootMtCtx), "rootmt") |
| 637 | idp.Bless(v23.GetPrincipal(serverCtx), "server") |
| 638 | idp.Bless(v23.GetPrincipal(mtCtx), "childmt") |
| 639 | idp.Bless(v23.GetPrincipal(clientCtx), "client") |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 640 | |
| 641 | // Setup the namespace root for all the "processes". |
| 642 | rootmt := runMT(t, rootMtCtx, "") |
| 643 | for _, ctx := range []*context.T{mtCtx, serverCtx, clientCtx} { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 644 | v23.GetNamespace(ctx).SetRoots(rootmt.name) |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 645 | } |
| 646 | // Disable caching in the client so that any Mount calls by the server |
| 647 | // are noticed immediately. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 648 | v23.GetNamespace(clientCtx).CacheCtl(naming.DisableCache(true)) |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 649 | |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 650 | // Intermediate mounttables should be authenticated. |
Suharsh Sivakumar | 77357d7 | 2015-02-21 22:14:20 -0800 | [diff] [blame] | 651 | mt := runMT(t, mtCtx, "mt") |
| 652 | // Mount a server on "mt". |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 653 | if err := mount("mt/server", serverEndpoint, time.Minute, naming.ReplaceMount(true)); err != nil { |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 654 | t.Error(err) |
Suharsh Sivakumar | 77357d7 | 2015-02-21 22:14:20 -0800 | [diff] [blame] | 655 | } |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 656 | // The namespace root should be authenticated too |
| 657 | if e, err := resolve("mt/server"); err != nil { |
| 658 | t.Errorf("Got (%v, %v)", e, err) |
| 659 | } else { |
| 660 | // Host:Port and Endpoint versions of the other namespace root |
| 661 | // (which has different blessings) |
| 662 | hproot := fmt.Sprintf("(otherroot)@%v", rootmt.endpoint.Addr()) |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 663 | eproot := naming.FormatEndpoint(rootmt.endpoint.Addr().Network(), rootmt.endpoint.Addr().String(), rootmt.endpoint.RoutingID(), naming.BlessingOpt("otherroot"), naming.ServesMountTable(rootmt.endpoint.ServesMountTable())) |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 664 | for _, root := range []string{hproot, eproot} { |
| 665 | name := naming.JoinAddressName(root, "mt") |
| 666 | // Rooted name resolutions should fail authorization because of the "otherrot" |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 667 | if e, err := resolve(name); verror.ErrorID(err) != verror.ErrNotTrusted.ID { |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 668 | t.Errorf("resolve(%q) returned (%v, errorid=%v %v), wanted errorid=%v", name, e, verror.ErrorID(err), err, verror.ErrNotTrusted.ID) |
| 669 | } |
| 670 | // But not fail if the skip-authorization option is provided |
| 671 | if e, err := resolve(name, options.SkipServerEndpointAuthorization{}); err != nil { |
| 672 | t.Errorf("resolve(%q): Got (%v, %v), expected resolution to succeed", name, e, err) |
| 673 | } |
| 674 | // The namespace root from the context should be authorized as well. |
| 675 | ctx, ns, _ := v23.SetNewNamespace(clientCtx, naming.JoinAddressName(root, "")) |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 676 | if e, err := ns.Resolve(ctx, "mt/server"); verror.ErrorID(err) != verror.ErrNotTrusted.ID { |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 677 | t.Errorf("resolve with root=%q returned (%v, errorid=%v %v), wanted errorid=%v", root, e, verror.ErrorID(err), err, verror.ErrNotTrusted.ID) |
| 678 | } |
| 679 | if _, err := ns.Resolve(ctx, "mt/server", options.SkipServerEndpointAuthorization{}); err != nil { |
| 680 | t.Errorf("resolve with root=%q should have succeeded when authorization checks are skipped. Got %v", err) |
| 681 | } |
| 682 | } |
| 683 | } |
| 684 | // Imagine that the network address of "mt" has been taken over by an |
| 685 | // attacker. However, this attacker cannot mess with the mount entry |
| 686 | // for "mt". This would result in "mt" and its mount entry (in the |
| 687 | // global mounttable) having inconsistent blessings. Simulate this by |
| 688 | // explicitly changing the mount entry for "mt". |
| 689 | goodChildMTEndpoint := naming.FormatEndpoint(mt.endpoint.Addr().Network(), mt.endpoint.Addr().String(), naming.BlessingOpt("idp/goodchildmt"), mt.endpoint.RoutingID()) |
David Why Use Two When One Will Do Presotto | 38788d4 | 2015-03-31 17:13:54 -0700 | [diff] [blame] | 690 | if err := v23.GetNamespace(mtCtx).Mount(mtCtx, "mt", goodChildMTEndpoint, time.Minute, naming.ServesMountTable(true), naming.ReplaceMount(true)); err != nil { |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 691 | t.Error(err) |
Suharsh Sivakumar | 77357d7 | 2015-02-21 22:14:20 -0800 | [diff] [blame] | 692 | } |
| 693 | |
Asim Shankar | 263c73b | 2015-03-19 18:31:26 -0700 | [diff] [blame] | 694 | if e, err := resolve("mt/server", options.SkipServerEndpointAuthorization{}); err != nil { |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 695 | t.Errorf("Resolve should succeed when skipping server authorization. Got (%v, %v)", e, err) |
Todd Wang | 8fa3876 | 2015-03-25 14:04:59 -0700 | [diff] [blame] | 696 | } else if e, err := resolve("mt/server"); verror.ErrorID(err) != verror.ErrNotTrusted.ID { |
Asim Shankar | b547ea9 | 2015-02-17 18:49:45 -0800 | [diff] [blame] | 697 | t.Errorf("Resolve should have failed with %q because an attacker has taken over the intermediate mounttable. Got (%+v, errorid=%q:%v)", verror.ErrNotTrusted.ID, e, verror.ErrorID(err), err) |
| 698 | } |
| 699 | } |
David Why Use Two When One Will Do Presotto | 18be8b0 | 2015-03-02 17:26:25 -0800 | [diff] [blame] | 700 | |
| 701 | // TestDelete tests deleting some parts of the name space. |
| 702 | func TestDelete(t *testing.T) { |
| 703 | _, c, cleanup := createContexts(t) |
| 704 | defer cleanup() |
| 705 | ns := v23.GetNamespace(c) |
| 706 | |
| 707 | // Create a root mount table with mount tables mounted at mt1, mt1, ... |
| 708 | root, _, _, stopper := createNamespace(t, c) |
| 709 | defer stopper() |
| 710 | ns.SetRoots(root.name) |
| 711 | |
| 712 | // We should be able to remove servers below the root. |
| 713 | if err := ns.Delete(c, "mt1", false); err != nil { |
| 714 | t.Errorf("Delete failed: %s", err) |
| 715 | } |
| 716 | |
| 717 | // Create a server below one level down. |
| 718 | if err := ns.Mount(c, "mt2/b/c", "/madeup:1111/server", time.Minute); err != nil { |
| 719 | t.Errorf("Mount mt2/b/c failed: %s", err) |
| 720 | } |
| 721 | |
| 722 | // We should not be able to delete mt2/b... |
| 723 | if err := ns.Delete(c, "mt2/b", false); err == nil { |
| 724 | t.Errorf("Delete mt2/b should have failed") |
| 725 | } |
| 726 | |
| 727 | // ...unless we include its children. |
| 728 | if err := ns.Delete(c, "mt2/b", true); err != nil { |
| 729 | t.Errorf("Delete failed: %s", err) |
| 730 | } |
| 731 | } |
Robin Thellend | 89e9523 | 2015-03-24 13:48:48 -0700 | [diff] [blame] | 732 | |
| 733 | type leafObject struct{} |
| 734 | |
| 735 | func (leafObject) Foo(rpc.ServerCall) error { |
| 736 | return nil |
| 737 | } |
| 738 | |
| 739 | func TestLeaf(t *testing.T) { |
| 740 | _, ctx, cleanup := createContexts(t) |
| 741 | defer cleanup() |
| 742 | root := runMT(t, ctx, "") |
| 743 | ns := v23.GetNamespace(ctx) |
| 744 | ns.SetRoots(root.name) |
| 745 | |
| 746 | server, err := v23.NewServer(ctx) |
| 747 | if err != nil { |
| 748 | boom(t, "v23.NewServer: %s", err) |
| 749 | } |
| 750 | ls := rpc.ListenSpec{Addrs: rpc.ListenAddrs{{"tcp", "127.0.0.1:0"}}} |
| 751 | if _, err := server.Listen(ls); err != nil { |
| 752 | boom(t, "Failed to Listen: %s", err) |
| 753 | } |
| 754 | if err := server.Serve("leaf", &leafObject{}, nil); err != nil { |
| 755 | boom(t, "server.Serve failed: %s", err) |
| 756 | } |
| 757 | |
| 758 | mountEntry, err := ns.Resolve(ctx, "leaf") |
| 759 | if err != nil { |
| 760 | boom(t, "ns.Resolve failed: %v", err) |
| 761 | } |
| 762 | if expected := true; mountEntry.IsLeaf != expected { |
| 763 | boom(t, "unexpected mountEntry.IsLeaf value. Got %v, expected %v", mountEntry.IsLeaf, expected) |
| 764 | } |
Robin Thellend | 0064374 | 2015-04-01 10:36:50 -0700 | [diff] [blame] | 765 | |
| 766 | c, err := ns.Glob(ctx, "leaf") |
| 767 | if err != nil { |
| 768 | boom(t, "ns.Glob failed: %v", err) |
| 769 | } |
| 770 | count := 0 |
| 771 | for result := range c { |
James Ring | c971dca | 2015-04-09 09:51:20 -0700 | [diff] [blame] | 772 | if me, ok := result.(*naming.GlobReplyEntry); ok { |
Robin Thellend | 0064374 | 2015-04-01 10:36:50 -0700 | [diff] [blame] | 773 | count++ |
James Ring | c971dca | 2015-04-09 09:51:20 -0700 | [diff] [blame] | 774 | if expected := true; me.Value.IsLeaf != expected { |
| 775 | boom(t, "unexpected me.IsLeaf value. Got %v, expected %v", me.Value.IsLeaf, expected) |
Robin Thellend | 0064374 | 2015-04-01 10:36:50 -0700 | [diff] [blame] | 776 | } |
| 777 | } |
| 778 | } |
| 779 | if count == 0 { |
| 780 | boom(t, "Glob did not return any results. Expected 1") |
| 781 | } |
Robin Thellend | 89e9523 | 2015-03-24 13:48:48 -0700 | [diff] [blame] | 782 | } |