Skip to content

Commit

Permalink
some more tests in grok_test.go
Browse files Browse the repository at this point in the history
  • Loading branch information
fstab committed Jul 28, 2016
1 parent 3f82fcc commit 562edce
Showing 1 changed file with 29 additions and 0 deletions.
29 changes: 29 additions & 0 deletions exporter/grok_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ package exporter

import (
"testing"
"strings"
)

func TestAllRegexpsCompile(t *testing.T) {
Expand All @@ -13,3 +14,31 @@ func TestAllRegexpsCompile(t *testing.T) {
}
}
}

func TestUnknownGrokPattern(t *testing.T) {
patterns := loadPatternDir(t)
_, err := Compile("%{USER} [a-z] %{SOME_UNKNOWN_PATTERN}.*", patterns)
if err == nil || ! strings.Contains(err.Error(), "SOME_UNKNOWN_PATTERN") {
t.Error("expected error message saying which pattern is undefined.")
}
}

func TestInvalidRegexp(t *testing.T) {
patterns := loadPatternDir(t)
_, err := Compile("%{USER} [a-z] \\", patterns) // wrong because regex cannot end with backslash
if err == nil || ! strings.Contains(err.Error(), "%{USER} [a-z] \\") {
t.Error("expected error message saying which pattern is invalid.")
}
}

func TestNamedCaptureGroup(t *testing.T) {
patterns := loadPatternDir(t)
regex, err := Compile("User %{USER:user} has logged in.", patterns)
if err != nil {
t.Error(err)
}
found := regex.Gsub("User fabian has logged in.", "\\k<user>")
if found != "fabian" {
t.Errorf("Expected to capture 'fabian', but captured '%v'.", found)
}
}

0 comments on commit 562edce

Please sign in to comment.