blob: d5bce8a43716cb1fd063ccbc7ece5ee05df869d4 [file] [log] [blame]
Robin Thellendd24f0842014-09-23 10:27:29 -07001package ipc
2
3import (
4 "reflect"
5 "sort"
6 "testing"
7
Robin Thellendd24f0842014-09-23 10:27:29 -07008 "veyron.io/veyron/veyron2/ipc"
9 "veyron.io/veyron/veyron2/naming"
Asim Shankarcc044212014-10-15 23:25:26 -070010 "veyron.io/veyron/veyron2/options"
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070011 "veyron.io/veyron/veyron2/vlog"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070012
13 "veyron.io/veyron/veyron/lib/stats"
Ankure49a86a2014-11-11 18:52:43 -080014 tsecurity "veyron.io/veyron/veyron/lib/testutil/security"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070015 "veyron.io/veyron/veyron/runtimes/google/ipc/stream/manager"
Asim Shankar8f05c222014-10-06 22:08:19 -070016 "veyron.io/veyron/veyron/runtimes/google/ipc/stream/vc"
Cosmos Nicolaoud6c3c9c2014-09-30 15:42:53 -070017 tnaming "veyron.io/veyron/veyron/runtimes/google/testing/mocks/naming"
Matt Rosencrantzb30286b2014-11-10 14:52:17 -080018 "veyron.io/veyron/veyron/runtimes/google/vtrace"
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070019 "veyron.io/veyron/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 Rosencrantzb30286b2014-11-10 14:52:17 -080033 store := vtrace.NewStore(10)
34 debugDisp := debug.NewDispatcher(vlog.Log.LogDir(), nil, store)
Cosmos Nicolaou8246a8b2014-11-01 09:32:36 -070035
Robin Thellendd24f0842014-09-23 10:27:29 -070036 sm := manager.InternalNew(naming.FixedRoutingID(0x555555555))
37 defer sm.Shutdown()
38 ns := tnaming.NewSimpleNamespace()
Matt Rosencrantz3e76f282014-11-10 09:38:57 -080039 server, err := InternalNewServer(testContext(), sm, ns, nil, options.ReservedNameDispatcher{debugDisp}, vc.LocalPrincipal{pserver})
Robin Thellendd24f0842014-09-23 10:27:29 -070040 if err != nil {
41 t.Fatalf("InternalNewServer failed: %v", err)
42 }
43 defer server.Stop()
Cosmos Nicolaou92dba582014-11-05 17:24:10 -080044 server.Serve("", &testObject{}, nil)
Cosmos Nicolaouf8d4c2b2014-10-23 22:36:38 -070045 ep, err := server.Listen(listenSpec)
Robin Thellendd24f0842014-09-23 10:27:29 -070046 if err != nil {
47 t.Fatalf("server.Listen failed: %v", err)
48 }
Asim Shankar8f05c222014-10-06 22:08:19 -070049 client, err := InternalNewClient(sm, ns, vc.LocalPrincipal{pclient})
Robin Thellendd24f0842014-09-23 10:27:29 -070050 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 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")
Asim Shankarcc044212014-10-15 23:25:26 -070075 call, err := client.StartCall(ctx, addr, "Value", nil, options.NoResolve(true))
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)
103 call, err := client.StartCall(ctx, addr, ipc.GlobMethod, []interface{}{tc.pattern}, options.NoResolve(true))
Robin Thellendc26c32e2014-10-06 17:44:04 -0700104 if err != nil {
105 t.Fatalf("client.StartCall failed: %v", err)
106 }
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 {
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 Thellendd24f0842014-09-23 10:27:29 -0700123}
124
125type testObject struct {
126}
127
Todd Wang1fe7cdd2014-11-12 12:51:49 -0800128func (o testObject) Foo(ipc.ServerContext) string {
Robin Thellendd24f0842014-09-23 10:27:29 -0700129 return "BAR"
130}