Debian Codegraph @

pdata/internal/generated_proto_span_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 // Code generated by "internal/cmd/pdatagen/main.go". DO NOT EDIT.
5 // To regenerate this file run "make genpdata".
6
7 package internal
8
9 import (
10 "strconv"
11 "testing"
12
13 "github.com/stretchr/testify/assert"
14 "github.com/stretchr/testify/require"
15 gootlptrace "go.opentelemetry.io/proto/slim/otlp/trace/v1"
16 "google.golang.org/protobuf/proto"
17
18 "go.opentelemetry.io/collector/featuregate"
19 "go.opentelemetry.io/collector/pdata/internal/json"
20 )
21
22 func TestCopySpan(t *testing.T) {
23 for name, src := range genTestEncodingValuesSpan() {
24 for _, pooling := range []bool{true, false} {
25 t.Run(name+"/Pooling="+strconv.FormatBool(pooling), func(t *testing.T) {
26 prevPooling := UseProtoPooling.IsEnabled()
27 require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), pooling))
28 defer func() {
29 require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), prevPooling))
30 }()
31
32 dest := NewSpan()
33 CopySpan(dest, src)
34 assert.Equal(t, src, dest)
35 CopySpan(dest, dest)
36 assert.Equal(t, src, dest)
37 })
38 }
39 }
40 }
41
42 func TestCopySpanSlice(t *testing.T) {
43 src := []Span{}
44 dest := []Span{}
45 // Test CopyTo empty
46 dest = CopySpanSlice(dest, src)
47 assert.Equal(t, []Span{}, dest)
48
49 // Test CopyTo larger slice
50 src = GenTestSpanSlice()
51 dest = CopySpanSlice(dest, src)
52 assert.Equal(t, GenTestSpanSlice(), dest)
53
54 // Test CopyTo same size slice
55 dest = CopySpanSlice(dest, src)
56 assert.Equal(t, GenTestSpanSlice(), dest)
57
58 // Test CopyTo smaller size slice
59 dest = CopySpanSlice(dest, []Span{})
60 assert.Len(t, dest, 0)
61
62 // Test CopyTo larger slice with enough capacity
63 dest = CopySpanSlice(dest, src)
64 assert.Equal(t, GenTestSpanSlice(), dest)
65 }
66
67 func TestCopySpanPtrSlice(t *testing.T) {
68 src := []*Span{}
69 dest := []*Span{}
70 // Test CopyTo empty
71 dest = CopySpanPtrSlice(dest, src)
72 assert.Equal(t, []*Span{}, dest)
73
74 // Test CopyTo larger slice
75 src = GenTestSpanPtrSlice()
76 dest = CopySpanPtrSlice(dest, src)
77 assert.Equal(t, GenTestSpanPtrSlice(), dest)
78
79 // Test CopyTo same size slice
80 dest = CopySpanPtrSlice(dest, src)
81 assert.Equal(t, GenTestSpanPtrSlice(), dest)
82
83 // Test CopyTo smaller size slice
84 dest = CopySpanPtrSlice(dest, []*Span{})
85 assert.Len(t, dest, 0)
86
87 // Test CopyTo larger slice with enough capacity
88 dest = CopySpanPtrSlice(dest, src)
89 assert.Equal(t, GenTestSpanPtrSlice(), dest)
90 }
91
92 func TestMarshalAndUnmarshalJSONSpanUnknown(t *testing.T) {
93 iter := json.BorrowIterator([]byte(`{"unknown": "string"}`))
94 defer json.ReturnIterator(iter)
95 dest := NewSpan()
96 dest.UnmarshalJSON(iter)
97 require.NoError(t, iter.Error())
98 assert.Equal(t, NewSpan(), dest)
99 }
100
101 func TestMarshalAndUnmarshalJSONSpan(t *testing.T) {
102 for name, src := range genTestEncodingValuesSpan() {
103 for _, pooling := range []bool{true, false} {
104 t.Run(name+"/Pooling="+strconv.FormatBool(pooling), func(t *testing.T) {
105 prevPooling := UseProtoPooling.IsEnabled()
106 require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), pooling))
107 defer func() {
108 require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), prevPooling))
109 }()
110
111 stream := json.BorrowStream(nil)
112 defer json.ReturnStream(stream)
113 src.MarshalJSON(stream)
114 require.NoError(t, stream.Error())
115
116 iter := json.BorrowIterator(stream.Buffer())
117 defer json.ReturnIterator(iter)
118 dest := NewSpan()
119 dest.UnmarshalJSON(iter)
120 require.NoError(t, iter.Error())
121
122 assert.Equal(t, src, dest)
123 DeleteSpan(dest, true)
124 })
125 }
126 }
127 }
128
129 func TestMarshalAndUnmarshalProtoSpanFailing(t *testing.T) {
130 for name, buf := range genTestFailingUnmarshalProtoValuesSpan() {
131 t.Run(name, func(t *testing.T) {
132 dest := NewSpan()
133 require.Error(t, dest.UnmarshalProto(buf))
134 })
135 }
136 }
137
138 func TestMarshalAndUnmarshalProtoSpanUnknown(t *testing.T) {
139 dest := NewSpan()
140 // message Test { required int64 field = 1313; } encoding { "field": "1234" }
141 require.NoError(t, dest.UnmarshalProto([]byte{0x88, 0x52, 0xD2, 0x09}))
142 assert.Equal(t, NewSpan(), dest)
143 }
144
145 func TestMarshalAndUnmarshalProtoSpan(t *testing.T) {
146 for name, src := range genTestEncodingValuesSpan() {
147 for _, pooling := range []bool{true, false} {
148 t.Run(name+"/Pooling="+strconv.FormatBool(pooling), func(t *testing.T) {
149 prevPooling := UseProtoPooling.IsEnabled()
150 require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), pooling))
151 defer func() {
152 require.NoError(t, featuregate.GlobalRegistry().Set(UseProtoPooling.ID(), prevPooling))
153 }()
154
155 buf := make([]byte, src.SizeProto())
156 gotSize := src.MarshalProto(buf)
157 assert.Equal(t, len(buf), gotSize)
158
159 dest := NewSpan()
160 require.NoError(t, dest.UnmarshalProto(buf))
161
162 assert.Equal(t, src, dest)
163 DeleteSpan(dest, true)
164 })
165 }
166 }
167 }
168
169 func TestMarshalAndUnmarshalProtoViaProtobufSpan(t *testing.T) {
170 for name, src := range genTestEncodingValuesSpan() {
171 t.Run(name, func(t *testing.T) {
172 buf := make([]byte, src.SizeProto())
173 gotSize := src.MarshalProto(buf)
174 assert.Equal(t, len(buf), gotSize)
175
176 goDest := &gootlptrace.Span{}
177 require.NoError(t, proto.Unmarshal(buf, goDest))
178
179 goBuf, err := proto.Marshal(goDest)
180 require.NoError(t, err)
181
182 dest := NewSpan()
183 require.NoError(t, dest.UnmarshalProto(goBuf))
184 assert.Equal(t, src, dest)
185 })
186 }
187 }
188
189 func genTestFailingUnmarshalProtoValuesSpan() map[string][]byte {
190 return map[string][]byte{
191 "invalid_field": {0x02},
192 "TraceId/wrong_wire_type": {0xc},
193 "TraceId/missing_value": {0xa},
194 "SpanId/wrong_wire_type": {0x14},
195 "SpanId/missing_value": {0x12},
196 "TraceState/wrong_wire_type": {0x1c},
197 "TraceState/missing_value": {0x1a},
198 "ParentSpanId/wrong_wire_type": {0x24},
199 "ParentSpanId/missing_value": {0x22},
200 "Flags/wrong_wire_type": {0x84, 0x1},
201 "Flags/missing_value": {0x85, 0x1},
202 "Name/wrong_wire_type": {0x2c},
203 "Name/missing_value": {0x2a},
204 "Kind/wrong_wire_type": {0x34},
205 "Kind/missing_value": {0x30},
206 "StartTimeUnixNano/wrong_wire_type": {0x3c},
207 "StartTimeUnixNano/missing_value": {0x39},
208 "EndTimeUnixNano/wrong_wire_type": {0x44},
209 "EndTimeUnixNano/missing_value": {0x41},
210 "Attributes/wrong_wire_type": {0x4c},
211 "Attributes/missing_value": {0x4a},
212 "DroppedAttributesCount/wrong_wire_type": {0x54},
213 "DroppedAttributesCount/missing_value": {0x50},
214 "Events/wrong_wire_type": {0x5c},
215 "Events/missing_value": {0x5a},
216 "DroppedEventsCount/wrong_wire_type": {0x64},
217 "DroppedEventsCount/missing_value": {0x60},
218 "Links/wrong_wire_type": {0x6c},
219 "Links/missing_value": {0x6a},
220 "DroppedLinksCount/wrong_wire_type": {0x74},
221 "DroppedLinksCount/missing_value": {0x70},
222 "Status/wrong_wire_type": {0x7c},
223 "Status/missing_value": {0x7a},
224 }
225 }
226
227 func genTestEncodingValuesSpan() map[string]*Span {
228 return map[string]*Span{
229 "empty": NewSpan(),
230 "TraceId/test": {TraceId: *GenTestTraceID()},
231 "SpanId/test": {SpanId: *GenTestSpanID()},
232 "TraceState/test": {TraceState: "test_tracestate"},
233 "ParentSpanId/test": {ParentSpanId: *GenTestSpanID()},
234 "Flags/test": {Flags: uint32(13)},
235 "Name/test": {Name: "test_name"},
236 "Kind/test": {Kind: SpanKind(13)},
237 "StartTimeUnixNano/test": {StartTimeUnixNano: uint64(13)},
238 "EndTimeUnixNano/test": {EndTimeUnixNano: uint64(13)},
239 "Attributes/test": {Attributes: []KeyValue{{}, *GenTestKeyValue()}},
240 "DroppedAttributesCount/test": {DroppedAttributesCount: uint32(13)},
241 "Events/test": {Events: []*SpanEvent{{}, GenTestSpanEvent()}},
242 "DroppedEventsCount/test": {DroppedEventsCount: uint32(13)},
243 "Links/test": {Links: []*SpanLink{{}, GenTestSpanLink()}},
244 "DroppedLinksCount/test": {DroppedLinksCount: uint32(13)},
245 "Status/test": {Status: *GenTestStatus()},
246 }
247 }