diff --git a/README.md b/README.md index 797de67a..ad4a94e8 100644 --- a/README.md +++ b/README.md @@ -137,6 +137,12 @@ Execute testunits for subprocessors go test -timeout 10s ./subprocessors/ -cover -v ``` +Execute a test for one specific testcase in a package + +``` +go test -timeout 10s -cover -v ./loggers -run TestSyslogRunJsonMode +``` + Build executable ``` diff --git a/loggers/syslog_test.go b/loggers/syslog_test.go index 777a8ee8..b2b644a3 100644 --- a/loggers/syslog_test.go +++ b/loggers/syslog_test.go @@ -51,3 +51,45 @@ func TestSyslogRunTextMode(t *testing.T) { t.Errorf("syslog error want dns.collector, got: %s", string(line)) } } + +func TestSyslogRunJsonMode(t *testing.T) { + // init logger + config := dnsutils.GetFakeConfig() + config.Loggers.Syslog.Transport = "tcp" + config.Loggers.Syslog.RemoteAddress = ":4000" + config.Loggers.Syslog.Mode = "json" + g := NewSyslog(config, logger.New(false)) + + // fake json receiver + fakeRcvr, err := net.Listen("tcp", ":4000") + if err != nil { + t.Fatal(err) + } + defer fakeRcvr.Close() + + // start the logger + go g.Run() + + // accept conn from logger + conn, err := fakeRcvr.Accept() + if err != nil { + return + } + defer conn.Close() + + // send fake dns message to logger + dm := dnsutils.GetFakeDnsMessage() + g.channel <- dm + + // read data on server side and decode-it + reader := bufio.NewReader(conn) + line, _, err := reader.ReadLine() + if err != nil { + t.Errorf("error to read line on syslog server: %s", err) + } + + pattern := regexp.MustCompile("\"qname\":\"dns.collector\"") + if !pattern.MatchString(string(line)) { + t.Errorf("syslog error want json, got: %s", string(line)) + } +}