Skip to content

Commit

Permalink
Merge pull request #2 from Ashmita152/fix-2556
Browse files Browse the repository at this point in the history
Fix 2556
  • Loading branch information
yurishkuro authored Nov 17, 2020
2 parents c333eee + 784b9d0 commit cea616b
Show file tree
Hide file tree
Showing 9 changed files with 270 additions and 17 deletions.
6 changes: 4 additions & 2 deletions cmd/anonymizer/app/uiconv/extractor.go
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ func NewExtractor(uiFile string, traceID string, reader *Reader, logger *zap.Log
// Run executes the extraction.
func (e *Extractor) Run() error {
e.logger.Info("Parsing captured file for trace", zap.String("trace_id", e.traceID))

var (
spans []uimodel.Span
span *uimodel.Span
Expand All @@ -63,7 +64,7 @@ func (e *Extractor) Run() error {
}
}
if err != io.EOF {
e.logger.Fatal("Failed when scanning the file", zap.Error(err))
return fmt.Errorf("failed when scanning the file: %w", err)
}
trace := uimodel.Trace{
TraceID: uimodel.TraceID(e.traceID),
Expand All @@ -86,7 +87,8 @@ func (e *Extractor) Run() error {
e.uiFile.Write([]byte(`{"data": [`))
e.uiFile.Write(jsonBytes)
e.uiFile.Write([]byte(`]}`))
e.uiFile.Sync()
e.uiFile.Close()
e.logger.Sugar().Infof("Wrote spans to UI file %s", e.uiFile.Name())
os.Exit(0)
return nil
}
118 changes: 118 additions & 0 deletions cmd/anonymizer/app/uiconv/extractor_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,118 @@
// Copyright (c) 2020 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package uiconv

import (
"io/ioutil"
"os"
"testing"

"github.com/go-openapi/swag"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"go.uber.org/zap"

"github.com/jaegertracing/jaeger/model"
)

type UITrace struct {
Data []model.Trace
}

func TestExtractor_TraceSuccess(t *testing.T) {
inputFile := "fixtures/trace_success.json"
outputFile := "fixtures/trace_success_ui_anonymized.json"
defer os.Remove(outputFile)

reader, err := NewReader(
inputFile,
zap.NewNop(),
)
require.NoError(t, err)

extractor, err := NewExtractor(
outputFile,
"2be38093ead7a083",
reader,
zap.NewNop(),
)
require.NoError(t, err)

err = extractor.Run()
require.NoError(t, err)

var trace UITrace
loadJSON(t, outputFile, &trace)

for i := range trace.Data {
for j := range trace.Data[i].Spans {
assert.Equal(t, "span.kind", trace.Data[i].Spans[j].Tags[0].Key)
}
}
}

func TestExtractor_TraceOutputFileError(t *testing.T) {
inputFile := "fixtures/trace_success.json"
outputFile := "fixtures/trace_success_ui_anonymized.json"
defer os.Remove(outputFile)

reader, err := NewReader(
inputFile,
zap.NewNop(),
)
require.NoError(t, err)

err = os.Chmod("fixtures", 0000)
require.NoError(t, err)
defer os.Chmod("fixtures", 0755)

_, err = NewExtractor(
outputFile,
"2be38093ead7a083",
reader,
zap.NewNop(),
)
require.Contains(t, err.Error(), "cannot create output file")
}

func TestExtractor_TraceScanError(t *testing.T) {
inputFile := "fixtures/trace_scan_error.json"
outputFile := "fixtures/trace_scan_error_ui_anonymized.json"
defer os.Remove(outputFile)

reader, err := NewReader(
inputFile,
zap.NewNop(),
)
require.NoError(t, err)

extractor, err := NewExtractor(
outputFile,
"2be38093ead7a083",
reader,
zap.NewNop(),
)
require.NoError(t, err)

err = extractor.Run()
require.Contains(t, err.Error(), "failed when scanning the file")
}

func loadJSON(t *testing.T, fileName string, i interface{}) {
b, err := ioutil.ReadFile(fileName)
require.NoError(t, err)
err = swag.ReadJSON(b, i)
require.NoError(t, err, "Failed to parse json fixture file %s", fileName)
}
Empty file.
2 changes: 2 additions & 0 deletions cmd/anonymizer/app/uiconv/fixtures/trace_invalid_json.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[{"traceID":"2be38093ead7a083","spanID":"7bd66f09ba90ea3d","duration": "invalid"}
]
2 changes: 2 additions & 0 deletions cmd/anonymizer/app/uiconv/fixtures/trace_scan_error.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
[{"traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34","duration":267037},
]
3 changes: 3 additions & 0 deletions cmd/anonymizer/app/uiconv/fixtures/trace_success.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
[{"traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34","flags":1,"operationName":"a071653098f9250d","references":[{"refType":"CHILD_OF","traceID":"2be38093ead7a083","spanID":"492770a15935810f"}],"startTime":1605223981761425,"duration":267037,"tags":[{"key":"span.kind","type":"string","value":"server"}],"logs":[],"process":{"serviceName":"16af988c443cff37","tags":[]},"warnings":null},
{"traceID":"2be38093ead7a083","spanID":"7bd66f09ba90ea3d","flags":1,"operationName":"471418097747d04a","references":[{"refType":"CHILD_OF","traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34"}],"startTime":1605223981965074,"duration":32782,"tags":[{"key":"span.kind","type":"string","value":"client"},{"key":"error","type":"bool","value":"true"}],"logs":[],"process":{"serviceName":"3c220036602f839e","tags":[]},"warnings":null}
]
1 change: 1 addition & 0 deletions cmd/anonymizer/app/uiconv/fixtures/trace_wrong_format.json
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
{"traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34","duration":267037}
80 changes: 80 additions & 0 deletions cmd/anonymizer/app/uiconv/module_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
// Copyright (c) 2020 The Jaeger Authors.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

package uiconv

import (
"os"
"testing"

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

func TestModule_TraceSuccess(t *testing.T) {
inputFile := "fixtures/trace_success.json"
outputFile := "fixtures/trace_success_ui_anonymized.json"
defer os.Remove(outputFile)

config := Config{
CapturedFile: inputFile,
UIFile: outputFile,
TraceID: "2be38093ead7a083",
}
err := Extract(config, zap.NewNop())
require.NoError(t, err)

var trace UITrace
loadJSON(t, outputFile, &trace)

for i := range trace.Data {
for j := range trace.Data[i].Spans {
assert.Equal(t, "span.kind", trace.Data[i].Spans[j].Tags[0].Key)
}
}
}

func TestModule_TraceNonExistent(t *testing.T) {
inputFile := "fixtures/trace_non_existent.json"
outputFile := "fixtures/trace_non_existent_ui_anonymized.json"
defer os.Remove(outputFile)

config := Config{
CapturedFile: inputFile,
UIFile: outputFile,
TraceID: "2be38093ead7a083",
}
err := Extract(config, zap.NewNop())
require.Contains(t, err.Error(), "cannot open captured file")
}

func TestModule_TraceOutputFileError(t *testing.T) {
inputFile := "fixtures/trace_success.json"
outputFile := "fixtures/trace_success_ui_anonymized.json"
defer os.Remove(outputFile)

config := Config{
CapturedFile: inputFile,
UIFile: outputFile,
TraceID: "2be38093ead7a083",
}

err := os.Chmod("fixtures", 0550)
require.NoError(t, err)
defer os.Chmod("fixtures", 0755)

err = Extract(config, zap.NewNop())
require.Contains(t, err.Error(), "cannot create output file")
}
75 changes: 60 additions & 15 deletions cmd/anonymizer/app/uiconv/reader_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,41 +16,86 @@ package uiconv

import (
"io"
"io/ioutil"
"os"
"testing"

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

const spanData = `[{"traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34","flags":1,"operationName":"a071653098f9250d","references":[{"refType":"CHILD_OF","traceID":"2be38093ead7a083","spanID":"492770a15935810f"}],"startTime":1605223981761425,"duration":267037,"tags":[{"key":"span.kind","type":"string","value":"server"}],"logs":[],"process":{"serviceName":"16af988c443cff37","tags":[]},"warnings":null},
{"traceID":"2be38093ead7a083","spanID":"7bd66f09ba90ea3d","flags":1,"operationName":"471418097747d04a","references":[{"refType":"CHILD_OF","traceID":"2be38093ead7a083","spanID":"7606ddfe69932d34"}],"startTime":1605223981965074,"duration":32782,"tags":[{"key":"span.kind","type":"string","value":"client"},{"key":"error","type":"bool","value":"true"}],"logs":[],"process":{"serviceName":"3c220036602f839e","tags":[]},"warnings":null}
]`

func TestReader(t *testing.T) {
f, err := ioutil.TempFile("", "captured-spans.json")
require.NoError(t, err)
defer os.Remove(f.Name())

_, err = f.Write([]byte(spanData))
require.NoError(t, err)

func TestReader_TraceSuccess(t *testing.T) {
inputFile := "fixtures/trace_success.json"
r, err := NewReader(
f.Name(),
inputFile,
zap.NewNop(),
)
require.NoError(t, err)

s1, err := r.NextSpan()
require.NoError(t, err)
assert.Equal(t, "a071653098f9250d", s1.OperationName)
assert.Equal(t, 1, r.spansRead)
assert.Equal(t, false, r.eofReached)

s2, err := r.NextSpan()
require.NoError(t, err)
assert.Equal(t, "471418097747d04a", s2.OperationName)
assert.Equal(t, 2, r.spansRead)
assert.Equal(t, true, r.eofReached)

_, err = r.NextSpan()
require.Equal(t, io.EOF, err)
assert.Equal(t, 2, r.spansRead)
assert.Equal(t, true, r.eofReached)
}

func TestReader_TraceNonExistent(t *testing.T) {
inputFile := "fixtures/trace_non_existent.json"
_, err := NewReader(
inputFile,
zap.NewNop(),
)
require.Contains(t, err.Error(), "cannot open captured file")
}

func TestReader_TraceEmpty(t *testing.T) {
inputFile := "fixtures/trace_empty.json"
r, err := NewReader(
inputFile,
zap.NewNop(),
)
require.NoError(t, err)

_, err = r.NextSpan()
require.Contains(t, err.Error(), "cannot read file")
assert.Equal(t, 0, r.spansRead)
assert.Equal(t, true, r.eofReached)
}

func TestReader_TraceWrongFormat(t *testing.T) {
inputFile := "fixtures/trace_wrong_format.json"
r, err := NewReader(
inputFile,
zap.NewNop(),
)
require.NoError(t, err)

_, err = r.NextSpan()
require.Equal(t, "file must begin with '['", err.Error())
assert.Equal(t, 0, r.spansRead)
assert.Equal(t, true, r.eofReached)
}

func TestReader_TraceInvalidJson(t *testing.T) {
inputFile := "fixtures/trace_invalid_json.json"
r, err := NewReader(
inputFile,
zap.NewNop(),
)
require.NoError(t, err)

_, err = r.NextSpan()
require.Contains(t, err.Error(), "cannot unmarshal span")
assert.Equal(t, 0, r.spansRead)
assert.Equal(t, true, r.eofReached)
}

0 comments on commit cea616b

Please sign in to comment.