/
provider_test.go
298 lines (252 loc) · 7.63 KB
/
provider_test.go
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
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
// Copyright Splunk, Inc.
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package configsource
import (
"context"
"errors"
"fmt"
"path"
"testing"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/mock"
"github.com/stretchr/testify/require"
"go.opentelemetry.io/collector/component"
"go.opentelemetry.io/collector/confmap"
"go.opentelemetry.io/collector/confmap/provider/fileprovider"
"go.uber.org/zap"
"github.com/signalfx/splunk-otel-collector/internal/configsource"
)
func TestConfigSourceConfigMapProvider(t *testing.T) {
tests := []struct {
parserProvider confmap.Provider
factories configsource.Factories
wantErr string
name string
uris []string
}{
{
name: "success",
},
{
name: "wrapped_parser_provider_get_error",
parserProvider: &mockParserProvider{
ErrOnGet: true,
},
wantErr: "mockParserProvider.Get() forced test error",
},
{
name: "new_manager_builder_error",
factories: configsource.Factories{
component.MustNewType("tstcfgsrc"): &MockCfgSrcFactory{
ErrOnCreateConfigSource: errors.New("new_manager_builder_error forced error"),
},
},
parserProvider: fileprovider.NewWithSettings(confmap.ProviderSettings{}),
uris: []string{"file:" + path.Join("testdata", "basic_config.yaml")},
wantErr: "failed to create config source tstcfgsrc",
},
{
name: "manager_resolve_error",
parserProvider: fileprovider.NewWithSettings(confmap.ProviderSettings{}),
uris: []string{"file:" + path.Join("testdata", "manager_resolve_error.yaml")},
wantErr: "config source \"tstcfgsrc\" failed to retrieve value: no value for selector \"selector\"",
},
{
name: "multiple_config_success",
parserProvider: fileprovider.NewWithSettings(confmap.ProviderSettings{}),
uris: []string{"file:" + path.Join("testdata", "arrays_and_maps_expected.yaml"),
"file:" + path.Join("testdata", "yaml_injection_expected.yaml")},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.factories == nil {
tt.factories = configsource.Factories{
component.MustNewType("tstcfgsrc"): &MockCfgSrcFactory{},
}
}
hookOne := &mockHook{}
hookTwo := &mockHook{}
hooks := []*mockHook{hookOne, hookTwo}
for _, h := range hooks {
h.On("OnNew")
h.On("OnRetrieve", mock.AnythingOfType("string"), mock.Anything)
h.On("OnShutdown")
}
var provider confmap.Provider
if tt.parserProvider == nil {
provider = &mockParserProvider{}
} else {
provider = tt.parserProvider
}
p := New(zap.NewNop(), []Hook{hookOne, hookTwo})
require.NotNil(t, p)
p.(*providerWrapper).factories = tt.factories
pp := p.Wrap(provider)
for _, h := range hooks {
h.AssertCalled(t, "OnNew")
h.AssertNotCalled(t, "OnRetrieve")
h.AssertNotCalled(t, "OnShutdown")
}
expectedScheme := provider.Scheme()
i := 0
for ok := true; ok; {
var uri string
if tt.uris != nil {
uri = tt.uris[i]
} else {
uri = fmt.Sprintf("%s:", provider.Scheme())
}
r, err := pp.Retrieve(context.Background(), uri, nil)
if tt.wantErr == "" {
require.NoError(t, err)
require.NotNil(t, r)
rMap, errAsConf := r.AsConf()
require.NoError(t, errAsConf)
assert.NotNil(t, rMap)
assert.NoError(t, r.Close(context.Background()))
} else {
assert.ErrorContains(t, err, tt.wantErr)
assert.Nil(t, r)
break
}
i++
ok = i < len(tt.uris)
}
for _, h := range hooks {
if tt.wantErr != "" {
h.AssertNotCalled(t, "OnRetrieve")
} else {
h.AssertCalled(t, "OnRetrieve", expectedScheme, mock.Anything)
}
h.AssertNotCalled(t, "OnShutdown")
}
assert.NoError(t, pp.Shutdown(context.Background()))
for _, h := range hooks {
h.AssertCalled(t, "OnShutdown")
}
})
}
}
type mockParserProvider struct {
ErrOnGet bool
}
var _ confmap.Provider = (*mockParserProvider)(nil)
func (mpp *mockParserProvider) Retrieve(context.Context, string, confmap.WatcherFunc) (*confmap.Retrieved, error) {
if mpp.ErrOnGet {
return nil, errors.New("mockParserProvider.Get() forced test error")
}
return confmap.NewRetrieved(confmap.New().ToStringMap())
}
func (mpp *mockParserProvider) Shutdown(context.Context) error {
return nil
}
func (mpp *mockParserProvider) Scheme() string {
return "mock"
}
type mockHook struct {
mock.Mock
}
var _ Hook = (*mockHook)(nil)
func (m *mockHook) OnNew() {
m.Called()
}
func (m *mockHook) OnRetrieve(scheme string, _ map[string]any) {
m.Called(scheme, mock.Anything)
}
func (m *mockHook) OnShutdown() {
m.Called()
}
type MockCfgSrcFactory struct {
ErrOnCreateConfigSource error
}
type MockCfgSrcSettings struct {
configsource.SourceSettings
Endpoint string `mapstructure:"endpoint"`
Token string `mapstructure:"token"`
}
var _ configsource.Settings = (*MockCfgSrcSettings)(nil)
var _ configsource.Factory = (*MockCfgSrcFactory)(nil)
func (m *MockCfgSrcFactory) Type() component.Type {
return component.MustNewType("tstcfgsrc")
}
func (m *MockCfgSrcFactory) CreateDefaultConfig() configsource.Settings {
return &MockCfgSrcSettings{
SourceSettings: configsource.NewSourceSettings(component.MustNewID("tstcfgsrc")),
Endpoint: "default_endpoint",
}
}
func (m *MockCfgSrcFactory) CreateConfigSource(_ context.Context, cfg configsource.Settings, _ *zap.Logger) (configsource.ConfigSource, error) {
if m.ErrOnCreateConfigSource != nil {
return nil, m.ErrOnCreateConfigSource
}
return &TestConfigSource{
ValueMap: map[string]valueEntry{
cfg.ID().String(): {
Value: cfg,
},
},
}, nil
}
// TestConfigSource a ConfigSource to be used in tests.
type TestConfigSource struct {
ValueMap map[string]valueEntry
ErrOnRetrieve error
ErrOnRetrieveEnd error
ErrOnClose error
OnRetrieve func(ctx context.Context, selector string, paramsConfigMap *confmap.Conf) error
}
type valueEntry struct {
Value any
WatchForUpdateCh chan error
}
var _ configsource.ConfigSource = (*TestConfigSource)(nil)
func (t *TestConfigSource) Retrieve(ctx context.Context, selector string, paramsConfigMap *confmap.Conf, watcher confmap.WatcherFunc) (*confmap.Retrieved, error) {
if t.OnRetrieve != nil {
if err := t.OnRetrieve(ctx, selector, paramsConfigMap); err != nil {
return nil, err
}
}
if t.ErrOnRetrieve != nil {
return nil, t.ErrOnRetrieve
}
entry, ok := t.ValueMap[selector]
if !ok {
return nil, fmt.Errorf("no value for selector %q", selector)
}
if entry.WatchForUpdateCh != nil {
doneCh := make(chan struct{})
startWatch(entry.WatchForUpdateCh, doneCh, watcher)
return confmap.NewRetrieved(entry.Value, confmap.WithRetrievedClose(func(ctx context.Context) error {
close(doneCh)
return nil
}))
}
return confmap.NewRetrieved(entry.Value)
}
func (t *TestConfigSource) Shutdown(context.Context) error {
return t.ErrOnClose
}
func startWatch(watchForUpdateCh chan error, doneCh chan struct{}, watcher confmap.WatcherFunc) {
go func() {
select {
case err := <-watchForUpdateCh:
watcher(&confmap.ChangeEvent{Error: err})
return
case <-doneCh:
return
}
}()
}