diff --git a/grok_test.go b/grok_test.go index 3144465..4f8ee17 100644 --- a/grok_test.go +++ b/grok_test.go @@ -1,6 +1,11 @@ package grok -import "testing" +import ( + "bufio" + "fmt" + "strings" + "testing" +) func TestNew(t *testing.T) { g, _ := New() @@ -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") + } +}