From 37f01f3aab3b978a8fecb428fca4d4c722141229 Mon Sep 17 00:00:00 2001 From: Andrey Smirnov Date: Mon, 18 Nov 2019 16:01:07 +0300 Subject: [PATCH] Rework build to use GitHub Actions, linting updates. --- .github/workflows/go.yml | 38 +++++++++++++++++++++++ .travis.yml | 13 -------- README.md | 6 ++-- go.mod | 10 +++++++ go.sum | 60 +++++++++++++++++++++++++++++++++++++ proxy/codec.go | 6 +++- proxy/examples_test.go | 12 ++++---- proxy/handler.go | 8 +++-- proxy/handler_test.go | 65 ++++++++++++++-------------------------- 9 files changed, 150 insertions(+), 68 deletions(-) create mode 100644 .github/workflows/go.yml delete mode 100644 .travis.yml create mode 100644 go.mod create mode 100644 go.sum diff --git a/.github/workflows/go.yml b/.github/workflows/go.yml new file mode 100644 index 0000000..6e70d1e --- /dev/null +++ b/.github/workflows/go.yml @@ -0,0 +1,38 @@ +name: Go +on: [push] +jobs: + + build: + name: Build + runs-on: ubuntu-latest + steps: + + - name: Set up Go 1.13 + uses: actions/setup-go@v1 + with: + go-version: 1.13 + id: go + + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + + - name: Get dependencies + run: | + go mod download + go mod verify + go list -mod=readonly all >/dev/null + + - name: Test + run: go test -v ./... + + lint: + name: Lint + runs-on: ubuntu-latest + steps: + - name: Check out code into the Go module directory + uses: actions/checkout@v1 + + - name: Run GolangCI-Lint + uses: actions-contrib/golangci-lint@master + with: + args: run diff --git a/.travis.yml b/.travis.yml deleted file mode 100644 index 1b38744..0000000 --- a/.travis.yml +++ /dev/null @@ -1,13 +0,0 @@ -sudo: false -language: go -go: - - 1.7 - - 1.8 - -install: - - go get google.golang.org/grpc - - go get golang.org/x/net/context - - go get github.com/stretchr/testify - -script: - - go test -race -v ./... diff --git a/README.md b/README.md index 06195e4..d8925d8 100644 --- a/README.md +++ b/README.md @@ -1,8 +1,7 @@ # gRPC Proxy -[![Travis Build](https://travis-ci.org/mwitkow/grpc-proxy.svg?branch=master)](https://travis-ci.org/mwitkow/grpc-proxy) -[![Go Report Card](https://goreportcard.com/badge/github.com/mwitkow/grpc-proxy)](https://goreportcard.com/report/github.com/mwitkow/grpc-proxy) -[![GoDoc](http://img.shields.io/badge/GoDoc-Reference-blue.svg)](https://godoc.org/github.com/mwitkow/grpc-proxy) +[![Go Report Card](https://goreportcard.com/badge/github.com/smira/grpc-proxy)](https://goreportcard.com/report/github.com/smira/grpc-proxy) +[![GoDoc](http://img.shields.io/badge/GoDoc-Reference-blue.svg)](https://godoc.org/github.com/smira/grpc-proxy) [![Apache 2.0 License](https://img.shields.io/badge/License-Apache%202.0-blue.svg)](LICENSE) [gRPC Go](https://github.com/grpc/grpc-go) Proxy server @@ -56,4 +55,3 @@ pb_test.RegisterTestServiceServer(server, &testImpl{}) ## License `grpc-proxy` is released under the Apache 2.0 license. See [LICENSE.txt](LICENSE.txt). - diff --git a/go.mod b/go.mod new file mode 100644 index 0000000..b6864e1 --- /dev/null +++ b/go.mod @@ -0,0 +1,10 @@ +module github.com/smira/grpc-proxy + +go 1.13 + +require ( + github.com/golang/protobuf v1.3.2 + github.com/stretchr/testify v1.4.0 + golang.org/x/net v0.0.0-20191116160921-f9c825593386 + google.golang.org/grpc v1.25.1 +) diff --git a/go.sum b/go.sum new file mode 100644 index 0000000..d0cfc61 --- /dev/null +++ b/go.sum @@ -0,0 +1,60 @@ +cloud.google.com/go v0.26.0/go.mod h1:aQUYkXzVsufM+DwF1aE+0xfcU+56JwCaLick0ClmMTw= +github.com/BurntSushi/toml v0.3.1/go.mod h1:xHWCNGjB5oqiDr8zfno3MHue2Ht5sIBksp03qcyfWMU= +github.com/census-instrumentation/opencensus-proto v0.2.1/go.mod h1:f6KPmirojxKA12rnyqOA5BBL4O983OfeGPqjHWSTneU= +github.com/client9/misspell v0.3.4/go.mod h1:qj6jICC3Q7zFZvVWo7KLAzC3yx5G7kyvSDkc90ppPyw= +github.com/davecgh/go-spew v1.1.0 h1:ZDRjVQ15GmhC3fiQ8ni8+OwkZQO4DARzQgrnXU1Liz8= +github.com/davecgh/go-spew v1.1.0/go.mod h1:J7Y8YcW2NihsgmVo/mv3lAwl/skON4iLHjSsI+c5H38= +github.com/envoyproxy/go-control-plane v0.9.0/go.mod h1:YTl/9mNaCwkRvm6d1a2C3ymFceY/DCBVvsKhRF0iEA4= +github.com/envoyproxy/protoc-gen-validate v0.1.0/go.mod h1:iSmxcyjqTsJpI2R4NaDN7+kN2VEUnK/pcBlmesArF7c= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b h1:VKtxabqXZkF25pY9ekfRL6a582T4P37/31XEstQ5p58= +github.com/golang/glog v0.0.0-20160126235308-23def4e6c14b/go.mod h1:SBH7ygxi8pfUlaOkMMuAQtPIUF8ecWP5IEl/CR7VP2Q= +github.com/golang/mock v1.1.1/go.mod h1:oTYuIxOrZwtPieC+H1uAHpcLFnEyAGVDL/k47Jfbm0A= +github.com/golang/protobuf v1.2.0/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/golang/protobuf v1.3.2 h1:6nsPYzhq5kReh6QImI3k5qWzO4PEbvbIW2cwSfR/6xs= +github.com/golang/protobuf v1.3.2/go.mod h1:6lQm79b+lXiMfvg/cZm0SGofjICqVBUtrP5yJMmIC1U= +github.com/google/go-cmp v0.2.0/go.mod h1:oXzfMopK8JAjlY9xF4vHSVASa0yLyX7SntLO5aqRK0M= +github.com/pmezard/go-difflib v1.0.0 h1:4DBwDE0NGyQoBHbLQYPwSUPoCMWR5BEzIk/f1lZbAQM= +github.com/pmezard/go-difflib v1.0.0/go.mod h1:iKH77koFhYxTK1pcRnkKkqfTogsbg7gZNVY4sRDYZ/4= +github.com/prometheus/client_model v0.0.0-20190812154241-14fe0d1b01d4/go.mod h1:xMI15A0UPsDsEKsMN9yxemIoYk6Tm2C1GtYGdfGttqA= +github.com/stretchr/objx v0.1.0/go.mod h1:HFkY916IF+rwdDfMAkV7OtwuqBVzrE8GR6GFx+wExME= +github.com/stretchr/testify v1.4.0 h1:2E4SXV/wtOkTonXsotYi4li6zVWxYlZuYNCXe9XRJyk= +github.com/stretchr/testify v1.4.0/go.mod h1:j7eGeouHqKxXV5pUuKE4zz7dFj8WfuZ+81PSLYec5m4= +golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w= +golang.org/x/exp v0.0.0-20190121172915-509febef88a4/go.mod h1:CJ0aWSM057203Lf6IL+f9T1iT9GByDxfZKAQTCR3kQA= +golang.org/x/lint v0.0.0-20181026193005-c67002cb31c3/go.mod h1:UVdnD1Gm6xHRNCYTkRU2/jEulfH38KcIWyp/GAMgvoE= +golang.org/x/lint v0.0.0-20190227174305-5b3e6a55c961/go.mod h1:wehouNa3lNwaWXcvxsM5YxQ5yQlVC4a0KAMCusXpPoU= +golang.org/x/lint v0.0.0-20190313153728-d0100b6bd8b3/go.mod h1:6SW0HCj/g11FgYtHlgUYUwCkIfeOF89ocIRzGO/8vkc= +golang.org/x/net v0.0.0-20180724234803-3673e40ba225/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20180826012351-8a410e7b638d/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190213061140-3a22650c66bd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4= +golang.org/x/net v0.0.0-20190311183353-d8887717615a/go.mod h1:t9HGtf8HONx5eT2rtn7q6eTqICYqUVnKs3thJo3Qplg= +golang.org/x/net v0.0.0-20191116160921-f9c825593386 h1:ktbWvQrW08Txdxno1PiDpSxPXG6ndGsfnJjRRtkM0LQ= +golang.org/x/net v0.0.0-20191116160921-f9c825593386/go.mod h1:z5CRVTTTmAJ677TzLLGU+0bjPO0LkuOLi4/5GtJWs/s= +golang.org/x/oauth2 v0.0.0-20180821212333-d2e6202438be/go.mod h1:N/0e6XlmueqKjAGxoOufVs8QHGRruUQn6yWY3a++T0U= +golang.org/x/sync v0.0.0-20180314180146-1d60e4601c6f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20181108010431-42b317875d0f/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sync v0.0.0-20190423024810-112230192c58/go.mod h1:RxMgew5VJxzue5/jJTE5uejpjVlOe/izrB70Jof72aM= +golang.org/x/sys v0.0.0-20180830151530-49385e6e1522/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a h1:1BGLXjeY4akVXGgbC9HugT3Jv3hCI0z56oJR5vAMgBU= +golang.org/x/sys v0.0.0-20190215142949-d0b11bdaac8a/go.mod h1:STP8DvDyc/dI5b8T5hshtkjS+E42TnysNCUPdjciGhY= +golang.org/x/text v0.3.0 h1:g61tztE5qeGQ89tm6NTjjM9VPIm088od1l6aSorWRWg= +golang.org/x/text v0.3.0/go.mod h1:NqM8EUOU14njkJ3fqMW+pc6Ldnwhi/IjpwHt7yyuwOQ= +golang.org/x/tools v0.0.0-20190114222345-bf090417da8b/go.mod h1:n7NCudcB/nEzxVGmLbDWY5pfWTLqBcC2KZ6jyYvM4mQ= +golang.org/x/tools v0.0.0-20190226205152-f727befe758c/go.mod h1:9Yl7xja0Znq3iFh3HoIrodX9oNMXvdceNzlUR8zjMvY= +golang.org/x/tools v0.0.0-20190311212946-11955173bddd/go.mod h1:LCzVGOaR6xXOjkQ3onu1FJEFr0SW1gC7cKk1uF8kGRs= +golang.org/x/tools v0.0.0-20190524140312-2c0ae7006135/go.mod h1:RgjU9mgBXZiqYHBnxXauZ1Gv1EHHAz9KjViQ78xBX0Q= +google.golang.org/appengine v1.1.0/go.mod h1:EbEs0AVv82hx2wNQdGPgUI5lhzA/G0D9YwlJXL52JkM= +google.golang.org/appengine v1.4.0/go.mod h1:xpcJRLb0r/rnEns0DIKYYv+WjYCduHsrkT7/EB5XEv4= +google.golang.org/genproto v0.0.0-20180817151627-c66870c02cf8/go.mod h1:JiN7NxoALGmiZfu7CAH4rXhgtRTLTxftemlI0sWmxmc= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55 h1:gSJIx1SDwno+2ElGhA4+qG2zF97qiUzTM+rQ0klBOcE= +google.golang.org/genproto v0.0.0-20190819201941-24fa4b261c55/go.mod h1:DMBHOl98Agz4BDEuKkezgsaosCRResVns1a3J2ZsMNc= +google.golang.org/grpc v1.19.0/go.mod h1:mqu4LbDTu4XGKhr4mRzUsmM4RtVoemTSY81AxZiDr8c= +google.golang.org/grpc v1.23.0/go.mod h1:Y5yQAOtifL1yxbo5wqy6BxZv8vAUGQwXBOALyacEbxg= +google.golang.org/grpc v1.25.1 h1:wdKvqQk7IttEw92GoRyKG2IDrUIpgpj6H6m81yfeMW0= +google.golang.org/grpc v1.25.1/go.mod h1:c3i+UQWmh7LiEpx4sFZnkU36qjEYZ0imhYfXVyQciAY= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405 h1:yhCVgyC4o1eVCa2tZl7eS0r+SDo693bJlVdllGtEeKM= +gopkg.in/check.v1 v0.0.0-20161208181325-20d25e280405/go.mod h1:Co6ibVJAznAaIkqp8huTwlJQCZ016jof/cbN4VW5Yz0= +gopkg.in/yaml.v2 v2.2.2 h1:ZCJp+EgiOT7lHqUV2J862kp8Qj64Jo6az82+3Td9dZw= +gopkg.in/yaml.v2 v2.2.2/go.mod h1:hI93XBmqTisBFMUTm0b8Fm+jr3Dg1NNxqwp+5A1VGuI= +honnef.co/go/tools v0.0.0-20190102054323-c2f93a96b099/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= +honnef.co/go/tools v0.0.0-20190523083050-ea95bdfd59fc/go.mod h1:rf3lG4BRIbNafJWhAfAdb/ePZxsR/4RtNHQocxwk9r4= diff --git a/proxy/codec.go b/proxy/codec.go index 846b9c4..b95c666 100644 --- a/proxy/codec.go +++ b/proxy/codec.go @@ -10,6 +10,8 @@ import ( // Codec returns a proxying grpc.Codec with the default protobuf codec as parent. // // See CodecWithParent. +// +//nolint: staticcheck func Codec() grpc.Codec { return CodecWithParent(&protoCodec{}) } @@ -20,12 +22,14 @@ func Codec() grpc.Codec { // to the schema of the forwarded messages. It basically treats a gRPC message frame as raw bytes. // However, if the server handler, or the client caller are not proxy-internal functions it will fall back // to trying to decode the message using a fallback codec. +// +//nolint: staticcheck func CodecWithParent(fallback grpc.Codec) grpc.Codec { return &rawCodec{fallback} } type rawCodec struct { - parentCodec grpc.Codec + parentCodec grpc.Codec //nolint: staticcheck } type frame struct { diff --git a/proxy/examples_test.go b/proxy/examples_test.go index bef4ce3..f79fd86 100644 --- a/proxy/examples_test.go +++ b/proxy/examples_test.go @@ -6,11 +6,13 @@ package proxy_test import ( "strings" - "github.com/mwitkow/grpc-proxy/proxy" "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/codes" "google.golang.org/grpc/metadata" + "google.golang.org/grpc/status" + + "github.com/smira/grpc-proxy/proxy" ) var ( @@ -38,7 +40,7 @@ func ExampleStreamDirector() { director = func(ctx context.Context, fullMethodName string) (context.Context, *grpc.ClientConn, error) { // Make sure we never forward internal services. if strings.HasPrefix(fullMethodName, "/com.example.internal.") { - return nil, nil, grpc.Errorf(codes.Unimplemented, "Unknown method") + return nil, nil, status.Errorf(codes.Unimplemented, "Unknown method") } md, ok := metadata.FromIncomingContext(ctx) // Copy the inbound metadata explicitly. @@ -48,13 +50,13 @@ func ExampleStreamDirector() { // Decide on which backend to dial if val, exists := md[":authority"]; exists && val[0] == "staging.api.example.com" { // Make sure we use DialContext so the dialing can be cancelled/time out together with the context. - conn, err := grpc.DialContext(ctx, "api-service.staging.svc.local", grpc.WithCodec(proxy.Codec())) + conn, err := grpc.DialContext(ctx, "api-service.staging.svc.local", grpc.WithCodec(proxy.Codec())) // nolint: staticcheck return outCtx, conn, err } else if val, exists := md[":authority"]; exists && val[0] == "api.example.com" { - conn, err := grpc.DialContext(ctx, "api-service.prod.svc.local", grpc.WithCodec(proxy.Codec())) + conn, err := grpc.DialContext(ctx, "api-service.prod.svc.local", grpc.WithCodec(proxy.Codec())) // nolint: staticcheck return outCtx, conn, err } } - return nil, nil, grpc.Errorf(codes.Unimplemented, "Unknown method") + return nil, nil, status.Errorf(codes.Unimplemented, "Unknown method") } } diff --git a/proxy/handler.go b/proxy/handler.go index 752f892..2a88468 100644 --- a/proxy/handler.go +++ b/proxy/handler.go @@ -9,6 +9,7 @@ import ( "golang.org/x/net/context" "google.golang.org/grpc" "google.golang.org/grpc/codes" + "google.golang.org/grpc/status" ) var ( @@ -61,7 +62,7 @@ func (s *handler) handler(srv interface{}, serverStream grpc.ServerStream) error // little bit of gRPC internals never hurt anyone fullMethodName, ok := grpc.MethodFromServerStream(serverStream) if !ok { - return grpc.Errorf(codes.Internal, "lowLevelServerStream not exists in context") + return status.Errorf(codes.Internal, "lowLevelServerStream not exists in context") } // We require that the director's returned context inherits from the serverStream.Context(). outgoingCtx, backendConn, err := s.director(serverStream.Context(), fullMethodName) @@ -87,6 +88,7 @@ func (s *handler) handler(srv interface{}, serverStream grpc.ServerStream) error if s2cErr == io.EOF { // this is the happy case where the sender has encountered io.EOF, and won't be sending anymore./ // the clientStream>serverStream may continue pumping though. + //nolint: errcheck clientStream.CloseSend() break } else { @@ -94,7 +96,7 @@ func (s *handler) handler(srv interface{}, serverStream grpc.ServerStream) error // to cancel the clientStream to the backend, let all of its goroutines be freed up by the CancelFunc and // exit with an error to the stack clientCancel() - return grpc.Errorf(codes.Internal, "failed proxying s2c: %v", s2cErr) + return status.Errorf(codes.Internal, "failed proxying s2c: %v", s2cErr) } case c2sErr := <-c2sErrChan: // This happens when the clientStream has nothing else to offer (io.EOF), returned a gRPC error. In those two @@ -108,7 +110,7 @@ func (s *handler) handler(srv interface{}, serverStream grpc.ServerStream) error return nil } } - return grpc.Errorf(codes.Internal, "gRPC proxying should never reach this stage.") + return status.Errorf(codes.Internal, "gRPC proxying should never reach this stage.") } func (s *handler) forwardClientToServer(src grpc.ClientStream, dst grpc.ServerStream) chan error { diff --git a/proxy/handler_test.go b/proxy/handler_test.go index c811408..b601738 100644 --- a/proxy/handler_test.go +++ b/proxy/handler_test.go @@ -12,7 +12,6 @@ import ( "testing" "time" - "github.com/mwitkow/grpc-proxy/proxy" "github.com/stretchr/testify/assert" "github.com/stretchr/testify/require" "github.com/stretchr/testify/suite" @@ -21,10 +20,12 @@ import ( "google.golang.org/grpc/codes" "google.golang.org/grpc/grpclog" "google.golang.org/grpc/metadata" + "google.golang.org/grpc/status" "fmt" - pb "github.com/mwitkow/grpc-proxy/testservice" + "github.com/smira/grpc-proxy/proxy" + pb "github.com/smira/grpc-proxy/testservice" ) const ( @@ -40,8 +41,7 @@ const ( // asserting service is implemented on the server side and serves as a handler for stuff type assertingService struct { - logger *grpclog.Logger - t *testing.T + t *testing.T } func (s *assertingService) PingEmpty(ctx context.Context, _ *pb.Empty) (*pb.PingResponse, error) { @@ -55,27 +55,27 @@ func (s *assertingService) PingEmpty(ctx context.Context, _ *pb.Empty) (*pb.Ping func (s *assertingService) Ping(ctx context.Context, ping *pb.PingRequest) (*pb.PingResponse, error) { // Send user trailers and headers. - grpc.SendHeader(ctx, metadata.Pairs(serverHeaderMdKey, "I like turtles.")) - grpc.SetTrailer(ctx, metadata.Pairs(serverTrailerMdKey, "I like ending turtles.")) + grpc.SendHeader(ctx, metadata.Pairs(serverHeaderMdKey, "I like turtles.")) //nolint: errcheck + grpc.SetTrailer(ctx, metadata.Pairs(serverTrailerMdKey, "I like ending turtles.")) //nolint: errcheck return &pb.PingResponse{Value: ping.Value, Counter: 42}, nil } func (s *assertingService) PingError(ctx context.Context, ping *pb.PingRequest) (*pb.Empty, error) { - return nil, grpc.Errorf(codes.FailedPrecondition, "Userspace error.") + return nil, status.Errorf(codes.FailedPrecondition, "Userspace error.") } func (s *assertingService) PingList(ping *pb.PingRequest, stream pb.TestService_PingListServer) error { // Send user trailers and headers. - stream.SendHeader(metadata.Pairs(serverHeaderMdKey, "I like turtles.")) + stream.SendHeader(metadata.Pairs(serverHeaderMdKey, "I like turtles.")) //nolint: errcheck for i := 0; i < countListResponses; i++ { - stream.Send(&pb.PingResponse{Value: ping.Value, Counter: int32(i)}) + stream.Send(&pb.PingResponse{Value: ping.Value, Counter: int32(i)}) //nolint: errcheck } - stream.SetTrailer(metadata.Pairs(serverTrailerMdKey, "I like ending turtles.")) + stream.SetTrailer(metadata.Pairs(serverTrailerMdKey, "I like ending turtles.")) //nolint: errcheck return nil } func (s *assertingService) PingStream(stream pb.TestService_PingStreamServer) error { - stream.SendHeader(metadata.Pairs(serverHeaderMdKey, "I like turtles.")) + stream.SendHeader(metadata.Pairs(serverHeaderMdKey, "I like turtles.")) // nolint: errcheck counter := int32(0) for { ping, err := stream.Recv() @@ -142,8 +142,8 @@ func (s *ProxyHappySuite) TestPingCarriesServerHeadersAndTrailers() { func (s *ProxyHappySuite) TestPingErrorPropagatesAppError() { _, err := s.testClient.PingError(s.ctx(), &pb.PingRequest{Value: "foo"}) require.Error(s.T(), err, "PingError should never succeed") - assert.Equal(s.T(), codes.FailedPrecondition, grpc.Code(err)) - assert.Equal(s.T(), "Userspace error.", grpc.ErrorDesc(err)) + assert.Equal(s.T(), codes.FailedPrecondition, status.Code(err)) + assert.Equal(s.T(), "Userspace error.", status.Convert(err).Message()) } func (s *ProxyHappySuite) TestDirectorErrorIsPropagated() { @@ -151,8 +151,8 @@ func (s *ProxyHappySuite) TestDirectorErrorIsPropagated() { ctx := metadata.NewOutgoingContext(s.ctx(), metadata.Pairs(rejectingMdKey, "true")) _, err := s.testClient.Ping(ctx, &pb.PingRequest{Value: "foo"}) require.Error(s.T(), err, "Director should reject this RPC") - assert.Equal(s.T(), codes.PermissionDenied, grpc.Code(err)) - assert.Equal(s.T(), "testing rejection", grpc.ErrorDesc(err)) + assert.Equal(s.T(), codes.PermissionDenied, status.Code(err)) + assert.Equal(s.T(), "testing rejection", status.Convert(err).Message()) } func (s *ProxyHappySuite) TestPingStream_FullDuplexWorks() { @@ -196,19 +196,19 @@ func (s *ProxyHappySuite) SetupSuite() { s.serverListener, err = net.Listen("tcp", "127.0.0.1:0") require.NoError(s.T(), err, "must be able to allocate a port for serverListener") - grpclog.SetLogger(log.New(os.Stderr, "grpc: ", log.LstdFlags)) + grpclog.SetLogger(log.New(os.Stderr, "grpc: ", log.LstdFlags)) // nolint: staticcheck s.server = grpc.NewServer() pb.RegisterTestServiceServer(s.server, &assertingService{t: s.T()}) // Setup of the proxy's Director. - s.serverClientConn, err = grpc.Dial(s.serverListener.Addr().String(), grpc.WithInsecure(), grpc.WithCodec(proxy.Codec())) + s.serverClientConn, err = grpc.Dial(s.serverListener.Addr().String(), grpc.WithInsecure(), grpc.WithCodec(proxy.Codec())) // nolint: staticcheck require.NoError(s.T(), err, "must not error on deferred client Dial") director := func(ctx context.Context, fullName string) (context.Context, *grpc.ClientConn, error) { md, ok := metadata.FromIncomingContext(ctx) if ok { if _, exists := md[rejectingMdKey]; exists { - return ctx, nil, grpc.Errorf(codes.PermissionDenied, "testing rejection") + return ctx, nil, status.Errorf(codes.PermissionDenied, "testing rejection") } } // Explicitly copy the metadata, otherwise the tests will fail. @@ -228,14 +228,16 @@ func (s *ProxyHappySuite) SetupSuite() { // Start the serving loops. s.T().Logf("starting grpc.Server at: %v", s.serverListener.Addr().String()) go func() { - s.server.Serve(s.serverListener) + s.server.Serve(s.serverListener) // nolint: errcheck }() s.T().Logf("starting grpc.Proxy at: %v", s.proxyListener.Addr().String()) go func() { - s.proxy.Serve(s.proxyListener) + s.proxy.Serve(s.proxyListener) // nolint: errcheck }() - clientConn, err := grpc.Dial(strings.Replace(s.proxyListener.Addr().String(), "127.0.0.1", "localhost", 1), grpc.WithInsecure(), grpc.WithTimeout(1*time.Second)) + ctx, ctxCancel := context.WithTimeout(context.Background(), 1*time.Second) + defer ctxCancel() + clientConn, err := grpc.DialContext(ctx, strings.Replace(s.proxyListener.Addr().String(), "127.0.0.1", "localhost", 1), grpc.WithInsecure()) require.NoError(s.T(), err, "must not error on deferred client Dial") s.testClient = pb.NewTestServiceClient(clientConn) } @@ -262,24 +264,3 @@ func (s *ProxyHappySuite) TearDownSuite() { func TestProxyHappySuite(t *testing.T) { suite.Run(t, &ProxyHappySuite{}) } - -// Abstraction that allows us to pass the *testing.T as a grpclogger. -type testingLog struct { - testing.T -} - -func (t *testingLog) Fatalln(args ...interface{}) { - t.T.Fatal(args...) -} - -func (t *testingLog) Print(args ...interface{}) { - t.T.Log(args...) -} - -func (t *testingLog) Printf(format string, args ...interface{}) { - t.T.Logf(format, args...) -} - -func (t *testingLog) Println(args ...interface{}) { - t.T.Log(args...) -}