forked from papertrail/remote_syslog2
-
Notifications
You must be signed in to change notification settings - Fork 0
/
remote_syslog_test.go
298 lines (245 loc) · 5.68 KB
/
remote_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
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
package main
import (
"bufio"
"fmt"
"io"
"net"
"os"
"regexp"
"strconv"
"strings"
"sync"
"testing"
"time"
"github.com/papertrail/remote_syslog2/syslog"
"github.com/stretchr/testify/assert"
)
const (
tmpdir = "./tmp"
)
var (
server *testSyslogServer
)
// main testing function to clean up after running
func TestMain(m *testing.M) {
os.Mkdir(tmpdir, 0755)
server = newTestSyslogServer("127.0.0.1:0")
go server.serve()
rs := m.Run()
server.closeCh <- struct{}{}
os.RemoveAll(tmpdir)
os.Exit(rs)
}
func TestFilters(t *testing.T) {
expressions := []*regexp.Regexp{}
expressions = append(expressions, regexp.MustCompile("\\d+"))
message := "test message"
if matchExps(message, expressions) {
t.Errorf("Did not expect \"%s\" to match \"%s\"", message, expressions[0])
}
message = "0000"
if !matchExps(message, expressions) {
t.Errorf("Expected \"%s\" to match \"%s\"", message, expressions[0])
}
}
func TestNewFileSeek(t *testing.T) {
assert := assert.New(t)
s := NewServer(testConfig())
go s.Start()
defer s.Close()
// just a quick rest to get the server started
time.Sleep(1 * time.Second)
for _, msg := range []string{
"welcome to the jungle",
"we got alerts and logs",
"we got everything you want",
"as long as it's alerts and logs",
} {
file := tmpLogFile()
defer file.Close()
writeLog(file, msg)
packet := <-server.packets
assert.Equal(msg, packet.Message)
}
}
func TestGlobCollisions(t *testing.T) {
assert := assert.New(t)
// Make sure we're running on a clean directory
os.RemoveAll(tmpdir)
os.Mkdir(tmpdir, 0755)
// Add colliding globs
config := testConfig()
config.Files = append(config.Files, LogFile{
Path: "tmp/*.log",
})
// Use an observable registry
testRegistry := &testRegistry{workers: make(map[string]int)}
s := NewServer(config)
s.registry = testRegistry
go s.Start()
defer s.Close()
// just a quick rest to get the server started
time.Sleep(1 * time.Second)
var files []*os.File
for i := 0; i < 50; i++ {
file := tmpLogFile()
files = append(files, file)
writeLog(file, "the most important message"+strconv.Itoa(i))
}
// NewFileCheckInterval = 1 second, so wait 1100ms for messages
time.Sleep(3000 * time.Millisecond)
testRegistry.mu.RLock()
for file, forwardCount := range testRegistry.workers {
assert.Equal(1, forwardCount, "Expected %s to be added once, got %d", file, forwardCount)
}
testRegistry.mu.RUnlock()
for _, file := range files {
file.Close()
}
}
// write to test log file
func writeLog(file *os.File, msg string) {
w := bufio.NewWriterSize(file, 1024*32)
if _, err := w.WriteString(msg + "\n"); err != nil {
panic(err)
}
w.Flush()
}
// creates a log file that matches our pattern (tmp/*.log)
func tmpLogFile() *os.File {
file, err := os.Create(fmt.Sprintf("tmp/%d.log", time.Now().UnixNano()))
if err != nil {
panic(err)
}
return file
}
func testConfig() *Config {
severity, _ := syslog.Severity("info")
facility, _ := syslog.Facility("user")
addr := server.addr()
return &Config{
ConnectTimeout: 10 * time.Second,
WriteTimeout: 10 * time.Second,
NewFileCheckInterval: 1 * time.Second,
LogLevels: "<root>=INFO",
TcpMaxLineLength: 99990,
NoDetach: true,
Hostname: "testhost",
Severity: severity,
Facility: facility,
Destination: struct {
Host string
Port int
Protocol string
}{
Host: addr.host,
Port: addr.port,
Protocol: "tcp",
},
Files: []LogFile{
{
Path: "tmp/*.log",
},
},
}
}
type testSyslogServer struct {
listener net.Listener
closeCh chan struct{}
packets chan syslog.Packet
}
type testAddr struct {
host string
port int
}
// testSyslogServer is a type for testing sent syslog messages
func newTestSyslogServer(addr string) *testSyslogServer {
var (
err error
s = &testSyslogServer{
closeCh: make(chan struct{}),
packets: make(chan syslog.Packet),
}
)
s.listener, err = net.Listen("tcp", addr)
if err != nil {
panic(err)
}
return s
}
func (s *testSyslogServer) addr() testAddr {
addr := s.listener.Addr()
addrParts := strings.Split(addr.String(), ":")
if len(addrParts) != 2 {
panic(fmt.Sprintf("invalid listener address: %s", addr.String()))
}
port, err := strconv.Atoi(addrParts[1])
if err != nil {
panic(err)
}
return testAddr{
host: addrParts[0],
port: port,
}
}
func (s *testSyslogServer) serve() {
for {
select {
case <-s.closeCh:
return
default:
conn, err := s.listener.Accept()
if err != nil {
panic(err)
}
reader := bufio.NewReader(conn)
for {
select {
case <-s.closeCh:
return
default:
line, err := reader.ReadString('\n')
if err != nil && err != io.EOF {
panic(err)
}
if err == io.EOF {
time.Sleep(100 * time.Millisecond)
continue
}
fmt.Printf(line)
packet, err := syslog.Parse(strings.TrimRight(line, "\n"))
if err != nil {
panic(err)
}
select {
case s.packets <- packet:
case <-time.After(time.Second):
}
}
}
}
}
}
// testRegistry is a WorkerRegistry implementation that keeps track of how many times a file was added
type testRegistry struct {
mu sync.RWMutex
workers map[string]int
}
func (tr *testRegistry) Exists(worker string) bool {
tr.mu.RLock()
defer tr.mu.RUnlock()
_, ok := tr.workers[worker]
return ok
}
func (tr *testRegistry) Add(worker string) {
tr.mu.Lock()
defer tr.mu.Unlock()
log.Tracef("Adding %s to worker registry", worker)
tr.workers[worker] += 1
}
func (tr *testRegistry) Remove(worker string) {
tr.mu.Lock()
defer tr.mu.Unlock()
log.Tracef("Removing %s from worker registry", worker)
delete(tr.workers, worker)
}