@@ -16,12 +16,15 @@ package helper
16
16
17
17
import (
18
18
"fmt"
19
+ "io/ioutil"
20
+ "path"
19
21
"strings"
20
22
"testing"
21
23
22
24
"github.com/open-telemetry/opentelemetry-log-collection/entry"
23
25
"github.com/open-telemetry/opentelemetry-log-collection/testutil"
24
26
"github.com/stretchr/testify/require"
27
+ yaml "gopkg.in/yaml.v2"
25
28
)
26
29
27
30
type severityTestCase struct {
@@ -392,3 +395,122 @@ func (tc severityTestCase) run(parseFrom entry.Field) func(*testing.T) {
392
395
require .Equal (t , tc .expected , ent .Severity )
393
396
}
394
397
}
398
+
399
+ type severityConfigTestCase struct {
400
+ name string
401
+ expectErr bool
402
+ expect * SeverityParserConfig
403
+ }
404
+
405
+ func TestGoldenSeverityParserConfig (t * testing.T ) {
406
+ cases := []severityConfigTestCase {
407
+ {
408
+ "default" ,
409
+ false ,
410
+ defaultSeverityCfg (),
411
+ },
412
+ {
413
+ "parse_from_simple" ,
414
+ false ,
415
+ func () * SeverityParserConfig {
416
+ cfg := defaultSeverityCfg ()
417
+ newParse := entry .NewRecordField ("from" )
418
+ cfg .ParseFrom = & newParse
419
+ return cfg
420
+ }(),
421
+ },
422
+ {
423
+ "mapping" ,
424
+ false ,
425
+ func () * SeverityParserConfig {
426
+ cfg := defaultSeverityCfg ()
427
+ cfg .Mapping = map [interface {}]interface {}{
428
+ "critical" : "5xx" ,
429
+ "error" : "4xx" ,
430
+ "info" : "3xx" ,
431
+ "debug" : "2xx" ,
432
+ }
433
+ return cfg
434
+ }(),
435
+ },
436
+ {
437
+ "preserve_to" ,
438
+ false ,
439
+ func () * SeverityParserConfig {
440
+ cfg := defaultSeverityCfg ()
441
+ preserve := entry .NewRecordField ("aField" )
442
+ cfg .PreserveTo = & preserve
443
+ return cfg
444
+ }(),
445
+ },
446
+ {
447
+ "preset" ,
448
+ false ,
449
+ func () * SeverityParserConfig {
450
+ cfg := defaultSeverityCfg ()
451
+ cfg .Preset = "default"
452
+ return cfg
453
+ }(),
454
+ },
455
+ }
456
+
457
+ for _ , tc := range cases {
458
+ t .Run ("yaml/" + tc .name , func (t * testing.T ) {
459
+ cfgFromYaml , yamlErr := configFromFileViaYaml (path .Join ("." , "severitytestdata" , fmt .Sprintf ("%s.yaml" , tc .name )))
460
+ if tc .expectErr {
461
+ require .Error (t , yamlErr )
462
+ } else {
463
+ require .NoError (t , yamlErr )
464
+ require .Equal (t , tc .expect , cfgFromYaml )
465
+ }
466
+ })
467
+ t .Run ("mapstructure/" + tc .name , func (t * testing.T ) {
468
+ cfgFromMapstructure := defaultSeverityCfg ()
469
+ mapErr := configFromFileViaMapstructure (
470
+ path .Join ("." , "severitytestdata" , fmt .Sprintf ("%s.yaml" , tc .name )),
471
+ cfgFromMapstructure ,
472
+ )
473
+ if tc .expectErr {
474
+ require .Error (t , mapErr )
475
+ } else {
476
+ require .NoError (t , mapErr )
477
+ require .Equal (t , tc .expect , cfgFromMapstructure )
478
+ }
479
+ })
480
+ }
481
+ }
482
+
483
+ func configFromFileViaYaml (file string ) (* SeverityParserConfig , error ) {
484
+ bytes , err := ioutil .ReadFile (file )
485
+ if err != nil {
486
+ return nil , fmt .Errorf ("could not find config file: %s" , err )
487
+ }
488
+
489
+ config := defaultSeverityCfg ()
490
+ if err := yaml .Unmarshal (bytes , config ); err != nil {
491
+ return nil , fmt .Errorf ("failed to read config file as yaml: %s" , err )
492
+ }
493
+
494
+ return config , nil
495
+ }
496
+
497
+ func configFromFileViaMapstructure (file string , result * SeverityParserConfig ) error {
498
+ bytes , err := ioutil .ReadFile (file )
499
+ if err != nil {
500
+ return fmt .Errorf ("could not find config file: %s" , err )
501
+ }
502
+
503
+ raw := map [string ]interface {}{}
504
+
505
+ if err := yaml .Unmarshal (bytes , raw ); err != nil {
506
+ return fmt .Errorf ("failed to read data from yaml: %s" , err )
507
+ }
508
+
509
+ err = UnmarshalMapstructure (raw , result )
510
+ return err
511
+ }
512
+
513
+ func defaultSeverityCfg () * SeverityParserConfig {
514
+ newCfg := NewSeverityParserConfig ()
515
+ return & newCfg
516
+ }
0 commit comments