From 0f6b1a9dbec823eb196edffe9e745c21e1ff0d94 Mon Sep 17 00:00:00 2001 From: Mrod1598 Date: Wed, 24 Mar 2021 16:37:51 -0400 Subject: [PATCH] Add golden config testing to time (#72) --- operator/helper/severity_test.go | 8 +- operator/helper/time_test.go | 122 ++++++++++++++++++ operator/helper/timetestdata/layout.yaml | 1 + operator/helper/timetestdata/layout_type.yaml | 1 + operator/helper/timetestdata/location.yaml | 1 + operator/helper/timetestdata/parse_from.yaml | 1 + operator/helper/timetestdata/preserve_to.yaml | 1 + 7 files changed, 131 insertions(+), 4 deletions(-) create mode 100644 operator/helper/timetestdata/layout.yaml create mode 100644 operator/helper/timetestdata/layout_type.yaml create mode 100644 operator/helper/timetestdata/location.yaml create mode 100644 operator/helper/timetestdata/parse_from.yaml create mode 100644 operator/helper/timetestdata/preserve_to.yaml diff --git a/operator/helper/severity_test.go b/operator/helper/severity_test.go index c0503ebef3c9..213746c910f0 100644 --- a/operator/helper/severity_test.go +++ b/operator/helper/severity_test.go @@ -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 { @@ -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, ) @@ -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) @@ -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) diff --git a/operator/helper/time_test.go b/operator/helper/time_test.go index c95953449eca..30cf644dc1d0 100644 --- a/operator/helper/time_test.go +++ b/operator/helper/time_test.go @@ -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) { @@ -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 +} diff --git a/operator/helper/timetestdata/layout.yaml b/operator/helper/timetestdata/layout.yaml new file mode 100644 index 000000000000..ad0770440a58 --- /dev/null +++ b/operator/helper/timetestdata/layout.yaml @@ -0,0 +1 @@ +layout: '%Y-%m-%d' \ No newline at end of file diff --git a/operator/helper/timetestdata/layout_type.yaml b/operator/helper/timetestdata/layout_type.yaml new file mode 100644 index 000000000000..84a7987b11d4 --- /dev/null +++ b/operator/helper/timetestdata/layout_type.yaml @@ -0,0 +1 @@ +layout_type: epoch \ No newline at end of file diff --git a/operator/helper/timetestdata/location.yaml b/operator/helper/timetestdata/location.yaml new file mode 100644 index 000000000000..617ef1c68e97 --- /dev/null +++ b/operator/helper/timetestdata/location.yaml @@ -0,0 +1 @@ +location: America/Shiprock \ No newline at end of file diff --git a/operator/helper/timetestdata/parse_from.yaml b/operator/helper/timetestdata/parse_from.yaml new file mode 100644 index 000000000000..3ae341f5e005 --- /dev/null +++ b/operator/helper/timetestdata/parse_from.yaml @@ -0,0 +1 @@ +parse_from: $.from \ No newline at end of file diff --git a/operator/helper/timetestdata/preserve_to.yaml b/operator/helper/timetestdata/preserve_to.yaml new file mode 100644 index 000000000000..af2ef4d8a9e9 --- /dev/null +++ b/operator/helper/timetestdata/preserve_to.yaml @@ -0,0 +1 @@ +preserve_to: aField \ No newline at end of file