config_test.go 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package jaegerreceiver
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "github.com/stretchr/testify/assert"
  8. "github.com/stretchr/testify/require"
  9. "go.opentelemetry.io/collector/component"
  10. "go.opentelemetry.io/collector/config/configgrpc"
  11. "go.opentelemetry.io/collector/config/confighttp"
  12. "go.opentelemetry.io/collector/config/confignet"
  13. "go.opentelemetry.io/collector/config/configtls"
  14. "go.opentelemetry.io/collector/confmap/confmaptest"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/jaegerreceiver/internal/metadata"
  16. )
  17. func TestLoadConfig(t *testing.T) {
  18. t.Parallel()
  19. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  20. require.NoError(t, err)
  21. tests := []struct {
  22. id component.ID
  23. expected component.Config
  24. }{
  25. {
  26. id: component.NewIDWithName(metadata.Type, "customname"),
  27. expected: &Config{
  28. Protocols: Protocols{
  29. GRPC: &configgrpc.GRPCServerSettings{
  30. NetAddr: confignet.NetAddr{
  31. Endpoint: "localhost:9876",
  32. Transport: "tcp",
  33. },
  34. },
  35. ThriftHTTP: &confighttp.HTTPServerSettings{
  36. Endpoint: ":3456",
  37. },
  38. ThriftCompact: &ProtocolUDP{
  39. Endpoint: "0.0.0.0:456",
  40. ServerConfigUDP: ServerConfigUDP{
  41. QueueSize: 100_000,
  42. MaxPacketSize: 131_072,
  43. Workers: 100,
  44. SocketBufferSize: 65_536,
  45. },
  46. },
  47. ThriftBinary: &ProtocolUDP{
  48. Endpoint: "0.0.0.0:789",
  49. ServerConfigUDP: ServerConfigUDP{
  50. QueueSize: 1_000,
  51. MaxPacketSize: 65_536,
  52. Workers: 5,
  53. SocketBufferSize: 0,
  54. },
  55. },
  56. },
  57. },
  58. },
  59. {
  60. id: component.NewIDWithName(metadata.Type, "defaults"),
  61. expected: &Config{
  62. Protocols: Protocols{
  63. GRPC: &configgrpc.GRPCServerSettings{
  64. NetAddr: confignet.NetAddr{
  65. Endpoint: defaultGRPCBindEndpoint,
  66. Transport: "tcp",
  67. },
  68. },
  69. ThriftHTTP: &confighttp.HTTPServerSettings{
  70. Endpoint: defaultHTTPBindEndpoint,
  71. },
  72. ThriftCompact: &ProtocolUDP{
  73. Endpoint: defaultThriftCompactBindEndpoint,
  74. ServerConfigUDP: defaultServerConfigUDP(),
  75. },
  76. ThriftBinary: &ProtocolUDP{
  77. Endpoint: defaultThriftBinaryBindEndpoint,
  78. ServerConfigUDP: defaultServerConfigUDP(),
  79. },
  80. },
  81. },
  82. },
  83. {
  84. id: component.NewIDWithName(metadata.Type, "mixed"),
  85. expected: &Config{
  86. Protocols: Protocols{
  87. GRPC: &configgrpc.GRPCServerSettings{
  88. NetAddr: confignet.NetAddr{
  89. Endpoint: "localhost:9876",
  90. Transport: "tcp",
  91. },
  92. },
  93. ThriftCompact: &ProtocolUDP{
  94. Endpoint: defaultThriftCompactBindEndpoint,
  95. ServerConfigUDP: defaultServerConfigUDP(),
  96. },
  97. },
  98. },
  99. },
  100. {
  101. id: component.NewIDWithName(metadata.Type, "tls"),
  102. expected: &Config{
  103. Protocols: Protocols{
  104. GRPC: &configgrpc.GRPCServerSettings{
  105. NetAddr: confignet.NetAddr{
  106. Endpoint: "localhost:9876",
  107. Transport: "tcp",
  108. },
  109. TLSSetting: &configtls.TLSServerSetting{
  110. TLSSetting: configtls.TLSSetting{
  111. CertFile: "/test.crt",
  112. KeyFile: "/test.key",
  113. },
  114. },
  115. },
  116. ThriftHTTP: &confighttp.HTTPServerSettings{
  117. Endpoint: ":3456",
  118. },
  119. },
  120. },
  121. },
  122. }
  123. for _, tt := range tests {
  124. t.Run(tt.id.String(), func(t *testing.T) {
  125. factory := NewFactory()
  126. cfg := factory.CreateDefaultConfig()
  127. sub, err := cm.Sub(tt.id.String())
  128. require.NoError(t, err)
  129. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  130. assert.NoError(t, component.ValidateConfig(cfg))
  131. assert.Equal(t, tt.expected, cfg)
  132. })
  133. }
  134. }
  135. func TestFailedLoadConfig(t *testing.T) {
  136. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  137. require.NoError(t, err)
  138. factory := NewFactory()
  139. cfg := factory.CreateDefaultConfig()
  140. sub, err := cm.Sub(component.NewIDWithName(metadata.Type, "typo_default_proto_config").String())
  141. require.NoError(t, err)
  142. err = component.UnmarshalConfig(sub, cfg)
  143. assert.EqualError(t, err, "1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift_htttp")
  144. sub, err = cm.Sub(component.NewIDWithName(metadata.Type, "bad_proto_config").String())
  145. require.NoError(t, err)
  146. err = component.UnmarshalConfig(sub, cfg)
  147. assert.EqualError(t, err, "1 error(s) decoding:\n\n* 'protocols' has invalid keys: thrift_htttp")
  148. sub, err = cm.Sub(component.NewIDWithName(metadata.Type, "empty").String())
  149. require.NoError(t, err)
  150. err = component.UnmarshalConfig(sub, cfg)
  151. assert.EqualError(t, err, "empty config for Jaeger receiver")
  152. }
  153. func TestInvalidConfig(t *testing.T) {
  154. testCases := []struct {
  155. desc string
  156. apply func(*Config)
  157. err string
  158. }{
  159. {
  160. desc: "thrift-http-no-port",
  161. apply: func(cfg *Config) {
  162. cfg.ThriftHTTP = &confighttp.HTTPServerSettings{
  163. Endpoint: "localhost:",
  164. }
  165. },
  166. err: "receiver creation with no port number for Thrift HTTP must fail",
  167. },
  168. {
  169. desc: "thrift-udp-compact-no-port",
  170. apply: func(cfg *Config) {
  171. cfg.ThriftCompact = &ProtocolUDP{
  172. Endpoint: "localhost:",
  173. }
  174. },
  175. err: "receiver creation with no port number for Thrift UDP - Compact must fail",
  176. },
  177. {
  178. desc: "thrift-udp-binary-no-port",
  179. apply: func(cfg *Config) {
  180. cfg.ThriftBinary = &ProtocolUDP{
  181. Endpoint: "localhost:",
  182. }
  183. },
  184. err: "receiver creation with no port number for Thrift UDP - Binary must fail",
  185. },
  186. {
  187. desc: "grpc-invalid-host",
  188. apply: func(cfg *Config) {
  189. cfg.GRPC = &configgrpc.GRPCServerSettings{
  190. NetAddr: confignet.NetAddr{
  191. Endpoint: "1234",
  192. Transport: "tcp",
  193. },
  194. }
  195. },
  196. err: "receiver creation with bad hostname must fail",
  197. },
  198. {
  199. desc: "no-protocols",
  200. apply: func(cfg *Config) {
  201. cfg.Protocols = Protocols{}
  202. },
  203. err: "receiver creation with no protocols must fail",
  204. },
  205. {
  206. desc: "port-outside-of-range",
  207. apply: func(cfg *Config) {
  208. cfg.ThriftBinary = &ProtocolUDP{
  209. Endpoint: "localhost:65536",
  210. }
  211. },
  212. err: "receiver creation with too large port number must fail",
  213. },
  214. }
  215. for _, tC := range testCases {
  216. t.Run(tC.desc, func(t *testing.T) {
  217. factory := NewFactory()
  218. cfg := factory.CreateDefaultConfig().(*Config)
  219. tC.apply(cfg)
  220. err := component.ValidateConfig(cfg)
  221. assert.Error(t, err, tC.err)
  222. })
  223. }
  224. }