Skip to content

Commit

Permalink
Add golden config tests to severity operator (#71)
Browse files Browse the repository at this point in the history
* Add golden config tests
  • Loading branch information
Mrod1598 authored Mar 24, 2021
1 parent e03f95d commit 2d9f6f0
Show file tree
Hide file tree
Showing 8 changed files with 140 additions and 0 deletions.
122 changes: 122 additions & 0 deletions operator/helper/severity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -16,12 +16,15 @@ package helper

import (
"fmt"
"io/ioutil"
"path"
"strings"
"testing"

"github.com/open-telemetry/opentelemetry-log-collection/entry"
"github.com/open-telemetry/opentelemetry-log-collection/testutil"
"github.com/stretchr/testify/require"
yaml "gopkg.in/yaml.v2"
)

type severityTestCase struct {
Expand Down Expand Up @@ -392,3 +395,122 @@ func (tc severityTestCase) run(parseFrom entry.Field) func(*testing.T) {
require.Equal(t, tc.expected, ent.Severity)
}
}

type severityConfigTestCase struct {
name string
expectErr bool
expect *SeverityParserConfig
}

func TestGoldenSeverityParserConfig(t *testing.T) {
cases := []severityConfigTestCase{
{
"default",
false,
defaultSeverityCfg(),
},
{
"parse_from_simple",
false,
func() *SeverityParserConfig {
cfg := defaultSeverityCfg()
newParse := entry.NewRecordField("from")
cfg.ParseFrom = &newParse
return cfg
}(),
},
{
"mapping",
false,
func() *SeverityParserConfig {
cfg := defaultSeverityCfg()
cfg.Mapping = map[interface{}]interface{}{
"critical": "5xx",
"error": "4xx",
"info": "3xx",
"debug": "2xx",
}
return cfg
}(),
},
{
"preserve_to",
false,
func() *SeverityParserConfig {
cfg := defaultSeverityCfg()
preserve := entry.NewRecordField("aField")
cfg.PreserveTo = &preserve
return cfg
}(),
},
{
"preset",
false,
func() *SeverityParserConfig {
cfg := defaultSeverityCfg()
cfg.Preset = "default"
return cfg
}(),
},
}

for _, tc := range cases {
t.Run("yaml/"+tc.name, func(t *testing.T) {
cfgFromYaml, yamlErr := configFromFileViaYaml(path.Join(".", "severitytestdata", fmt.Sprintf("%s.yaml", tc.name)))
if tc.expectErr {
require.Error(t, yamlErr)
} else {
require.NoError(t, yamlErr)
require.Equal(t, tc.expect, cfgFromYaml)
}
})
t.Run("mapstructure/"+tc.name, func(t *testing.T) {
cfgFromMapstructure := defaultSeverityCfg()
mapErr := configFromFileViaMapstructure(
path.Join(".", "severitytestdata", fmt.Sprintf("%s.yaml", tc.name)),
cfgFromMapstructure,
)
if tc.expectErr {
require.Error(t, mapErr)
} else {
require.NoError(t, mapErr)
require.Equal(t, tc.expect, cfgFromMapstructure)
}
})
}
}

func configFromFileViaYaml(file string) (*SeverityParserConfig, error) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("could not find config file: %s", err)
}

config := defaultSeverityCfg()
if err := yaml.Unmarshal(bytes, config); err != nil {
return nil, fmt.Errorf("failed to read config file as yaml: %s", err)
}

return config, nil
}

func configFromFileViaMapstructure(file string, result *SeverityParserConfig) error {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return fmt.Errorf("could not find config file: %s", err)
}

raw := map[string]interface{}{}

if err := yaml.Unmarshal(bytes, raw); err != nil {
return fmt.Errorf("failed to read data from yaml: %s", err)
}

err = UnmarshalMapstructure(raw, result)
return err
}

func defaultSeverityCfg() *SeverityParserConfig {
newCfg := NewSeverityParserConfig()
return &newCfg
}
1 change: 1 addition & 0 deletions operator/helper/severitytestdata/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: severity_parser
6 changes: 6 additions & 0 deletions operator/helper/severitytestdata/mapping.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,6 @@
type: severity_parser
mapping:
critical: 5xx
error: 4xx
info: 3xx
debug: 2xx
2 changes: 2 additions & 0 deletions operator/helper/severitytestdata/on_error_drop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: severity_parser
on_error: drop
2 changes: 2 additions & 0 deletions operator/helper/severitytestdata/parse_from_simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: severity_parser
parse_from: $.from
2 changes: 2 additions & 0 deletions operator/helper/severitytestdata/preserve_to.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: severity_parser
preserve_to: aField
2 changes: 2 additions & 0 deletions operator/helper/severitytestdata/preset.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: severity_parser
preset: default
3 changes: 3 additions & 0 deletions operator/helper/severitytestdata/severity.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
type: severity_parser
severity:
parse_from: severity_field

0 comments on commit 2d9f6f0

Please sign in to comment.