config_test.go 2.3 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package carbonreceiver
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/stretchr/testify/assert"
  9. "github.com/stretchr/testify/require"
  10. "go.opentelemetry.io/collector/component"
  11. "go.opentelemetry.io/collector/config/confignet"
  12. "go.opentelemetry.io/collector/confmap/confmaptest"
  13. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver/internal/metadata"
  14. "github.com/open-telemetry/opentelemetry-collector-contrib/receiver/carbonreceiver/protocol"
  15. )
  16. func TestLoadConfig(t *testing.T) {
  17. t.Parallel()
  18. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  19. require.NoError(t, err)
  20. tests := []struct {
  21. id component.ID
  22. expected component.Config
  23. }{
  24. {
  25. id: component.NewIDWithName(metadata.Type, ""),
  26. expected: createDefaultConfig(),
  27. },
  28. {
  29. id: component.NewIDWithName(metadata.Type, "receiver_settings"),
  30. expected: &Config{
  31. NetAddr: confignet.NetAddr{
  32. Endpoint: "localhost:8080",
  33. Transport: "udp",
  34. },
  35. TCPIdleTimeout: 5 * time.Second,
  36. Parser: &protocol.Config{
  37. Type: "plaintext",
  38. Config: &protocol.PlaintextConfig{},
  39. },
  40. },
  41. },
  42. {
  43. id: component.NewIDWithName(metadata.Type, "regex"),
  44. expected: &Config{
  45. NetAddr: confignet.NetAddr{
  46. Endpoint: "localhost:2003",
  47. Transport: "tcp",
  48. },
  49. TCPIdleTimeout: 30 * time.Second,
  50. Parser: &protocol.Config{
  51. Type: "regex",
  52. Config: &protocol.RegexParserConfig{
  53. Rules: []*protocol.RegexRule{
  54. {
  55. Regexp: `(?P<key_base>test)\.env(?P<key_env>[^.]*)\.(?P<key_host>[^.]*)`,
  56. NamePrefix: "name-prefix",
  57. Labels: map[string]string{
  58. "dot.key": "dot.value",
  59. "key": "value",
  60. },
  61. MetricType: "cumulative",
  62. },
  63. {
  64. Regexp: `(?P<key_just>test)\.(?P<key_match>.*)`,
  65. },
  66. },
  67. MetricNameSeparator: "_",
  68. },
  69. },
  70. },
  71. },
  72. }
  73. for _, tt := range tests {
  74. t.Run(tt.id.String(), func(t *testing.T) {
  75. factory := NewFactory()
  76. cfg := factory.CreateDefaultConfig()
  77. sub, err := cm.Sub(tt.id.String())
  78. require.NoError(t, err)
  79. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  80. assert.NoError(t, component.ValidateConfig(cfg))
  81. assert.Equal(t, tt.expected, cfg)
  82. })
  83. }
  84. }