blob: 1c5782ca933f238cdf20964ec9a68b57f49d61e9 [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
Jiri Simsa764efb72014-12-25 20:57:03 -08009 "v.io/core/veyron2/ipc"
10 "v.io/core/veyron2/naming"
11 "v.io/core/veyron2/options"
12 "v.io/core/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070013
Jiri Simsa764efb72014-12-25 20:57:03 -080014 "v.io/core/veyron/lib/stats"
15 tsecurity "v.io/core/veyron/lib/testutil/security"
16 "v.io/core/veyron/runtimes/google/ipc/stream/manager"
17 "v.io/core/veyron/runtimes/google/ipc/stream/vc"
18 tnaming "v.io/core/veyron/runtimes/google/testing/mocks/naming"
Jiri Simsa764efb72014-12-25 20:57:03 -080019 "v.io/core/veyron/services/mgmt/debug"
Robin Thellendd24f0842014-09-23 10:27:29 -070020)
21
22func TestDebugServer(t *testing.T) {
Asim Shankar8f05c222014-10-06 22:08:19 -070023 // Setup the client and server principals, with the client willing to share its
24 // blessing with the server.
25 var (
Ankure49a86a2014-11-11 18:52:43 -080026 pclient = tsecurity.NewPrincipal("client")
27 pserver = tsecurity.NewPrincipal("server")
Asim Shankar8f05c222014-10-06 22:08:19 -070028 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 Rosencrantz5f98d942015-01-08 13:48:30 -080033 debugDisp := debug.NewDispatcher(vlog.Log.LogDir(), nil)
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070034
Robin Thellendd24f0842014-09-23 10:27:29 -070035 sm := manager.InternalNew(naming.FixedRoutingID(0x555555555))
36 defer sm.Shutdown()
37 ns := tnaming.NewSimpleNamespace()
Matt Rosencrantz3e76f282014-11-10 09:38:57 -080038 server, err := InternalNewServer(testContext(), sm, ns, nil, options.ReservedNameDispatcher{debugDisp}, vc.LocalPrincipal{pserver})
Robin Thellendd24f0842014-09-23 10:27:29 -070039 if err != nil {
40 t.Fatalf("InternalNewServer failed: %v", err)
41 }
42 defer server.Stop()
Cosmos Nicolaou92dba582014-11-05 17:24:10 -080043 server.Serve("", &testObject{}, nil)
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080044 eps, err := server.Listen(listenSpec)
Robin Thellendd24f0842014-09-23 10:27:29 -070045 if err != nil {
46 t.Fatalf("server.Listen failed: %v", err)
47 }
Asim Shankar8f05c222014-10-06 22:08:19 -070048 client, err := InternalNewClient(sm, ns, vc.LocalPrincipal{pclient})
Robin Thellendd24f0842014-09-23 10:27:29 -070049 if err != nil {
50 t.Fatalf("InternalNewClient failed: %v", err)
51 }
52 defer client.Close()
53 ctx := testContext()
Cosmos Nicolaou28dabfc2014-12-15 22:51:07 -080054 ep := eps[0]
Robin Thellendd24f0842014-09-23 10:27:29 -070055 // Call the Foo method on ""
56 {
David Why Use Two When One Will Do Presotto3da1c792014-10-03 11:15:53 -070057 addr := naming.JoinAddressName(ep.String(), "")
Robin Thellendd24f0842014-09-23 10:27:29 -070058 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 Thellendd24f0842014-09-23 10:27:29 -070070 // 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 Presotto3da1c792014-10-03 11:15:53 -070074 addr := naming.JoinAddressName(ep.String(), "__debug/stats/testing/foo")
Suharsh Sivakumarb59a96d2015-01-09 16:39:54 -080075 call, err := client.StartCall(ctx, addr, "Value", nil, options.NoResolve{})
Robin Thellendd24f0842014-09-23 10:27:29 -070076 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 Thellendc26c32e2014-10-06 17:44:04 -070090
91 // Call Glob
92 testcases := []struct {
93 name, pattern string
94 expected []string
95 }{
96 {"", "*", []string{}},
97 {"", "__*", []string{"__debug"}},
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080098 {"", "__*/*", []string{"__debug/logs", "__debug/pprof", "__debug/stats", "__debug/vtrace"}},
99 {"__debug", "*", []string{"logs", "pprof", "stats", "vtrace"}},
Robin Thellendc26c32e2014-10-06 17:44:04 -0700100 }
101 for _, tc := range testcases {
David Why Use Two When One Will Do Presottoadf0ca12014-11-13 10:49:01 -0800102 addr := naming.JoinAddressName(ep.String(), tc.name)
Suharsh Sivakumarb59a96d2015-01-09 16:39:54 -0800103 call, err := client.StartCall(ctx, addr, ipc.GlobMethod, []interface{}{tc.pattern}, options.NoResolve{})
Robin Thellendc26c32e2014-10-06 17:44:04 -0700104 if err != nil {
Robin Thellend877ac012014-12-10 14:19:28 -0800105 t.Fatalf("client.StartCall failed for %q: %v", tc.name, err)
Robin Thellendc26c32e2014-10-06 17:44:04 -0700106 }
107 results := []string{}
108 for {
Todd Wang1aa57692014-11-11 13:53:29 -0800109 var me naming.VDLMountEntry
Robin Thellendc26c32e2014-10-06 17:44:04 -0700110 if err := call.Recv(&me); err != nil {
Bogdan Caprita1ec40522014-12-10 14:56:33 -0800111 if err != io.EOF {
112 t.Fatalf("Recv failed for %q: %v. Results received thus far: %q", tc.name, err, results)
113 }
Robin Thellendc26c32e2014-10-06 17:44:04 -0700114 break
115 }
116 results = append(results, me.Name)
117 }
118 if ferr := call.Finish(&err); ferr != nil {
Robin Thellend877ac012014-12-10 14:19:28 -0800119 t.Fatalf("call.Finish failed for %q: %v", tc.name, ferr)
Robin Thellendc26c32e2014-10-06 17:44:04 -0700120 }
121 sort.Strings(results)
122 if !reflect.DeepEqual(tc.expected, results) {
Robin Thellend877ac012014-12-10 14:19:28 -0800123 t.Errorf("unexpected results for %q. Got %v, want %v", tc.name, results, tc.expected)
Robin Thellendc26c32e2014-10-06 17:44:04 -0700124 }
125 }
Robin Thellendd24f0842014-09-23 10:27:29 -0700126}
127
128type testObject struct {
129}
130
Todd Wang1fe7cdd2014-11-12 12:51:49 -0800131func (o testObject) Foo(ipc.ServerContext) string {
Robin Thellendd24f0842014-09-23 10:27:29 -0700132 return "BAR"
133}