| 1 |
// Copyright The OpenTelemetry Authors |
| 2 |
// SPDX-License-Identifier: Apache-2.0 |
| 3 |
|
| 4 |
package otelcol |
| 5 |
|
| 6 |
import ( |
| 7 |
"context" |
| 8 |
"os" |
| 9 |
"path/filepath" |
| 10 |
"testing" |
| 11 |
|
| 12 |
"github.com/stretchr/testify/assert" |
| 13 |
"github.com/stretchr/testify/require" |
| 14 |
yaml "go.yaml.in/yaml/v3" |
| 15 |
|
| 16 |
"go.opentelemetry.io/collector/component" |
| 17 |
"go.opentelemetry.io/collector/confmap" |
| 18 |
"go.opentelemetry.io/collector/connector/connectortest" |
| 19 |
"go.opentelemetry.io/collector/exporter/exportertest" |
| 20 |
"go.opentelemetry.io/collector/extension/extensiontest" |
| 21 |
"go.opentelemetry.io/collector/pipeline" |
| 22 |
"go.opentelemetry.io/collector/processor/processortest" |
| 23 |
"go.opentelemetry.io/collector/receiver/receivertest" |
| 24 |
"go.opentelemetry.io/collector/service" |
| 25 |
"go.opentelemetry.io/collector/service/pipelines" |
| 26 |
) |
| 27 |
|
| 28 |
func TestConfigProvider(t *testing.T) { |
| 29 |
nopComponentID := component.MustNewID("nop") |
| 30 |
nopConComponentID := component.MustNewIDWithName("nop", "con") |
| 31 |
|
| 32 |
tests := map[string]struct { |
| 33 |
filename string |
| 34 |
factories func() (Factories, error) |
| 35 |
expectedConfig *Config |
| 36 |
}{ |
| 37 |
"nop": { |
| 38 |
filename: "otelcol-nop.yaml", |
| 39 |
factories: nopFactories, |
| 40 |
expectedConfig: &Config{ |
| 41 |
Connectors: map[component.ID]component.Config{ |
| 42 |
nopConComponentID: connectortest.NewNopFactory().CreateDefaultConfig(), |
| 43 |
}, |
| 44 |
Exporters: map[component.ID]component.Config{ |
| 45 |
nopComponentID: exportertest.NewNopFactory().CreateDefaultConfig(), |
| 46 |
}, |
| 47 |
Extensions: map[component.ID]component.Config{ |
| 48 |
nopComponentID: extensiontest.NewNopFactory().CreateDefaultConfig(), |
| 49 |
}, |
| 50 |
Processors: map[component.ID]component.Config{ |
| 51 |
nopComponentID: processortest.NewNopFactory().CreateDefaultConfig(), |
| 52 |
}, |
| 53 |
Receivers: map[component.ID]component.Config{ |
| 54 |
nopComponentID: receivertest.NewNopFactory().CreateDefaultConfig(), |
| 55 |
}, |
| 56 |
Service: service.Config{ |
| 57 |
Telemetry: fakeTelemetryConfig{}, |
| 58 |
Extensions: []component.ID{nopComponentID}, |
| 59 |
Pipelines: map[pipeline.ID]*pipelines.PipelineConfig{ |
| 60 |
pipeline.NewID(pipeline.SignalTraces): { |
| 61 |
Receivers: []component.ID{nopComponentID}, |
| 62 |
Processors: []component.ID{nopComponentID}, |
| 63 |
Exporters: []component.ID{nopComponentID, nopConComponentID}, |
| 64 |
}, |
| 65 |
pipeline.NewID(pipeline.SignalMetrics): { |
| 66 |
Receivers: []component.ID{nopComponentID}, |
| 67 |
Processors: []component.ID{nopComponentID}, |
| 68 |
Exporters: []component.ID{nopComponentID}, |
| 69 |
}, |
| 70 |
pipeline.NewID(pipeline.SignalLogs): { |
| 71 |
Receivers: []component.ID{nopComponentID, nopConComponentID}, |
| 72 |
Processors: []component.ID{nopComponentID}, |
| 73 |
Exporters: []component.ID{nopComponentID}, |
| 74 |
}, |
| 75 |
}, |
| 76 |
}, |
| 77 |
}, |
| 78 |
}, |
| 79 |
} |
| 80 |
|
| 81 |
for name, test := range tests { |
| 82 |
t.Run(name, func(t *testing.T) { |
| 83 |
yamlBytes, err := os.ReadFile(filepath.Join("testdata", test.filename)) |
| 84 |
require.NoError(t, err) |
| 85 |
|
| 86 |
yamlProvider := newFakeProvider("yaml", func(context.Context, string, confmap.WatcherFunc) (*confmap.Retrieved, error) { |
| 87 |
var rawConf any |
| 88 |
if yamlErr := yaml.Unmarshal(yamlBytes, &rawConf); yamlErr != nil { |
| 89 |
return nil, yamlErr |
| 90 |
} |
| 91 |
return confmap.NewRetrieved(rawConf) |
| 92 |
}) |
| 93 |
|
| 94 |
set := ConfigProviderSettings{ |
| 95 |
ResolverSettings: confmap.ResolverSettings{ |
| 96 |
URIs: []string{"yaml:" + string(yamlBytes)}, |
| 97 |
ProviderFactories: []confmap.ProviderFactory{yamlProvider}, |
| 98 |
}, |
| 99 |
} |
| 100 |
|
| 101 |
cp, err := NewConfigProvider(set) |
| 102 |
require.NoError(t, err) |
| 103 |
|
| 104 |
factories, err := test.factories() |
| 105 |
require.NoError(t, err) |
| 106 |
|
| 107 |
cfg, err := cp.Get(context.Background(), factories) |
| 108 |
require.NoError(t, err) |
| 109 |
|
| 110 |
assert.Equal(t, test.expectedConfig, cfg) |
| 111 |
}) |
| 112 |
} |
| 113 |
} |