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 | |
Todd Wang | 5987a94 | 2015-04-06 11:06:17 -0700 | [diff] [blame] | 5 | package mounttablelib |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 6 | |
| 7 | import ( |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 8 | "encoding/json" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 9 | "errors" |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 10 | "fmt" |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 11 | "io" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 12 | "reflect" |
| 13 | "runtime/debug" |
| 14 | "sort" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 15 | "testing" |
| 16 | "time" |
| 17 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 18 | "v.io/v23" |
| 19 | "v.io/v23/context" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 20 | "v.io/v23/naming" |
| 21 | "v.io/v23/options" |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 22 | "v.io/v23/rpc" |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 23 | "v.io/v23/security" |
Todd Wang | 387d8a4 | 2015-03-30 17:09:05 -0700 | [diff] [blame] | 24 | "v.io/v23/security/access" |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 25 | "v.io/v23/services/stats" |
| 26 | "v.io/v23/vdl" |
Jiri Simsa | 337af23 | 2015-02-27 14:36:46 -0800 | [diff] [blame] | 27 | "v.io/x/lib/vlog" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 28 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 29 | libstats "v.io/x/ref/lib/stats" |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 30 | "v.io/x/ref/services/debug/debuglib" |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 31 | "v.io/x/ref/test" |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 32 | "v.io/x/ref/test/testutil" |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 33 | ) |
| 34 | |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 35 | // Simulate different processes with different runtimes. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 36 | // rootCtx is the one running the mounttable service. |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 37 | const ttlSecs = 60 * 60 |
| 38 | |
| 39 | func boom(t *testing.T, f string, v ...interface{}) { |
| 40 | t.Logf(f, v...) |
| 41 | t.Fatal(string(debug.Stack())) |
| 42 | } |
| 43 | |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 44 | func doMount(t *testing.T, ctx *context.T, ep, suffix, service string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 45 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 46 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 47 | if err := client.Call(ctx, name, "Mount", []interface{}{service, uint32(ttlSecs), 0}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 48 | if !shouldSucceed { |
| 49 | return |
| 50 | } |
| 51 | boom(t, "Failed to Mount %s onto %s: %s", service, name, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 52 | } |
| 53 | } |
| 54 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 55 | func doUnmount(t *testing.T, ctx *context.T, ep, suffix, service string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 56 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 57 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 58 | if err := client.Call(ctx, name, "Unmount", []interface{}{service}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 59 | if !shouldSucceed { |
| 60 | return |
| 61 | } |
Alex Fandrianto | a2e7c37 | 2015-04-20 10:46:10 -0700 | [diff] [blame] | 62 | boom(t, "Failed to Unmount %s off of %s: %s", service, name, err) |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 63 | } |
| 64 | } |
| 65 | |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 66 | func doGetPermissions(t *testing.T, ctx *context.T, ep, suffix string, shouldSucceed bool) (perms access.Permissions, version string) { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 67 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 68 | client := v23.GetClient(ctx) |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 69 | if err := client.Call(ctx, name, "GetPermissions", nil, []interface{}{&perms, &version}, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 70 | if !shouldSucceed { |
| 71 | return |
| 72 | } |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 73 | boom(t, "Failed to GetPermissions %s: %s", name, err) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 74 | } |
| 75 | return |
| 76 | } |
| 77 | |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 78 | func doSetPermissions(t *testing.T, ctx *context.T, ep, suffix string, perms access.Permissions, version string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 79 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 80 | client := v23.GetClient(ctx) |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 81 | if err := client.Call(ctx, name, "SetPermissions", []interface{}{perms, version}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 82 | if !shouldSucceed { |
| 83 | return |
| 84 | } |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 85 | boom(t, "Failed to SetPermissions %s: %s", name, err) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 86 | } |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 87 | } |
| 88 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 89 | func doDeleteNode(t *testing.T, ctx *context.T, ep, suffix string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 90 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 91 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 92 | if err := client.Call(ctx, name, "Delete", []interface{}{false}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 93 | if !shouldSucceed { |
| 94 | return |
| 95 | } |
| 96 | boom(t, "Failed to Delete node %s: %s", name, err) |
| 97 | } |
| 98 | } |
| 99 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 100 | func doDeleteSubtree(t *testing.T, ctx *context.T, ep, suffix string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 101 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 102 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 103 | if err := client.Call(ctx, name, "Delete", []interface{}{true}, nil, options.NoResolve{}); err != nil { |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 104 | if !shouldSucceed { |
| 105 | return |
| 106 | } |
| 107 | boom(t, "Failed to Delete subtree %s: %s", name, err) |
| 108 | } |
| 109 | } |
| 110 | |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 111 | func mountentry2names(e *naming.MountEntry) []string { |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 112 | names := make([]string, len(e.Servers)) |
| 113 | for idx, s := range e.Servers { |
| 114 | names[idx] = naming.JoinAddressName(s.Server, e.Name) |
| 115 | } |
| 116 | return names |
| 117 | } |
| 118 | |
| 119 | func strslice(strs ...string) []string { |
| 120 | return strs |
| 121 | } |
| 122 | |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 123 | func resolve(ctx *context.T, name string) (*naming.MountEntry, error) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 124 | // Resolve the name one level. |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 125 | var entry naming.MountEntry |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 126 | client := v23.GetClient(ctx) |
| 127 | if err := client.Call(ctx, name, "ResolveStep", nil, []interface{}{&entry}, options.NoResolve{}); err != nil { |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 128 | return nil, err |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 129 | } |
| 130 | if len(entry.Servers) < 1 { |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 131 | return nil, errors.New("resolve returned no servers") |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 132 | } |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 133 | return &entry, nil |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 134 | } |
| 135 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 136 | func export(t *testing.T, ctx *context.T, name, contents string) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 137 | // Resolve the name. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 138 | resolved, err := resolve(ctx, name) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 139 | if err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 140 | boom(t, "Failed to Export.Resolve %s: %s", name, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 141 | } |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 142 | // Export the value. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 143 | client := v23.GetClient(ctx) |
Suharsh Sivakumar | 1abd5a8 | 2015-04-10 00:24:14 -0700 | [diff] [blame] | 144 | if err := client.Call(ctx, mountentry2names(resolved)[0], "Export", []interface{}{contents, true}, nil, options.NoResolve{}); err != nil { |
| 145 | boom(t, "Failed to Export.Call %s to %s: %s", name, contents, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 146 | } |
| 147 | } |
| 148 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 149 | func checkContents(t *testing.T, ctx *context.T, name, expected string, shouldSucceed bool) { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 150 | // Resolve the name. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 151 | resolved, err := resolve(ctx, name) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 152 | if err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 153 | if !shouldSucceed { |
| 154 | return |
| 155 | } |
| 156 | boom(t, "Failed to Resolve %s: %s", name, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 157 | } |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 158 | // Look up the value. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 159 | client := v23.GetClient(ctx) |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 160 | call, err := client.StartCall(ctx, mountentry2names(resolved)[0], "Lookup", nil, options.NoResolve{}) |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 161 | if err != nil { |
| 162 | if shouldSucceed { |
| 163 | boom(t, "Failed Lookup.StartCall %s: %s", name, err) |
| 164 | } |
| 165 | return |
| 166 | } |
| 167 | var contents []byte |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 168 | if err := call.Finish(&contents); err != nil { |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 169 | if shouldSucceed { |
| 170 | boom(t, "Failed to Lookup %s: %s", name, err) |
| 171 | } |
| 172 | return |
| 173 | } |
| 174 | if string(contents) != expected { |
| 175 | boom(t, "Lookup %s, expected %q, got %q", name, expected, contents) |
| 176 | } |
| 177 | if !shouldSucceed { |
| 178 | boom(t, "Lookup %s, expected failure, got %q", name, contents) |
| 179 | } |
| 180 | } |
| 181 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 182 | func newMT(t *testing.T, permsFile, persistDir, statsDir string, rootCtx *context.T) (rpc.Server, string) { |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 183 | reservedDisp := debuglib.NewDispatcher(vlog.Log.LogDir, nil) |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 184 | ctx := v23.WithReservedNameDispatcher(rootCtx, reservedDisp) |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 185 | server, err := v23.NewServer(ctx, options.ServesMountTable(true)) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 186 | if err != nil { |
| 187 | boom(t, "r.NewServer: %s", err) |
| 188 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 189 | // Add mount table service. |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 190 | mt, err := NewMountTableDispatcher(permsFile, persistDir, statsDir) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 191 | if err != nil { |
Bogdan Caprita | e96cd04 | 2015-02-03 17:32:57 -0800 | [diff] [blame] | 192 | boom(t, "NewMountTableDispatcher: %v", err) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 193 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 194 | // Start serving on a loopback address. |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 195 | eps, err := server.Listen(v23.GetListenSpec(ctx)) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 196 | if err != nil { |
| 197 | boom(t, "Failed to Listen mount table: %s", err) |
| 198 | } |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 199 | if err := server.ServeDispatcher("", mt); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 200 | boom(t, "Failed to register mock collection: %s", err) |
| 201 | } |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 202 | estr := eps[0].String() |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 203 | t.Logf("endpoint %s", estr) |
| 204 | return server, estr |
| 205 | } |
| 206 | |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 207 | func newCollection(t *testing.T, rootCtx *context.T) (rpc.Server, string) { |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 208 | server, err := v23.NewServer(rootCtx) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 209 | if err != nil { |
| 210 | boom(t, "r.NewServer: %s", err) |
| 211 | } |
| 212 | // Start serving on a loopback address. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 213 | eps, err := server.Listen(v23.GetListenSpec(rootCtx)) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 214 | if err != nil { |
| 215 | boom(t, "Failed to Listen mount table: %s", err) |
| 216 | } |
| 217 | // Add a collection service. This is just a service we can mount |
| 218 | // and test against. |
| 219 | cPrefix := "collection" |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 220 | if err := server.ServeDispatcher(cPrefix, newCollectionServer()); err != nil { |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 221 | boom(t, "Failed to register mock collection: %s", err) |
| 222 | } |
Cosmos Nicolaou | 28dabfc | 2014-12-15 22:51:07 -0800 | [diff] [blame] | 223 | estr := eps[0].String() |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 224 | t.Logf("endpoint %s", estr) |
| 225 | return server, estr |
| 226 | } |
| 227 | |
| 228 | func TestMountTable(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 229 | rootCtx, aliceCtx, bobCtx, shutdown := initTest() |
| 230 | defer shutdown() |
| 231 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 232 | mt, mtAddr := newMT(t, "testdata/test.perms", "", "testMountTable", rootCtx) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 233 | defer mt.Stop() |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 234 | collection, collectionAddr := newCollection(t, rootCtx) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 235 | defer collection.Stop() |
| 236 | |
| 237 | collectionName := naming.JoinAddressName(collectionAddr, "collection") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 238 | |
| 239 | // Mount the collection server into the mount table. |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 240 | vlog.Infof("Mount the collection server into the mount table.") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 241 | doMount(t, rootCtx, mtAddr, "stuff", collectionName, true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 242 | |
| 243 | // Create a few objects and make sure we can read them. |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 244 | vlog.Infof("Create a few objects.") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 245 | export(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/the/rain"), "the rain") |
| 246 | export(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/in/spain"), "in spain") |
| 247 | export(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/falls"), "falls mainly on the plain") |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 248 | vlog.Infof("Make sure we can read them.") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 249 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/the/rain"), "the rain", true) |
| 250 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/in/spain"), "in spain", true) |
| 251 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/falls"), "falls mainly on the plain", true) |
| 252 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "/stuff/falls"), "falls mainly on the plain", true) |
| 253 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/nonexistant"), "falls mainly on the plain", false) |
| 254 | checkContents(t, bobCtx, naming.JoinAddressName(mtAddr, "stuff/the/rain"), "the rain", true) |
| 255 | checkContents(t, aliceCtx, naming.JoinAddressName(mtAddr, "stuff/the/rain"), "the rain", false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 256 | |
| 257 | // Test multiple mounts. |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 258 | vlog.Infof("Multiple mounts.") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 259 | doMount(t, rootCtx, mtAddr, "a/b", collectionName, true) |
| 260 | doMount(t, rootCtx, mtAddr, "x/y", collectionName, true) |
| 261 | doMount(t, rootCtx, mtAddr, "alpha//beta", collectionName, true) |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 262 | vlog.Infof("Make sure we can read them.") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 263 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuff/falls"), "falls mainly on the plain", true) |
| 264 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", true) |
| 265 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "x/y/falls"), "falls mainly on the plain", true) |
| 266 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "alpha/beta/falls"), "falls mainly on the plain", true) |
| 267 | checkContents(t, aliceCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", true) |
| 268 | checkContents(t, bobCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 269 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 270 | // Test getting/setting AccessLists. |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 271 | perms, version := doGetPermissions(t, rootCtx, mtAddr, "stuff", true) |
| 272 | doSetPermissions(t, rootCtx, mtAddr, "stuff", perms, "xyzzy", false) // bad version |
| 273 | doSetPermissions(t, rootCtx, mtAddr, "stuff", perms, version, true) // correct version |
Adam Sadovsky | b1f9e3c | 2015-04-08 11:03:49 -0700 | [diff] [blame] | 274 | _, nversion := doGetPermissions(t, rootCtx, mtAddr, "stuff", true) |
| 275 | if nversion == version { |
| 276 | boom(t, "version didn't change after SetPermissions: %s", nversion) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 277 | } |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 278 | doSetPermissions(t, rootCtx, mtAddr, "stuff", perms, "", true) // no version |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 279 | |
| 280 | // Bob should be able to create nodes under the mounttable root but not alice. |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 281 | doSetPermissions(t, aliceCtx, mtAddr, "onlybob", perms, "", false) |
| 282 | doSetPermissions(t, bobCtx, mtAddr, "onlybob", perms, "", true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 283 | |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 284 | // Test that setting Permissions to permissions that don't include the the setter's |
Suharsh Sivakumar | 7120abd | 2015-04-15 13:54:23 -0700 | [diff] [blame] | 285 | // blessings in Admin, automatically add their Blessings to Admin to prevent |
| 286 | // locking everyone out. |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 287 | perms, _ = doGetPermissions(t, bobCtx, mtAddr, "onlybob", true) |
| 288 | noRootPerms := perms.Copy() |
| 289 | noRootPerms.Clear("bob", "Admin") |
| 290 | doSetPermissions(t, bobCtx, mtAddr, "onlybob", noRootPerms, "", true) |
Suharsh Sivakumar | 7120abd | 2015-04-15 13:54:23 -0700 | [diff] [blame] | 291 | // This should succeed, because "bob" should automatically be added to "Admin" |
| 292 | // even though he cleared himself from "Admin". |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 293 | doSetPermissions(t, bobCtx, mtAddr, "onlybob", perms, "", true) |
| 294 | // Test that adding a non-standard perms is normalized when retrieved. |
| 295 | admin := perms["Admin"] |
Suharsh Sivakumar | 7120abd | 2015-04-15 13:54:23 -0700 | [diff] [blame] | 296 | admin.In = []security.BlessingPattern{"bob", "bob"} |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 297 | perms["Admin"] = admin |
| 298 | doSetPermissions(t, bobCtx, mtAddr, "onlybob", perms, "", true) |
| 299 | perms, _ = doGetPermissions(t, bobCtx, mtAddr, "onlybob", true) |
| 300 | if got, want := perms["Admin"].In, []security.BlessingPattern{"bob"}; !reflect.DeepEqual(got, want) { |
Suharsh Sivakumar | 7120abd | 2015-04-15 13:54:23 -0700 | [diff] [blame] | 301 | boom(t, "got %v, want %v", got, want) |
| 302 | } |
| 303 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 304 | // Test generic unmount. |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 305 | vlog.Info("Test generic unmount.") |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 306 | doUnmount(t, rootCtx, mtAddr, "a/b", "", true) |
| 307 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 308 | |
| 309 | // Test specific unmount. |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 310 | vlog.Info("Test specific unmount.") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 311 | doMount(t, rootCtx, mtAddr, "a/b", collectionName, true) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 312 | doUnmount(t, rootCtx, mtAddr, "a/b", collectionName, true) |
| 313 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "a/b/falls"), "falls mainly on the plain", false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 314 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 315 | // Try timing out a mount. |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 316 | vlog.Info("Try timing out a mount.") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 317 | ft := NewFakeTimeClock() |
| 318 | setServerListClock(ft) |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 319 | doMount(t, rootCtx, mtAddr, "stuffWithTTL", collectionName, true) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 320 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuffWithTTL/the/rain"), "the rain", true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 321 | ft.advance(time.Duration(ttlSecs+4) * time.Second) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 322 | checkContents(t, rootCtx, naming.JoinAddressName(mtAddr, "stuffWithTTL/the/rain"), "the rain", false) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 323 | |
David Why Use Two When One Will Do Presotto | 5add67e | 2014-07-31 11:14:30 -0700 | [diff] [blame] | 324 | // Test unauthorized mount. |
| 325 | vlog.Info("Test unauthorized mount.") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 326 | doMount(t, bobCtx, mtAddr, "/a/b", collectionName, false) |
| 327 | doMount(t, aliceCtx, mtAddr, "/a/b", collectionName, false) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 328 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 329 | doUnmount(t, bobCtx, mtAddr, "x/y", collectionName, false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 330 | } |
| 331 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 332 | func doGlobX(t *testing.T, ctx *context.T, ep, suffix, pattern string, joinServer bool) []string { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 333 | name := naming.JoinAddressName(ep, suffix) |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 334 | client := v23.GetClient(ctx) |
Matt Rosencrantz | 94502cf | 2015-03-18 09:43:44 -0700 | [diff] [blame] | 335 | call, err := client.StartCall(ctx, name, rpc.GlobMethod, []interface{}{pattern}, options.NoResolve{}) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 336 | if err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 337 | boom(t, "Glob.StartCall %s %s: %s", name, pattern, err) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 338 | } |
| 339 | var reply []string |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 340 | for { |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 341 | var gr naming.GlobReply |
David Why Use Two When One Will Do Presotto | e5e62d0 | 2015-02-19 14:35:20 -0800 | [diff] [blame] | 342 | err := call.Recv(&gr) |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 343 | if err == io.EOF { |
| 344 | break |
| 345 | } |
| 346 | if err != nil { |
| 347 | boom(t, "Glob.StartCall %s: %s", name, pattern, err) |
| 348 | } |
David Why Use Two When One Will Do Presotto | e5e62d0 | 2015-02-19 14:35:20 -0800 | [diff] [blame] | 349 | switch v := gr.(type) { |
Todd Wang | 2331dd0 | 2015-03-17 15:38:39 -0700 | [diff] [blame] | 350 | case naming.GlobReplyEntry: |
David Why Use Two When One Will Do Presotto | e5e62d0 | 2015-02-19 14:35:20 -0800 | [diff] [blame] | 351 | if joinServer && len(v.Value.Servers) > 0 { |
| 352 | reply = append(reply, naming.JoinAddressName(v.Value.Servers[0].Server, v.Value.Name)) |
| 353 | } else { |
| 354 | reply = append(reply, v.Value.Name) |
| 355 | } |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 356 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 357 | } |
Todd Wang | e77f995 | 2015-02-18 13:20:50 -0800 | [diff] [blame] | 358 | if err := call.Finish(); err != nil { |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 359 | boom(t, "Glob.Finish %s: %s", name, pattern, err) |
Shyam Jayaraman | c4aed6e | 2014-07-22 14:25:06 -0700 | [diff] [blame] | 360 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 361 | return reply |
| 362 | } |
| 363 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 364 | func doGlob(t *testing.T, ctx *context.T, ep, suffix, pattern string) []string { |
| 365 | return doGlobX(t, ctx, ep, suffix, pattern, false) |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 366 | } |
| 367 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 368 | // checkMatch verified that the two slices contain the same string items, albeit |
| 369 | // not necessarily in the same order. Item repetitions are allowed, but their |
| 370 | // numbers need to match as well. |
| 371 | func checkMatch(t *testing.T, want []string, got []string) { |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 372 | if len(want) == 0 && len(got) == 0 { |
| 373 | return |
| 374 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 375 | w := sort.StringSlice(want) |
| 376 | w.Sort() |
| 377 | g := sort.StringSlice(got) |
| 378 | g.Sort() |
| 379 | if !reflect.DeepEqual(w, g) { |
| 380 | boom(t, "Glob expected %v got %v", want, got) |
| 381 | } |
| 382 | } |
| 383 | |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 384 | // checkExists makes sure a name exists (or not). |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 385 | func checkExists(t *testing.T, ctx *context.T, ep, suffix string, shouldSucceed bool) { |
| 386 | x := doGlobX(t, ctx, ep, "", suffix, false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 387 | if len(x) != 1 || x[0] != suffix { |
| 388 | if shouldSucceed { |
| 389 | boom(t, "Failed to find %s", suffix) |
| 390 | } |
| 391 | return |
| 392 | } |
| 393 | if !shouldSucceed { |
| 394 | boom(t, "%s exists but shouldn't", suffix) |
| 395 | } |
| 396 | } |
| 397 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 398 | func TestGlob(t *testing.T) { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 399 | rootCtx, shutdown := test.InitForTest() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 400 | defer shutdown() |
| 401 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 402 | server, estr := newMT(t, "", "", "testGlob", rootCtx) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 403 | defer server.Stop() |
| 404 | |
| 405 | // set up a mount space |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 406 | fakeServer := naming.JoinAddressName(estr, "quux") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 407 | doMount(t, rootCtx, estr, "one/bright/day", fakeServer, true) |
| 408 | doMount(t, rootCtx, estr, "in/the/middle", fakeServer, true) |
| 409 | doMount(t, rootCtx, estr, "of/the/night", fakeServer, true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 410 | |
| 411 | // Try various globs. |
| 412 | tests := []struct { |
| 413 | in string |
| 414 | expected []string |
| 415 | }{ |
| 416 | {"*", []string{"one", "in", "of"}}, |
Bogdan Caprita | b6b195a | 2014-06-11 17:55:03 -0700 | [diff] [blame] | 417 | {"...", []string{"", "one", "in", "of", "one/bright", "in/the", "of/the", "one/bright/day", "in/the/middle", "of/the/night"}}, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 418 | {"*/...", []string{"one", "in", "of", "one/bright", "in/the", "of/the", "one/bright/day", "in/the/middle", "of/the/night"}}, |
Bogdan Caprita | b6b195a | 2014-06-11 17:55:03 -0700 | [diff] [blame] | 419 | {"one/...", []string{"one", "one/bright", "one/bright/day"}}, |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 420 | {"of/the/night/two/dead/boys", []string{"of/the/night"}}, |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 421 | {"*/the", []string{"in/the", "of/the"}}, |
| 422 | {"*/the/...", []string{"in/the", "of/the", "in/the/middle", "of/the/night"}}, |
| 423 | {"o*", []string{"one", "of"}}, |
| 424 | {"", []string{""}}, |
| 425 | } |
| 426 | for _, test := range tests { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 427 | out := doGlob(t, rootCtx, estr, "", test.in) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 428 | checkMatch(t, test.expected, out) |
| 429 | } |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 430 | |
| 431 | // Test Glob on a name that is under a mounted server. The result should the |
| 432 | // the address the mounted server with the extra suffix. |
| 433 | { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 434 | results := doGlobX(t, rootCtx, estr, "of/the/night/two/dead/boys/got/up/to/fight", "*", true) |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 435 | if len(results) != 1 { |
| 436 | boom(t, "Unexpected number of results. Got %v, want 1", len(results)) |
| 437 | } |
David Why Use Two When One Will Do Presotto | 9c374c2 | 2014-10-21 16:16:30 -0700 | [diff] [blame] | 438 | _, suffix := naming.SplitAddressName(results[0]) |
| 439 | if expected := "quux/two/dead/boys/got/up/to/fight"; suffix != expected { |
Robin Thellend | 434e39f | 2014-08-27 14:46:27 -0700 | [diff] [blame] | 440 | boom(t, "Unexpected suffix. Got %v, want %v", suffix, expected) |
| 441 | } |
| 442 | } |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 443 | } |
| 444 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 445 | func TestAccessListTemplate(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 446 | rootCtx, aliceCtx, bobCtx, shutdown := initTest() |
| 447 | defer shutdown() |
| 448 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 449 | server, estr := newMT(t, "testdata/test.perms", "", "testAccessListTemplate", rootCtx) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 450 | defer server.Stop() |
| 451 | fakeServer := naming.JoinAddressName(estr, "quux") |
Tilak Sharma | da5165e | 2014-10-07 16:39:51 -0700 | [diff] [blame] | 452 | |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 453 | // Noone should be able to mount on someone else's names. |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 454 | doMount(t, aliceCtx, estr, "users/ted", fakeServer, false) |
| 455 | doMount(t, bobCtx, estr, "users/carol", fakeServer, false) |
| 456 | doMount(t, rootCtx, estr, "users/george", fakeServer, false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 457 | |
| 458 | // Anyone should be able to mount on their own names. |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 459 | doMount(t, aliceCtx, estr, "users/alice", fakeServer, true) |
| 460 | doMount(t, bobCtx, estr, "users/bob", fakeServer, true) |
| 461 | doMount(t, rootCtx, estr, "users/root", fakeServer, true) |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 462 | |
| 463 | // Make sure the counter works. |
| 464 | doUnmount(t, aliceCtx, estr, "users/alice", "", true) |
| 465 | doUnmount(t, bobCtx, estr, "users/bob", "", true) |
| 466 | doUnmount(t, rootCtx, estr, "users/root", "", true) |
| 467 | perms := access.Permissions{"Admin": access.AccessList{In: []security.BlessingPattern{security.AllPrincipals}}} |
| 468 | doSetPermissions(t, aliceCtx, estr, "users/alice/a/b/c/d", perms, "", true) |
| 469 | doSetPermissions(t, aliceCtx, estr, "users/alice/a/b/c/d", perms, "", true) |
| 470 | |
| 471 | // Do we obey limits? |
| 472 | for i := 0; i < defaultMaxNodesPerUser-5; i++ { |
| 473 | node := fmt.Sprintf("users/alice/a/b/c/d/%d", i) |
| 474 | doSetPermissions(t, aliceCtx, estr, node, perms, "", true) |
| 475 | } |
| 476 | doSetPermissions(t, aliceCtx, estr, "users/alice/a/b/c/d/straw", perms, "", false) |
| 477 | |
| 478 | // See if the stats numbers are correct. |
| 479 | testcases := []struct { |
| 480 | key string |
| 481 | expected interface{} |
| 482 | }{ |
| 483 | {"alice", int64(defaultMaxNodesPerUser)}, |
| 484 | {"bob", int64(0)}, |
| 485 | {"root", int64(0)}, |
| 486 | {localUser, int64(3)}, |
| 487 | } |
| 488 | for _, tc := range testcases { |
| 489 | name := "testAccessListTemplate/num-nodes-per-user/" + tc.key |
| 490 | got, err := libstats.Value(name) |
| 491 | if err != nil { |
| 492 | t.Errorf("unexpected error getting map entry for %s: %s", name, err) |
| 493 | } |
| 494 | if got != tc.expected { |
| 495 | t.Errorf("unexpected getting map entry for %s. Got %v, want %v", name, got, tc.expected) |
| 496 | } |
| 497 | } |
| 498 | } |
| 499 | |
| 500 | func getUserNodeCounts(t *testing.T) (counts map[string]int32) { |
| 501 | s, err := libstats.Value("mounttable/num-nodes-per-user") |
| 502 | if err != nil { |
| 503 | boom(t, "Can't get mounttable statistics") |
| 504 | } |
| 505 | // This string is a json encoded map. Decode. |
| 506 | switch v := s.(type) { |
| 507 | default: |
| 508 | boom(t, "Wrong type for mounttable statistics") |
| 509 | case string: |
| 510 | err = json.Unmarshal([]byte(v), &counts) |
| 511 | if err != nil { |
| 512 | boom(t, "Can't unmarshal mounttable statistics") |
| 513 | } |
| 514 | } |
| 515 | return |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 516 | } |
| 517 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 518 | func TestGlobAccessLists(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 519 | rootCtx, aliceCtx, bobCtx, shutdown := initTest() |
| 520 | defer shutdown() |
| 521 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 522 | server, estr := newMT(t, "testdata/test.perms", "", "testGlobAccessLists", rootCtx) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 523 | defer server.Stop() |
| 524 | |
| 525 | // set up a mount space |
| 526 | fakeServer := naming.JoinAddressName(estr, "quux") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 527 | doMount(t, aliceCtx, estr, "one/bright/day", fakeServer, false) // Fails because alice can't mount there. |
| 528 | doMount(t, bobCtx, estr, "one/bright/day", fakeServer, true) |
| 529 | doMount(t, rootCtx, estr, "a/b/c", fakeServer, true) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 530 | |
| 531 | // Try various globs. |
| 532 | tests := []struct { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 533 | ctx *context.T |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 534 | in string |
| 535 | expected []string |
| 536 | }{ |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 537 | {rootCtx, "*", []string{"one", "a", "stuff", "users"}}, |
| 538 | {aliceCtx, "*", []string{"one", "a", "users"}}, |
| 539 | {bobCtx, "*", []string{"one", "stuff", "users"}}, |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 540 | // bob, alice, and root have different visibility to the space. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 541 | {rootCtx, "*/...", []string{"one", "a", "one/bright", "a/b", "one/bright/day", "a/b/c", "stuff", "users"}}, |
| 542 | {aliceCtx, "*/...", []string{"one", "a", "one/bright", "a/b", "one/bright/day", "a/b/c", "users"}}, |
| 543 | {bobCtx, "*/...", []string{"one", "one/bright", "one/bright/day", "stuff", "users"}}, |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 544 | } |
| 545 | for _, test := range tests { |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 546 | out := doGlob(t, test.ctx, estr, "", test.in) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 547 | checkMatch(t, test.expected, out) |
| 548 | } |
| 549 | } |
| 550 | |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 551 | func TestCleanup(t *testing.T) { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 552 | rootCtx, shutdown := test.InitForTest() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 553 | defer shutdown() |
| 554 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 555 | server, estr := newMT(t, "", "", "testCleanup", rootCtx) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 556 | defer server.Stop() |
| 557 | |
| 558 | // Set up one mount. |
| 559 | fakeServer := naming.JoinAddressName(estr, "quux") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 560 | doMount(t, rootCtx, estr, "one/bright/day", fakeServer, true) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 561 | checkMatch(t, []string{"one", "one/bright", "one/bright/day"}, doGlob(t, rootCtx, estr, "", "*/...")) |
| 562 | |
| 563 | // After the unmount nothing should be left |
| 564 | doUnmount(t, rootCtx, estr, "one/bright/day", "", true) |
David Why Use Two When One Will Do Presotto | 4d26a99 | 2015-05-06 10:17:09 -0700 | [diff] [blame] | 565 | checkMatch(t, nil, doGlob(t, rootCtx, estr, "", "one")) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 566 | checkMatch(t, nil, doGlob(t, rootCtx, estr, "", "*/...")) |
| 567 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 568 | // Set up a mount, then set the AccessList. |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 569 | doMount(t, rootCtx, estr, "one/bright/day", fakeServer, true) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 570 | checkMatch(t, []string{"one", "one/bright", "one/bright/day"}, doGlob(t, rootCtx, estr, "", "*/...")) |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 571 | perms := access.Permissions{"Read": access.AccessList{In: []security.BlessingPattern{security.AllPrincipals}}} |
| 572 | doSetPermissions(t, rootCtx, estr, "one/bright", perms, "", true) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 573 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 574 | // After the unmount we should still have everything above the AccessList. |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 575 | doUnmount(t, rootCtx, estr, "one/bright/day", "", true) |
| 576 | checkMatch(t, []string{"one", "one/bright"}, doGlob(t, rootCtx, estr, "", "*/...")) |
| 577 | } |
| 578 | |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 579 | func TestDelete(t *testing.T) { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 580 | rootCtx, aliceCtx, bobCtx, shutdown := initTest() |
| 581 | defer shutdown() |
| 582 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 583 | server, estr := newMT(t, "testdata/test.perms", "", "testDelete", rootCtx) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 584 | defer server.Stop() |
| 585 | |
| 586 | // set up a mount space |
| 587 | fakeServer := naming.JoinAddressName(estr, "quux") |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 588 | doMount(t, bobCtx, estr, "one/bright/day", fakeServer, true) |
| 589 | doMount(t, rootCtx, estr, "a/b/c", fakeServer, true) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 590 | |
| 591 | // It shouldn't be possible to delete anything with children unless explicitly requested. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 592 | doDeleteNode(t, rootCtx, estr, "a/b", false) |
| 593 | checkExists(t, rootCtx, estr, "a/b", true) |
| 594 | doDeleteSubtree(t, rootCtx, estr, "a/b", true) |
| 595 | checkExists(t, rootCtx, estr, "a/b", false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 596 | |
| 597 | // Alice shouldn't be able to delete what bob created but bob and root should. |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 598 | doDeleteNode(t, aliceCtx, estr, "one/bright/day", false) |
| 599 | checkExists(t, rootCtx, estr, "one/bright/day", true) |
| 600 | doDeleteNode(t, rootCtx, estr, "one/bright/day", true) |
| 601 | checkExists(t, rootCtx, estr, "one/bright/day", false) |
| 602 | doDeleteNode(t, bobCtx, estr, "one/bright", true) |
| 603 | checkExists(t, rootCtx, estr, "one/bright", false) |
David Why Use Two When One Will Do Presotto | ab2bcf2 | 2015-01-05 13:14:01 -0800 | [diff] [blame] | 604 | } |
| 605 | |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 606 | func TestServerFormat(t *testing.T) { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 607 | rootCtx, shutdown := test.InitForTest() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 608 | defer shutdown() |
| 609 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 610 | server, estr := newMT(t, "", "", "testerverFormat", rootCtx) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 611 | defer server.Stop() |
| 612 | |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 613 | doMount(t, rootCtx, estr, "endpoint", naming.JoinAddressName(estr, "life/on/the/mississippi"), true) |
| 614 | doMount(t, rootCtx, estr, "hostport", "/atrampabroad:8000", true) |
| 615 | doMount(t, rootCtx, estr, "invalid/not/rooted", "atrampabroad:8000", false) |
| 616 | doMount(t, rootCtx, estr, "invalid/no/port", "/atrampabroad", false) |
| 617 | doMount(t, rootCtx, estr, "invalid/endpoint", "/@following the equator:8000@@@", false) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 618 | } |
| 619 | |
| 620 | func TestExpiry(t *testing.T) { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 621 | rootCtx, shutdown := test.InitForTest() |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 622 | defer shutdown() |
| 623 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 624 | server, estr := newMT(t, "", "", "testExpiry", rootCtx) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 625 | defer server.Stop() |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 626 | collection, collectionAddr := newCollection(t, rootCtx) |
Cosmos Nicolaou | fdc838b | 2014-06-30 21:44:27 -0700 | [diff] [blame] | 627 | defer collection.Stop() |
| 628 | |
| 629 | collectionName := naming.JoinAddressName(collectionAddr, "collection") |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 630 | |
| 631 | ft := NewFakeTimeClock() |
| 632 | setServerListClock(ft) |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 633 | doMount(t, rootCtx, estr, "a1/b1", collectionName, true) |
| 634 | doMount(t, rootCtx, estr, "a1/b2", collectionName, true) |
| 635 | doMount(t, rootCtx, estr, "a2/b1", collectionName, true) |
| 636 | doMount(t, rootCtx, estr, "a2/b2/c", collectionName, true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 637 | |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 638 | checkMatch(t, []string{"a1/b1", "a2/b1"}, doGlob(t, rootCtx, estr, "", "*/b1/...")) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 639 | ft.advance(time.Duration(ttlSecs/2) * time.Second) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 640 | checkMatch(t, []string{"a1/b1", "a2/b1"}, doGlob(t, rootCtx, estr, "", "*/b1/...")) |
| 641 | checkMatch(t, []string{"c"}, doGlob(t, rootCtx, estr, "a2/b2", "*")) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 642 | // Refresh only a1/b1. All the other mounts will expire upon the next |
| 643 | // ft advance. |
Asim Shankar | 43d1f93 | 2015-03-24 20:57:56 -0700 | [diff] [blame] | 644 | doMount(t, rootCtx, estr, "a1/b1", collectionName, true) |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 645 | ft.advance(time.Duration(ttlSecs/2+4) * time.Second) |
David Why Use Two When One Will Do Presotto | a410017 | 2015-01-14 09:25:45 -0800 | [diff] [blame] | 646 | checkMatch(t, []string{"a1", "a1/b1"}, doGlob(t, rootCtx, estr, "", "*/...")) |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 647 | checkMatch(t, []string{"a1/b1"}, doGlob(t, rootCtx, estr, "", "*/b1/...")) |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 648 | } |
| 649 | |
Benjamin Prosnitz | b60efb9 | 2015-03-11 17:47:43 -0700 | [diff] [blame] | 650 | func TestBadAccessLists(t *testing.T) { |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 651 | _, err := NewMountTableDispatcher("testdata/invalid.perms", "", "mounttable") |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 652 | if err == nil { |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 653 | boom(t, "Expected json parse error in permissions file") |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 654 | } |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 655 | _, err = NewMountTableDispatcher("testdata/doesntexist.perms", "", "mounttable") |
Robin Thellend | c4b5169 | 2015-02-05 17:11:16 -0800 | [diff] [blame] | 656 | if err != nil { |
David Why Use Two When One Will Do Presotto | ba9593f | 2015-04-22 12:29:26 -0700 | [diff] [blame] | 657 | boom(t, "Missing permissions file should not cause an error") |
Ryan Brown | ac97265 | 2014-05-19 10:57:32 -0700 | [diff] [blame] | 658 | } |
Jiri Simsa | 5293dcb | 2014-05-10 09:56:38 -0700 | [diff] [blame] | 659 | } |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 660 | |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 661 | func getCounter(t *testing.T, ctx *context.T, name string) int64 { |
| 662 | st := stats.StatsClient(name) |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 663 | v, err := st.Value(ctx) |
| 664 | if err != nil { |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 665 | t.Fatalf("Failed to get %q: %v", name, err) |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 666 | return -1 |
| 667 | } |
| 668 | var value int64 |
| 669 | if err := vdl.Convert(&value, v); err != nil { |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 670 | t.Fatalf("Unexpected value type for %q: %v", name, err) |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 671 | } |
| 672 | return value |
| 673 | } |
| 674 | |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 675 | func nodeCount(t *testing.T, ctx *context.T, addr string) int64 { |
| 676 | name := naming.JoinAddressName(addr, "__debug/stats/mounttable/num-nodes") |
| 677 | return getCounter(t, ctx, name) |
| 678 | } |
| 679 | |
| 680 | func serverCount(t *testing.T, ctx *context.T, addr string) int64 { |
| 681 | name := naming.JoinAddressName(addr, "__debug/stats/mounttable/num-mounted-servers") |
| 682 | return getCounter(t, ctx, name) |
| 683 | } |
| 684 | |
| 685 | func TestStatsCounters(t *testing.T) { |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 686 | rootCtx, shutdown := test.InitForTest() |
| 687 | defer shutdown() |
| 688 | |
Robin Thellend | 62909b2 | 2015-04-23 13:54:55 -0700 | [diff] [blame] | 689 | ft := NewFakeTimeClock() |
| 690 | setServerListClock(ft) |
| 691 | |
David Why Use Two When One Will Do Presotto | c74ac70 | 2015-05-07 15:33:52 -0700 | [diff] [blame] | 692 | server, estr := newMT(t, "", "", "mounttable", rootCtx) |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 693 | defer server.Stop() |
| 694 | |
| 695 | // Test flat tree |
| 696 | for i := 1; i <= 10; i++ { |
| 697 | name := fmt.Sprintf("node%d", i) |
| 698 | addr := naming.JoinAddressName(estr, name) |
| 699 | doMount(t, rootCtx, estr, name, addr, true) |
| 700 | if expected, got := int64(i+1), nodeCount(t, rootCtx, estr); got != expected { |
| 701 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 702 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 703 | if expected, got := int64(i), serverCount(t, rootCtx, estr); got != expected { |
| 704 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 705 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 706 | } |
| 707 | for i := 1; i <= 10; i++ { |
| 708 | name := fmt.Sprintf("node%d", i) |
| 709 | if i%2 == 0 { |
| 710 | doUnmount(t, rootCtx, estr, name, "", true) |
| 711 | } else { |
| 712 | doDeleteSubtree(t, rootCtx, estr, name, true) |
| 713 | } |
| 714 | if expected, got := int64(11-i), nodeCount(t, rootCtx, estr); got != expected { |
| 715 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 716 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 717 | if expected, got := int64(10-i), serverCount(t, rootCtx, estr); got != expected { |
| 718 | t.Errorf("Unexpected number of server. Got %d, expected %d", got, expected) |
| 719 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 720 | } |
| 721 | |
| 722 | // Test deep tree |
| 723 | doMount(t, rootCtx, estr, "1/2/3/4/5/6/7/8/9a/10", naming.JoinAddressName(estr, ""), true) |
| 724 | doMount(t, rootCtx, estr, "1/2/3/4/5/6/7/8/9b/11", naming.JoinAddressName(estr, ""), true) |
| 725 | if expected, got := int64(13), nodeCount(t, rootCtx, estr); got != expected { |
| 726 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 727 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 728 | if expected, got := int64(2), serverCount(t, rootCtx, estr); got != expected { |
| 729 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 730 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 731 | doDeleteSubtree(t, rootCtx, estr, "1/2/3/4/5", true) |
| 732 | if expected, got := int64(5), nodeCount(t, rootCtx, estr); got != expected { |
| 733 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 734 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 735 | if expected, got := int64(0), serverCount(t, rootCtx, estr); got != expected { |
| 736 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 737 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 738 | doDeleteSubtree(t, rootCtx, estr, "1", true) |
| 739 | if expected, got := int64(1), nodeCount(t, rootCtx, estr); got != expected { |
| 740 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 741 | } |
Robin Thellend | 0aed6da | 2015-04-17 14:39:35 -0700 | [diff] [blame] | 742 | |
| 743 | // Test multiple servers per node |
| 744 | for i := 1; i <= 5; i++ { |
| 745 | server := naming.JoinAddressName(estr, fmt.Sprintf("addr%d", i)) |
| 746 | doMount(t, rootCtx, estr, "node1", server, true) |
| 747 | doMount(t, rootCtx, estr, "node2", server, true) |
| 748 | if expected, got := int64(3), nodeCount(t, rootCtx, estr); got != expected { |
| 749 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 750 | } |
| 751 | if expected, got := int64(2*i), serverCount(t, rootCtx, estr); got != expected { |
| 752 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 753 | } |
| 754 | } |
| 755 | doUnmount(t, rootCtx, estr, "node1", "", true) |
| 756 | if expected, got := int64(2), nodeCount(t, rootCtx, estr); got != expected { |
| 757 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 758 | } |
| 759 | if expected, got := int64(5), serverCount(t, rootCtx, estr); got != expected { |
| 760 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 761 | } |
| 762 | for i := 1; i <= 5; i++ { |
| 763 | server := naming.JoinAddressName(estr, fmt.Sprintf("addr%d", i)) |
| 764 | doUnmount(t, rootCtx, estr, "node2", server, true) |
| 765 | expectedNodes := int64(2) |
| 766 | if i == 5 { |
| 767 | expectedNodes = 1 |
| 768 | } |
| 769 | if expected, got := expectedNodes, nodeCount(t, rootCtx, estr); got != expected { |
| 770 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 771 | } |
| 772 | if expected, got := int64(5-i), serverCount(t, rootCtx, estr); got != expected { |
| 773 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 774 | } |
| 775 | } |
Robin Thellend | 93b1f6d | 2015-04-22 12:31:34 -0700 | [diff] [blame] | 776 | |
| 777 | // Mount on an existing intermediate node. |
| 778 | doMount(t, rootCtx, estr, "1/2/3/4/5/6/7/8/9a/10", naming.JoinAddressName(estr, ""), true) |
| 779 | doMount(t, rootCtx, estr, "1/2/3/4/5", naming.JoinAddressName(estr, ""), true) |
| 780 | if expected, got := int64(6), nodeCount(t, rootCtx, estr); got != expected { |
| 781 | t.Errorf("Unexpected number of nodes. Got %d, expected %d", got, expected) |
| 782 | } |
| 783 | if expected, got := int64(1), serverCount(t, rootCtx, estr); got != expected { |
| 784 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 785 | } |
Robin Thellend | 62909b2 | 2015-04-23 13:54:55 -0700 | [diff] [blame] | 786 | |
| 787 | // Test expired mounts |
| 788 | // "1/2/3/4/5" is still mounted from earlier. |
| 789 | ft.advance(time.Duration(ttlSecs+4) * time.Second) |
| 790 | if _, err := resolve(rootCtx, naming.JoinAddressName(estr, "1/2/3/4/5")); err == nil { |
| 791 | t.Errorf("Expected failure. Got success") |
| 792 | } |
| 793 | if expected, got := int64(0), serverCount(t, rootCtx, estr); got != expected { |
| 794 | t.Errorf("Unexpected number of servers. Got %d, expected %d", got, expected) |
| 795 | } |
Robin Thellend | fd82dcc | 2015-04-14 21:16:59 -0700 | [diff] [blame] | 796 | } |
| 797 | |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 798 | func initTest() (rootCtx *context.T, aliceCtx *context.T, bobCtx *context.T, shutdown v23.Shutdown) { |
Cosmos Nicolaou | 1381f8a | 2015-03-13 09:40:34 -0700 | [diff] [blame] | 799 | test.Init() |
| 800 | ctx, shutdown := test.InitForTest() |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 801 | var err error |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 802 | if rootCtx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal("root")); err != nil { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 803 | panic("failed to set root principal") |
Matt Rosencrantz | ac32b6c | 2014-12-01 15:49:18 -0800 | [diff] [blame] | 804 | } |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 805 | if aliceCtx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal("alice")); err != nil { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 806 | panic("failed to set alice principal") |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 807 | } |
Todd Wang | ad49204 | 2015-04-17 15:58:40 -0700 | [diff] [blame] | 808 | if bobCtx, err = v23.WithPrincipal(ctx, testutil.NewPrincipal("bob")); err != nil { |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 809 | panic("failed to set bob principal") |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 810 | } |
Matt Rosencrantz | 6edab56 | 2015-01-12 11:07:55 -0800 | [diff] [blame] | 811 | for _, r := range []*context.T{rootCtx, aliceCtx, bobCtx} { |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 812 | // A hack to set the namespace roots to a value that won't work. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 813 | v23.GetNamespace(r).SetRoots() |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 814 | // And have all principals recognize each others blessings. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 815 | p1 := v23.GetPrincipal(r) |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 816 | for _, other := range []*context.T{rootCtx, aliceCtx, bobCtx} { |
Asim Shankar | 4a69828 | 2015-03-21 21:59:18 -0700 | [diff] [blame] | 817 | // testutil.NewPrincipal has already setup each |
Asim Shankar | 8bc78d6 | 2015-02-03 16:53:27 -0800 | [diff] [blame] | 818 | // principal to use the same blessing for both server |
| 819 | // and client activities. |
Jiri Simsa | 6ac9522 | 2015-02-23 16:11:49 -0800 | [diff] [blame] | 820 | if err := p1.AddToRoots(v23.GetPrincipal(other).BlessingStore().Default()); err != nil { |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 821 | panic(err) |
| 822 | } |
| 823 | } |
| 824 | } |
Suharsh Sivakumar | 19fbf99 | 2015-01-23 11:02:27 -0800 | [diff] [blame] | 825 | return rootCtx, aliceCtx, bobCtx, shutdown |
Asim Shankar | 8829291 | 2014-10-09 19:41:07 -0700 | [diff] [blame] | 826 | } |