|
| 1 | +// Copyright The OpenTelemetry Authors |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | + |
| 4 | +package metricstarttimeprocessor |
| 5 | + |
| 6 | +import ( |
| 7 | + "path/filepath" |
| 8 | + "testing" |
| 9 | + "time" |
| 10 | + |
| 11 | + "github.com/stretchr/testify/assert" |
| 12 | + "github.com/stretchr/testify/require" |
| 13 | + "go.opentelemetry.io/collector/component" |
| 14 | + "go.opentelemetry.io/collector/confmap/confmaptest" |
| 15 | + "go.opentelemetry.io/collector/confmap/xconfmap" |
| 16 | + |
| 17 | + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/metricstarttimeprocessor/internal/metadata" |
| 18 | + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/metricstarttimeprocessor/internal/subtractinitial" |
| 19 | + "github.com/open-telemetry/opentelemetry-collector-contrib/processor/metricstarttimeprocessor/internal/truereset" |
| 20 | +) |
| 21 | + |
| 22 | +func TestLoadConfig(t *testing.T) { |
| 23 | + t.Parallel() |
| 24 | + |
| 25 | + tests := []struct { |
| 26 | + id component.ID |
| 27 | + expected component.Config |
| 28 | + errorMessage string |
| 29 | + }{ |
| 30 | + { |
| 31 | + id: component.NewIDWithName(metadata.Type, ""), |
| 32 | + expected: &Config{ |
| 33 | + Strategy: truereset.Type, |
| 34 | + GCInterval: 10 * time.Minute, |
| 35 | + }, |
| 36 | + }, |
| 37 | + { |
| 38 | + id: component.NewIDWithName(metadata.Type, "subtract_initial_point"), |
| 39 | + expected: &Config{ |
| 40 | + Strategy: subtractinitial.Type, |
| 41 | + GCInterval: 10 * time.Minute, |
| 42 | + }, |
| 43 | + }, |
| 44 | + { |
| 45 | + id: component.NewIDWithName(metadata.Type, "gc_interval"), |
| 46 | + expected: &Config{ |
| 47 | + Strategy: truereset.Type, |
| 48 | + GCInterval: 1 * time.Hour, |
| 49 | + }, |
| 50 | + }, |
| 51 | + { |
| 52 | + id: component.NewIDWithName(metadata.Type, "negative_interval"), |
| 53 | + errorMessage: "gc_interval must be positive", |
| 54 | + }, |
| 55 | + { |
| 56 | + id: component.NewIDWithName(metadata.Type, "invalid_strategy"), |
| 57 | + errorMessage: "\"bad\" is not a valid strategy", |
| 58 | + }, |
| 59 | + } |
| 60 | + |
| 61 | + for _, tt := range tests { |
| 62 | + t.Run(tt.id.String(), func(t *testing.T) { |
| 63 | + cm, err := confmaptest.LoadConf(filepath.Join("testdata", "config.yaml")) |
| 64 | + require.NoError(t, err) |
| 65 | + |
| 66 | + factory := NewFactory() |
| 67 | + cfg := factory.CreateDefaultConfig() |
| 68 | + sub, err := cm.Sub(tt.id.String()) |
| 69 | + require.NoError(t, err) |
| 70 | + require.NoError(t, sub.Unmarshal(cfg)) |
| 71 | + |
| 72 | + if tt.expected == nil { |
| 73 | + assert.EqualError(t, xconfmap.Validate(cfg), tt.errorMessage) |
| 74 | + return |
| 75 | + } |
| 76 | + assert.NoError(t, xconfmap.Validate(cfg)) |
| 77 | + assert.Equal(t, tt.expected, cfg) |
| 78 | + }) |
| 79 | + } |
| 80 | +} |
0 commit comments