Skip to content

Commit

Permalink
Add option to fail on ErrorReports
Browse files Browse the repository at this point in the history
  • Loading branch information
AndreasBergmeier6176 committed Nov 8, 2023
1 parent 9ea93c5 commit e5d77ac
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 3 deletions.
7 changes: 6 additions & 1 deletion pkg/gcp/test/errorreporting.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package test

import (
"context"
"testing"

"cloud.google.com/go/errorreporting/apiv1beta1/errorreportingpb"
)
Expand All @@ -11,10 +12,14 @@ var (
)

type fakeErrorreportingServer struct {
ReportedEvents []*errorreportingpb.ReportedErrorEvent
tForFailOnEvent *testing.T
ReportedEvents []*errorreportingpb.ReportedErrorEvent
}

func (s *fakeErrorreportingServer) ReportErrorEvent(ctx context.Context, req *errorreportingpb.ReportErrorEventRequest) (*errorreportingpb.ReportErrorEventResponse, error) {
if s.tForFailOnEvent != nil {
s.tForFailOnEvent.Fatal("Unexpected ErrorEvent reported:", req.Event)
}
s.ReportedEvents = append(s.ReportedEvents, req.Event)
return &errorreportingpb.ReportErrorEventResponse{}, nil
}
27 changes: 25 additions & 2 deletions pkg/gcp/test/test.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package test
import (
"context"
"net"
"testing"

"cloud.google.com/go/errorreporting"
"cloud.google.com/go/errorreporting/apiv1beta1/errorreportingpb"
Expand All @@ -28,7 +29,30 @@ type server struct {
fes *fakeErrorreportingServer
}

func MustMakeTestServices(ctx context.Context, project, serviceName string) *server {
type testServicesOption struct {
failOnErrorReports bool
}

func WithFailOnErrorReports() testServicesOption {
return testServicesOption{
failOnErrorReports: true,
}
}

func MustMakeTestServices(t *testing.T, ctx context.Context, project, serviceName string, opts ...testServicesOption) *server {

var fes *fakeErrorreportingServer
for _, opt := range opts {
if opt.failOnErrorReports {
fes = &fakeErrorreportingServer{
tForFailOnEvent: t,
}
}
}

if fes == nil {
fes = &fakeErrorreportingServer{}
}

l, err := net.Listen("tcp", "localhost:0")
if err != nil {
Expand All @@ -42,7 +66,6 @@ func MustMakeTestServices(ctx context.Context, project, serviceName string) *ser
}

grpcServer := grpc.NewServer()
fes := &fakeErrorreportingServer{}
errorreportingpb.RegisterReportErrorsServiceServer(grpcServer, fes)
loggingpb.RegisterLoggingServiceV2Server(grpcServer, loggingServer)
fakeServerAddr := l.Addr().String()
Expand Down

0 comments on commit e5d77ac

Please sign in to comment.