config_test.go 2.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package honeycombmarkerexporter
  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/confmap/confmaptest"
  11. "go.opentelemetry.io/collector/exporter/exporterhelper"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/honeycombmarkerexporter/internal/metadata"
  13. )
  14. func TestLoadConfig(t *testing.T) {
  15. t.Parallel()
  16. cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
  17. require.NoError(t, err)
  18. tests := []struct {
  19. id component.ID
  20. expected component.Config
  21. }{
  22. {
  23. id: component.NewIDWithName(metadata.Type, ""),
  24. expected: &Config{
  25. APIKey: "test-apikey",
  26. APIURL: "https://api.honeycomb.io",
  27. Markers: []Marker{
  28. {
  29. Type: "fooType",
  30. Rules: Rules{
  31. LogConditions: []string{
  32. `body == "test"`,
  33. },
  34. },
  35. },
  36. },
  37. },
  38. },
  39. {
  40. id: component.NewIDWithName(metadata.Type, "all_fields"),
  41. expected: &Config{
  42. QueueSettings: exporterhelper.NewDefaultQueueSettings(),
  43. RetrySettings: exporterhelper.NewDefaultRetrySettings(),
  44. APIKey: "test-apikey",
  45. APIURL: "https://api.testhost.io",
  46. Markers: []Marker{
  47. {
  48. Type: "fooType",
  49. MessageKey: "test message",
  50. URLKey: "https://api.testhost.io",
  51. Rules: Rules{
  52. LogConditions: []string{
  53. `body == "test"`,
  54. },
  55. },
  56. DatasetSlug: "testing",
  57. },
  58. },
  59. },
  60. },
  61. {
  62. id: component.NewIDWithName(metadata.Type, "bad_syntax_log"),
  63. },
  64. {
  65. id: component.NewIDWithName(metadata.Type, "no_conditions"),
  66. },
  67. {
  68. id: component.NewIDWithName(metadata.Type, "no_api_key"),
  69. },
  70. {
  71. id: component.NewIDWithName(metadata.Type, "no_markers_supplied"),
  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. if tt.expected == nil {
  82. err = component.ValidateConfig(cfg)
  83. assert.Error(t, err)
  84. return
  85. }
  86. assert.NoError(t, component.ValidateConfig(cfg))
  87. assert.Equal(t, tt.expected, cfg)
  88. })
  89. }
  90. }
  91. func withDefaultConfig(fns ...func(*Config)) *Config {
  92. cfg := createDefaultConfig().(*Config)
  93. for _, fn := range fns {
  94. fn(cfg)
  95. }
  96. return cfg
  97. }