Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 1 | package ipc |
| 2 | |
| 3 | import ( |
| 4 | "reflect" |
| 5 | "sort" |
| 6 | "testing" |
| 7 | |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 8 | "veyron.io/veyron/veyron2/ipc" |
| 9 | "veyron.io/veyron/veyron2/naming" |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 10 | "veyron.io/veyron/veyron2/options" |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 11 | "veyron.io/veyron/veyron2/vlog" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 12 | |
| 13 | "veyron.io/veyron/veyron/lib/stats" |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 14 | tsecurity "veyron.io/veyron/veyron/lib/testutil/security" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 15 | "veyron.io/veyron/veyron/runtimes/google/ipc/stream/manager" |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 16 | "veyron.io/veyron/veyron/runtimes/google/ipc/stream/vc" |
Cosmos Nicolaou | d6c3c9c | 2014-09-30 15:42:53 -0700 | [diff] [blame] | 17 | tnaming "veyron.io/veyron/veyron/runtimes/google/testing/mocks/naming" |
Matt Rosencrantz | b30286b | 2014-11-10 14:52:17 -0800 | [diff] [blame] | 18 | "veyron.io/veyron/veyron/runtimes/google/vtrace" |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 19 | "veyron.io/veyron/veyron/services/mgmt/debug" |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 20 | ) |
| 21 | |
| 22 | func TestDebugServer(t *testing.T) { |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 23 | // Setup the client and server principals, with the client willing to share its |
| 24 | // blessing with the server. |
| 25 | var ( |
Ankur | e49a86a | 2014-11-11 18:52:43 -0800 | [diff] [blame] | 26 | pclient = tsecurity.NewPrincipal("client") |
| 27 | pserver = tsecurity.NewPrincipal("server") |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 28 | bclient = bless(pserver, pclient, "client") // server/client blessing. |
| 29 | ) |
| 30 | pclient.AddToRoots(bclient) // Client recognizes "server" as a root of blessings. |
| 31 | pclient.BlessingStore().Set(bclient, "server") // Client presents bclient to server |
| 32 | |
Matt Rosencrantz | b30286b | 2014-11-10 14:52:17 -0800 | [diff] [blame] | 33 | store := vtrace.NewStore(10) |
| 34 | debugDisp := debug.NewDispatcher(vlog.Log.LogDir(), nil, store) |
Cosmos Nicolaou | 8246a8b | 2014-11-01 09:32:36 -0700 | [diff] [blame] | 35 | |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 36 | sm := manager.InternalNew(naming.FixedRoutingID(0x555555555)) |
| 37 | defer sm.Shutdown() |
| 38 | ns := tnaming.NewSimpleNamespace() |
Matt Rosencrantz | 3e76f28 | 2014-11-10 09:38:57 -0800 | [diff] [blame] | 39 | server, err := InternalNewServer(testContext(), sm, ns, nil, options.ReservedNameDispatcher{debugDisp}, vc.LocalPrincipal{pserver}) |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 40 | if err != nil { |
| 41 | t.Fatalf("InternalNewServer failed: %v", err) |
| 42 | } |
| 43 | defer server.Stop() |
Cosmos Nicolaou | 92dba58 | 2014-11-05 17:24:10 -0800 | [diff] [blame] | 44 | server.Serve("", &testObject{}, nil) |
Cosmos Nicolaou | f8d4c2b | 2014-10-23 22:36:38 -0700 | [diff] [blame] | 45 | ep, err := server.Listen(listenSpec) |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 46 | if err != nil { |
| 47 | t.Fatalf("server.Listen failed: %v", err) |
| 48 | } |
Asim Shankar | 8f05c22 | 2014-10-06 22:08:19 -0700 | [diff] [blame] | 49 | client, err := InternalNewClient(sm, ns, vc.LocalPrincipal{pclient}) |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 50 | if err != nil { |
| 51 | t.Fatalf("InternalNewClient failed: %v", err) |
| 52 | } |
| 53 | defer client.Close() |
| 54 | ctx := testContext() |
| 55 | // Call the Foo method on "" |
| 56 | { |
David Why Use Two When One Will Do Presotto | 3da1c79 | 2014-10-03 11:15:53 -0700 | [diff] [blame] | 57 | addr := naming.JoinAddressName(ep.String(), "") |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 58 | call, err := client.StartCall(ctx, addr, "Foo", nil) |
| 59 | if err != nil { |
| 60 | t.Fatalf("client.StartCall failed: %v", err) |
| 61 | } |
| 62 | var value string |
| 63 | if ferr := call.Finish(&value); ferr != nil { |
| 64 | t.Fatalf("call.Finish failed: %v", ferr) |
| 65 | } |
| 66 | if want := "BAR"; value != want { |
| 67 | t.Errorf("unexpected value: Got %v, want %v", value, want) |
| 68 | } |
| 69 | } |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 70 | // Call Value on __debug/stats/testing/foo |
| 71 | { |
| 72 | foo := stats.NewString("testing/foo") |
| 73 | foo.Set("The quick brown fox jumps over the lazy dog") |
David Why Use Two When One Will Do Presotto | 3da1c79 | 2014-10-03 11:15:53 -0700 | [diff] [blame] | 74 | addr := naming.JoinAddressName(ep.String(), "__debug/stats/testing/foo") |
Asim Shankar | cc04421 | 2014-10-15 23:25:26 -0700 | [diff] [blame] | 75 | call, err := client.StartCall(ctx, addr, "Value", nil, options.NoResolve(true)) |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 76 | if err != nil { |
| 77 | t.Fatalf("client.StartCall failed: %v", err) |
| 78 | } |
| 79 | var value string |
| 80 | if ferr := call.Finish(&value, &err); ferr != nil { |
| 81 | t.Fatalf("call.Finish failed: %v", ferr) |
| 82 | } |
| 83 | if err != nil { |
| 84 | t.Fatalf("unexpected error: %v", err) |
| 85 | } |
| 86 | if want := foo.Value(); value != want { |
| 87 | t.Errorf("unexpected result: Got %v, want %v", value, want) |
| 88 | } |
| 89 | } |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 90 | |
| 91 | // Call Glob |
| 92 | testcases := []struct { |
| 93 | name, pattern string |
| 94 | expected []string |
| 95 | }{ |
| 96 | {"", "*", []string{}}, |
| 97 | {"", "__*", []string{"__debug"}}, |
Matt Rosencrantz | b30286b | 2014-11-10 14:52:17 -0800 | [diff] [blame] | 98 | {"", "__*/*", []string{"__debug/logs", "__debug/pprof", "__debug/stats", "__debug/vtrace"}}, |
| 99 | {"__debug", "*", []string{"logs", "pprof", "stats", "vtrace"}}, |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 100 | } |
| 101 | for _, tc := range testcases { |
David Why Use Two When One Will Do Presotto | adf0ca1 | 2014-11-13 10:49:01 -0800 | [diff] [blame] | 102 | addr := naming.JoinAddressName(ep.String(), tc.name) |
| 103 | call, err := client.StartCall(ctx, addr, ipc.GlobMethod, []interface{}{tc.pattern}, options.NoResolve(true)) |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 104 | if err != nil { |
| 105 | t.Fatalf("client.StartCall failed: %v", err) |
| 106 | } |
| 107 | results := []string{} |
| 108 | for { |
Todd Wang | 1aa5769 | 2014-11-11 13:53:29 -0800 | [diff] [blame] | 109 | var me naming.VDLMountEntry |
Robin Thellend | c26c32e | 2014-10-06 17:44:04 -0700 | [diff] [blame] | 110 | if err := call.Recv(&me); err != nil { |
| 111 | break |
| 112 | } |
| 113 | results = append(results, me.Name) |
| 114 | } |
| 115 | if ferr := call.Finish(&err); ferr != nil { |
| 116 | t.Fatalf("call.Finish failed: %v", ferr) |
| 117 | } |
| 118 | sort.Strings(results) |
| 119 | if !reflect.DeepEqual(tc.expected, results) { |
| 120 | t.Errorf("unexpected results. Got %v, want %v", results, tc.expected) |
| 121 | } |
| 122 | } |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 123 | } |
| 124 | |
| 125 | type testObject struct { |
| 126 | } |
| 127 | |
Todd Wang | 1fe7cdd | 2014-11-12 12:51:49 -0800 | [diff] [blame] | 128 | func (o testObject) Foo(ipc.ServerContext) string { |
Robin Thellend | d24f084 | 2014-09-23 10:27:29 -0700 | [diff] [blame] | 129 | return "BAR" |
| 130 | } |