config_test.go 2.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package mezmoexporter
  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/confmap/confmaptest"
  14. "go.opentelemetry.io/collector/exporter/exporterhelper"
  15. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/mezmoexporter/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. defaultCfg := createDefaultConfig().(*Config)
  22. defaultCfg.IngestURL = defaultIngestURL
  23. defaultCfg.IngestKey = "00000000000000000000000000000000"
  24. tests := []struct {
  25. id component.ID
  26. expected component.Config
  27. }{
  28. {
  29. id: component.NewIDWithName(metadata.Type, ""),
  30. expected: defaultCfg,
  31. },
  32. {
  33. id: component.NewIDWithName(metadata.Type, "allsettings"),
  34. expected: &Config{
  35. HTTPClientSettings: confighttp.HTTPClientSettings{
  36. Timeout: 5 * time.Second,
  37. },
  38. RetrySettings: exporterhelper.RetrySettings{
  39. Enabled: false,
  40. InitialInterval: 99 * time.Second,
  41. MaxInterval: 199 * time.Second,
  42. MaxElapsedTime: 299 * time.Minute,
  43. RandomizationFactor: backoff.DefaultRandomizationFactor,
  44. Multiplier: backoff.DefaultMultiplier,
  45. },
  46. QueueSettings: exporterhelper.QueueSettings{
  47. Enabled: false,
  48. NumConsumers: 7,
  49. QueueSize: 17,
  50. },
  51. IngestURL: "https://alternate.mezmo.com/otel/ingest/rest",
  52. IngestKey: "1234509876",
  53. },
  54. },
  55. }
  56. for _, tt := range tests {
  57. t.Run(tt.id.String(), func(t *testing.T) {
  58. factory := NewFactory()
  59. cfg := factory.CreateDefaultConfig()
  60. sub, err := cm.Sub(tt.id.String())
  61. require.NoError(t, err)
  62. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  63. assert.NoError(t, component.ValidateConfig(cfg))
  64. assert.Equal(t, tt.expected, cfg)
  65. })
  66. }
  67. }
  68. func TestConfigInvalidEndpoint(t *testing.T) {
  69. cfg := createDefaultConfig().(*Config)
  70. cfg.IngestURL = "urn:something:12345"
  71. assert.Error(t, cfg.Validate())
  72. }
  73. func TestConfig_Validate_Path(t *testing.T) {
  74. factory := NewFactory()
  75. cfg := factory.CreateDefaultConfig().(*Config)
  76. cfg.IngestURL = "https://example.com:8088/ingest/rest"
  77. cfg.IngestKey = "1234-1234"
  78. assert.NoError(t, cfg.Validate())
  79. cfg.IngestURL = "https://example.com:8088/v1/ABC123"
  80. cfg.IngestKey = "1234-1234"
  81. assert.NoError(t, cfg.Validate())
  82. // Set values that don't have a valid default.
  83. cfg.IngestURL = "/nohost/path"
  84. cfg.IngestKey = "testToken"
  85. assert.Error(t, cfg.Validate())
  86. }