Skip to content

Commit

Permalink
added test for ParseStream
Browse files Browse the repository at this point in the history
  • Loading branch information
paulstuart committed Apr 22, 2016
1 parent 2badf97 commit e8d8c43
Showing 1 changed file with 48 additions and 1 deletion.
49 changes: 48 additions & 1 deletion grok_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,11 @@
package grok

import "testing"
import (
"bufio"
"fmt"
"strings"
"testing"
)

func TestNew(t *testing.T) {
g, _ := New()
Expand Down Expand Up @@ -659,3 +664,45 @@ func TestGrok_AddPatternsFromMap_complex(t *testing.T) {
t.Errorf("bad match: expected 333666, got %s", mss["match"])
}
}

func TestParseStream(t *testing.T) {
g, _ := New()
pTest := func(m map[string]string) error {
ts, ok := m["timestamp"]
if !ok {
t.Error("timestamp not found")
}
if len(ts) == 0 {
t.Error("empty timestamp")
}
return nil
}
const testLog = `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207
127.0.0.1 - - [23/Apr/2014:22:59:32 +0200] "GET /index.php HTTP/1.1" 404 207
127.0.0.1 - - [23/Apr/2014:23:00:32 +0200] "GET /index.php HTTP/1.1" 404 207
`

r := bufio.NewReader(strings.NewReader(testLog))
if err := g.ParseStream(r, "%{COMMONAPACHELOG}", pTest); err != nil {
t.Fatal(err)
}
}

func TestParseStreamError(t *testing.T) {
g, _ := New()
pTest := func(m map[string]string) error {
if _, ok := m["timestamp"]; !ok {
return fmt.Errorf("timestamp not found")
}
return nil
}
const testLog = `127.0.0.1 - - [23/Apr/2014:22:58:32 +0200] "GET /index.php HTTP/1.1" 404 207
127.0.0.1 - - [xxxxxxxxxxxxxxxxxxxx +0200] "GET /index.php HTTP/1.1" 404 207
127.0.0.1 - - [23/Apr/2014:23:00:32 +0200] "GET /index.php HTTP/1.1" 404 207
`

r := bufio.NewReader(strings.NewReader(testLog))
if err := g.ParseStream(r, "%{COMMONAPACHELOG}", pTest); err == nil {
t.Fatal("Error expected")
}
}

0 comments on commit e8d8c43

Please sign in to comment.