Skip to content

Commit

Permalink
Add regex parser golden configs (#65)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrod1598 authored Mar 19, 2021
1 parent 7439d24 commit 02bb510
Show file tree
Hide file tree
Showing 9 changed files with 205 additions and 0 deletions.
181 changes: 181 additions & 0 deletions operator/builtin/parser/regex/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,181 @@
// Copyright The OpenTelemetry Authors
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package regex

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

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

type testCase struct {
name string
expectErr bool
expect *RegexParserConfig
}

func TestRegexParserGoldenConfig(t *testing.T) {
cases := []testCase{
{
"default",
false,
defaultCfg(),
},
{
"parse_from_simple",
false,
func() *RegexParserConfig {
cfg := defaultCfg()
cfg.ParseFrom = entry.NewRecordField("from")
return cfg
}(),
},
{
"parse_to_simple",
false,
func() *RegexParserConfig {
cfg := defaultCfg()
cfg.ParseTo = entry.NewRecordField("log")
return cfg
}(),
},
{
"on_error_drop",
false,
func() *RegexParserConfig {
cfg := defaultCfg()
cfg.OnError = "drop"
return cfg
}(),
},
{
"timestamp",
false,
func() *RegexParserConfig {
cfg := defaultCfg()
parseField := entry.NewRecordField("timestamp_field")
newTime := helper.TimeParser{
LayoutType: "strptime",
Layout: "%Y-%m-%d",
ParseFrom: &parseField,
}
cfg.TimeParser = &newTime
return cfg
}(),
},
{
"severity",
false,
func() *RegexParserConfig {
cfg := defaultCfg()
parseField := entry.NewRecordField("severity_field")
severityField := helper.NewSeverityParserConfig()
severityField.ParseFrom = &parseField
mapping := map[interface{}]interface{}{
"critical": "5xx",
"error": "4xx",
"info": "3xx",
"debug": "2xx",
}
severityField.Mapping = mapping
cfg.SeverityParserConfig = &severityField
return cfg
}(),
},
{
"preserve_to",
false,
func() *RegexParserConfig {
cfg := defaultCfg()
preserve := entry.NewRecordField("aField")
cfg.PreserveTo = &preserve
return cfg
}(),
},
{
"regex",
false,
func() *RegexParserConfig {
cfg := defaultCfg()
cfg.Regex = "^Host=(?P<host>[^,]+), Type=(?P<type>.*)$"
return cfg
}(),
},
}

for _, tc := range cases {
t.Run("yaml/"+tc.name, func(t *testing.T) {
cfgFromYaml, yamlErr := configFromFileViaYaml(path.Join(".", "testdata", 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 := defaultCfg()
mapErr := configFromFileViaMapstructure(
path.Join(".", "testdata", 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) (*RegexParserConfig, error) {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return nil, fmt.Errorf("could not find config file: %s", err)
}

config := defaultCfg()
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 *RegexParserConfig) 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 = helper.UnmarshalMapstructure(raw, result)
return err
}

func defaultCfg() *RegexParserConfig {
return NewRegexParserConfig("regex_parser")
}
1 change: 1 addition & 0 deletions operator/builtin/parser/regex/testdata/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: regex_parser
2 changes: 2 additions & 0 deletions operator/builtin/parser/regex/testdata/on_error_drop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: regex_parser
on_error: "drop"
2 changes: 2 additions & 0 deletions operator/builtin/parser/regex/testdata/parse_from_simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: regex_parser
parse_from: "$.from"
2 changes: 2 additions & 0 deletions operator/builtin/parser/regex/testdata/parse_to_simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: regex_parser
parse_to: "log"
2 changes: 2 additions & 0 deletions operator/builtin/parser/regex/testdata/preserve_to.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: regex_parser
preserve_to: "aField"
2 changes: 2 additions & 0 deletions operator/builtin/parser/regex/testdata/regex.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: regex_parser
regex: '^Host=(?P<host>[^,]+), Type=(?P<type>.*)$'
8 changes: 8 additions & 0 deletions operator/builtin/parser/regex/testdata/severity.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: regex_parser
severity:
parse_from: severity_field
mapping:
critical: 5xx
error: 4xx
info: 3xx
debug: 2xx
5 changes: 5 additions & 0 deletions operator/builtin/parser/regex/testdata/timestamp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: regex_parser
timestamp:
parse_from: timestamp_field
layout_type: strptime
layout: '%Y-%m-%d'

0 comments on commit 02bb510

Please sign in to comment.