This repository has been archived by the owner on May 25, 2018. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
syslog_test.go
173 lines (149 loc) · 3.82 KB
/
syslog_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
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
package golog
import (
"net"
"runtime"
"sort"
"strconv"
"strings"
"sync"
"testing"
"time"
"os"
)
func checkOutput(result string, f Facility, p Priority, prefix, msg string, t *testing.T) {
expectedStart := "<" + strconv.Itoa(int(f)*8+int(p)) + ">"
expectedEnd := p.String() + ": " + prefix + msg
if !strings.HasPrefix(result, expectedStart) || !strings.HasSuffix(result, expectedEnd) {
errmsg := "Failed log consistency check:\nExpected '%s'\nResult '%s'"
t.Errorf(errmsg, expectedStart+" ... "+expectedEnd, result)
}
}
func syslogIsAvailable() bool {
return runtime.GOOS != "windows" && os.Getenv("IN_DOCKER_FLAG") != "1"
}
func runSyslogReader(c net.PacketConn, msgChan chan<- string) {
var buf [4096]byte
var rcvd string = ""
for {
n, _, err := c.ReadFrom(buf[0:])
if err != nil || n == 0 {
break
}
rcvd += string(buf[0:n])
}
msgChan <- rcvd
c.Close()
}
func startServer(msgChan chan<- string) (serverAddr string, err error) {
c, e := net.ListenPacket("udp", "127.0.0.1:0")
if e != nil {
return "", e
}
serverAddr = c.LocalAddr().String()
c.SetReadDeadline(time.Now().Add(100 * time.Millisecond))
go runSyslogReader(c, msgChan)
return serverAddr, nil
}
func checkSyslogNewProcessor(f Facility, p Priority, t *testing.T) {
proc, err := NewSyslogProcessor(f, p)
if err != nil {
t.Fatalf("NewSyslogProcessor(f=%d, p=%d) failed: %s", f, p, err.Error())
} else {
proc.Close()
}
}
func TestNew(t *testing.T) {
if !syslogIsAvailable() {
return
}
checkSyslogNewProcessor(LOCAL0, LOG_DEBUG, t)
}
func TestDialSyslog(t *testing.T) {
if !syslogIsAvailable() {
return
}
conn, err := DialSyslog("", "")
if err != nil {
t.Fatalf("Couldn't connect to syslog: %s", err.Error())
} else {
conn.Close()
}
}
func createSyslogger(servAddy, prefix string, f Facility, p Priority, t *testing.T) *Logger {
sysProc, err := NewSyslogProcessorAt("udp", servAddy, f, p)
if err != nil {
t.Fatalf("Coudln't create processor to listen to syslog: %s", err.Error())
}
logger := NewLogger(prefix)
logger.AddProcessor("syslog", sysProc)
return logger
}
// essentially closes the log
func closeSyslog(logger *Logger) {
logger.Close()
}
func checkSyslogPost(f Facility, p Priority, t *testing.T) {
msgChan := make(chan string)
servAddy, err := startServer(msgChan)
if err != nil {
t.Fatalf("Couldn't start syslog listener: %s", err.Error())
}
prefix := "syslog_single_test: "
minPriority := LOG_DEBUG
message := "Testing."
logger := createSyslogger(servAddy, prefix, f, minPriority, t)
logger.Logf(p, message)
rcvd := <-msgChan
checkOutput(rcvd, f, p, prefix, message+"\n", t)
closeSyslog(logger)
}
func TestSingleLogWrite(t *testing.T) {
if !syslogIsAvailable() {
return
}
checkSyslogPost(LOCAL0, LOG_INFO, t)
}
func TestMultipleLogWrites(t *testing.T) {
if !syslogIsAvailable() {
return
}
for _, f := range SyslogFacilities() {
for _, p := range Priorities() {
checkSyslogPost(f, p, t)
}
}
}
func TestConcurrentSyslogWrite(t *testing.T) {
if !syslogIsAvailable() {
return
}
msgChan := make(chan string)
servAddy, err := startServer(msgChan)
if err != nil {
t.Fatalf("Couldn't start syslog listener: %s", err.Error())
}
prefix := "syslog_conc_test: "
facility := LOCAL0
minPriority := LOG_DEBUG
logger := createSyslogger(servAddy, prefix, facility, minPriority, t)
total_routines := 50
var wg sync.WaitGroup
for i := 0; i < total_routines; i++ {
var tmp int = i
wg.Add(1)
go func() {
logger.Infof("Testing routine %08d", tmp)
wg.Done()
}()
}
wg.Wait()
dur, _ := time.ParseDuration("2s")
time.Sleep(dur)
logs := <-msgChan
log_lines := strings.Split(strings.TrimSpace(logs), "\n")
sort.Strings(log_lines)
if len(log_lines) != total_routines {
errmsg := "Some log lines are missing! Expected %d, Found %d"
t.Fatalf(errmsg, total_routines, len(log_lines))
}
}