Skip to content

Commit

Permalink
test(anonymizer): add test for New and SaveMapping
Browse files Browse the repository at this point in the history
Signed-off-by: Eng Zer Jun <engzerjun@gmail.com>
  • Loading branch information
Juneezee committed Oct 2, 2021
1 parent 027b76e commit 1cd47d7
Showing 1 changed file with 59 additions and 0 deletions.
59 changes: 59 additions & 0 deletions cmd/anonymizer/app/anonymizer/anonymizer_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,10 +15,13 @@
package anonymizer

import (
"os"
"path/filepath"
"testing"
"time"

"github.com/stretchr/testify/assert"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/model"
)
Expand Down Expand Up @@ -73,6 +76,62 @@ var span2 = &model.Span{
StartTime: time.Unix(300, 0),
}

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

file, err := os.CreateTemp(tempDir, "mapping.json")
assert.NoError(t, err)

_, err = file.Write([]byte(`
{
"services": {
"api": "hashed_api"
},
"operations": {
"[api]:delete": "hashed_api_delete"
}
}
`))
assert.NoError(t, err)

anonymizer := New(file.Name(), Options{}, nopLogger)
assert.NotNil(t, anonymizer)
}

func TestAnonymizer_SaveMapping(t *testing.T) {
nopLogger := zap.NewNop()
mapping := mapping{
Services: make(map[string]string),
Operations: make(map[string]string),
}

tests := []struct {
name string
mappingFile string
}{
{
name: "fail to write mapping file",
mappingFile: "",
},
{
name: "save mapping file successfully",
mappingFile: filepath.Join(t.TempDir(), "mapping.json"),
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
anonymizer := Anonymizer{
logger: nopLogger,
mapping: mapping,
mappingFile: tt.mappingFile,
}
anonymizer.SaveMapping()
})
}
}

func TestAnonymizer_FilterStandardTags(t *testing.T) {
expected := []model.KeyValue{
model.Bool("error", true),
Expand Down

0 comments on commit 1cd47d7

Please sign in to comment.