config_test.go 2.7 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package alertmanagerexporter
  4. import (
  5. "path/filepath"
  6. "testing"
  7. "time"
  8. "github.com/cenkalti/backoff/v4"
  9. "github.com/stretchr/testify/assert"
  10. "github.com/stretchr/testify/require"
  11. "go.opentelemetry.io/collector/component"
  12. "go.opentelemetry.io/collector/config/confighttp"
  13. "go.opentelemetry.io/collector/config/configopaque"
  14. "go.opentelemetry.io/collector/config/configtls"
  15. "go.opentelemetry.io/collector/confmap/confmaptest"
  16. "go.opentelemetry.io/collector/exporter/exporterhelper"
  17. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/alertmanagerexporter/internal/metadata"
  18. )
  19. func TestLoadConfig(t *testing.T) {
  20. t.Parallel()
  21. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  22. require.NoError(t, err)
  23. // Endpoint doesn't have a default value so set it directly.
  24. defaultCfg := createDefaultConfig().(*Config)
  25. tests := []struct {
  26. id component.ID
  27. expected component.Config
  28. }{
  29. {
  30. id: component.NewIDWithName(metadata.Type, ""),
  31. expected: defaultCfg,
  32. },
  33. {
  34. id: component.NewIDWithName(metadata.Type, "2"),
  35. expected: &Config{
  36. GeneratorURL: "opentelemetry-collector",
  37. DefaultSeverity: "info",
  38. SeverityAttribute: "foo",
  39. TimeoutSettings: exporterhelper.TimeoutSettings{
  40. Timeout: 10 * time.Second,
  41. },
  42. RetrySettings: exporterhelper.RetrySettings{
  43. Enabled: true,
  44. InitialInterval: 10 * time.Second,
  45. MaxInterval: 1 * time.Minute,
  46. MaxElapsedTime: 10 * time.Minute,
  47. RandomizationFactor: backoff.DefaultRandomizationFactor,
  48. Multiplier: backoff.DefaultMultiplier,
  49. },
  50. QueueSettings: exporterhelper.QueueSettings{
  51. Enabled: true,
  52. NumConsumers: 2,
  53. QueueSize: 10,
  54. },
  55. HTTPClientSettings: confighttp.HTTPClientSettings{
  56. Headers: map[string]configopaque.String{
  57. "can you have a . here?": "F0000000-0000-0000-0000-000000000000",
  58. "header1": "234",
  59. "another": "somevalue",
  60. },
  61. Endpoint: "a.new.alertmanager.target:9093",
  62. TLSSetting: configtls.TLSClientSetting{
  63. TLSSetting: configtls.TLSSetting{
  64. CAFile: "/var/lib/mycert.pem",
  65. },
  66. },
  67. ReadBufferSize: 0,
  68. WriteBufferSize: 524288,
  69. Timeout: time.Second * 10,
  70. },
  71. },
  72. },
  73. }
  74. for _, tt := range tests {
  75. t.Run(tt.id.String(), func(t *testing.T) {
  76. factory := NewFactory()
  77. cfg := factory.CreateDefaultConfig()
  78. sub, err := cm.Sub(tt.id.String())
  79. require.NoError(t, err)
  80. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  81. assert.NoError(t, component.ValidateConfig(cfg))
  82. assert.Equal(t, tt.expected, cfg)
  83. })
  84. }
  85. }