| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package service |
| 5 |
|
| 6 |
import ( |
| 7 |
"errors" |
| 8 |
"testing" |
| 9 |
|
| 10 |
"github.com/stretchr/testify/assert" |
| 11 |
"github.com/stretchr/testify/require" |
| 12 |
|
| 13 |
"go.opentelemetry.io/collector/component" |
| 14 |
"go.opentelemetry.io/collector/confmap" |
| 15 |
"go.opentelemetry.io/collector/confmap/xconfmap" |
| 16 |
"go.opentelemetry.io/collector/pipeline" |
| 17 |
"go.opentelemetry.io/collector/service/extensions" |
| 18 |
"go.opentelemetry.io/collector/service/pipelines" |
| 19 |
) |
| 20 |
|
| 21 |
func TestConfigValidate(t *testing.T) { |
| 22 |
testCases := []struct { |
| 23 |
name string // test case name (also file name containing config yaml) |
| 24 |
cfgFn func() *Config |
| 25 |
expected error |
| 26 |
}{ |
| 27 |
{ |
| 28 |
name: "valid", |
| 29 |
cfgFn: generateConfig, |
| 30 |
expected: nil, |
| 31 |
}, |
| 32 |
{ |
| 33 |
name: "valid-telemetry-config", |
| 34 |
cfgFn: func() *Config { |
| 35 |
cfg := generateConfig() |
| 36 |
cfg.Telemetry = fakeTelemetryConfig{Invalid: false} |
| 37 |
return cfg |
| 38 |
}, |
| 39 |
expected: nil, |
| 40 |
}, |
| 41 |
{ |
| 42 |
name: "duplicate-processor-reference", |
| 43 |
cfgFn: func() *Config { |
| 44 |
cfg := generateConfig() |
| 45 |
pipe := cfg.Pipelines[pipeline.NewID(pipeline.SignalTraces)] |
| 46 |
pipe.Processors = append(pipe.Processors, pipe.Processors...) |
| 47 |
return cfg |
| 48 |
}, |
| 49 |
expected: errors.New(`references processor "nop" multiple times`), |
| 50 |
}, |
| 51 |
{ |
| 52 |
name: "invalid-telemetry-config", |
| 53 |
cfgFn: func() *Config { |
| 54 |
cfg := generateConfig() |
| 55 |
cfg.Telemetry = fakeTelemetryConfig{Invalid: true} |
| 56 |
return cfg |
| 57 |
}, |
| 58 |
expected: errors.New("telemetry: invalid config"), |
| 59 |
}, |
| 60 |
} |
| 61 |
|
| 62 |
for _, tt := range testCases { |
| 63 |
t.Run(tt.name, func(t *testing.T) { |
| 64 |
cfg := tt.cfgFn() |
| 65 |
err := xconfmap.Validate(cfg) |
| 66 |
if tt.expected != nil { |
| 67 |
assert.ErrorContains(t, err, tt.expected.Error()) |
| 68 |
} else { |
| 69 |
assert.NoError(t, err) |
| 70 |
} |
| 71 |
}) |
| 72 |
} |
| 73 |
} |
| 74 |
|
| 75 |
func TestConfmapMarshalConfig(t *testing.T) { |
| 76 |
conf := confmap.New() |
| 77 |
|
| 78 |
require.NoError(t, conf.Marshal(Config{ |
| 79 |
Telemetry: fakeTelemetryConfig{}, |
| 80 |
})) |
| 81 |
assert.Equal(t, map[string]any{ |
| 82 |
"pipelines": map[string]any(nil), |
| 83 |
"telemetry": map[string]any{"invalid": false}, |
| 84 |
}, conf.ToStringMap()) |
| 85 |
} |
| 86 |
|
| 87 |
func generateConfig() *Config { |
| 88 |
return &Config{ |
| 89 |
Extensions: extensions.Config{component.MustNewID("nop")}, |
| 90 |
Pipelines: pipelines.Config{ |
| 91 |
pipeline.NewID(pipeline.SignalTraces): { |
| 92 |
Receivers: []component.ID{component.MustNewID("nop")}, |
| 93 |
Processors: []component.ID{component.MustNewID("nop")}, |
| 94 |
Exporters: []component.ID{component.MustNewID("nop")}, |
| 95 |
}, |
| 96 |
}, |
| 97 |
} |
| 98 |
} |
| 99 |
|
| 100 |
type fakeTelemetryConfig struct { |
| 101 |
Invalid bool `mapstructure:"invalid"` |
| 102 |
} |
| 103 |
|
| 104 |
func (cfg fakeTelemetryConfig) Validate() error { |
| 105 |
if cfg.Invalid { |
| 106 |
return errors.New("invalid config") |
| 107 |
} |
| 108 |
return nil |
| 109 |
} |