Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add unit tests for anonymizer/app/writer #5162

Merged
merged 10 commits into from
Feb 9, 2024
1 change: 0 additions & 1 deletion cmd/anonymizer/app/writer/.nocover

This file was deleted.

124 changes: 124 additions & 0 deletions cmd/anonymizer/app/writer/writer_test.go
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 writer

import (
"os"
"os/exec"
"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()

config := Config{
MaxSpansCount: 10,
CapturedFile: tempDir + "/captured.json",
AnonymizedFile: tempDir + "/anonymized.json",
MappingFile: tempDir + "/mapping.json",
}
_, err := New(config, nopLogger)
require.NoError(t, err)
h4shk4t marked this conversation as resolved.
Show resolved Hide resolved

config = Config{
CapturedFile: tempDir + "/nonexistent_directory/captured.json",
AnonymizedFile: tempDir + "/anonymized.json",
MappingFile: tempDir + "/mapping.json",
}
_, err = New(config, nopLogger)
require.Error(t, err)
h4shk4t marked this conversation as resolved.
Show resolved Hide resolved

config = Config{
CapturedFile: tempDir + "/captured.json",
AnonymizedFile: tempDir + "/nonexistent_directory/anonymized.json",
MappingFile: tempDir + "/mapping.json",
}
_, err = New(config, nopLogger)
require.Error(t, err)
}

func TestWriter_WriteSpan(t *testing.T) {
nopLogger := zap.NewNop()
tempDir := t.TempDir()

config := Config{
MaxSpansCount: 101,
CapturedFile: tempDir + "/captured.json",
AnonymizedFile: tempDir + "/anonymized.json",
MappingFile: tempDir + "/mapping.json",
}

writer, err := New(config, nopLogger)
require.NoError(t, err)
h4shk4t marked this conversation as resolved.
Show resolved Hide resolved

for i := 0; i <= 99; i++ {
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
err = writer.WriteSpan(span)
require.NoError(t, err)
}
}

func TestWriter_WriteSpan_Exits(t *testing.T) {
if os.Getenv("BE_SUBPROCESS") == "1" {
h4shk4t marked this conversation as resolved.
Show resolved Hide resolved
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)
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved

err = writer.WriteSpan(span)
require.NoError(t, err)

require.Error(t, writer.WriteSpan(span))
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved

return
}

cmd := exec.Command(os.Args[0], "-test.run=TestWriter_WriteSpan_Exits")
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
cmd.Env = append(os.Environ(), "BE_SUBPROCESS=1")
err := cmd.Run()
// The process should have exited with status 1, but exec.Command returns
yurishkuro marked this conversation as resolved.
Show resolved Hide resolved
// an *ExitError when the process exits with a non-zero status.
require.NoError(t, err)
}
Loading