@@ -19,32 +19,25 @@ import (
1919 "bufio"
2020 "context"
2121 "errors"
22- "flag"
2322 "fmt"
2423 "net/http"
2524 "runtime"
26- "sort"
2725 "strconv"
2826 "strings"
2927 "syscall"
3028 "testing"
31- "time"
3229
3330 "github.com/prometheus/common/expfmt"
34- "github.com/spf13/cobra"
3531 "github.com/stretchr/testify/assert"
3632 "github.com/stretchr/testify/require"
3733 "go.uber.org/zap"
3834 "go.uber.org/zap/zapcore"
3935
4036 "go.opentelemetry.io/collector/component"
4137 "go.opentelemetry.io/collector/config"
42- "go.opentelemetry.io/collector/config/configparser"
43- "go.opentelemetry.io/collector/processor/attributesprocessor"
44- "go.opentelemetry.io/collector/processor/batchprocessor"
45- "go.opentelemetry.io/collector/receiver/jaegerreceiver"
4638 "go.opentelemetry.io/collector/service/defaultcomponents"
4739 "go.opentelemetry.io/collector/service/internal/builder"
40+ "go.opentelemetry.io/collector/service/parserprovider"
4841 "go.opentelemetry.io/collector/testutil"
4942)
5043
@@ -149,10 +142,8 @@ func TestApplication_StartAsGoRoutine(t *testing.T) {
149142
150143 params := Parameters {
151144 ApplicationStartInfo : component .DefaultApplicationStartInfo (),
152- ConfigFactory : func (_ * cobra.Command , factories component.Factories ) (* config.Config , error ) {
153- return constructMimumalOpConfig (t , factories ), nil
154- },
155- Factories : factories ,
145+ ParserProvider : new (minimalParserLoader ),
146+ Factories : factories ,
156147 }
157148 app , err := New (params )
158149 require .NoError (t , err )
@@ -225,105 +216,9 @@ func assertMetrics(t *testing.T, prefix string, metricsPort uint16, mandatoryLab
225216 }
226217}
227218
228- func TestSetFlag (t * testing.T ) {
229- factories , err := defaultcomponents .Components ()
230- require .NoError (t , err )
231- params := Parameters {
232- Factories : factories ,
233- }
234- t .Run ("unknown_component" , func (t * testing.T ) {
235- app , err := New (params )
236- require .NoError (t , err )
237- err = app .rootCmd .ParseFlags ([]string {
238- "--config=testdata/otelcol-config.yaml" ,
239- "--set=processors.doesnotexist.timeout=2s" ,
240- })
241- require .NoError (t , err )
242- cfg , err := FileLoaderConfigFactory (app .rootCmd , factories )
243- require .Error (t , err )
244- require .Nil (t , cfg )
245-
246- })
247- t .Run ("component_not_added_to_pipeline" , func (t * testing.T ) {
248- app , err := New (params )
249- require .NoError (t , err )
250- err = app .rootCmd .ParseFlags ([]string {
251- "--config=testdata/otelcol-config.yaml" ,
252- "--set=processors.batch/foo.timeout=2s" ,
253- })
254- require .NoError (t , err )
255- cfg , err := FileLoaderConfigFactory (app .rootCmd , factories )
256- require .NoError (t , err )
257- assert .NotNil (t , cfg )
258- err = cfg .Validate ()
259- require .NoError (t , err )
260-
261- var processors []string
262- for k := range cfg .Processors {
263- processors = append (processors , k )
264- }
265- sort .Strings (processors )
266- // batch/foo is not added to the pipeline
267- assert .Equal (t , []string {"attributes" , "batch" , "batch/foo" }, processors )
268- assert .Equal (t , []string {"attributes" , "batch" }, cfg .Service .Pipelines ["traces" ].Processors )
269- })
270- t .Run ("ok" , func (t * testing.T ) {
271- app , err := New (params )
272- require .NoError (t , err )
273-
274- err = app .rootCmd .ParseFlags ([]string {
275- "--config=testdata/otelcol-config.yaml" ,
276- "--set=processors.batch.timeout=2s" ,
277- // Arrays are overridden and object arrays cannot be indexed
278- // this creates actions array of size 1
279- "--set=processors.attributes.actions.key=foo" ,
280- "--set=processors.attributes.actions.value=bar" ,
281- "--set=receivers.jaeger.protocols.grpc.endpoint=localhost:12345" ,
282- "--set=extensions.health_check.endpoint=localhost:8080" ,
283- })
284- require .NoError (t , err )
285- cfg , err := FileLoaderConfigFactory (app .rootCmd , factories )
286- require .NoError (t , err )
287- require .NotNil (t , cfg )
288- err = cfg .Validate ()
289- require .NoError (t , err )
290-
291- assert .Equal (t , 2 , len (cfg .Processors ))
292- batch := cfg .Processors ["batch" ].(* batchprocessor.Config )
293- assert .Equal (t , time .Second * 2 , batch .Timeout )
294- jaeger := cfg .Receivers ["jaeger" ].(* jaegerreceiver.Config )
295- assert .Equal (t , "localhost:12345" , jaeger .GRPC .NetAddr .Endpoint )
296- attributes := cfg .Processors ["attributes" ].(* attributesprocessor.Config )
297- require .Equal (t , 1 , len (attributes .Actions ))
298- assert .Equal (t , "foo" , attributes .Actions [0 ].Key )
299- assert .Equal (t , "bar" , attributes .Actions [0 ].Value )
300- })
301- }
302-
303- func TestSetFlag_component_does_not_exist (t * testing.T ) {
304- factories , err := defaultcomponents .Components ()
305- require .NoError (t , err )
306-
307- cmd := & cobra.Command {}
308- addSetFlag (cmd .Flags ())
309- fs := & flag.FlagSet {}
310- builder .Flags (fs )
311- cmd .Flags ().AddGoFlagSet (fs )
312- cmd .ParseFlags ([]string {
313- "--config=testdata/otelcol-config.yaml" ,
314- "--set=processors.batch.timeout=2s" ,
315- // Arrays are overridden and object arrays cannot be indexed
316- // this creates actions array of size 1
317- "--set=processors.attributes.actions.key=foo" ,
318- "--set=processors.attributes.actions.value=bar" ,
319- "--set=receivers.jaeger.protocols.grpc.endpoint=localhost:12345" ,
320- })
321- cfg , err := FileLoaderConfigFactory (cmd , factories )
322- require .NoError (t , err )
323- require .NotNil (t , cfg )
324- }
219+ type minimalParserLoader struct {}
325220
326- func constructMimumalOpConfig ( t * testing. T , factories component. Factories ) * config.Config {
221+ func ( * minimalParserLoader ) Get () ( * config.Parser , error ) {
327222 configStr := `
328223receivers:
329224 otlp:
@@ -347,37 +242,36 @@ service:
347242 v := config .NewViper ()
348243 v .SetConfigType ("yaml" )
349244 v .ReadConfig (strings .NewReader (configStr ))
350- cfg , err := configparser .Load (config .ParserFromViper (v ), factories )
351- assert .NoError (t , err )
352- err = cfg .Validate ()
353- assert .NoError (t , err )
354- return cfg
245+ return config .ParserFromViper (v ), nil
246+ }
247+
248+ type errParserLoader struct {
249+ err error
250+ }
251+
252+ func (epl * errParserLoader ) Get () (* config.Parser , error ) {
253+ return nil , epl .err
355254}
356255
357256func TestApplication_updateService (t * testing.T ) {
358257 factories , err := defaultcomponents .Components ()
359258 require .NoError (t , err )
360259 ctx := context .Background ()
361260 sentinelError := errors .New ("sentinel error" )
362- returnConfigFactoryFn := func (cfg * config.Config , err error ) ConfigFactory {
363- return func (* cobra.Command , component.Factories ) (* config.Config , error ) {
364- return cfg , err
365- }
366- }
367261
368262 tests := []struct {
369- name string
370- configFactory ConfigFactory
371- service * service
372- skip bool
263+ name string
264+ parserProvider parserprovider. ParserProvider
265+ service * service
266+ skip bool
373267 }{
374268 {
375- name : "first_load_err" ,
376- configFactory : returnConfigFactoryFn ( nil , sentinelError ) ,
269+ name : "first_load_err" ,
270+ parserProvider : & errParserLoader { err : sentinelError } ,
377271 },
378272 {
379- name : "retire_service_ok_load_err" ,
380- configFactory : returnConfigFactoryFn ( nil , sentinelError ) ,
273+ name : "retire_service_ok_load_err" ,
274+ parserProvider : & errParserLoader { err : sentinelError } ,
381275 service : & service {
382276 logger : zap .NewNop (),
383277 builtExporters : builder.Exporters {},
@@ -387,8 +281,8 @@ func TestApplication_updateService(t *testing.T) {
387281 },
388282 },
389283 {
390- name : "retire_service_ok_load_ok" ,
391- configFactory : returnConfigFactoryFn ( constructMimumalOpConfig ( t , factories ), nil ),
284+ name : "retire_service_ok_load_ok" ,
285+ parserProvider : new ( minimalParserLoader ),
392286 service : & service {
393287 logger : zap .NewNop (),
394288 builtExporters : builder.Exporters {},
@@ -407,10 +301,10 @@ func TestApplication_updateService(t *testing.T) {
407301 }
408302
409303 app := Application {
410- logger : zap .NewNop (),
411- configFactory : tt .configFactory ,
412- factories : factories ,
413- service : tt .service ,
304+ logger : zap .NewNop (),
305+ parserProvider : tt .parserProvider ,
306+ factories : factories ,
307+ service : tt .service ,
414308 }
415309
416310 err := app .updateService (ctx )
0 commit comments