123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package skywalkingreceiver
- import (
- "context"
- "path/filepath"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component"
- "go.opentelemetry.io/collector/component/componenttest"
- "go.opentelemetry.io/collector/config/configgrpc"
- "go.opentelemetry.io/collector/config/confighttp"
- "go.opentelemetry.io/collector/config/confignet"
- "go.opentelemetry.io/collector/config/configtls"
- "go.opentelemetry.io/collector/confmap/confmaptest"
- "go.opentelemetry.io/collector/consumer/consumertest"
- "go.opentelemetry.io/collector/receiver/receivertest"
- "github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent"
- "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/skywalkingreceiver/internal/metadata"
- )
- func TestTypeStr(t *testing.T) {
- factory := NewFactory()
- assert.Equal(t, "skywalking", string(factory.Type()))
- }
- func TestCreateDefaultConfig(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- assert.NotNil(t, cfg, "failed to create default config")
- assert.NoError(t, componenttest.CheckConfigStruct(cfg))
- }
- func TestCreateReceiver(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- // have to enable at least one protocol for the skywalking receiver to be created
- cfg.(*Config).Protocols.GRPC = &configgrpc.GRPCServerSettings{
- NetAddr: confignet.NetAddr{
- Endpoint: defaultGRPCBindEndpoint,
- Transport: "tcp",
- },
- }
- traceSink := new(consumertest.TracesSink)
- set := receivertest.NewNopCreateSettings()
- tReceiver, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
- assert.NoError(t, err, "trace receiver creation failed")
- assert.NotNil(t, tReceiver, "trace receiver creation failed")
- metricSink := new(consumertest.MetricsSink)
- mReceiver, err := factory.CreateMetricsReceiver(context.Background(), set, cfg, metricSink)
- assert.NoError(t, err, "metric receiver creation failed")
- assert.NotNil(t, mReceiver, "metric receiver creation failed")
- }
- func TestCreateReceiverGeneralConfig(t *testing.T) {
- cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
- require.NoError(t, err)
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "customname").String())
- require.NoError(t, err)
- require.NoError(t, component.UnmarshalConfig(sub, cfg))
- set := receivertest.NewNopCreateSettings()
- traceSink := new(consumertest.TracesSink)
- tReceiver, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
- assert.NoError(t, err, "trace receiver creation failed")
- assert.NotNil(t, tReceiver, "trace receiver creation failed")
- metricSink := new(consumertest.MetricsSink)
- mReceiver, err := factory.CreateMetricsReceiver(context.Background(), set, cfg, metricSink)
- assert.NoError(t, err, "metric receiver creation failed")
- assert.NotNil(t, mReceiver, "metric receiver creation failed")
- }
- func TestCreateDefaultGRPCEndpoint(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- cfg.(*Config).Protocols.GRPC = &configgrpc.GRPCServerSettings{
- NetAddr: confignet.NetAddr{
- Endpoint: defaultGRPCBindEndpoint,
- Transport: "tcp",
- },
- }
- traceSink := new(consumertest.TracesSink)
- set := receivertest.NewNopCreateSettings()
- r, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
- assert.NoError(t, err, "unexpected error creating receiver")
- assert.Equal(t, 11800, r.(*sharedcomponent.SharedComponent).
- Unwrap().(*swReceiver).config.CollectorGRPCPort, "grpc port should be default")
- }
- func TestCreateTLSGPRCEndpoint(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- cfg.(*Config).Protocols.GRPC = &configgrpc.GRPCServerSettings{
- NetAddr: confignet.NetAddr{
- Endpoint: defaultGRPCBindEndpoint,
- Transport: "tcp",
- },
- TLSSetting: &configtls.TLSServerSetting{
- TLSSetting: configtls.TLSSetting{
- CertFile: "./testdata/server.crt",
- KeyFile: "./testdata/server.key",
- },
- },
- }
- set := receivertest.NewNopCreateSettings()
- traceSink := new(consumertest.TracesSink)
- _, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
- assert.NoError(t, err, "tls-enabled receiver creation failed")
- }
- func TestCreateTLSHTTPEndpoint(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- cfg.(*Config).Protocols.HTTP = &confighttp.HTTPServerSettings{
- Endpoint: defaultHTTPBindEndpoint,
- TLSSetting: &configtls.TLSServerSetting{
- TLSSetting: configtls.TLSSetting{
- CertFile: "./testdata/server.crt",
- KeyFile: "./testdata/server.key",
- },
- },
- }
- set := receivertest.NewNopCreateSettings()
- traceSink := new(consumertest.TracesSink)
- _, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
- assert.NoError(t, err, "tls-enabled receiver creation failed")
- }
- func TestCreateInvalidHTTPEndpoint(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- cfg.(*Config).Protocols.HTTP = &confighttp.HTTPServerSettings{
- Endpoint: defaultHTTPBindEndpoint,
- }
- set := receivertest.NewNopCreateSettings()
- traceSink := new(consumertest.TracesSink)
- r, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
- assert.NoError(t, err, "unexpected error creating receiver")
- assert.Equal(t, 12800, r.(*sharedcomponent.SharedComponent).
- Unwrap().(*swReceiver).config.CollectorHTTPPort, "http port should be default")
- }
|