| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
package profiles |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"errors" |
| 9 |
"net" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
"google.golang.org/grpc" |
| 15 |
"google.golang.org/grpc/codes" |
| 16 |
"google.golang.org/grpc/credentials/insecure" |
| 17 |
"google.golang.org/grpc/status" |
| 18 |
|
| 19 |
"go.opentelemetry.io/collector/consumer/consumererror" |
| 20 |
"go.opentelemetry.io/collector/consumer/consumertest" |
| 21 |
"go.opentelemetry.io/collector/consumer/xconsumer" |
| 22 |
"go.opentelemetry.io/collector/pdata/pprofile/pprofileotlp" |
| 23 |
"go.opentelemetry.io/collector/pdata/testdata" |
| 24 |
) |
| 25 |
|
| 26 |
func TestExport(t *testing.T) { |
| 27 |
td := testdata.GenerateProfiles(1) |
| 28 |
req := pprofileotlp.NewExportRequestFromProfiles(td) |
| 29 |
|
| 30 |
profileSink := new(consumertest.ProfilesSink) |
| 31 |
profileClient := makeProfileServiceClient(t, profileSink) |
| 32 |
resp, err := profileClient.Export(context.Background(), req) |
| 33 |
require.NoError(t, err, "Failed to export profile: %v", err) |
| 34 |
require.NotNil(t, resp, "The response is missing") |
| 35 |
|
| 36 |
require.Len(t, profileSink.AllProfiles(), 1) |
| 37 |
assert.Equal(t, td, profileSink.AllProfiles()[0]) |
| 38 |
} |
| 39 |
|
| 40 |
func TestExport_EmptyRequest(t *testing.T) { |
| 41 |
profileSink := new(consumertest.ProfilesSink) |
| 42 |
profileClient := makeProfileServiceClient(t, profileSink) |
| 43 |
resp, err := profileClient.Export(context.Background(), pprofileotlp.NewExportRequest()) |
| 44 |
require.NoError(t, err, "Failed to export profile: %v", err) |
| 45 |
assert.NotNil(t, resp, "The response is missing") |
| 46 |
} |
| 47 |
|
| 48 |
func TestExport_NonPermanentErrorConsumer(t *testing.T) { |
| 49 |
td := testdata.GenerateProfiles(1) |
| 50 |
req := pprofileotlp.NewExportRequestFromProfiles(td) |
| 51 |
|
| 52 |
profileClient := makeProfileServiceClient(t, consumertest.NewErr(errors.New("my error"))) |
| 53 |
resp, err := profileClient.Export(context.Background(), req) |
| 54 |
require.EqualError(t, err, "rpc error: code = Unavailable desc = my error") |
| 55 |
require.ErrorIs(t, err, status.Error(codes.Unavailable, "my error")) |
| 56 |
assert.Equal(t, pprofileotlp.ExportResponse{}, resp) |
| 57 |
} |
| 58 |
|
| 59 |
func TestExport_PermanentErrorConsumer(t *testing.T) { |
| 60 |
ld := testdata.GenerateProfiles(1) |
| 61 |
req := pprofileotlp.NewExportRequestFromProfiles(ld) |
| 62 |
|
| 63 |
profileClient := makeProfileServiceClient(t, consumertest.NewErr(consumererror.NewPermanent(errors.New("my error")))) |
| 64 |
resp, err := profileClient.Export(context.Background(), req) |
| 65 |
require.EqualError(t, err, "rpc error: code = Internal desc = Permanent error: my error") |
| 66 |
require.ErrorIs(t, err, status.Error(codes.Internal, "Permanent error: my error")) |
| 67 |
assert.Equal(t, pprofileotlp.ExportResponse{}, resp) |
| 68 |
} |
| 69 |
|
| 70 |
func makeProfileServiceClient(t *testing.T, tc xconsumer.Profiles) pprofileotlp.GRPCClient { |
| 71 |
addr := otlpReceiverOnGRPCServer(t, tc) |
| 72 |
cc, err := grpc.NewClient(addr.String(), grpc.WithTransportCredentials(insecure.NewCredentials())) |
| 73 |
require.NoError(t, err, "Failed to create the profileServiceClient: %v", err) |
| 74 |
t.Cleanup(func() { |
| 75 |
require.NoError(t, cc.Close()) |
| 76 |
}) |
| 77 |
|
| 78 |
return pprofileotlp.NewGRPCClient(cc) |
| 79 |
} |
| 80 |
|
| 81 |
func otlpReceiverOnGRPCServer(t *testing.T, tc xconsumer.Profiles) net.Addr { |
| 82 |
ln, err := net.Listen("tcp", "localhost:") |
| 83 |
require.NoError(t, err, "Failed to find an available address to run the gRPC server: %v", err) |
| 84 |
|
| 85 |
t.Cleanup(func() { |
| 86 |
require.NoError(t, ln.Close()) |
| 87 |
}) |
| 88 |
|
| 89 |
r := New(tc) |
| 90 |
|
| 91 |
srv := grpc.NewServer() |
| 92 |
pprofileotlp.RegisterGRPCServer(srv, r) |
| 93 |
go func() { |
| 94 |
_ = srv.Serve(ln) |
| 95 |
}() |
| 96 |
|
| 97 |
return ln.Addr() |
| 98 |
} |