Files
aac1a8f39c [confmap] Stabilize confmap validation facilities (#15142)
<!--Ex. Fixing a bug - Describe the bug and how this fixes the issue.
Ex. Adding a feature - Explain what this achieves.-->
#### Description

The `xconfmap.Validator` interface has existed in some form for a very
long time now and is ready to be stabilized. Similarly, the
`xconfmap.Validate` method has existed in it's current state for over a
year and doesn't have any open issues I'm aware of. It should also be
ready for stabilization.

Many modules depend on `xconfmap` merely for the validation facilities
(as can be seen by the go.mod diffs), so this will allow them to depend
on only stable modules.

---------

Co-authored-by: Alex Boten <[email protected]>
2026-05-08 13:33:15 +00:00

136 lines
3.8 KiB
Go

// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package pipelines
import (
"errors"
"testing"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/featuregate"
"go.opentelemetry.io/collector/pipeline"
"go.opentelemetry.io/collector/pipeline/xpipeline"
"go.opentelemetry.io/collector/service/internal/metadata"
)
func TestConfigValidate(t *testing.T) {
testCases := []struct {
name string // test case name (also file name containing config yaml)
cfgFn func(*testing.T) Config
expected error
}{
{
name: "valid",
cfgFn: generateConfig,
expected: nil,
},
{
name: "duplicate-processor-reference",
cfgFn: func(*testing.T) Config {
cfg := generateConfig(t)
pipe := cfg[pipeline.NewID(pipeline.SignalTraces)]
pipe.Processors = append(pipe.Processors, pipe.Processors...)
return cfg
},
expected: errors.New(`references processor "nop" multiple times`),
},
{
name: "missing-pipeline-receivers",
cfgFn: func(*testing.T) Config {
cfg := generateConfig(t)
cfg[pipeline.NewID(pipeline.SignalTraces)].Receivers = nil
return cfg
},
expected: errMissingServicePipelineReceivers,
},
{
name: "missing-pipeline-exporters",
cfgFn: func(*testing.T) Config {
cfg := generateConfig(t)
cfg[pipeline.NewID(pipeline.SignalTraces)].Exporters = nil
return cfg
},
expected: errMissingServicePipelineExporters,
},
{
name: "missing-pipelines",
cfgFn: func(*testing.T) Config {
return nil
},
expected: errMissingServicePipelines,
},
{
name: "disabled-featuregate-profiles",
cfgFn: func(*testing.T) Config {
cfg := generateConfig(t)
cfg[pipeline.NewID(xpipeline.SignalProfiles)] = &PipelineConfig{
Receivers: []component.ID{component.MustNewID("nop")},
Processors: []component.ID{component.MustNewID("nop")},
Exporters: []component.ID{component.MustNewID("nop")},
}
return cfg
},
expected: errors.New(`profiling signal support is at alpha level, gated under the "service.profilesSupport" feature gate`),
},
{
name: "enabled-featuregate-profiles",
cfgFn: func(t *testing.T) Config {
require.NoError(t, featuregate.GlobalRegistry().Set(metadata.ServiceProfilesSupportFeatureGate.ID(), true))
cfg := generateConfig(t)
cfg[pipeline.NewID(xpipeline.SignalProfiles)] = &PipelineConfig{
Receivers: []component.ID{component.MustNewID("nop")},
Processors: []component.ID{component.MustNewID("nop")},
Exporters: []component.ID{component.MustNewID("nop")},
}
return cfg
},
expected: nil,
},
}
for _, tt := range testCases {
t.Run(tt.name, func(t *testing.T) {
cfg := tt.cfgFn(t)
if tt.expected != nil {
require.ErrorContains(t, confmap.Validate(cfg), tt.expected.Error())
} else {
require.NoError(t, confmap.Validate(cfg))
}
// Clean up the profiles support gate, which may have been enabled in `cfgFn`.
require.NoError(t, featuregate.GlobalRegistry().Set(metadata.ServiceProfilesSupportFeatureGate.ID(), false))
})
}
}
func TestNoPipelinesFeatureGate(t *testing.T) {
cfg := Config{}
require.Error(t, confmap.Validate(cfg))
gate := AllowNoPipelines
require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), true))
defer func() {
require.NoError(t, featuregate.GlobalRegistry().Set(gate.ID(), false))
}()
require.NoError(t, confmap.Validate(cfg))
}
func generateConfig(t *testing.T) Config {
t.Helper()
return map[pipeline.ID]*PipelineConfig{
pipeline.NewID(pipeline.SignalTraces): {
Receivers: []component.ID{component.MustNewID("nop")},
Processors: []component.ID{component.MustNewID("nop")},
Exporters: []component.ID{component.MustNewID("nop")},
},
}
}