Skip to content

Commit

Permalink
Merge pull request #1655 from justinsb/capture_grpc
Browse files Browse the repository at this point in the history
tests: capture GRPC traffic also
  • Loading branch information
google-oss-prow[bot] authored Jun 6, 2024
2 parents cb5c68d + 114c7ee commit 040ef44
Show file tree
Hide file tree
Showing 8 changed files with 644 additions and 1 deletion.
42 changes: 42 additions & 0 deletions config/tests/samples/create/harness.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,9 @@ import (
"golang.org/x/oauth2/google"
cloudresourcemanagerv1 "google.golang.org/api/cloudresourcemanager/v1"
"google.golang.org/api/option"
"google.golang.org/grpc"
"google.golang.org/protobuf/encoding/protojson"
"google.golang.org/protobuf/proto"
"gopkg.in/dnaeon/go-vcr.v3/recorder"
"k8s.io/apimachinery/pkg/apis/meta/v1/unstructured"
"k8s.io/apimachinery/pkg/runtime/schema"
Expand Down Expand Up @@ -264,11 +267,14 @@ func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptio
}
}

var mockCloudGRPCClientConnection *grpc.ClientConn
if targetGCP := os.Getenv("E2E_GCP_TARGET"); targetGCP == "mock" {
t.Logf("creating mock gcp")

mockCloud := mockgcp.NewMockRoundTripper(t, h.client, storage.NewInMemoryStorage())

mockCloudGRPCClientConnection = mockCloud.NewGRPCConnection(ctx)

roundTripper := http.RoundTripper(mockCloud)

ctx = context.WithValue(ctx, httpRoundTripperKey, roundTripper)
Expand Down Expand Up @@ -448,6 +454,42 @@ func NewHarnessWithOptions(ctx context.Context, t *testing.T, opts *HarnessOptio
return ret
}
} else {
// Intercept (and log) GRPC requests
grpcUnaryInterceptor := func(ctx context.Context, method string, req, reply any, cc *grpc.ClientConn, invoker grpc.UnaryInvoker, opts ...grpc.CallOption) error {
entry := &test.LogEntry{}

entry.Request.URL = method
entry.Request.Method = "GRPC"

if req != nil {
requestBytes, _ := protojson.Marshal(req.(proto.Message))
entry.Request.Body = string(requestBytes)
}

if mockCloudGRPCClientConnection != nil {
cc = mockCloudGRPCClientConnection
}
err := invoker(ctx, method, req, reply, cc, opts...)

if reply != nil {
replyBytes, _ := protojson.Marshal(reply.(proto.Message))
entry.Response.Body = string(replyBytes)
}

if err != nil {
entry.Response.Status = fmt.Sprintf("error: %v", err)
} else {
entry.Response.Status = "OK"
}

for _, eventSink := range eventSinks {
eventSink.AddHTTPEvent(ctx, entry)
}
return err
}

transport_tpg.GRPCUnaryClientInterceptor = grpcUnaryInterceptor

// Intercept (and log) DCL requests
if len(eventSinks) != 0 {
if kccConfig.HTTPClient == nil {
Expand Down
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -49,6 +49,7 @@ require (
golang.org/x/sync v0.7.0
golang.org/x/time v0.5.0
google.golang.org/api v0.177.0
google.golang.org/grpc v1.63.2
google.golang.org/protobuf v1.34.0
gopkg.in/dnaeon/go-vcr.v3 v3.2.0
gopkg.in/yaml.v2 v2.4.0
Expand Down Expand Up @@ -212,7 +213,6 @@ require (
google.golang.org/genproto v0.0.0-20240401170217-c3f982113cda // indirect
google.golang.org/genproto/googleapis/api v0.0.0-20240429193739-8cf5692501f6 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20240429193739-8cf5692501f6 // indirect
google.golang.org/grpc v1.63.2 // indirect
gopkg.in/inf.v0 v0.9.1 // indirect
gopkg.in/warnings.v0 v0.1.2 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
Expand Down
5 changes: 5 additions & 0 deletions mockgcp/common/operations/operations.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (

pb "google.golang.org/genproto/googleapis/longrunning"
rpcstatus "google.golang.org/genproto/googleapis/rpc/status"
"google.golang.org/grpc"
"google.golang.org/grpc/codes"
"google.golang.org/grpc/status"
"google.golang.org/protobuf/proto"
Expand All @@ -44,6 +45,10 @@ func NewOperationsService(storage storage.Storage) *Operations {
}
}

func (s *Operations) RegisterGRPCServices(grpcServer *grpc.Server) {
pb.RegisterOperationsServer(grpcServer, s)
}

func (s *Operations) NewLRO(ctx context.Context) (*pb.Operation, error) {
now := time.Now()
millis := now.UnixMilli()
Expand Down
13 changes: 13 additions & 0 deletions mockgcp/mock_http_roundtrip.go
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,7 @@ import (

"google.golang.org/grpc"
"google.golang.org/grpc/credentials/insecure"
"k8s.io/klog/v2"
"sigs.k8s.io/controller-runtime/pkg/client"

"github.com/GoogleCloudPlatform/k8s-config-connector/mockgcp/common"
Expand Down Expand Up @@ -187,6 +188,18 @@ func NewMockRoundTripper(t *testing.T, k8sClient client.Client, storage storage.
return mockRoundTripper
}

func (m *mockRoundTripper) NewGRPCConnection(ctx context.Context) *grpc.ClientConn {
endpoint := m.grpcListener.Addr().String()

var opts []grpc.DialOption
opts = append(opts, grpc.WithTransportCredentials(insecure.NewCredentials()))
conn, err := grpc.DialContext(ctx, endpoint, opts...)
if err != nil {
klog.Fatalf("error dialing grpc endpoint %q: %v", endpoint, err)
}
return conn
}

func (m *mockRoundTripper) prefilterRequest(req *http.Request) error {
if req.Body != nil {
var requestBody bytes.Buffer
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
# Copyright 2024 Google LLC
#
# Licensed under the Apache License, Version 2.0 (the "License");
# you may not use this file except in compliance with the License.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.

apiVersion: bigtable.cnrm.cloud.google.com/v1beta1
kind: BigtableTable
metadata:
annotations:
cnrm.cloud.google.com/management-conflict-prevention-policy: none
cnrm.cloud.google.com/project-id: ${projectId}
cnrm.cloud.google.com/state-into-spec: merge
finalizers:
- cnrm.cloud.google.com/finalizer
- cnrm.cloud.google.com/deletion-defender
generation: 3
labels:
cnrm-test: "true"
name: bigtabletable-${uniqueId}
namespace: ${uniqueId}
spec:
columnFamily:
- family: family3
- family: family2
deletionProtection: UNPROTECTED
instanceRef:
name: bigtable-dep-${uniqueId}
resourceID: bigtabletable-${uniqueId}
splitKeys:
- a
status:
conditions:
- lastTransitionTime: "1970-01-01T00:00:00Z"
message: The resource is up to date
reason: UpToDate
status: "True"
type: Ready
observedGeneration: 3
Loading

0 comments on commit 040ef44

Please sign in to comment.