Skip to content
This repository has been archived by the owner on May 25, 2022. It is now read-only.

URI Parser Tests #281

Merged
merged 2 commits into from
Sep 27, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/codecov.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ env:
# Path to where test results will be saved.
TEST_RESULTS: /tmp/test-results
# Default minimum version of Go to support.
DEFAULT_GO_VERSION: 1.15
DEFAULT_GO_VERSION: 1.17
jobs:
test-coverage:
runs-on: ubuntu-latest
Expand Down
106 changes: 106 additions & 0 deletions operator/builtin/parser/uri/config_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,106 @@
// 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 uri

import (
"testing"

"github.com/open-telemetry/opentelemetry-log-collection/entry"
"github.com/open-telemetry/opentelemetry-log-collection/operator/helper"
"github.com/open-telemetry/opentelemetry-log-collection/operator/helper/operatortest"
)

func TestRegexParserGoldenConfig(t *testing.T) {
cases := []operatortest.ConfigUnmarshalTest{
{
Name: "default",
Expect: defaultCfg(),
},
{
Name: "parse_from_simple",
Expect: func() *URIParserConfig {
cfg := defaultCfg()
cfg.ParseFrom = entry.NewBodyField("from")
return cfg
}(),
},
{
Name: "parse_to_simple",
Expect: func() *URIParserConfig {
cfg := defaultCfg()
cfg.ParseTo = entry.NewBodyField("log")
return cfg
}(),
},
{
Name: "on_error_drop",
Expect: func() *URIParserConfig {
cfg := defaultCfg()
cfg.OnError = "drop"
return cfg
}(),
},
{
Name: "timestamp",
Expect: func() *URIParserConfig {
cfg := defaultCfg()
parseField := entry.NewBodyField("timestamp_field")
newTime := helper.TimeParser{
LayoutType: "strptime",
Layout: "%Y-%m-%d",
ParseFrom: &parseField,
}
cfg.TimeParser = &newTime
return cfg
}(),
},
{
Name: "severity",
Expect: func() *URIParserConfig {
cfg := defaultCfg()
parseField := entry.NewBodyField("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
}(),
},
{
Name: "preserve_to",
Expect: func() *URIParserConfig {
cfg := defaultCfg()
preserve := entry.NewBodyField("aField")
cfg.PreserveTo = &preserve
return cfg
}(),
},
}

for _, tc := range cases {
t.Run(tc.Name, func(t *testing.T) {
tc.Run(t, defaultCfg())
})
}
}

func defaultCfg() *URIParserConfig {
return NewURIParserConfig("uri_parser")
}
1 change: 1 addition & 0 deletions operator/builtin/parser/uri/testdata/default.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
type: uri_parser
2 changes: 2 additions & 0 deletions operator/builtin/parser/uri/testdata/on_error_drop.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: uri_parser
on_error: "drop"
2 changes: 2 additions & 0 deletions operator/builtin/parser/uri/testdata/parse_from_simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: uri_parser
parse_from: "$.from"
2 changes: 2 additions & 0 deletions operator/builtin/parser/uri/testdata/parse_to_simple.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: uri_parser
parse_to: "log"
2 changes: 2 additions & 0 deletions operator/builtin/parser/uri/testdata/preserve_to.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
type: uri_parser
preserve_to: "aField"
8 changes: 8 additions & 0 deletions operator/builtin/parser/uri/testdata/severity.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
type: uri_parser
severity:
parse_from: severity_field
mapping:
critical: 5xx
error: 4xx
info: 3xx
debug: 2xx
5 changes: 5 additions & 0 deletions operator/builtin/parser/uri/testdata/timestamp.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
type: uri_parser
timestamp:
parse_from: timestamp_field
layout_type: strptime
layout: '%Y-%m-%d'
157 changes: 157 additions & 0 deletions operator/builtin/parser/uri/uri_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -15,13 +15,15 @@
package uri

import (
"context"
"net/url"
"testing"

"github.com/stretchr/testify/require"
"gopkg.in/yaml.v2"

"github.com/open-telemetry/opentelemetry-log-collection/entry"
"github.com/open-telemetry/opentelemetry-log-collection/operator"
"github.com/open-telemetry/opentelemetry-log-collection/operator/helper"
"github.com/open-telemetry/opentelemetry-log-collection/testutil"
)
Expand All @@ -34,6 +36,12 @@ func newTestParser(t *testing.T) *URIParser {
return op.(*URIParser)
}

func TestInit(t *testing.T) {
builder, ok := operator.DefaultRegistry.Lookup("uri_parser")
require.True(t, ok, "expected uri_parser to be registered")
require.Equal(t, "uri_parser", builder().Type())
}

func TestURIParserBuildFailure(t *testing.T) {
cfg := NewURIParserConfig("test")
cfg.OnError = "invalid_on_error"
Expand Down Expand Up @@ -63,6 +71,149 @@ func TestRegexParserInvalidType(t *testing.T) {
require.Contains(t, err.Error(), "type '[]int' cannot be parsed as URI")
}

func TestProcess(t *testing.T) {
cases := []struct {
name string
op func() (operator.Operator, error)
input *entry.Entry
expect *entry.Entry
}{
{
"default",
func() (operator.Operator, error) {
cfg := NewURIParserConfig("test_id")
ops, err := cfg.Build(testutil.NewBuildContext(t))
if err != nil {
return nil, err
}
return ops[0], nil
},
&entry.Entry{
Body: "https://google.com:443/path?user=dev",
},
&entry.Entry{
Body: map[string]interface{}{
"host": "google.com",
"port": "443",
"path": "/path",
"query": map[string]interface{}{
"user": []interface{}{
"dev",
},
},
"scheme": "https",
},
},
},
{
"parse-to",
func() (operator.Operator, error) {
cfg := NewURIParserConfig("test_id")
cfg.ParseFrom = entry.NewBodyField("url")
cfg.ParseTo = entry.NewBodyField("url2")
ops, err := cfg.Build(testutil.NewBuildContext(t))
if err != nil {
return nil, err
}
return ops[0], nil
},
&entry.Entry{
Body: map[string]interface{}{
"url": "https://google.com:443/path?user=dev",
},
},
&entry.Entry{
Body: map[string]interface{}{
"url2": map[string]interface{}{
"host": "google.com",
"port": "443",
"path": "/path",
"query": map[string]interface{}{
"user": []interface{}{
"dev",
},
},
"scheme": "https",
},
},
},
},
{
"parse-from",
func() (operator.Operator, error) {
cfg := NewURIParserConfig("test_id")
cfg.ParseFrom = entry.NewBodyField("url")
ops, err := cfg.Build(testutil.NewBuildContext(t))
if err != nil {
return nil, err
}
return ops[0], nil
},
&entry.Entry{
Body: map[string]interface{}{
"url": "https://google.com:443/path?user=dev",
},
},
&entry.Entry{
Body: map[string]interface{}{
"host": "google.com",
"port": "443",
"path": "/path",
"query": map[string]interface{}{
"user": []interface{}{
"dev",
},
},
"scheme": "https",
},
},
},
{
"parse-from-and-preserve",
func() (operator.Operator, error) {
cfg := NewURIParserConfig("test_id")
cfg.ParseFrom = entry.NewBodyField("url")
cfg.PreserveTo = &cfg.ParseFrom
ops, err := cfg.Build(testutil.NewBuildContext(t))
if err != nil {
return nil, err
}
return ops[0], nil
},
&entry.Entry{
Body: map[string]interface{}{
"url": "https://google.com:443/path?user=dev",
},
},
&entry.Entry{
Body: map[string]interface{}{
"host": "google.com",
"port": "443",
"path": "/path",
"query": map[string]interface{}{
"user": []interface{}{
"dev",
},
},
"scheme": "https",
"url": "https://google.com:443/path?user=dev",
},
},
},
}

for _, tc := range cases {
t.Run(tc.name, func(t *testing.T) {
op, err := tc.op()
require.NoError(t, err, "did not expect operator function to return an error, this is a bug with the test case")

err = op.Process(context.Background(), tc.input)
require.NoError(t, err)
require.Equal(t, tc.expect, tc.input)
})
}
}

func TestURIParserParse(t *testing.T) {
cases := []struct {
name string
Expand Down Expand Up @@ -212,6 +363,12 @@ func TestParseURI(t *testing.T) {
},
false,
},
{
"invalid-query",
"?q;go",
map[string]interface{}{},
true,
},
{
"scheme-path",
"http:///v1/app",
Expand Down