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.

148 changes: 148 additions & 0 deletions cmd/anonymizer/app/writer/writer_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,148 @@
// 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()

tests := []struct {
name string
config Config
}{
{
name: "successfully create writer",
config: Config{
MaxSpansCount: 10,
CapturedFile: tempDir + "/captured.json",
AnonymizedFile: tempDir + "/anonymized.json",
MappingFile: tempDir + "/mapping.json",
},
},
{
name: "captured.json doesn't exist",
config: Config{
CapturedFile: tempDir + "/nonexistent_directory/captured.json",
h4shk4t marked this conversation as resolved.
Show resolved Hide resolved
AnonymizedFile: tempDir + "/anonymized.json",
MappingFile: tempDir + "/mapping.json",
},
},
{
name: "anonymized.json doesn't exist",
config: Config{
CapturedFile: tempDir + "/captured.json",
AnonymizedFile: tempDir + "/nonexistent_directory/anonymized.json",
MappingFile: tempDir + "/mapping.json",
},
},
}

t.Run(tests[0].name, func(t *testing.T) {
_, err := New(tests[0].config, nopLogger)
require.NoError(t, err)
})
h4shk4t marked this conversation as resolved.
Show resolved Hide resolved
t.Run(tests[1].name, func(t *testing.T) {
_, err := New(tests[1].config, nopLogger)
require.Error(t, err)
})
t.Run(tests[2].name, func(t *testing.T) {
_, err := New(tests[2].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)

err = writer.WriteSpan(span)
if err == nil {
t.Fatal("expected an error but got none")
}
h4shk4t 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.
if err != nil {
t.Fatalf("process ran with err %v, want exit status 1", err)
}
h4shk4t marked this conversation as resolved.
Show resolved Hide resolved
}