telegraf/plugins/inputs/zipkin/handler_test.go

134 lines
3.2 KiB
Go
Raw Normal View History

2017-08-03 08:58:26 +08:00
package zipkin
import (
"bytes"
"io"
2017-08-03 08:58:26 +08:00
"net/http"
"net/http/httptest"
"os"
2017-08-03 08:58:26 +08:00
"strconv"
"testing"
"time"
"github.com/stretchr/testify/require"
"github.com/influxdata/telegraf/plugins/inputs/zipkin/trace"
2017-08-03 08:58:26 +08:00
)
type mockRecorder struct {
data trace.Trace
err error
2017-08-03 08:58:26 +08:00
}
func (m *mockRecorder) record(t trace.Trace) error {
m.data = t
2017-08-03 08:58:26 +08:00
return nil
}
func (m *mockRecorder) error(err error) {
m.err = err
2017-08-03 08:58:26 +08:00
}
func TestSpanHandler(t *testing.T) {
dat, err := os.ReadFile("testdata/threespans.dat")
2017-08-03 08:58:26 +08:00
if err != nil {
t.Fatalf("Could not find file %s\n", "testdata/threespans.dat")
}
w := httptest.NewRecorder()
r := httptest.NewRequest(
"POST",
"http://server.local/api/v1/spans",
io.NopCloser(
2017-08-03 08:58:26 +08:00
bytes.NewReader(dat)))
r.Header.Set("Content-Type", "application/x-thrift")
handler := newSpanHandler("/api/v1/spans")
mockRecorder := &mockRecorder{}
2017-08-03 08:58:26 +08:00
handler.recorder = mockRecorder
handler.spans(w, r)
require.Equal(t, http.StatusNoContent, w.Code)
2017-08-03 08:58:26 +08:00
got := mockRecorder.data
2017-08-03 08:58:26 +08:00
parentID := strconv.FormatInt(22964302721410078, 16)
want := trace.Trace{
{
2017-08-03 08:58:26 +08:00
Name: "Child",
ID: "7047c59776af8a1b",
2017-08-03 08:58:26 +08:00
TraceID: "22c4fc8ab3669045",
ParentID: parentID,
Timestamp: time.Unix(0, 1498688360851331*int64(time.Microsecond)).UTC(),
Duration: time.Duration(53106) * time.Microsecond,
ServiceName: "trivial",
Annotations: make([]trace.Annotation, 0),
BinaryAnnotations: []trace.BinaryAnnotation{
{
2017-08-03 08:58:26 +08:00
Key: "lc",
Value: "trivial",
Host: "127.0.0.1",
ServiceName: "trivial",
},
},
},
{
2017-08-03 08:58:26 +08:00
Name: "Child",
ID: "17020eb55a8bfe5",
2017-08-03 08:58:26 +08:00
TraceID: "22c4fc8ab3669045",
ParentID: parentID,
Timestamp: time.Unix(0, 1498688360904552*int64(time.Microsecond)).UTC(),
Duration: time.Duration(50410) * time.Microsecond,
ServiceName: "trivial",
Annotations: make([]trace.Annotation, 0),
BinaryAnnotations: []trace.BinaryAnnotation{
{
2017-08-03 08:58:26 +08:00
Key: "lc",
Value: "trivial",
Host: "127.0.0.1",
ServiceName: "trivial",
},
},
},
{
2017-08-03 08:58:26 +08:00
Name: "Parent",
ID: "5195e96239641e",
2017-08-03 08:58:26 +08:00
TraceID: "22c4fc8ab3669045",
ParentID: parentID,
2017-08-03 08:58:26 +08:00
Timestamp: time.Unix(0, 1498688360851318*int64(time.Microsecond)).UTC(),
Duration: time.Duration(103680) * time.Microsecond,
ServiceName: "trivial",
Annotations: []trace.Annotation{
{
2017-08-03 08:58:26 +08:00
Timestamp: time.Unix(0, 1498688360851325*int64(time.Microsecond)).UTC(),
Value: "Starting child #0",
Host: "127.0.0.1",
ServiceName: "trivial",
},
{
2017-08-03 08:58:26 +08:00
Timestamp: time.Unix(0, 1498688360904545*int64(time.Microsecond)).UTC(),
Value: "Starting child #1",
Host: "127.0.0.1",
ServiceName: "trivial",
},
{
2017-08-03 08:58:26 +08:00
Timestamp: time.Unix(0, 1498688360954992*int64(time.Microsecond)).UTC(),
Value: "A Log",
Host: "127.0.0.1",
ServiceName: "trivial",
},
},
BinaryAnnotations: []trace.BinaryAnnotation{
{
2017-08-03 08:58:26 +08:00
Key: "lc",
Value: "trivial",
Host: "127.0.0.1",
ServiceName: "trivial",
},
},
},
}
require.Equal(t, want, got)
2017-08-03 08:58:26 +08:00
}