Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add unit tests for anonymizer/app/writer (#5162)
## Which problem is this PR solving? - Part of [#5068](#5068) ## Description of the changes - Added unit tests for `cmd/anonymizer/app/writer/` ## How was this change tested? - `go test -cover -v ./cmd/anonymizer/app/writer` - `make lint test` ## 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: Ashutosh Srivastava <ashutosh3002@gmail.com> Co-authored-by: Yuri Shkuro <yurishkuro@users.noreply.github.com>
- Loading branch information
1 parent
bd7c9d6
commit 951943f
Showing
4 changed files
with
131 additions
and
5 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
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,117 @@ | ||
// Copyright (c) 2024 The Jaeger Authors. | ||
// SPDX-License-Identifier: Apache-2.0 | ||
|
||
package writer | ||
|
||
import ( | ||
"testing" | ||
"time" | ||
|
||
"github.com/stretchr/testify/require" | ||
"go.uber.org/zap" | ||
|
||
"github.com/jaegertracing/jaeger/model" | ||
) | ||
|
||
var tags = []model.KeyValue{ | ||
model.Bool("error", true), | ||
model.String("http.method", "POST"), | ||
model.Bool("foobar", true), | ||
} | ||
|
||
var traceID = model.NewTraceID(1, 2) | ||
|
||
var span = &model.Span{ | ||
TraceID: traceID, | ||
SpanID: model.NewSpanID(1), | ||
Process: &model.Process{ | ||
ServiceName: "serviceName", | ||
Tags: tags, | ||
}, | ||
OperationName: "operationName", | ||
Tags: tags, | ||
Logs: []model.Log{ | ||
{ | ||
Timestamp: time.Now(), | ||
Fields: []model.KeyValue{ | ||
model.String("logKey", "logValue"), | ||
}, | ||
}, | ||
}, | ||
Duration: time.Second * 5, | ||
StartTime: time.Unix(300, 0), | ||
} | ||
|
||
func TestNew(t *testing.T) { | ||
nopLogger := zap.NewNop() | ||
tempDir := t.TempDir() | ||
|
||
t.Run("no error", func(t *testing.T) { | ||
config := Config{ | ||
MaxSpansCount: 10, | ||
CapturedFile: tempDir + "/captured.json", | ||
AnonymizedFile: tempDir + "/anonymized.json", | ||
MappingFile: tempDir + "/mapping.json", | ||
} | ||
_, err := New(config, nopLogger) | ||
require.NoError(t, err) | ||
}) | ||
|
||
t.Run("CapturedFile does not exist", func(t *testing.T) { | ||
config := Config{ | ||
CapturedFile: tempDir + "/nonexistent_directory/captured.json", | ||
AnonymizedFile: tempDir + "/anonymized.json", | ||
MappingFile: tempDir + "/mapping.json", | ||
} | ||
_, err := New(config, nopLogger) | ||
require.ErrorContains(t, err, "cannot create output file") | ||
}) | ||
|
||
t.Run("AnonymizedFile does not exist", func(t *testing.T) { | ||
config := Config{ | ||
CapturedFile: tempDir + "/captured.json", | ||
AnonymizedFile: tempDir + "/nonexistent_directory/anonymized.json", | ||
MappingFile: tempDir + "/mapping.json", | ||
} | ||
_, err := New(config, nopLogger) | ||
require.ErrorContains(t, err, "cannot create output file") | ||
}) | ||
} | ||
|
||
func TestWriter_WriteSpan(t *testing.T) { | ||
nopLogger := zap.NewNop() | ||
t.Run("write span", func(t *testing.T) { | ||
tempDir := t.TempDir() | ||
config := Config{ | ||
MaxSpansCount: 10, | ||
CapturedFile: tempDir + "/captured.json", | ||
AnonymizedFile: tempDir + "/anonymized.json", | ||
MappingFile: tempDir + "/mapping.json", | ||
} | ||
|
||
writer, err := New(config, nopLogger) | ||
require.NoError(t, err) | ||
defer writer.Close() | ||
|
||
for i := 0; i < 9; i++ { | ||
err = writer.WriteSpan(span) | ||
require.NoError(t, err) | ||
} | ||
}) | ||
t.Run("write span with MaxSpansCount", func(t *testing.T) { | ||
tempDir := t.TempDir() | ||
config := Config{ | ||
MaxSpansCount: 1, | ||
CapturedFile: tempDir + "/captured.json", | ||
AnonymizedFile: tempDir + "/anonymized.json", | ||
MappingFile: tempDir + "/mapping.json", | ||
} | ||
|
||
writer, err := New(config, zap.NewNop()) | ||
require.NoError(t, err) | ||
defer writer.Close() | ||
|
||
err = writer.WriteSpan(span) | ||
require.ErrorIs(t, err, ErrMaxSpansCountReached) | ||
}) | ||
} |
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