blob: 966952ff48ad90e55f23fe656ed54239ef38283b [file] [log] [blame]
Robin Thellendd24f0842014-09-23 10:27:29 -07001package ipc
2
3import (
Bogdan Caprita1ec40522014-12-10 14:56:33 -08004 "io"
Robin Thellendd24f0842014-09-23 10:27:29 -07005 "reflect"
6 "sort"
7 "testing"
8
Robin Thellendd24f0842014-09-23 10:27:29 -07009 "veyron.io/veyron/veyron2/ipc"
10 "veyron.io/veyron/veyron2/naming"
Asim Shankarcc044212014-10-15 23:25:26 -070011 "veyron.io/veyron/veyron2/options"
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070012 "veyron.io/veyron/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070013
14 "veyron.io/veyron/veyron/lib/stats"
Ankure49a86a2014-11-11 18:52:43 -080015 tsecurity "veyron.io/veyron/veyron/lib/testutil/security"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070016 "veyron.io/veyron/veyron/runtimes/google/ipc/stream/manager"
Asim Shankar8f05c222014-10-06 22:08:19 -070017 "veyron.io/veyron/veyron/runtimes/google/ipc/stream/vc"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070018 tnaming "veyron.io/veyron/veyron/runtimes/google/testing/mocks/naming"
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080019 "veyron.io/veyron/veyron/runtimes/google/vtrace"
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070020 "veyron.io/veyron/veyron/services/mgmt/debug"
Robin Thellendd24f0842014-09-23 10:27:29 -070021)
22
23func TestDebugServer(t *testing.T) {
Asim Shankar8f05c222014-10-06 22:08:19 -070024 // Setup the client and server principals, with the client willing to share its
25 // blessing with the server.
26 var (
Ankure49a86a2014-11-11 18:52:43 -080027 pclient = tsecurity.NewPrincipal("client")
28 pserver = tsecurity.NewPrincipal("server")
Asim Shankar8f05c222014-10-06 22:08:19 -070029 bclient = bless(pserver, pclient, "client") // server/client blessing.
30 )
31 pclient.AddToRoots(bclient) // Client recognizes "server" as a root of blessings.
32 pclient.BlessingStore().Set(bclient, "server") // Client presents bclient to server
33
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080034 store := vtrace.NewStore(10)
35 debugDisp := debug.NewDispatcher(vlog.Log.LogDir(), nil, store)
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070036
Robin Thellendd24f0842014-09-23 10:27:29 -070037 sm := manager.InternalNew(naming.FixedRoutingID(0x555555555))
38 defer sm.Shutdown()
39 ns := tnaming.NewSimpleNamespace()
Matt Rosencrantz3e76f282014-11-10 09:38:57 -080040 server, err := InternalNewServer(testContext(), sm, ns, nil, options.ReservedNameDispatcher{debugDisp}, vc.LocalPrincipal{pserver})
Robin Thellendd24f0842014-09-23 10:27:29 -070041 if err != nil {
42 t.Fatalf("InternalNewServer failed: %v", err)
43 }
44 defer server.Stop()
Cosmos Nicolaou92dba582014-11-05 17:24:10 -080045 server.Serve("", &testObject{}, nil)
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080046 eps, err := server.Listen(listenSpec)
Robin Thellendd24f0842014-09-23 10:27:29 -070047 if err != nil {
48 t.Fatalf("server.Listen failed: %v", err)
49 }
Asim Shankar8f05c222014-10-06 22:08:19 -070050 client, err := InternalNewClient(sm, ns, vc.LocalPrincipal{pclient})
Robin Thellendd24f0842014-09-23 10:27:29 -070051 if err != nil {
52 t.Fatalf("InternalNewClient failed: %v", err)
53 }
54 defer client.Close()
55 ctx := testContext()
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080056 ep := eps[0]
Robin Thellendd24f0842014-09-23 10:27:29 -070057 // Call the Foo method on ""
58 {
David Why Use Two When One Will Do Presotto3da1c792014-10-03 11:15:53 -070059 addr := naming.JoinAddressName(ep.String(), "")
Robin Thellendd24f0842014-09-23 10:27:29 -070060 call, err := client.StartCall(ctx, addr, "Foo", nil)
61 if err != nil {
62 t.Fatalf("client.StartCall failed: %v", err)
63 }
64 var value string
65 if ferr := call.Finish(&value); ferr != nil {
66 t.Fatalf("call.Finish failed: %v", ferr)
67 }
68 if want := "BAR"; value != want {
69 t.Errorf("unexpected value: Got %v, want %v", value, want)
70 }
71 }
Robin Thellendd24f0842014-09-23 10:27:29 -070072 // Call Value on __debug/stats/testing/foo
73 {
74 foo := stats.NewString("testing/foo")
75 foo.Set("The quick brown fox jumps over the lazy dog")
David Why Use Two When One Will Do Presotto3da1c792014-10-03 11:15:53 -070076 addr := naming.JoinAddressName(ep.String(), "__debug/stats/testing/foo")
Asim Shankarcc044212014-10-15 23:25:26 -070077 call, err := client.StartCall(ctx, addr, "Value", nil, options.NoResolve(true))
Robin Thellendd24f0842014-09-23 10:27:29 -070078 if err != nil {
79 t.Fatalf("client.StartCall failed: %v", err)
80 }
81 var value string
82 if ferr := call.Finish(&value, &err); ferr != nil {
83 t.Fatalf("call.Finish failed: %v", ferr)
84 }
85 if err != nil {
86 t.Fatalf("unexpected error: %v", err)
87 }
88 if want := foo.Value(); value != want {
89 t.Errorf("unexpected result: Got %v, want %v", value, want)
90 }
91 }
Robin Thellendc26c32e2014-10-06 17:44:04 -070092
93 // Call Glob
94 testcases := []struct {
95 name, pattern string
96 expected []string
97 }{
98 {"", "*", []string{}},
99 {"", "__*", []string{"__debug"}},
Matt Rosencrantzb30286b2014-11-10 14:52:17 -0800100 {"", "__*/*", []string{"__debug/logs", "__debug/pprof", "__debug/stats", "__debug/vtrace"}},
101 {"__debug", "*", []string{"logs", "pprof", "stats", "vtrace"}},
Robin Thellendc26c32e2014-10-06 17:44:04 -0700102 }
103 for _, tc := range testcases {
David Why Use Two When One Will Do Presottoadf0ca12014-11-13 10:49:01 -0800104 addr := naming.JoinAddressName(ep.String(), tc.name)
105 call, err := client.StartCall(ctx, addr, ipc.GlobMethod, []interface{}{tc.pattern}, options.NoResolve(true))
Robin Thellendc26c32e2014-10-06 17:44:04 -0700106 if err != nil {
Robin Thellend877ac012014-12-10 14:19:28 -0800107 t.Fatalf("client.StartCall failed for %q: %v", tc.name, err)
Robin Thellendc26c32e2014-10-06 17:44:04 -0700108 }
109 results := []string{}
110 for {
Todd Wang1aa57692014-11-11 13:53:29 -0800111 var me naming.VDLMountEntry
Robin Thellendc26c32e2014-10-06 17:44:04 -0700112 if err := call.Recv(&me); err != nil {
Bogdan Caprita1ec40522014-12-10 14:56:33 -0800113 if err != io.EOF {
114 t.Fatalf("Recv failed for %q: %v. Results received thus far: %q", tc.name, err, results)
115 }
Robin Thellendc26c32e2014-10-06 17:44:04 -0700116 break
117 }
118 results = append(results, me.Name)
119 }
120 if ferr := call.Finish(&err); ferr != nil {
Robin Thellend877ac012014-12-10 14:19:28 -0800121 t.Fatalf("call.Finish failed for %q: %v", tc.name, ferr)
Robin Thellendc26c32e2014-10-06 17:44:04 -0700122 }
123 sort.Strings(results)
124 if !reflect.DeepEqual(tc.expected, results) {
Robin Thellend877ac012014-12-10 14:19:28 -0800125 t.Errorf("unexpected results for %q. Got %v, want %v", tc.name, results, tc.expected)
Robin Thellendc26c32e2014-10-06 17:44:04 -0700126 }
127 }
Robin Thellendd24f0842014-09-23 10:27:29 -0700128}
129
130type testObject struct {
131}
132
Todd Wang1fe7cdd2014-11-12 12:51:49 -0800133func (o testObject) Foo(ipc.ServerContext) string {
Robin Thellendd24f0842014-09-23 10:27:29 -0700134 return "BAR"
135}