From 2d9f6f0f30ffc88dbbe4887309ff418b0bdcad8e Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Wed, 24 Mar 2021 15:07:53 -0400 Subject: [PATCH] Add golden config tests to severity operator (#71) * Add golden config tests --- operator/helper/severity_test.go | 122 ++++++++++++++++++ operator/helper/severitytestdata/default.yaml | 1 + operator/helper/severitytestdata/mapping.yaml | 6 + .../severitytestdata/on_error_drop.yaml | 2 + .../severitytestdata/parse_from_simple.yaml | 2 + .../helper/severitytestdata/preserve_to.yaml | 2 + operator/helper/severitytestdata/preset.yaml | 2 + .../helper/severitytestdata/severity.yaml | 3 + 8 files changed, 140 insertions(+) create mode 100644 operator/helper/severitytestdata/default.yaml create mode 100644 operator/helper/severitytestdata/mapping.yaml create mode 100644 operator/helper/severitytestdata/on_error_drop.yaml create mode 100644 operator/helper/severitytestdata/parse_from_simple.yaml create mode 100644 operator/helper/severitytestdata/preserve_to.yaml create mode 100644 operator/helper/severitytestdata/preset.yaml create mode 100644 operator/helper/severitytestdata/severity.yaml diff --git a/operator/helper/severity_test.go b/operator/helper/severity_test.go index 2ff4ad1db4f8..c0503ebef3c9 100644 --- a/operator/helper/severity_test.go +++ b/operator/helper/severity_test.go @@ -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 { @@ -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 +} diff --git a/operator/helper/severitytestdata/default.yaml b/operator/helper/severitytestdata/default.yaml new file mode 100644 index 000000000000..8a6acdcf9db5 --- /dev/null +++ b/operator/helper/severitytestdata/default.yaml @@ -0,0 +1 @@ +type: severity_parser \ No newline at end of file diff --git a/operator/helper/severitytestdata/mapping.yaml b/operator/helper/severitytestdata/mapping.yaml new file mode 100644 index 000000000000..f75625ab5794 --- /dev/null +++ b/operator/helper/severitytestdata/mapping.yaml @@ -0,0 +1,6 @@ +type: severity_parser +mapping: + critical: 5xx + error: 4xx + info: 3xx + debug: 2xx \ No newline at end of file diff --git a/operator/helper/severitytestdata/on_error_drop.yaml b/operator/helper/severitytestdata/on_error_drop.yaml new file mode 100644 index 000000000000..0ed9cb3a3cef --- /dev/null +++ b/operator/helper/severitytestdata/on_error_drop.yaml @@ -0,0 +1,2 @@ +type: severity_parser +on_error: drop \ No newline at end of file diff --git a/operator/helper/severitytestdata/parse_from_simple.yaml b/operator/helper/severitytestdata/parse_from_simple.yaml new file mode 100644 index 000000000000..3aa85ea441e7 --- /dev/null +++ b/operator/helper/severitytestdata/parse_from_simple.yaml @@ -0,0 +1,2 @@ +type: severity_parser +parse_from: $.from \ No newline at end of file diff --git a/operator/helper/severitytestdata/preserve_to.yaml b/operator/helper/severitytestdata/preserve_to.yaml new file mode 100644 index 000000000000..55d6f5b2b75d --- /dev/null +++ b/operator/helper/severitytestdata/preserve_to.yaml @@ -0,0 +1,2 @@ +type: severity_parser +preserve_to: aField \ No newline at end of file diff --git a/operator/helper/severitytestdata/preset.yaml b/operator/helper/severitytestdata/preset.yaml new file mode 100644 index 000000000000..fa947abe4253 --- /dev/null +++ b/operator/helper/severitytestdata/preset.yaml @@ -0,0 +1,2 @@ +type: severity_parser +preset: default diff --git a/operator/helper/severitytestdata/severity.yaml b/operator/helper/severitytestdata/severity.yaml new file mode 100644 index 000000000000..92fa8dca5975 --- /dev/null +++ b/operator/helper/severitytestdata/severity.yaml @@ -0,0 +1,3 @@ +type: severity_parser +severity: + parse_from: severity_field \ No newline at end of file