Skip to content

Commit

Permalink
test added for json support #8
Browse files Browse the repository at this point in the history
  • Loading branch information
dmachard committed Nov 10, 2021
1 parent b3c2513 commit c207612
Show file tree
Hide file tree
Showing 2 changed files with 48 additions and 0 deletions.
6 changes: 6 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

```
Expand Down
42 changes: 42 additions & 0 deletions loggers/syslog_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
}
}

0 comments on commit c207612

Please sign in to comment.