Skip to content

Commit 551446d

Browse files
author
Matthew Fisher
committed
test(logger): add log message tests
1 parent f77cfe0 commit 551446d

1 file changed

Lines changed: 80 additions & 0 deletions

File tree

logger/syslog/message_test.go

Lines changed: 80 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,80 @@
1+
package syslog
2+
3+
import (
4+
"net"
5+
"testing"
6+
"time"
7+
)
8+
9+
func TestMessageNetSrc(t *testing.T) {
10+
tcpAddress, err := net.ResolveTCPAddr("tcp", "localhost:1234")
11+
12+
if err != nil {
13+
t.Errorf("could not resolve TCP address")
14+
}
15+
16+
m := &Message{time.Now(), tcpAddress, 0, 0, time.Now(), "", "", "", "", ""}
17+
if m.NetSrc() != "127.0.0.1" {
18+
t.Errorf("expected 127.0.0.1, got ", m.NetSrc())
19+
}
20+
21+
udpAddress, err := net.ResolveUDPAddr("udp", "localhost:1234")
22+
23+
if err != nil {
24+
t.Errorf("could not resolve UDP address")
25+
}
26+
27+
m.Source = udpAddress
28+
if m.NetSrc() != "127.0.0.1" {
29+
t.Errorf("expected 127.0.0.1, got ", m.NetSrc())
30+
}
31+
32+
unixAddress, err := net.ResolveUnixAddr("unix", "/tmp/str")
33+
34+
if err != nil {
35+
t.Errorf("could not resolve unix address")
36+
}
37+
38+
m.Source = unixAddress
39+
if m.NetSrc() != "/tmp/str" {
40+
t.Errorf("expected /tmp/str, got ", m.NetSrc())
41+
}
42+
43+
unknownAddress, err := net.ResolveIPAddr("ip4", "localhost")
44+
45+
if err != nil {
46+
t.Errorf("could not resolve unknown address")
47+
}
48+
49+
m.Source = unknownAddress
50+
if m.NetSrc() != "127.0.0.1" {
51+
t.Errorf("expected 127.0.0.1, got ", m.NetSrc())
52+
}
53+
}
54+
55+
func TestMessageFormat(t *testing.T) {
56+
tcpAddress, err := net.ResolveTCPAddr("tcp", "localhost:1234")
57+
58+
if err != nil {
59+
t.Errorf("could not resolve TCP address")
60+
}
61+
62+
m := &Message{
63+
time.Now(),
64+
tcpAddress,
65+
0,
66+
0,
67+
time.Now(),
68+
"localhost",
69+
"TEST",
70+
"hello world",
71+
"",
72+
"",
73+
}
74+
75+
timeLayout := "2006-01-02 15:04:05"
76+
expectedOutput := m.Time.Format(timeLayout) + " localhost hello world"
77+
if m.String() != expectedOutput {
78+
t.Errorf("expected '" + expectedOutput + "', got '" + m.String() + "'.")
79+
}
80+
}

0 commit comments

Comments
 (0)