|
5 | 5 | package model_test
|
6 | 6 |
|
7 | 7 | import (
|
8 |
| - "bytes" |
9 | 8 | "testing"
|
10 | 9 |
|
11 |
| - "github.com/gogo/protobuf/jsonpb" |
12 |
| - "github.com/gogo/protobuf/proto" |
13 | 10 | "github.com/stretchr/testify/assert"
|
14 | 11 | "github.com/stretchr/testify/require"
|
15 |
| - "go.opentelemetry.io/collector/pdata/pcommon" |
16 | 12 |
|
17 | 13 | "github.com/jaegertracing/jaeger/model"
|
18 |
| - "github.com/jaegertracing/jaeger/model/prototest" |
19 | 14 | )
|
20 | 15 |
|
21 |
| -// TraceID/SpanID fields are defined as bytes in proto, backed by custom types in Go. |
22 |
| -// Unfortunately, that means they require manual implementations of proto & json serialization. |
23 |
| -// To ensure that it's the same as the default protobuf serialization, file jaeger_test.proto |
24 |
| -// contains a copy of SpanRef message without any gogo options. This test file is compiled with |
25 |
| -// plain protoc -go_out (without gogo). This test performs proto and JSON marshaling/unmarshaling |
26 |
| -// to ensure that the outputs of manual and standard serialization are identical. |
27 |
| -func TestTraceSpanIDMarshalProto(t *testing.T) { |
28 |
| - testCases := []struct { |
29 |
| - name string |
30 |
| - marshal func(proto.Message) ([]byte, error) |
31 |
| - unmarshal func([]byte, proto.Message) error |
32 |
| - expected string |
33 |
| - }{ |
34 |
| - { |
35 |
| - name: "protobuf", |
36 |
| - marshal: proto.Marshal, |
37 |
| - unmarshal: proto.Unmarshal, |
38 |
| - }, |
39 |
| - { |
40 |
| - name: "JSON", |
41 |
| - marshal: func(m proto.Message) ([]byte, error) { |
42 |
| - out := new(bytes.Buffer) |
43 |
| - err := new(jsonpb.Marshaler).Marshal(out, m) |
44 |
| - if err != nil { |
45 |
| - return nil, err |
46 |
| - } |
47 |
| - return out.Bytes(), nil |
48 |
| - }, |
49 |
| - unmarshal: func(in []byte, m proto.Message) error { |
50 |
| - return jsonpb.Unmarshal(bytes.NewReader(in), m) |
51 |
| - }, |
52 |
| - expected: `{"traceId":"AAAAAAAAAAIAAAAAAAAAAw==","spanId":"AAAAAAAAAAs="}`, |
53 |
| - }, |
54 |
| - } |
55 |
| - for _, testCase := range testCases { |
56 |
| - t.Run(testCase.name, func(t *testing.T) { |
57 |
| - ref1 := model.SpanRef{TraceID: model.NewTraceID(2, 3), SpanID: model.NewSpanID(11)} |
58 |
| - ref2 := prototest.SpanRef{ |
59 |
| - // TODO: would be cool to fuzz that test |
60 |
| - TraceId: []byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3}, |
61 |
| - SpanId: []byte{0, 0, 0, 0, 0, 0, 0, 11}, |
62 |
| - } |
63 |
| - d1, err := testCase.marshal(&ref1) |
64 |
| - require.NoError(t, err) |
65 |
| - d2, err := testCase.marshal(&ref2) |
66 |
| - require.NoError(t, err) |
67 |
| - assert.Equal(t, d2, d1) |
68 |
| - if testCase.expected != "" { |
69 |
| - assert.Equal(t, testCase.expected, string(d1)) |
70 |
| - } |
71 |
| - // test unmarshal |
72 |
| - var ref1u model.SpanRef |
73 |
| - err = testCase.unmarshal(d2, &ref1u) |
74 |
| - require.NoError(t, err) |
75 |
| - assert.Equal(t, ref1, ref1u) |
76 |
| - }) |
77 |
| - } |
78 |
| -} |
79 |
| - |
80 | 16 | func TestSpanIDFromBytes(t *testing.T) {
|
81 | 17 | errTests := [][]byte{
|
82 | 18 | {0, 0, 0, 0},
|
@@ -118,74 +54,3 @@ func TestTraceIDFromBytes(t *testing.T) {
|
118 | 54 | assert.Equal(t, test.expected, traceID)
|
119 | 55 | }
|
120 | 56 | }
|
121 |
| - |
122 |
| -func TestToOTELTraceID(t *testing.T) { |
123 |
| - modelTraceID := model.TraceID{ |
124 |
| - Low: 3, |
125 |
| - High: 2, |
126 |
| - } |
127 |
| - otelTraceID := modelTraceID.ToOTELTraceID() |
128 |
| - expected := []byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3} |
129 |
| - require.Equal(t, pcommon.TraceID(expected), otelTraceID) |
130 |
| -} |
131 |
| - |
132 |
| -func TestTraceIDFromOTEL(t *testing.T) { |
133 |
| - otelTraceID := pcommon.TraceID([]byte{0, 0, 0, 0, 0, 0, 0, 2, 0, 0, 0, 0, 0, 0, 0, 3}) |
134 |
| - expected := model.TraceID{ |
135 |
| - Low: 3, |
136 |
| - High: 2, |
137 |
| - } |
138 |
| - require.Equal(t, expected, model.TraceIDFromOTEL(otelTraceID)) |
139 |
| -} |
140 |
| - |
141 |
| -func TestToOTELSpanID(t *testing.T) { |
142 |
| - tests := []struct { |
143 |
| - name string |
144 |
| - spanID model.SpanID |
145 |
| - expected pcommon.SpanID |
146 |
| - }{ |
147 |
| - { |
148 |
| - name: "zero span ID", |
149 |
| - spanID: model.NewSpanID(0), |
150 |
| - expected: pcommon.NewSpanIDEmpty(), |
151 |
| - }, |
152 |
| - { |
153 |
| - name: "non-zero span ID", |
154 |
| - spanID: model.NewSpanID(1), |
155 |
| - expected: pcommon.SpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 1}), |
156 |
| - }, |
157 |
| - } |
158 |
| - |
159 |
| - for _, test := range tests { |
160 |
| - t.Run(test.name, func(t *testing.T) { |
161 |
| - actual := test.spanID.ToOTELSpanID() |
162 |
| - assert.Equal(t, test.expected, actual) |
163 |
| - }) |
164 |
| - } |
165 |
| -} |
166 |
| - |
167 |
| -func TestSpanIDFromOTEL(t *testing.T) { |
168 |
| - tests := []struct { |
169 |
| - name string |
170 |
| - otelSpanID pcommon.SpanID |
171 |
| - expected model.SpanID |
172 |
| - }{ |
173 |
| - { |
174 |
| - name: "zero span ID", |
175 |
| - otelSpanID: pcommon.NewSpanIDEmpty(), |
176 |
| - expected: model.NewSpanID(0), |
177 |
| - }, |
178 |
| - { |
179 |
| - name: "non-zero span ID", |
180 |
| - otelSpanID: pcommon.SpanID([8]byte{0, 0, 0, 0, 0, 0, 0, 1}), |
181 |
| - expected: model.NewSpanID(1), |
182 |
| - }, |
183 |
| - } |
184 |
| - |
185 |
| - for _, test := range tests { |
186 |
| - t.Run(test.name, func(t *testing.T) { |
187 |
| - actual := model.SpanIDFromOTEL(test.otelSpanID) |
188 |
| - assert.Equal(t, test.expected, actual) |
189 |
| - }) |
190 |
| - } |
191 |
| -} |
0 commit comments