forked from jaegertracing/jaeger
-
Notifications
You must be signed in to change notification settings - Fork 0
/
ids_test.go
130 lines (121 loc) · 4.08 KB
/
ids_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
// Copyright (c) 2019 The Jaeger Authors.
// Copyright (c) 2018 Uber Technologies, Inc.
//
// 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 model_test
import (
"bytes"
"testing"
"github.com/gogo/protobuf/jsonpb"
"github.com/golang/protobuf/proto"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/jaegertracing/jaeger/model"
"github.com/jaegertracing/jaeger/model/prototest"
)
// TraceID/SpanID fields are defined as bytes in proto, backed by custom types in Go.
// Unfortunately, that means they require manual implementations of proto & json serialization.
// To ensure that it's the same as the default protobuf serialization, file jaeger_test.proto
// contains a copy of SpanRef message without any gogo options. This test file is compiled with
// plain protoc -go_out (without gogo). This test performs proto and JSON marshaling/unmarshaling
// to ensure that the outputs of manual and standard serialization are identical.
func TestTraceSpanIDMarshalProto(t *testing.T) {
testCases := []struct {
name string
marshal func(proto.Message) ([]byte, error)
unmarshal func([]byte, proto.Message) error
expected string
}{
{
name: "protobuf",
marshal: proto.Marshal,
unmarshal: proto.Unmarshal,
},
{
name: "JSON",
marshal: func(m proto.Message) ([]byte, error) {
out := new(bytes.Buffer)
err := new(jsonpb.Marshaler).Marshal(out, m)
if err != nil {
return nil, err
}
return out.Bytes(), nil
},
unmarshal: func(in []byte, m proto.Message) error {
return jsonpb.Unmarshal(bytes.NewReader(in), m)
},
expected: `{"traceId":"AAAAAAAAAAIAAAAAAAAAAw==","spanId":"AAAAAAAAAAs="}`,
},
}
for _, testCase := range testCases {
t.Run(testCase.name, func(t *testing.T) {
ref1 := model.SpanRef{TraceID: model.NewTraceID(2, 3), SpanID: model.NewSpanID(11)}
ref2 := prototest.SpanRef{
// TODO: would be cool to fuzz that test
TraceId: []byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3},
SpanId: []byte{0, 0, 0, 0, 0, 0, 0, 11},
}
d1, err := testCase.marshal(&ref1)
require.NoError(t, err)
d2, err := testCase.marshal(&ref2)
require.NoError(t, err)
assert.Equal(t, d2, d1)
if testCase.expected != "" {
assert.Equal(t, testCase.expected, string(d1))
}
// test unmarshal
var ref1u model.SpanRef
err = testCase.unmarshal(d2, &ref1u)
require.NoError(t, err)
assert.Equal(t, ref1, ref1u)
})
}
}
func TestSpanIDFromBytes(t *testing.T) {
errTests := [][]byte{
{0, 0, 0, 0},
{0, 0, 0, 0, 0, 0, 0, 13, 0},
}
for _, data := range errTests {
_, err := model.SpanIDFromBytes(data)
require.Error(t, err)
assert.Equal(t, err.Error(), "invalid length for SpanID")
}
spanID, err := model.SpanIDFromBytes([]byte{0, 0, 0, 0, 0, 0, 0, 13})
require.NoError(t, err)
assert.Equal(t, spanID, model.NewSpanID(13))
}
func TestTraceIDFromBytes(t *testing.T) {
errTests := [][]byte{
{0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 0, 0, 13},
{0, 0, 0, 0, 0, 0, 0, 13, 0, 0, 0, 0, 0, 0, 13},
{0, 0, 0, 0, 0, 0, 13},
}
for _, data := range errTests {
_, err := model.TraceIDFromBytes(data)
require.Error(t, err)
assert.Equal(t, err.Error(), "invalid length for TraceID")
}
tests := []struct {
data []byte
expected model.TraceID
}{
{data: []byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3}, expected: model.NewTraceID(2, 3)},
{data: []byte{0, 0, 0, 0, 0, 0, 0, 2}, expected: model.NewTraceID(0, 2)},
}
for _, test := range tests {
traceID, err := model.TraceIDFromBytes(test.data)
require.NoError(t, err)
assert.Equal(t, traceID, test.expected)
}
}