factory_test.go 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package skywalkingreceiver
  4. import (
  5. "context"
  6. "path/filepath"
  7. "testing"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component"
  11. "go.opentelemetry.io/collector/component/componenttest"
  12. "go.opentelemetry.io/collector/config/configgrpc"
  13. "go.opentelemetry.io/collector/config/confighttp"
  14. "go.opentelemetry.io/collector/config/confignet"
  15. "go.opentelemetry.io/collector/config/configtls"
  16. "go.opentelemetry.io/collector/confmap/confmaptest"
  17. "go.opentelemetry.io/collector/consumer/consumertest"
  18. "go.opentelemetry.io/collector/receiver/receivertest"
  19. "github.com/open-telemetry/opentelemetry-collector-contrib/internal/sharedcomponent"
  20. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/skywalkingreceiver/internal/metadata"
  21. )
  22. func TestTypeStr(t *testing.T) {
  23. factory := NewFactory()
  24. assert.Equal(t, "skywalking", string(factory.Type()))
  25. }
  26. func TestCreateDefaultConfig(t *testing.T) {
  27. factory := NewFactory()
  28. cfg := factory.CreateDefaultConfig()
  29. assert.NotNil(t, cfg, "failed to create default config")
  30. assert.NoError(t, componenttest.CheckConfigStruct(cfg))
  31. }
  32. func TestCreateReceiver(t *testing.T) {
  33. factory := NewFactory()
  34. cfg := factory.CreateDefaultConfig()
  35. // have to enable at least one protocol for the skywalking receiver to be created
  36. cfg.(*Config).Protocols.GRPC = &configgrpc.GRPCServerSettings{
  37. NetAddr: confignet.NetAddr{
  38. Endpoint: defaultGRPCBindEndpoint,
  39. Transport: "tcp",
  40. },
  41. }
  42. traceSink := new(consumertest.TracesSink)
  43. set := receivertest.NewNopCreateSettings()
  44. tReceiver, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
  45. assert.NoError(t, err, "trace receiver creation failed")
  46. assert.NotNil(t, tReceiver, "trace receiver creation failed")
  47. metricSink := new(consumertest.MetricsSink)
  48. mReceiver, err := factory.CreateMetricsReceiver(context.Background(), set, cfg, metricSink)
  49. assert.NoError(t, err, "metric receiver creation failed")
  50. assert.NotNil(t, mReceiver, "metric receiver creation failed")
  51. }
  52. func TestCreateReceiverGeneralConfig(t *testing.T) {
  53. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  54. require.NoError(t, err)
  55. factory := NewFactory()
  56. cfg := factory.CreateDefaultConfig()
  57. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "customname").String())
  58. require.NoError(t, err)
  59. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  60. set := receivertest.NewNopCreateSettings()
  61. traceSink := new(consumertest.TracesSink)
  62. tReceiver, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
  63. assert.NoError(t, err, "trace receiver creation failed")
  64. assert.NotNil(t, tReceiver, "trace receiver creation failed")
  65. metricSink := new(consumertest.MetricsSink)
  66. mReceiver, err := factory.CreateMetricsReceiver(context.Background(), set, cfg, metricSink)
  67. assert.NoError(t, err, "metric receiver creation failed")
  68. assert.NotNil(t, mReceiver, "metric receiver creation failed")
  69. }
  70. func TestCreateDefaultGRPCEndpoint(t *testing.T) {
  71. factory := NewFactory()
  72. cfg := factory.CreateDefaultConfig()
  73. cfg.(*Config).Protocols.GRPC = &configgrpc.GRPCServerSettings{
  74. NetAddr: confignet.NetAddr{
  75. Endpoint: defaultGRPCBindEndpoint,
  76. Transport: "tcp",
  77. },
  78. }
  79. traceSink := new(consumertest.TracesSink)
  80. set := receivertest.NewNopCreateSettings()
  81. r, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
  82. assert.NoError(t, err, "unexpected error creating receiver")
  83. assert.Equal(t, 11800, r.(*sharedcomponent.SharedComponent).
  84. Unwrap().(*swReceiver).config.CollectorGRPCPort, "grpc port should be default")
  85. }
  86. func TestCreateTLSGPRCEndpoint(t *testing.T) {
  87. factory := NewFactory()
  88. cfg := factory.CreateDefaultConfig()
  89. cfg.(*Config).Protocols.GRPC = &configgrpc.GRPCServerSettings{
  90. NetAddr: confignet.NetAddr{
  91. Endpoint: defaultGRPCBindEndpoint,
  92. Transport: "tcp",
  93. },
  94. TLSSetting: &configtls.TLSServerSetting{
  95. TLSSetting: configtls.TLSSetting{
  96. CertFile: "./testdata/server.crt",
  97. KeyFile: "./testdata/server.key",
  98. },
  99. },
  100. }
  101. set := receivertest.NewNopCreateSettings()
  102. traceSink := new(consumertest.TracesSink)
  103. _, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
  104. assert.NoError(t, err, "tls-enabled receiver creation failed")
  105. }
  106. func TestCreateTLSHTTPEndpoint(t *testing.T) {
  107. factory := NewFactory()
  108. cfg := factory.CreateDefaultConfig()
  109. cfg.(*Config).Protocols.HTTP = &confighttp.HTTPServerSettings{
  110. Endpoint: defaultHTTPBindEndpoint,
  111. TLSSetting: &configtls.TLSServerSetting{
  112. TLSSetting: configtls.TLSSetting{
  113. CertFile: "./testdata/server.crt",
  114. KeyFile: "./testdata/server.key",
  115. },
  116. },
  117. }
  118. set := receivertest.NewNopCreateSettings()
  119. traceSink := new(consumertest.TracesSink)
  120. _, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
  121. assert.NoError(t, err, "tls-enabled receiver creation failed")
  122. }
  123. func TestCreateInvalidHTTPEndpoint(t *testing.T) {
  124. factory := NewFactory()
  125. cfg := factory.CreateDefaultConfig()
  126. cfg.(*Config).Protocols.HTTP = &confighttp.HTTPServerSettings{
  127. Endpoint: defaultHTTPBindEndpoint,
  128. }
  129. set := receivertest.NewNopCreateSettings()
  130. traceSink := new(consumertest.TracesSink)
  131. r, err := factory.CreateTracesReceiver(context.Background(), set, cfg, traceSink)
  132. assert.NoError(t, err, "unexpected error creating receiver")
  133. assert.Equal(t, 12800, r.(*sharedcomponent.SharedComponent).
  134. Unwrap().(*swReceiver).config.CollectorHTTPPort, "http port should be default")
  135. }