Skip to content

Commit

Permalink
Add golden config testing to time (#72)
Browse files Browse the repository at this point in the history
  • Loading branch information
Mrod1598 authored Mar 24, 2021
1 parent ba8a43d commit 0f6b1a9
Show file tree
Hide file tree
Showing 7 changed files with 131 additions and 4 deletions.
8 changes: 4 additions & 4 deletions operator/helper/severity_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -456,7 +456,7 @@ func TestGoldenSeverityParserConfig(t *testing.T) {

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)))
cfgFromYaml, yamlErr := severityConfigFromFileViaYaml(path.Join(".", "severitytestdata", fmt.Sprintf("%s.yaml", tc.name)))
if tc.expectErr {
require.Error(t, yamlErr)
} else {
Expand All @@ -466,7 +466,7 @@ func TestGoldenSeverityParserConfig(t *testing.T) {
})
t.Run("mapstructure/"+tc.name, func(t *testing.T) {
cfgFromMapstructure := defaultSeverityCfg()
mapErr := configFromFileViaMapstructure(
mapErr := severityConfigFromFileViaMapstructure(
path.Join(".", "severitytestdata", fmt.Sprintf("%s.yaml", tc.name)),
cfgFromMapstructure,
)
Expand All @@ -480,7 +480,7 @@ func TestGoldenSeverityParserConfig(t *testing.T) {
}
}

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

func configFromFileViaMapstructure(file string, result *SeverityParserConfig) error {
func severityConfigFromFileViaMapstructure(file string, result *SeverityParserConfig) error {
bytes, err := ioutil.ReadFile(file)
if err != nil {
return fmt.Errorf("could not find config file: %s", err)
Expand Down
122 changes: 122 additions & 0 deletions operator/helper/time_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,17 @@
package helper

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

"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"
)

func Test_setTimestampYear(t *testing.T) {
Expand Down Expand Up @@ -613,3 +617,121 @@ func makeTestEntry(field entry.Field, value interface{}) *entry.Entry {
_ = e.Set(field, value)
return e
}

type timeConfigTestCase struct {
name string
expectErr bool
expect *TimeParser
}

func TestGoldenTimeParserConfig(t *testing.T) {
cases := []timeConfigTestCase{
{
"parse_from",
false,
func() *TimeParser {
cfg := defaultTimeCfg()
newParse := entry.NewRecordField("from")
cfg.ParseFrom = &newParse
return cfg
}(),
},
{
"layout",
false,
func() *TimeParser {
cfg := defaultTimeCfg()
cfg.Layout = "%Y-%m-%d"
return cfg
}(),
},
{
"layout_type",
false,
func() *TimeParser {
cfg := defaultTimeCfg()
cfg.LayoutType = "epoch"
return cfg
}(),
},
{
"preserve_to",
false,
func() *TimeParser {
cfg := defaultTimeCfg()
newPreserve := entry.NewRecordField("aField")
cfg.PreserveTo = &newPreserve
return cfg
}(),
},
{
"location",
false,
func() *TimeParser {
cfg := defaultTimeCfg()
cfg.Location = "America/Shiprock"
return cfg
}(),
},
}

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

config := defaultTimeCfg()
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 timeConfigFromFileViaMapstructure(file string, result *TimeParser) 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 defaultTimeCfg() *TimeParser {
newCfg := NewTimeParser()
return &newCfg
}
1 change: 1 addition & 0 deletions operator/helper/timetestdata/layout.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layout: '%Y-%m-%d'
1 change: 1 addition & 0 deletions operator/helper/timetestdata/layout_type.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
layout_type: epoch
1 change: 1 addition & 0 deletions operator/helper/timetestdata/location.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
location: America/Shiprock
1 change: 1 addition & 0 deletions operator/helper/timetestdata/parse_from.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
parse_from: $.from
1 change: 1 addition & 0 deletions operator/helper/timetestdata/preserve_to.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
preserve_to: aField

0 comments on commit 0f6b1a9

Please sign in to comment.