-
Notifications
You must be signed in to change notification settings - Fork 2.5k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add tests for anonymizer/app/query. (#5638)
<!-- !! Please DELETE this comment before posting. We appreciate your contribution to the Jaeger project! ππ --> ## Which problem is this PR solving? - Part of #5068 ## Description of the changes - This commit adds tests for the cmd/anonymizer/app/query package. ## How was this change tested? - `go test ./cmd/anonymizer/app/query -v -cover` (85.7%) ## Checklist - [x] I have read https://github.com/jaegertracing/jaeger/blob/master/CONTRIBUTING_GUIDELINES.md - [x] I have signed all commits - [x] I have added unit tests for the new functionality - [x] I have run lint and test steps successfully - for `jaeger`: `make lint test` - for `jaeger-ui`: `yarn lint` and `yarn test` --------- Signed-off-by: Pratham <prathms007@gmail.com> Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
- Loading branch information
1 parent
d8185ac
commit 60c2efb
Showing
5 changed files
with
148 additions
and
2 deletions.
There are no files selected for viewing
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
// Copyright (c) 2024 The Jaeger Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package query | ||
|
||
import ( | ||
"testing" | ||
|
||
"github.com/jaegertracing/jaeger/pkg/testutils" | ||
) | ||
|
||
func TestMain(m *testing.M) { | ||
testutils.VerifyGoLeaks(m) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,124 @@ | ||
// Copyright (c) 2024 The Jaeger Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package query | ||
|
||
import ( | ||
"net" | ||
"testing" | ||
|
||
"github.com/stretchr/testify/assert" | ||
"github.com/stretchr/testify/mock" | ||
"github.com/stretchr/testify/require" | ||
"google.golang.org/grpc" | ||
|
||
"github.com/jaegertracing/jaeger/cmd/query/app" | ||
"github.com/jaegertracing/jaeger/cmd/query/app/querysvc" | ||
"github.com/jaegertracing/jaeger/model" | ||
"github.com/jaegertracing/jaeger/plugin/metrics/disabled" | ||
"github.com/jaegertracing/jaeger/proto-gen/api_v2" | ||
dependencyStoreMocks "github.com/jaegertracing/jaeger/storage/dependencystore/mocks" | ||
"github.com/jaegertracing/jaeger/storage/spanstore" | ||
spanstoremocks "github.com/jaegertracing/jaeger/storage/spanstore/mocks" | ||
) | ||
|
||
var ( | ||
matchContext = mock.AnythingOfType("*context.valueCtx") | ||
matchTraceID = mock.AnythingOfType("model.TraceID") | ||
|
||
mockInvalidTraceID = "xyz" | ||
mockTraceID = model.NewTraceID(0, 123456) | ||
|
||
mockTraceGRPC = &model.Trace{ | ||
Spans: []*model.Span{ | ||
{ | ||
TraceID: mockTraceID, | ||
SpanID: model.NewSpanID(1), | ||
Process: &model.Process{}, | ||
}, | ||
{ | ||
TraceID: mockTraceID, | ||
SpanID: model.NewSpanID(2), | ||
Process: &model.Process{}, | ||
}, | ||
}, | ||
Warnings: []string{}, | ||
} | ||
) | ||
|
||
type testServer struct { | ||
address net.Addr | ||
server *grpc.Server | ||
spanReader *spanstoremocks.Reader | ||
} | ||
|
||
func newTestServer(t *testing.T) *testServer { | ||
spanReader := &spanstoremocks.Reader{} | ||
metricsReader, err := disabled.NewMetricsReader() | ||
require.NoError(t, err) | ||
|
||
q := querysvc.NewQueryService( | ||
spanReader, | ||
&dependencyStoreMocks.Reader{}, | ||
querysvc.QueryServiceOptions{}, | ||
) | ||
h := app.NewGRPCHandler(q, metricsReader, app.GRPCHandlerOptions{}) | ||
|
||
server := grpc.NewServer() | ||
api_v2.RegisterQueryServiceServer(server, h) | ||
|
||
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 &testServer{ | ||
server: server, | ||
address: lis.Addr(), | ||
spanReader: spanReader, | ||
} | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
server := newTestServer(t) | ||
|
||
query, err := New(server.address.String()) | ||
require.NoError(t, err) | ||
defer query.Close() | ||
|
||
assert.NotNil(t, query) | ||
} | ||
|
||
func TestQueryTrace(t *testing.T) { | ||
s := newTestServer(t) | ||
q, err := New(s.address.String()) | ||
require.NoError(t, err) | ||
defer q.Close() | ||
|
||
t.Run("No error", func(t *testing.T) { | ||
s.spanReader.On("GetTrace", matchContext, matchTraceID).Return( | ||
mockTraceGRPC, nil).Once() | ||
|
||
spans, err := q.QueryTrace(mockTraceID.String()) | ||
require.NoError(t, err) | ||
assert.Equal(t, len(spans), len(mockTraceGRPC.Spans)) | ||
}) | ||
|
||
t.Run("Invalid TraceID", func(t *testing.T) { | ||
_, err := q.QueryTrace(mockInvalidTraceID) | ||
assert.ErrorContains(t, err, "failed to convert the provided trace id") | ||
}) | ||
|
||
t.Run("Trace not found", func(t *testing.T) { | ||
s.spanReader.On("GetTrace", matchContext, matchTraceID).Return( | ||
nil, spanstore.ErrTraceNotFound).Once() | ||
|
||
spans, err := q.QueryTrace(mockTraceID.String()) | ||
assert.Nil(t, spans) | ||
assert.ErrorIs(t, err, spanstore.ErrTraceNotFound) | ||
}) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters