forked from open-telemetry/opentelemetry-collector
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconfigprovider_test.go
More file actions
108 lines (83 loc) · 2.72 KB
/
configprovider_test.go
File metadata and controls
108 lines (83 loc) · 2.72 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
// Copyright The OpenTelemetry Authors
// SPDX-License-Identifier: Apache-2.0
package otelcol
import (
"context"
"os"
"path/filepath"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"gopkg.in/yaml.v3"
"go.opentelemetry.io/collector/confmap"
)
func newConfig(yamlBytes []byte, factories Factories) (*Config, error) {
var stringMap = map[string]interface{}{}
err := yaml.Unmarshal(yamlBytes, stringMap)
if err != nil {
return nil, err
}
conf := confmap.NewFromStringMap(stringMap)
cfg, err := unmarshal(conf, factories)
if err != nil {
return nil, err
}
return &Config{
Receivers: cfg.Receivers.Configs(),
Processors: cfg.Processors.Configs(),
Exporters: cfg.Exporters.Configs(),
Connectors: cfg.Connectors.Configs(),
Extensions: cfg.Extensions.Configs(),
Service: cfg.Service,
}, nil
}
func TestConfigProviderYaml(t *testing.T) {
yamlBytes, err := os.ReadFile(filepath.Join("testdata", "otelcol-nop.yaml"))
require.NoError(t, err)
uriLocation := "yaml:" + string(yamlBytes)
yamlProvider := newFakeProvider("yaml", func(_ context.Context, _ string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) {
var rawConf any
if err = yaml.Unmarshal(yamlBytes, &rawConf); err != nil {
return nil, err
}
return confmap.NewRetrieved(rawConf)
})
set := ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{uriLocation},
ProviderFactories: []confmap.ProviderFactory{yamlProvider},
},
}
cp, err := NewConfigProvider(set)
require.NoError(t, err)
factories, err := nopFactories()
require.NoError(t, err)
cfg, err := cp.Get(context.Background(), factories)
require.NoError(t, err)
configNop, err := newConfig(yamlBytes, factories)
require.NoError(t, err)
assert.EqualValues(t, configNop, cfg)
}
func TestConfigProviderFile(t *testing.T) {
uriLocation := "file:" + filepath.Join("testdata", "otelcol-nop.yaml")
fileProvider := newFakeProvider("file", func(_ context.Context, _ string, _ confmap.WatcherFunc) (*confmap.Retrieved, error) {
return confmap.NewRetrieved(newConfFromFile(t, uriLocation[5:]))
})
set := ConfigProviderSettings{
ResolverSettings: confmap.ResolverSettings{
URIs: []string{uriLocation},
ProviderFactories: []confmap.ProviderFactory{fileProvider},
},
}
cp, err := NewConfigProvider(set)
require.NoError(t, err)
factories, err := nopFactories()
require.NoError(t, err)
cfg, err := cp.Get(context.Background(), factories)
require.NoError(t, err)
yamlBytes, err := os.ReadFile(filepath.Join("testdata", "otelcol-nop.yaml"))
require.NoError(t, err)
configNop, err := newConfig(yamlBytes, factories)
require.NoError(t, err)
assert.EqualValues(t, configNop, cfg)
}