From e7a021a82169de50f1c1c48733465c6bb40ef936 Mon Sep 17 00:00:00 2001 From: Yuri Shkuro Date: Wed, 27 Dec 2023 01:30:40 -0500 Subject: [PATCH] Clean-up APIv3 tests Signed-off-by: Yuri Shkuro --- cmd/query/app/apiv3/grpc_gateway.go | 2 +- cmd/query/app/apiv3/grpc_gateway_test.go | 115 +++++++----- cmd/query/app/apiv3/grpc_handler_test.go | 229 ++++++++--------------- cmd/query/app/apiv3/package_test.go | 16 ++ cmd/query/app/server.go | 2 +- 5 files changed, 173 insertions(+), 191 deletions(-) create mode 100644 cmd/query/app/apiv3/package_test.go diff --git a/cmd/query/app/apiv3/grpc_gateway.go b/cmd/query/app/apiv3/grpc_gateway.go index 2d396cd6381..a300252b2ad 100644 --- a/cmd/query/app/apiv3/grpc_gateway.go +++ b/cmd/query/app/apiv3/grpc_gateway.go @@ -32,7 +32,7 @@ import ( ) // RegisterGRPCGateway registers api_v3 endpoints into provided mux. -func RegisterGRPCGateway(ctx context.Context, logger *zap.Logger, r *mux.Router, basePath string, grpcEndpoint string, grpcTLS tlscfg.Options, tm *tenancy.Manager) error { +func RegisterGRPCGateway(ctx context.Context, logger *zap.Logger, r *mux.Router, basePath string, grpcEndpoint string, grpcTLS *tlscfg.Options, tm *tenancy.Manager) error { grpcEndpoint = netutils.FixLocalhost([]string{grpcEndpoint})[0] jsonpb := &runtime.JSONPb{} diff --git a/cmd/query/app/apiv3/grpc_gateway_test.go b/cmd/query/app/apiv3/grpc_gateway_test.go index 384aa825188..e39c25e81be 100644 --- a/cmd/query/app/apiv3/grpc_gateway_test.go +++ b/cmd/query/app/apiv3/grpc_gateway_test.go @@ -15,10 +15,10 @@ package apiv3 import ( - "bytes" "context" "encoding/json" "fmt" + "io" "net" "net/http" "strings" @@ -27,7 +27,6 @@ import ( "github.com/gorilla/mux" "github.com/grpc-ecosystem/grpc-gateway/runtime" "github.com/stretchr/testify/assert" - "github.com/stretchr/testify/mock" "github.com/stretchr/testify/require" "go.uber.org/zap" "google.golang.org/grpc" @@ -45,23 +44,32 @@ import ( var testCertKeyLocation = "../../../../pkg/config/tlscfg/testdata/" -func testGRPCGateway(t *testing.T, basePath string, serverTLS tlscfg.Options, clientTLS tlscfg.Options) { - testGRPCGatewayWithTenancy(t, basePath, serverTLS, clientTLS, - tenancy.Options{ - Enabled: false, - }, - func(*http.Request) {}) +type testGateway struct { + reader *spanstoremocks.Reader + url string } -func setupGRPCGateway(t *testing.T, basePath string, serverTLS tlscfg.Options, clientTLS tlscfg.Options, tenancyOptions tenancy.Options) (*spanstoremocks.Reader, net.Listener, *grpc.Server, context.CancelFunc, *http.Server) { - r := &spanstoremocks.Reader{} +func setupGRPCGateway( + t *testing.T, + basePath string, + serverTLS, clientTLS *tlscfg.Options, + tenancyOptions tenancy.Options, +) *testGateway { + // *spanstoremocks.Reader, net.Listener, *grpc.Server, context.CancelFunc, *http.Server + gw := &testGateway{ + reader: &spanstoremocks.Reader{}, + } - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) + q := querysvc.NewQueryService(gw.reader, + &dependencyStoreMocks.Reader{}, + querysvc.QueryServiceOptions{}, + ) var serverGRPCOpts []grpc.ServerOption if serverTLS.Enabled { config, err := serverTLS.Config(zap.NewNop()) require.NoError(t, err) + t.Cleanup(func() { serverTLS.Close() }) creds := credentials.NewTLS(config) serverGRPCOpts = append(serverGRPCOpts, grpc.Creds(creds)) } @@ -77,17 +85,25 @@ func setupGRPCGateway(t *testing.T, basePath string, serverTLS tlscfg.Options, c QueryService: q, } api_v3.RegisterQueryServiceServer(grpcServer, h) - lis, _ := net.Listen("tcp", ":0") + lis, err := net.Listen("tcp", ":0") + require.NoError(t, err) + go func() { err := grpcServer.Serve(lis) require.NoError(t, err) }() + t.Cleanup(func() { grpcServer.Stop() }) router := &mux.Router{} router = router.PathPrefix(basePath).Subrouter() ctx, cancel := context.WithCancel(context.Background()) - err := RegisterGRPCGateway(ctx, zap.NewNop(), router, basePath, lis.Addr().String(), clientTLS, tenancy.NewManager(&tenancyOptions)) + err = RegisterGRPCGateway( + ctx, zap.NewNop(), router, basePath, + lis.Addr().String(), clientTLS, tenancy.NewManager(&tenancyOptions), + ) require.NoError(t, err) + t.Cleanup(func() { cancel() }) + t.Cleanup(func() { clientTLS.Close() }) httpLis, err := net.Listen("tcp", ":0") require.NoError(t, err) @@ -98,23 +114,39 @@ func setupGRPCGateway(t *testing.T, basePath string, serverTLS tlscfg.Options, c err = httpServer.Serve(httpLis) require.Equal(t, http.ErrServerClosed, err) }() - return r, httpLis, grpcServer, cancel, httpServer + t.Cleanup(func() { httpServer.Shutdown(context.Background()) }) + + gw.url = fmt.Sprintf( + "http://localhost%s%s", + strings.Replace(httpLis.Addr().String(), "[::]", "", 1), + basePath) + return gw } -func testGRPCGatewayWithTenancy(t *testing.T, basePath string, serverTLS tlscfg.Options, clientTLS tlscfg.Options, +func testGRPCGateway( + t *testing.T, basePath string, + serverTLS, clientTLS *tlscfg.Options, +) { + testGRPCGatewayWithTenancy(t, basePath, serverTLS, clientTLS, + tenancy.Options{ + Enabled: false, + }, + func(*http.Request) { /* setupRequest : no changes */ }, + ) +} + +func testGRPCGatewayWithTenancy( + t *testing.T, + basePath string, + serverTLS *tlscfg.Options, + clientTLS *tlscfg.Options, tenancyOptions tenancy.Options, setupRequest func(*http.Request), ) { - defer serverTLS.Close() - defer clientTLS.Close() - - reader, httpLis, grpcServer, cancel, httpServer := setupGRPCGateway(t, basePath, serverTLS, clientTLS, tenancyOptions) - defer grpcServer.Stop() - defer cancel() - defer httpServer.Shutdown(context.Background()) + gw := setupGRPCGateway(t, basePath, serverTLS, clientTLS, tenancyOptions) traceID := model.NewTraceID(150, 160) - reader.On("GetTrace", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("model.TraceID")).Return( + gw.reader.On("GetTrace", matchContext, matchTraceID).Return( &model.Trace{ Spans: []*model.Span{ { @@ -125,28 +157,28 @@ func testGRPCGatewayWithTenancy(t *testing.T, basePath string, serverTLS tlscfg. }, }, nil).Once() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost%s%s/api/v3/traces/123", strings.Replace(httpLis.Addr().String(), "[::]", "", 1), basePath), nil) + req, err := http.NewRequest(http.MethodGet, gw.url+"/api/v3/traces/123", nil) require.NoError(t, err) req.Header.Set("Content-Type", "application/json") setupRequest(req) response, err := http.DefaultClient.Do(req) require.NoError(t, err) - buf := bytes.Buffer{} - _, err = buf.ReadFrom(response.Body) + body, err := io.ReadAll(response.Body) require.NoError(t, err) + require.NoError(t, response.Body.Close()) jsonpb := &runtime.JSONPb{} var envelope envelope - err = json.Unmarshal(buf.Bytes(), &envelope) + err = json.Unmarshal(body, &envelope) require.NoError(t, err) var spansResponse api_v3.SpansResponseChunk err = jsonpb.Unmarshal(envelope.Result, &spansResponse) require.NoError(t, err) assert.Len(t, spansResponse.GetResourceSpans(), 1) - assert.Equal(t, uint64ToTraceID(t, traceID.High, traceID.Low), spansResponse.GetResourceSpans()[0].GetScopeSpans()[0].GetSpans()[0].GetTraceId()) + assert.Equal(t, bytesOfTraceID(t, traceID.High, traceID.Low), spansResponse.GetResourceSpans()[0].GetScopeSpans()[0].GetSpans()[0].GetTraceId()) } -func uint64ToTraceID(t *testing.T, high, low uint64) []byte { +func bytesOfTraceID(t *testing.T, high, low uint64) []byte { traceID := model.NewTraceID(high, low) buf := make([]byte, 16) _, err := traceID.MarshalTo(buf) @@ -155,17 +187,17 @@ func uint64ToTraceID(t *testing.T, high, low uint64) []byte { } func TestGRPCGateway(t *testing.T) { - testGRPCGateway(t, "/", tlscfg.Options{}, tlscfg.Options{}) + testGRPCGateway(t, "/", &tlscfg.Options{}, &tlscfg.Options{}) } -func TestGRPCGateway_TLS_with_base_path(t *testing.T) { - serverTLS := tlscfg.Options{ +func TestGRPCGatewayWithBasePathAndTLS(t *testing.T) { + serverTLS := &tlscfg.Options{ Enabled: true, CAPath: testCertKeyLocation + "/example-CA-cert.pem", CertPath: testCertKeyLocation + "/example-server-cert.pem", KeyPath: testCertKeyLocation + "/example-server-key.pem", } - clientTLS := tlscfg.Options{ + clientTLS := &tlscfg.Options{ Enabled: true, CAPath: testCertKeyLocation + "/example-CA-cert.pem", CertPath: testCertKeyLocation + "/example-client-cert.pem", @@ -180,12 +212,12 @@ type envelope struct { Result json.RawMessage `json:"result"` } -func TestTenancyGRPCGateway(t *testing.T) { +func TestGRPCGatewayWithTenancy(t *testing.T) { tenancyOptions := tenancy.Options{ Enabled: true, } tm := tenancy.NewManager(&tenancyOptions) - testGRPCGatewayWithTenancy(t, "/", tlscfg.Options{}, tlscfg.Options{}, + testGRPCGatewayWithTenancy(t, "/", &tlscfg.Options{}, &tlscfg.Options{}, // Configure the gateway to forward tenancy header from HTTP to GRPC tenancyOptions, // Add a tenancy header on outbound requests @@ -197,15 +229,12 @@ func TestTenancyGRPCGateway(t *testing.T) { func TestTenancyGRPCRejection(t *testing.T) { basePath := "/" tenancyOptions := tenancy.Options{Enabled: true} - reader, httpLis, grpcServer, cancel, httpServer := setupGRPCGateway(t, - basePath, tlscfg.Options{}, tlscfg.Options{}, + gw := setupGRPCGateway(t, + basePath, &tlscfg.Options{}, &tlscfg.Options{}, tenancyOptions) - defer grpcServer.Stop() - defer cancel() - defer httpServer.Shutdown(context.Background()) traceID := model.NewTraceID(150, 160) - reader.On("GetTrace", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("model.TraceID")).Return( + gw.reader.On("GetTrace", matchContext, matchTraceID).Return( &model.Trace{ Spans: []*model.Span{ { @@ -216,12 +245,13 @@ func TestTenancyGRPCRejection(t *testing.T) { }, }, nil).Once() - req, err := http.NewRequest(http.MethodGet, fmt.Sprintf("http://localhost%s%s/api/v3/traces/123", strings.Replace(httpLis.Addr().String(), "[::]", "", 1), basePath), nil) + req, err := http.NewRequest(http.MethodGet, gw.url+"/api/v3/traces/123", nil) require.NoError(t, err) req.Header.Set("Content-Type", "application/json") // We don't set tenant header response, err := http.DefaultClient.Do(req) require.NoError(t, err) + require.NoError(t, response.Body.Close()) require.Equal(t, http.StatusForbidden, response.StatusCode) // Try again with tenant header set @@ -229,6 +259,7 @@ func TestTenancyGRPCRejection(t *testing.T) { req.Header.Set(tm.Header, "acme") response, err = http.DefaultClient.Do(req) require.NoError(t, err) + require.NoError(t, response.Body.Close()) require.Equal(t, http.StatusOK, response.StatusCode) // Skip unmarshal of response; it is enough that it succeeded } diff --git a/cmd/query/app/apiv3/grpc_handler_test.go b/cmd/query/app/apiv3/grpc_handler_test.go index 403258771a6..58fd6617d0c 100644 --- a/cmd/query/app/apiv3/grpc_handler_test.go +++ b/cmd/query/app/apiv3/grpc_handler_test.go @@ -36,22 +36,62 @@ import ( spanstoremocks "github.com/jaegertracing/jaeger/storage/spanstore/mocks" ) +var ( + matchContext = mock.AnythingOfType("*context.valueCtx") + matchTraceID = mock.AnythingOfType("model.TraceID") +) + func newGrpcServer(t *testing.T, handler *Handler) (*grpc.Server, net.Addr) { server := grpc.NewServer() api_v3.RegisterQueryServiceServer(server, handler) - lis, _ := net.Listen("tcp", ":0") + lis, err := net.Listen("tcp", ":0") + require.NoError(t, err) go func() { err := server.Serve(lis) require.NoError(t, err) }() - + t.Cleanup(func() { server.Stop() }) return server, lis.Addr() } +type testServerClient struct { + server *grpc.Server + address net.Addr + reader *spanstoremocks.Reader + client api_v3.QueryServiceClient +} + +func newTestServerClient(t *testing.T) *testServerClient { + tsc := &testServerClient{ + reader: &spanstoremocks.Reader{}, + } + + q := querysvc.NewQueryService( + tsc.reader, + &dependencyStoreMocks.Reader{}, + querysvc.QueryServiceOptions{}, + ) + h := &Handler{ + QueryService: q, + } + tsc.server, tsc.address = newGrpcServer(t, h) + + conn, err := grpc.DialContext( + context.Background(), + tsc.address.String(), + grpc.WithTransportCredentials(insecure.NewCredentials()), + ) + require.NoError(t, err) + t.Cleanup(func() { conn.Close() }) + tsc.client = api_v3.NewQueryServiceClient(conn) + + return tsc +} + func TestGetTrace(t *testing.T) { - r := &spanstoremocks.Reader{} - r.On("GetTrace", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("model.TraceID")).Return( + tsc := newTestServerClient(t) + tsc.reader.On("GetTrace", matchContext, matchTraceID).Return( &model.Trace{ Spans: []*model.Span{ { @@ -60,20 +100,11 @@ func TestGetTrace(t *testing.T) { }, }, nil).Once() - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{ - QueryService: q, - } - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - getTraceStream, err := client.GetTrace(context.Background(), &api_v3.GetTraceRequest{ - TraceId: "156", - }) + getTraceStream, err := tsc.client.GetTrace(context.Background(), + &api_v3.GetTraceRequest{ + TraceId: "156", + }, + ) require.NoError(t, err) spansChunk, err := getTraceStream.Recv() require.NoError(t, err) @@ -81,23 +112,12 @@ func TestGetTrace(t *testing.T) { assert.Equal(t, "foobar", spansChunk.GetResourceSpans()[0].GetScopeSpans()[0].GetSpans()[0].GetName()) } -func TestGetTrace_storage_error(t *testing.T) { - r := &spanstoremocks.Reader{} - r.On("GetTrace", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("model.TraceID")).Return( +func TestGetTraceStorageError(t *testing.T) { + tsc := newTestServerClient(t) + tsc.reader.On("GetTrace", matchContext, matchTraceID).Return( nil, fmt.Errorf("storage_error")).Once() - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{ - QueryService: q, - } - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - getTraceStream, err := client.GetTrace(context.Background(), &api_v3.GetTraceRequest{ + getTraceStream, err := tsc.client.GetTrace(context.Background(), &api_v3.GetTraceRequest{ TraceId: "156", }) require.NoError(t, err) @@ -107,25 +127,14 @@ func TestGetTrace_storage_error(t *testing.T) { assert.Nil(t, spansChunk) } -func TestGetTrace_traceID_error(t *testing.T) { - r := &spanstoremocks.Reader{} - r.On("GetTrace", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("model.TraceID")).Return( +func TestGetTraceTraceIDError(t *testing.T) { + tsc := newTestServerClient(t) + tsc.reader.On("GetTrace", matchContext, matchTraceID).Return( &model.Trace{ Spans: []*model.Span{}, }, nil).Once() - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{ - QueryService: q, - } - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - getTraceStream, err := client.GetTrace(context.Background(), &api_v3.GetTraceRequest{ + getTraceStream, err := tsc.client.GetTrace(context.Background(), &api_v3.GetTraceRequest{ TraceId: "Z", }) require.NoError(t, err) @@ -136,8 +145,8 @@ func TestGetTrace_traceID_error(t *testing.T) { } func TestFindTraces(t *testing.T) { - r := &spanstoremocks.Reader{} - r.On("FindTraces", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("*spanstore.TraceQueryParameters")).Return( + tsc := newTestServerClient(t) + tsc.reader.On("FindTraces", matchContext, mock.AnythingOfType("*spanstore.TraceQueryParameters")).Return( []*model.Trace{ { Spans: []*model.Span{ @@ -148,18 +157,7 @@ func TestFindTraces(t *testing.T) { }, }, nil).Once() - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{ - QueryService: q, - } - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - responseStream, err := client.FindTraces(context.Background(), &api_v3.FindTracesRequest{ + responseStream, err := tsc.client.FindTraces(context.Background(), &api_v3.FindTracesRequest{ Query: &api_v3.TraceQueryParameters{ ServiceName: "myservice", OperationName: "opname", @@ -176,24 +174,16 @@ func TestFindTraces(t *testing.T) { assert.Len(t, recv.GetResourceSpans(), 1) } -func TestFindTraces_query_nil(t *testing.T) { - q := querysvc.NewQueryService(&spanstoremocks.Reader{}, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{QueryService: q} - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - responseStream, err := client.FindTraces(context.Background(), &api_v3.FindTracesRequest{}) +func TestFindTracesQueryNil(t *testing.T) { + tsc := newTestServerClient(t) + responseStream, err := tsc.client.FindTraces(context.Background(), &api_v3.FindTracesRequest{}) require.NoError(t, err) recv, err := responseStream.Recv() require.Error(t, err) assert.Contains(t, err.Error(), "missing query") assert.Nil(t, recv) - responseStream, err = client.FindTraces(context.Background(), &api_v3.FindTracesRequest{ + responseStream, err = tsc.client.FindTraces(context.Background(), &api_v3.FindTracesRequest{ Query: &api_v3.TraceQueryParameters{ StartTimeMin: nil, StartTimeMax: nil, @@ -206,23 +196,12 @@ func TestFindTraces_query_nil(t *testing.T) { assert.Nil(t, recv) } -func TestFindTraces_storage_error(t *testing.T) { - r := &spanstoremocks.Reader{} - r.On("FindTraces", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("*spanstore.TraceQueryParameters")).Return( +func TestFindTracesStorageError(t *testing.T) { + tsc := newTestServerClient(t) + tsc.reader.On("FindTraces", matchContext, mock.AnythingOfType("*spanstore.TraceQueryParameters")).Return( nil, fmt.Errorf("storage_error"), nil).Once() - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{ - QueryService: q, - } - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - responseStream, err := client.FindTraces(context.Background(), &api_v3.FindTracesRequest{ + responseStream, err := tsc.client.FindTraces(context.Background(), &api_v3.FindTracesRequest{ Query: &api_v3.TraceQueryParameters{ StartTimeMin: &types.Timestamp{}, StartTimeMax: &types.Timestamp{}, @@ -238,69 +217,36 @@ func TestFindTraces_storage_error(t *testing.T) { } func TestGetServices(t *testing.T) { - r := &spanstoremocks.Reader{} - r.On("GetServices", mock.AnythingOfType("*context.valueCtx")).Return( + tsc := newTestServerClient(t) + tsc.reader.On("GetServices", matchContext).Return( []string{"foo"}, nil).Once() - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{ - QueryService: q, - } - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - response, err := client.GetServices(context.Background(), &api_v3.GetServicesRequest{}) + response, err := tsc.client.GetServices(context.Background(), &api_v3.GetServicesRequest{}) require.NoError(t, err) assert.Equal(t, []string{"foo"}, response.GetServices()) } -func TestGetServices_storage_error(t *testing.T) { - r := &spanstoremocks.Reader{} - r.On("GetServices", mock.AnythingOfType("*context.valueCtx")).Return( +func TestGetServicesStorageError(t *testing.T) { + tsc := newTestServerClient(t) + tsc.reader.On("GetServices", matchContext).Return( nil, fmt.Errorf("storage_error")).Once() - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{ - QueryService: q, - } - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - response, err := client.GetServices(context.Background(), &api_v3.GetServicesRequest{}) + response, err := tsc.client.GetServices(context.Background(), &api_v3.GetServicesRequest{}) require.Error(t, err) assert.Contains(t, err.Error(), "storage_error") assert.Nil(t, response) } func TestGetOperations(t *testing.T) { - r := &spanstoremocks.Reader{} - r.On("GetOperations", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("spanstore.OperationQueryParameters")).Return( + tsc := newTestServerClient(t) + tsc.reader.On("GetOperations", matchContext, mock.AnythingOfType("spanstore.OperationQueryParameters")).Return( []spanstore.Operation{ { Name: "get_users", }, }, nil).Once() - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{ - QueryService: q, - } - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - response, err := client.GetOperations(context.Background(), &api_v3.GetOperationsRequest{}) + response, err := tsc.client.GetOperations(context.Background(), &api_v3.GetOperationsRequest{}) require.NoError(t, err) assert.Equal(t, []*api_v3.Operation{ { @@ -309,23 +255,12 @@ func TestGetOperations(t *testing.T) { }, response.GetOperations()) } -func TestGetOperations_storage_error(t *testing.T) { - r := &spanstoremocks.Reader{} - r.On("GetOperations", mock.AnythingOfType("*context.valueCtx"), mock.AnythingOfType("spanstore.OperationQueryParameters")).Return( +func TestGetOperationsStorageError(t *testing.T) { + tsc := newTestServerClient(t) + tsc.reader.On("GetOperations", matchContext, mock.AnythingOfType("spanstore.OperationQueryParameters")).Return( nil, fmt.Errorf("storage_error")).Once() - q := querysvc.NewQueryService(r, &dependencyStoreMocks.Reader{}, querysvc.QueryServiceOptions{}) - h := &Handler{ - QueryService: q, - } - server, addr := newGrpcServer(t, h) - defer server.Stop() - - conn, err := grpc.DialContext(context.Background(), addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) - require.NoError(t, err) - defer conn.Close() - client := api_v3.NewQueryServiceClient(conn) - response, err := client.GetOperations(context.Background(), &api_v3.GetOperationsRequest{}) + response, err := tsc.client.GetOperations(context.Background(), &api_v3.GetOperationsRequest{}) require.Error(t, err) assert.Contains(t, err.Error(), "storage_error") assert.Nil(t, response) diff --git a/cmd/query/app/apiv3/package_test.go b/cmd/query/app/apiv3/package_test.go new file mode 100644 index 00000000000..3326271251b --- /dev/null +++ b/cmd/query/app/apiv3/package_test.go @@ -0,0 +1,16 @@ +// Copyright (c) 2023 The Jaeger Authors. +// SPDX-License-Identifier: Apache-2.0 + +package apiv3 + +import ( + "testing" + + "go.uber.org/goleak" +) + +func TestMain(m *testing.M) { + if true { + goleak.VerifyTestMain(m) + } +} diff --git a/cmd/query/app/server.go b/cmd/query/app/server.go index 42fb886d9e5..662ad4a6834 100644 --- a/cmd/query/app/server.go +++ b/cmd/query/app/server.go @@ -166,7 +166,7 @@ func createHTTPServer(querySvc *querysvc.QueryService, metricsQuerySvc querysvc. } ctx, closeGRPCGateway := context.WithCancel(context.Background()) - if err := apiv3.RegisterGRPCGateway(ctx, logger, r, queryOpts.BasePath, queryOpts.GRPCHostPort, queryOpts.TLSGRPC, tm); err != nil { + if err := apiv3.RegisterGRPCGateway(ctx, logger, r, queryOpts.BasePath, queryOpts.GRPCHostPort, &queryOpts.TLSGRPC, tm); err != nil { closeGRPCGateway() return nil, nil, err }