Skip to content

Commit f28e33f

Browse files
authored
Merge pull request #138 from hashicorp/reflection
Register grpc reflection service
2 parents 9a3b870 + f8f6b75 commit f28e33f

File tree

4 files changed

+55
-1
lines changed

4 files changed

+55
-1
lines changed

go.mod

+1
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ require (
66
github.com/golang/protobuf v1.3.4
77
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd
88
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb
9+
github.com/jhump/protoreflect v1.6.0
910
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77
1011
github.com/oklog/run v1.0.0
1112
github.com/stretchr/testify v1.3.0 // indirect

go.sum

+6
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfU
1111
github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A=
1212
github.com/golang/protobuf v1.2.0 h1:P3YflyNX/ehuJFLhxviNdFxQPkGK5cDcApsge1SqnvM=
1313
github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
14+
github.com/golang/protobuf v1.3.1/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1415
github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U=
1516
github.com/golang/protobuf v1.3.4 h1:87PNWwrRvUSnqS4dlcBU/ftvOIBep4sYuBLlh6rX2wk=
1617
github.com/golang/protobuf v1.3.4/go.mod h1:vzj43D7+SQXF/4pzW/hwtAqwc6iTitCiVSaWz5lYuqw=
@@ -20,6 +21,8 @@ github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd h1:rNuUHR+CvK1I
2021
github.com/hashicorp/go-hclog v0.0.0-20180709165350-ff2cf002a8dd/go.mod h1:9bjs9uLqI8l75knNv3lV1kA55veR+WUPSiKIWcQHudI=
2122
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb h1:b5rjCoWHc7eqmAS4/qyk21ZsHyb6Mxv/jykxvNTkU4M=
2223
github.com/hashicorp/yamux v0.0.0-20180604194846-3520598351bb/go.mod h1:+NfK9FKeTrX5uv1uIXGdwYDTeHna2qgaIlx54MXqjAM=
24+
github.com/jhump/protoreflect v1.6.0 h1:h5jfMVslIg6l29nsMs0D8Wj17RDVdNYti0vDN/PZZoE=
25+
github.com/jhump/protoreflect v1.6.0/go.mod h1:eaTn3RZAmMBcV0fifFvlm6VHNz3wSkYyXYWUh7ymB74=
2326
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77 h1:7GoSOOW2jpsfkntVKaS2rAr1TJqfcxotyaUcuxoZSzg=
2427
github.com/mitchellh/go-testing-interface v0.0.0-20171004221916-a61a99592b77/go.mod h1:kRemZodwjscx+RGhAo8eIhFbs2+BFgRtFPeD/KE+zxI=
2528
github.com/oklog/run v1.0.0 h1:Ru7dDtJNOyC66gQ5dQmaCa0qIsAUFY3sFpK1Xk8igrw=
@@ -35,6 +38,7 @@ golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL
3538
golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE=
3639
golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU=
3740
golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc=
41+
golang.org/x/net v0.0.0-20180530234432-1e491301e022/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
3842
golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
3943
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d h1:g9qWBGx4puODJTMVyoPrpoxPFgVGd+z1DZwjfRu4d0I=
4044
golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
@@ -56,10 +60,12 @@ golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3
5660
golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q=
5761
google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM=
5862
google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4=
63+
google.golang.org/genproto v0.0.0-20170818010345-ee236bd376b0/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
5964
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8 h1:Nw54tB0rB7hY/N0NQvRW8DG4Yk3Q6T9cu9RcFQDu1tc=
6065
google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc=
6166
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE=
6267
google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc=
68+
google.golang.org/grpc v1.8.0/go.mod h1:yo6s7OP7yaDglbqo1J04qKzAhqBH6lvTonzMVmEdcZw=
6369
google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c=
6470
google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg=
6571
google.golang.org/grpc v1.27.1 h1:zvIju4sqAGvwKspUQOhwnpcqSbzi7/H6QomNNjTL4sk=

grpc_client_test.go

+44-1
Original file line numberDiff line numberDiff line change
@@ -5,8 +5,10 @@ import (
55
"reflect"
66
"testing"
77

8-
"github.com/hashicorp/go-plugin/test/grpc"
8+
grpctest "github.com/hashicorp/go-plugin/test/grpc"
9+
"github.com/jhump/protoreflect/grpcreflect"
910
"google.golang.org/grpc"
11+
reflectpb "google.golang.org/grpc/reflection/grpc_reflection_v1alpha"
1012
)
1113

1214
func TestGRPCClient_App(t *testing.T) {
@@ -104,3 +106,44 @@ func TestGRPCClient_Ping(t *testing.T) {
104106
t.Fatal("should error")
105107
}
106108
}
109+
110+
func TestGRPCClient_Reflection(t *testing.T) {
111+
ctx := context.Background()
112+
113+
client, server := TestPluginGRPCConn(t, map[string]Plugin{
114+
"test": new(testGRPCInterfacePlugin),
115+
})
116+
defer client.Close()
117+
defer server.Stop()
118+
119+
refClient := grpcreflect.NewClient(ctx, reflectpb.NewServerReflectionClient(client.Conn))
120+
121+
svcs, err := refClient.ListServices()
122+
if err != nil {
123+
t.Fatalf("err: %s", err)
124+
}
125+
126+
// TODO: maybe only assert some specific services here to make test more resilient
127+
expectedSvcs := []string{"grpc.health.v1.Health", "grpc.reflection.v1alpha.ServerReflection", "grpctest.Test", "plugin.GRPCBroker", "plugin.GRPCController", "plugin.GRPCStdio"}
128+
129+
if !reflect.DeepEqual(svcs, expectedSvcs) {
130+
t.Fatalf("expected: %v\ngot: %v", expectedSvcs, svcs)
131+
}
132+
133+
healthDesc, err := refClient.ResolveService("grpc.health.v1.Health")
134+
if err != nil {
135+
t.Fatalf("err: %s", err)
136+
}
137+
138+
methods := healthDesc.GetMethods()
139+
var methodNames []string
140+
for _, m := range methods {
141+
methodNames = append(methodNames, m.GetName())
142+
}
143+
144+
expectedMethodNames := []string{"Check", "Watch"}
145+
146+
if !reflect.DeepEqual(methodNames, expectedMethodNames) {
147+
t.Fatalf("expected: %v\ngot: %v", expectedMethodNames, methodNames)
148+
}
149+
}

grpc_server.go

+4
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ import (
1414
"google.golang.org/grpc/credentials"
1515
"google.golang.org/grpc/health"
1616
"google.golang.org/grpc/health/grpc_health_v1"
17+
"google.golang.org/grpc/reflection"
1718
)
1819

1920
// GRPCServiceName is the name of the service that the health check should
@@ -74,6 +75,9 @@ func (s *GRPCServer) Init() error {
7475
GRPCServiceName, grpc_health_v1.HealthCheckResponse_SERVING)
7576
grpc_health_v1.RegisterHealthServer(s.server, healthCheck)
7677

78+
// Register the reflection service
79+
reflection.Register(s.server)
80+
7781
// Register the broker service
7882
brokerServer := newGRPCBrokerServer()
7983
plugin.RegisterGRPCBrokerServer(s.server, brokerServer)

0 commit comments

Comments
 (0)