123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142 |
- // Copyright The OpenTelemetry Authors
- // SPDX-License-Identifier: Apache-2.0
- package fileexporter
- import (
- "path/filepath"
- "testing"
- "time"
- "github.com/stretchr/testify/assert"
- "github.com/stretchr/testify/require"
- "go.opentelemetry.io/collector/component"
- "go.opentelemetry.io/collector/confmap/confmaptest"
- "github.com/open-telemetry/opentelemetry-collector-contrib/exporter/fileexporter/internal/metadata"
- )
- func TestLoadConfig(t *testing.T) {
- t.Parallel()
- cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml"))
- require.NoError(t, err)
- tests := []struct {
- id component.ID
- expected component.Config
- errorMessage string
- }{
- {
- id: component.NewIDWithName(metadata.Type, "2"),
- expected: &Config{
- Path: "./filename.json",
- Rotation: &Rotation{
- MaxMegabytes: 10,
- MaxDays: 3,
- MaxBackups: 3,
- LocalTime: true,
- },
- FormatType: formatTypeJSON,
- FlushInterval: time.Second,
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "3"),
- expected: &Config{
- Path: "./filename",
- Rotation: &Rotation{
- MaxMegabytes: 10,
- MaxDays: 3,
- MaxBackups: 3,
- LocalTime: true,
- },
- FormatType: formatTypeProto,
- Compression: compressionZSTD,
- FlushInterval: time.Second,
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "rotation_with_default_settings"),
- expected: &Config{
- Path: "./foo",
- FormatType: formatTypeJSON,
- Rotation: &Rotation{
- MaxBackups: defaultMaxBackups,
- },
- FlushInterval: time.Second,
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "rotation_with_custom_settings"),
- expected: &Config{
- Path: "./foo",
- Rotation: &Rotation{
- MaxMegabytes: 1234,
- MaxBackups: defaultMaxBackups,
- },
- FormatType: formatTypeJSON,
- FlushInterval: time.Second,
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "compression_error"),
- errorMessage: "compression is not supported",
- },
- {
- id: component.NewIDWithName(metadata.Type, "format_error"),
- errorMessage: "format type is not supported",
- },
- {
- id: component.NewIDWithName(metadata.Type, "flush_interval_5"),
- expected: &Config{
- Path: "./flushed",
- FlushInterval: 5,
- FormatType: formatTypeJSON,
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "flush_interval_5s"),
- expected: &Config{
- Path: "./flushed",
- FlushInterval: 5 * time.Second,
- FormatType: formatTypeJSON,
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "flush_interval_500ms"),
- expected: &Config{
- Path: "./flushed",
- FlushInterval: 500 * time.Millisecond,
- FormatType: formatTypeJSON,
- },
- },
- {
- id: component.NewIDWithName(metadata.Type, "flush_interval_negative_value"),
- errorMessage: "flush_interval must be larger than zero",
- },
- {
- id: component.NewIDWithName(metadata.Type, ""),
- errorMessage: "path must be non-empty",
- },
- }
- for _, tt := range tests {
- t.Run(tt.id.String(), func(t *testing.T) {
- factory := NewFactory()
- cfg := factory.CreateDefaultConfig()
- sub, err := cm.Sub(tt.id.String())
- require.NoError(t, err)
- require.NoError(t, component.UnmarshalConfig(sub, cfg))
- if tt.expected == nil {
- assert.EqualError(t, component.ValidateConfig(cfg), tt.errorMessage)
- return
- }
- assert.NoError(t, component.ValidateConfig(cfg))
- assert.Equal(t, tt.expected, cfg)
- })
- }
- }
|