From e8d8c436e4ae35bdb7f40c389a4943a387ebd840 Mon Sep 17 00:00:00 2001 From: Paul Stuart Date: Fri, 22 Apr 2016 08:54:07 -0700 Subject: [PATCH] added test for ParseStream --- grok_test.go | 49 ++++++++++++++++++++++++++++++++++++++++++++++++- 1 file changed, 48 insertions(+), 1 deletion(-) 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") + } +}