@@ -7,6 +7,7 @@ package otelcol
7
7
import (
8
8
"context"
9
9
"errors"
10
+ "os"
10
11
"path/filepath"
11
12
"sync"
12
13
"syscall"
@@ -15,6 +16,8 @@ import (
15
16
16
17
"github.com/stretchr/testify/assert"
17
18
"github.com/stretchr/testify/require"
19
+ "go.uber.org/zap"
20
+ "gopkg.in/yaml.v3"
18
21
19
22
"go.opentelemetry.io/collector/component"
20
23
"go.opentelemetry.io/collector/confmap"
@@ -34,7 +37,7 @@ func TestCollectorStartAsGoRoutine(t *testing.T) {
34
37
set := CollectorSettings {
35
38
BuildInfo : component .NewDefaultBuildInfo (),
36
39
Factories : nopFactories ,
37
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
40
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
38
41
}
39
42
col , err := NewCollector (set )
40
43
require .NoError (t , err )
@@ -55,7 +58,7 @@ func TestCollectorCancelContext(t *testing.T) {
55
58
set := CollectorSettings {
56
59
BuildInfo : component .NewDefaultBuildInfo (),
57
60
Factories : nopFactories ,
58
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
61
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
59
62
}
60
63
col , err := NewCollector (set )
61
64
require .NoError (t , err )
@@ -87,10 +90,10 @@ func TestCollectorStateAfterConfigChange(t *testing.T) {
87
90
BuildInfo : component .NewDefaultBuildInfo (),
88
91
Factories : nopFactories ,
89
92
// this will be overwritten, but we need something to get past validation
90
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
93
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
91
94
})
92
95
require .NoError (t , err )
93
- provider , err := NewConfigProvider (newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}))
96
+ provider , err := NewConfigProvider (newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}))
94
97
require .NoError (t , err )
95
98
col .configProvider = & mockCfgProvider {ConfigProvider : provider , watcher : watcher }
96
99
@@ -116,7 +119,7 @@ func TestCollectorReportError(t *testing.T) {
116
119
col , err := NewCollector (CollectorSettings {
117
120
BuildInfo : component .NewDefaultBuildInfo (),
118
121
Factories : nopFactories ,
119
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
122
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
120
123
})
121
124
require .NoError (t , err )
122
125
@@ -162,7 +165,7 @@ func TestComponentStatusWatcher(t *testing.T) {
162
165
col , err := NewCollector (CollectorSettings {
163
166
BuildInfo : component .NewDefaultBuildInfo (),
164
167
Factories : func () (Factories , error ) { return factories , nil },
165
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-statuswatcher.yaml" )}),
168
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-statuswatcher.yaml" )}),
166
169
})
167
170
require .NoError (t , err )
168
171
@@ -225,7 +228,7 @@ func TestCollectorSendSignal(t *testing.T) {
225
228
col , err := NewCollector (CollectorSettings {
226
229
BuildInfo : component .NewDefaultBuildInfo (),
227
230
Factories : nopFactories ,
228
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
231
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
229
232
})
230
233
require .NoError (t , err )
231
234
@@ -253,7 +256,7 @@ func TestCollectorFailedShutdown(t *testing.T) {
253
256
col , err := NewCollector (CollectorSettings {
254
257
BuildInfo : component .NewDefaultBuildInfo (),
255
258
Factories : nopFactories ,
256
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
259
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
257
260
})
258
261
require .NoError (t , err )
259
262
@@ -278,7 +281,7 @@ func TestCollectorStartInvalidConfig(t *testing.T) {
278
281
col , err := NewCollector (CollectorSettings {
279
282
BuildInfo : component .NewDefaultBuildInfo (),
280
283
Factories : nopFactories ,
281
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-invalid.yaml" )}),
284
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-invalid.yaml" )}),
282
285
})
283
286
require .NoError (t , err )
284
287
assert .Error (t , col .Run (context .Background ()))
@@ -294,7 +297,7 @@ func TestNewCollectorInvalidConfigProviderSettings(t *testing.T) {
294
297
}
295
298
296
299
func TestNewCollectorUseConfig (t * testing.T ) {
297
- set := newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-nop.yaml" )})
300
+ set := newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-nop.yaml" )})
298
301
299
302
col , err := NewCollector (CollectorSettings {
300
303
BuildInfo : component .NewDefaultBuildInfo (),
@@ -335,7 +338,7 @@ func TestCollectorStartWithTraceContextPropagation(t *testing.T) {
335
338
set := CollectorSettings {
336
339
BuildInfo : component .NewDefaultBuildInfo (),
337
340
Factories : nopFactories ,
338
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , tt .file )}),
341
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , tt .file )}),
339
342
}
340
343
341
344
col , err := NewCollector (set )
@@ -367,7 +370,7 @@ func TestCollectorRun(t *testing.T) {
367
370
set := CollectorSettings {
368
371
BuildInfo : component .NewDefaultBuildInfo (),
369
372
Factories : nopFactories ,
370
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , tt .file )}),
373
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , tt .file )}),
371
374
}
372
375
col , err := NewCollector (set )
373
376
require .NoError (t , err )
@@ -385,7 +388,7 @@ func TestCollectorShutdownBeforeRun(t *testing.T) {
385
388
set := CollectorSettings {
386
389
BuildInfo : component .NewDefaultBuildInfo (),
387
390
Factories : nopFactories ,
388
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
391
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-nop.yaml" )}),
389
392
}
390
393
col , err := NewCollector (set )
391
394
require .NoError (t , err )
@@ -405,7 +408,7 @@ func TestCollectorClosedStateOnStartUpError(t *testing.T) {
405
408
set := CollectorSettings {
406
409
BuildInfo : component .NewDefaultBuildInfo (),
407
410
Factories : nopFactories ,
408
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-invalid.yaml" )}),
411
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-invalid.yaml" )}),
409
412
}
410
413
col , err := NewCollector (set )
411
414
require .NoError (t , err )
@@ -426,7 +429,7 @@ func TestCollectorDryRun(t *testing.T) {
426
429
settings : CollectorSettings {
427
430
BuildInfo : component .NewDefaultBuildInfo (),
428
431
Factories : nopFactories ,
429
- ConfigProviderSettings : newDefaultConfigProviderSettings ([]string {filepath .Join ("testdata" , "otelcol-invalid.yaml" )}),
432
+ ConfigProviderSettings : newDefaultConfigProviderSettings (t , []string {filepath .Join ("testdata" , "otelcol-invalid.yaml" )}),
430
433
},
431
434
expectedErr : `service::pipelines::traces: references processor "invalid" which is not configured` ,
432
435
},
@@ -492,3 +495,92 @@ func (*failureProvider) Scheme() string {
492
495
func (* failureProvider ) Shutdown (context.Context ) error {
493
496
return nil
494
497
}
498
+
499
+ type fakeProvider struct {
500
+ scheme string
501
+ ret func (ctx context.Context , uri string , watcher confmap.WatcherFunc ) (* confmap.Retrieved , error )
502
+ logger * zap.Logger
503
+ }
504
+
505
+ func (f * fakeProvider ) Retrieve (ctx context.Context , uri string , watcher confmap.WatcherFunc ) (* confmap.Retrieved , error ) {
506
+ return f .ret (ctx , uri , watcher )
507
+ }
508
+
509
+ func (f * fakeProvider ) Scheme () string {
510
+ return f .scheme
511
+ }
512
+
513
+ func (f * fakeProvider ) Shutdown (context.Context ) error {
514
+ return nil
515
+ }
516
+
517
+ func newFakeProvider (scheme string , ret func (ctx context.Context , uri string , watcher confmap.WatcherFunc ) (* confmap.Retrieved , error )) confmap.ProviderFactory {
518
+ return confmap .NewProviderFactory (func (ps confmap.ProviderSettings ) confmap.Provider {
519
+ return & fakeProvider {
520
+ scheme : scheme ,
521
+ ret : ret ,
522
+ logger : ps .Logger ,
523
+ }
524
+ })
525
+ }
526
+
527
+ func newEnvProvider () confmap.ProviderFactory {
528
+ return newFakeProvider ("env" , func (_ context.Context , uri string , _ confmap.WatcherFunc ) (* confmap.Retrieved , error ) {
529
+ // When using `env` as the default scheme for tests, the uri will not include `env:`.
530
+ // Instead of duplicating the switch cases, the scheme is added instead.
531
+ if uri [0 :4 ] != "env:" {
532
+ uri = "env:" + uri
533
+ }
534
+ switch uri {
535
+ case "env:COMPLEX_VALUE" :
536
+ return confmap .NewRetrieved ([]any {"localhost:3042" })
537
+ case "env:HOST" :
538
+ return confmap .NewRetrieved ("localhost" )
539
+ case "env:OS" :
540
+ return confmap .NewRetrieved ("ubuntu" )
541
+ case "env:PR" :
542
+ return confmap .NewRetrieved ("amd" )
543
+ case "env:PORT" :
544
+ return confmap .NewRetrieved (3044 )
545
+ case "env:INT" :
546
+ return confmap .NewRetrieved (1 )
547
+ case "env:INT32" :
548
+ return confmap .NewRetrieved (32 )
549
+ case "env:INT64" :
550
+ return confmap .NewRetrieved (64 )
551
+ case "env:FLOAT32" :
552
+ return confmap .NewRetrieved (float32 (3.25 ))
553
+ case "env:FLOAT64" :
554
+ return confmap .NewRetrieved (float64 (6.4 ))
555
+ case "env:BOOL" :
556
+ return confmap .NewRetrieved (true )
557
+ }
558
+ return nil , errors .New ("impossible" )
559
+ })
560
+ }
561
+
562
+ func newDefaultConfigProviderSettings (t testing.TB , uris []string ) ConfigProviderSettings {
563
+ fileProvider := newFakeProvider ("file" , func (_ context.Context , uri string , _ confmap.WatcherFunc ) (* confmap.Retrieved , error ) {
564
+ return confmap .NewRetrieved (newConfFromFile (t , uri [5 :]))
565
+ })
566
+ return ConfigProviderSettings {
567
+ ResolverSettings : confmap.ResolverSettings {
568
+ URIs : uris ,
569
+ ProviderFactories : []confmap.ProviderFactory {
570
+ fileProvider ,
571
+ newEnvProvider (),
572
+ },
573
+ },
574
+ }
575
+ }
576
+
577
+ // newConfFromFile creates a new Conf by reading the given file.
578
+ func newConfFromFile (t testing.TB , fileName string ) map [string ]any {
579
+ content , err := os .ReadFile (filepath .Clean (fileName ))
580
+ require .NoErrorf (t , err , "unable to read the file %v" , fileName )
581
+
582
+ var data map [string ]any
583
+ require .NoError (t , yaml .Unmarshal (content , & data ), "unable to parse yaml" )
584
+
585
+ return confmap .NewFromStringMap (data ).ToStringMap ()
586
+ }
0 commit comments