Debian Codegraph @

pdata/xpdata/request/profiles_request_test.go

raw
1 // Copyright The OpenTelemetry Authors
2 // SPDX-License-Identifier: Apache-2.0
3
4 package request // import "go.opentelemetry.io/collector/pdata/xpdata/request"
5
6 import (
7 "context"
8 "testing"
9
10 "github.com/stretchr/testify/assert"
11 "github.com/stretchr/testify/require"
12 "go.opentelemetry.io/otel/trace"
13
14 "go.opentelemetry.io/collector/pdata/pprofile"
15 "go.opentelemetry.io/collector/pdata/testdata"
16 )
17
18 func TestMarshalUnmarshalProfilesRequest(t *testing.T) {
19 profiles := testdata.GenerateProfiles(3)
20
21 // unmarshal profiles request with a context
22 spanCtx := fakeSpanContext(t)
23 buf, err := MarshalProfiles(trace.ContextWithSpanContext(context.Background(), spanCtx), profiles)
24 require.NoError(t, err)
25 gotCtx, gotProfiles, err := UnmarshalProfiles(buf)
26 require.NoError(t, err)
27 assert.Equal(t, spanCtx, trace.SpanContextFromContext(gotCtx))
28 assert.Equal(t, profiles, gotProfiles)
29
30 // unmarshal profiles request with empty context
31 buf, err = MarshalProfiles(context.Background(), profiles)
32 require.NoError(t, err)
33 gotCtx, gotProfiles, err = UnmarshalProfiles(buf)
34 require.NoError(t, err)
35 assert.Equal(t, context.Background(), gotCtx)
36 assert.Equal(t, profiles, gotProfiles)
37
38 // unmarshal corrupted data
39 _, _, err = UnmarshalProfiles(buf[:len(buf)-1])
40 require.ErrorContains(t, err, "failed to unmarshal profiles request")
41
42 // unmarshal invalid format (bare profiles)
43 buf, err = (&pprofile.ProtoMarshaler{}).MarshalProfiles(profiles)
44 require.NoError(t, err)
45 _, _, err = UnmarshalProfiles(buf)
46 require.ErrorIs(t, err, ErrInvalidFormat)
47 }