forked from RackSec/srslog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
formatter_test.go
57 lines (51 loc) · 1.63 KB
/
formatter_test.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
package srslog
import (
"fmt"
"os"
"testing"
"time"
"strings"
)
func TestDefaultFormatter(t *testing.T) {
out := DefaultFormatter(LOG_ERR, "hostname", "tag", "content")
expected := fmt.Sprintf("<%d> %s %s %s[%d]: %s",
LOG_ERR, time.Now().Format(time.RFC3339), "hostname", "tag", os.Getpid(), "content")
if out != expected {
t.Errorf("expected %v got %v", expected, out)
}
}
func TestUnixFormatter(t *testing.T) {
out := UnixFormatter(LOG_ERR, "hostname", "tag", "content")
expected := fmt.Sprintf("<%d>%s %s[%d]: %s",
LOG_ERR, time.Now().Format(time.Stamp), "tag", os.Getpid(), "content")
if out != expected {
t.Errorf("expected %v got %v", expected, out)
}
}
func TestRFC3164Formatter(t *testing.T) {
out := RFC3164Formatter(LOG_ERR, "hostname", "tag", "content")
expected := fmt.Sprintf("<%d>%s %s %s[%d]: %s",
LOG_ERR, time.Now().Format(time.Stamp), "hostname", "tag", os.Getpid(), "content")
if out != expected {
t.Errorf("expected %v got %v", expected, out)
}
}
func TestRFC5424Formatter(t *testing.T) {
out := RFC5424Formatter(LOG_ERR, "hostname", "tag", "content")
expected := fmt.Sprintf("<%d>%d %s %s %s %d %s - %s",
LOG_ERR, 1, time.Now().Format(time.RFC3339), "hostname", truncateStartStr(os.Args[0], appNameMaxLength),
os.Getpid(), "tag", "content")
if out != expected {
t.Errorf("expected %v got %v", expected, out)
}
}
func TestTruncateStartStr(t *testing.T) {
out := truncateStartStr("abcde", 3)
if strings.Compare(out, "cde" ) != 0 {
t.Errorf("expected \"cde\" got %v", out)
}
out = truncateStartStr("abcde", 5)
if strings.Compare(out, "abcde" ) != 0 {
t.Errorf("expected \"abcde\" got %v", out)
}
}