123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package jaegerreceiver
- import (
- "path/filepath"
- "testing"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component"
- "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"
- "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver/internal/metadata"
- )
- func TestLoadConfig(t *testing.T) {
- t.Parallel()
- cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
- require.NoError(t, err)
- tests := []struct {
- id component.ID
- expected component.Config
- }{
- {
- id: component.NewIDWithName(metadata.Type, "customname"),
- expected: &Config{
- Protocols: Protocols{
- GRPC: &configgrpc.GRPCServerSettings{
- NetAddr: confignet.NetAddr{
- Endpoint: "localhost:9876",
- Transport: "tcp",
- },
- },
- ThriftHTTP: &confighttp.HTTPServerSettings{
- Endpoint: ":3456",
- },
- ThriftCompact: &ProtocolUDP{
- Endpoint: "0.0.0.0:456",
- ServerConfigUDP: ServerConfigUDP{
- QueueSize: 100_000,
- MaxPacketSize: 131_072,
- Workers: 100,
- SocketBufferSize: 65_536,
- },
- },
- ThriftBinary: &ProtocolUDP{
- Endpoint: "0.0.0.0:789",
- ServerConfigUDP: ServerConfigUDP{
- QueueSize: 1_000,
- MaxPacketSize: 65_536,
- Workers: 5,
- SocketBufferSize: 0,
- },
- },
- },
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "defaults"),
- expected: &Config{
- Protocols: Protocols{
- GRPC: &configgrpc.GRPCServerSettings{
- NetAddr: confignet.NetAddr{
- Endpoint: defaultGRPCBindEndpoint,
- Transport: "tcp",
- },
- },
- ThriftHTTP: &confighttp.HTTPServerSettings{
- Endpoint: defaultHTTPBindEndpoint,
- },
- ThriftCompact: &ProtocolUDP{
- Endpoint: defaultThriftCompactBindEndpoint,
- ServerConfigUDP: defaultServerConfigUDP(),
- },
- ThriftBinary: &ProtocolUDP{
- Endpoint: defaultThriftBinaryBindEndpoint,
- ServerConfigUDP: defaultServerConfigUDP(),
- },
- },
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "mixed"),
- expected: &Config{
- Protocols: Protocols{
- GRPC: &configgrpc.GRPCServerSettings{
- NetAddr: confignet.NetAddr{
- Endpoint: "localhost:9876",
- Transport: "tcp",
- },
- },
- ThriftCompact: &ProtocolUDP{
- Endpoint: defaultThriftCompactBindEndpoint,
- ServerConfigUDP: defaultServerConfigUDP(),
- },
- },
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "tls"),
- expected: &Config{
- Protocols: Protocols{
- GRPC: &configgrpc.GRPCServerSettings{
- NetAddr: confignet.NetAddr{
- Endpoint: "localhost:9876",
- Transport: "tcp",
- },
- TLSSetting: &configtls.TLSServerSetting{
- TLSSetting: configtls.TLSSetting{
- CertFile: "/test.crt",
- KeyFile: "/test.key",
- },
- },
- },
- ThriftHTTP: &confighttp.HTTPServerSettings{
- Endpoint: ":3456",
- },
- },
- },
- },
- }
- for _, tt := range tests {
- t.Run(tt.id.String(), func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- sub, err := cm.Sub(tt.id.String())
- require.NoError(t, err)
- require.NoError(t, component.UnmarshalConfig(sub, cfg))
- assert.NoError(t, component.ValidateConfig(cfg))
- assert.Equal(t, tt.expected, cfg)
- })
- }
- }
- func TestFailedLoadConfig(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, "typo_default_proto_config").String())
- require.NoError(t, err)
- err = component.UnmarshalConfig(sub, cfg)
- assert.EqualError(t, err, "1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift_htttp")
- sub, err = cm.Sub(component.NewIDWithName(metadata.Type, "bad_proto_config").String())
- require.NoError(t, err)
- err = component.UnmarshalConfig(sub, cfg)
- assert.EqualError(t, err, "1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift_htttp")
- sub, err = cm.Sub(component.NewIDWithName(metadata.Type, "empty").String())
- require.NoError(t, err)
- err = component.UnmarshalConfig(sub, cfg)
- assert.EqualError(t, err, "empty config for Jaeger receiver")
- }
- func TestInvalidConfig(t *testing.T) {
- testCases := []struct {
- desc string
- apply func(*Config)
- err string
- }{
- {
- desc: "thrift-http-no-port",
- apply: func(cfg *Config) {
- cfg.ThriftHTTP = &confighttp.HTTPServerSettings{
- Endpoint: "localhost:",
- }
- },
- err: "receiver creation with no port number for Thrift HTTP must fail",
- },
- {
- desc: "thrift-udp-compact-no-port",
- apply: func(cfg *Config) {
- cfg.ThriftCompact = &ProtocolUDP{
- Endpoint: "localhost:",
- }
- },
- err: "receiver creation with no port number for Thrift UDP - Compact must fail",
- },
- {
- desc: "thrift-udp-binary-no-port",
- apply: func(cfg *Config) {
- cfg.ThriftBinary = &ProtocolUDP{
- Endpoint: "localhost:",
- }
- },
- err: "receiver creation with no port number for Thrift UDP - Binary must fail",
- },
- {
- desc: "grpc-invalid-host",
- apply: func(cfg *Config) {
- cfg.GRPC = &configgrpc.GRPCServerSettings{
- NetAddr: confignet.NetAddr{
- Endpoint: "1234",
- Transport: "tcp",
- },
- }
- },
- err: "receiver creation with bad hostname must fail",
- },
- {
- desc: "no-protocols",
- apply: func(cfg *Config) {
- cfg.Protocols = Protocols{}
- },
- err: "receiver creation with no protocols must fail",
- },
- {
- desc: "port-outside-of-range",
- apply: func(cfg *Config) {
- cfg.ThriftBinary = &ProtocolUDP{
- Endpoint: "localhost:65536",
- }
- },
- err: "receiver creation with too large port number must fail",
- },
- }
- for _, tC := range testCases {
- t.Run(tC.desc, func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig().(*Config)
- tC.apply(cfg)
- err := component.ValidateConfig(cfg)
- assert.Error(t, err, tC.err)
- })
- }
- }
|