config_test.go 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  1. // Copyright The OpenTelemetry Authors
  2. // SPDX-License-Identifier: Apache-2.0
  3. package fileexporter
  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/confmap/confmaptest"
  12. "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/fileexporter/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. errorMessage string
  22. }{
  23. {
  24. id: component.NewIDWithName(metadata.Type, "2"),
  25. expected: &Config{
  26. Path: "./filename.json",
  27. Rotation: &Rotation{
  28. MaxMegabytes: 10,
  29. MaxDays: 3,
  30. MaxBackups: 3,
  31. LocalTime: true,
  32. },
  33. FormatType: formatTypeJSON,
  34. FlushInterval: time.Second,
  35. },
  36. },
  37. {
  38. id: component.NewIDWithName(metadata.Type, "3"),
  39. expected: &Config{
  40. Path: "./filename",
  41. Rotation: &Rotation{
  42. MaxMegabytes: 10,
  43. MaxDays: 3,
  44. MaxBackups: 3,
  45. LocalTime: true,
  46. },
  47. FormatType: formatTypeProto,
  48. Compression: compressionZSTD,
  49. FlushInterval: time.Second,
  50. },
  51. },
  52. {
  53. id: component.NewIDWithName(metadata.Type, "rotation_with_default_settings"),
  54. expected: &Config{
  55. Path: "./foo",
  56. FormatType: formatTypeJSON,
  57. Rotation: &Rotation{
  58. MaxBackups: defaultMaxBackups,
  59. },
  60. FlushInterval: time.Second,
  61. },
  62. },
  63. {
  64. id: component.NewIDWithName(metadata.Type, "rotation_with_custom_settings"),
  65. expected: &Config{
  66. Path: "./foo",
  67. Rotation: &Rotation{
  68. MaxMegabytes: 1234,
  69. MaxBackups: defaultMaxBackups,
  70. },
  71. FormatType: formatTypeJSON,
  72. FlushInterval: time.Second,
  73. },
  74. },
  75. {
  76. id: component.NewIDWithName(metadata.Type, "compression_error"),
  77. errorMessage: "compression is not supported",
  78. },
  79. {
  80. id: component.NewIDWithName(metadata.Type, "format_error"),
  81. errorMessage: "format type is not supported",
  82. },
  83. {
  84. id: component.NewIDWithName(metadata.Type, "flush_interval_5"),
  85. expected: &Config{
  86. Path: "./flushed",
  87. FlushInterval: 5,
  88. FormatType: formatTypeJSON,
  89. },
  90. },
  91. {
  92. id: component.NewIDWithName(metadata.Type, "flush_interval_5s"),
  93. expected: &Config{
  94. Path: "./flushed",
  95. FlushInterval: 5 * time.Second,
  96. FormatType: formatTypeJSON,
  97. },
  98. },
  99. {
  100. id: component.NewIDWithName(metadata.Type, "flush_interval_500ms"),
  101. expected: &Config{
  102. Path: "./flushed",
  103. FlushInterval: 500 * time.Millisecond,
  104. FormatType: formatTypeJSON,
  105. },
  106. },
  107. {
  108. id: component.NewIDWithName(metadata.Type, "flush_interval_negative_value"),
  109. errorMessage: "flush_interval must be larger than zero",
  110. },
  111. {
  112. id: component.NewIDWithName(metadata.Type, ""),
  113. errorMessage: "path must be non-empty",
  114. },
  115. }
  116. for _, tt := range tests {
  117. t.Run(tt.id.String(), func(t *testing.T) {
  118. factory := NewFactory()
  119. cfg := factory.CreateDefaultConfig()
  120. sub, err := cm.Sub(tt.id.String())
  121. require.NoError(t, err)
  122. require.NoError(t, component.UnmarshalConfig(sub, cfg))
  123. if tt.expected == nil {
  124. assert.EqualError(t, component.ValidateConfig(cfg), tt.errorMessage)
  125. return
  126. }
  127. assert.NoError(t, component.ValidateConfig(cfg))
  128. assert.Equal(t, tt.expected, cfg)
  129. })
  130. }
  131. }