forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
syslog_test.go
205 lines (164 loc) · 5.25 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
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
package syslog
import (
"net"
"sync"
"testing"
"time"
"github.com/influxdata/telegraf"
framing "github.com/influxdata/telegraf/internal/syslog"
"github.com/influxdata/telegraf/metric"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
)
func TestGetSyslogMessageWithFramingOctectCounting(t *testing.T) {
// Init plugin
s := newSyslog()
s.initializeSyslogMapper()
// Init metrics
m1, _ := metric.New(
"testmetric",
map[string]string{
"hostname": "testhost",
},
map[string]interface{}{},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(m1)
require.NoError(t, err)
messageBytesWithFraming, err := s.getSyslogMessageBytesWithFraming(syslogMessage)
require.NoError(t, err)
assert.Equal(t, "59 <13>1 2010-11-10T23:00:00Z testhost Telegraf - testmetric -", string(messageBytesWithFraming), "Incorrect Octect counting framing")
}
func TestGetSyslogMessageWithFramingNonTransparent(t *testing.T) {
// Init plugin
s := newSyslog()
s.initializeSyslogMapper()
s.Framing = framing.NonTransparent
// Init metrics
m1, _ := metric.New(
"testmetric",
map[string]string{
"hostname": "testhost",
},
map[string]interface{}{},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC),
)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(m1)
require.NoError(t, err)
messageBytesWithFraming, err := s.getSyslogMessageBytesWithFraming(syslogMessage)
require.NoError(t, err)
assert.Equal(t, "<13>1 2010-11-10T23:00:00Z testhost Telegraf - testmetric -\x00", string(messageBytesWithFraming), "Incorrect Octect counting framing")
}
func TestSyslogWriteWithTcp(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
s := newSyslog()
s.Address = "tcp://" + listener.Addr().String()
err = s.Connect()
require.NoError(t, err)
lconn, err := listener.Accept()
require.NoError(t, err)
testSyslogWriteWithStream(t, s, lconn)
}
func TestSyslogWriteWithUdp(t *testing.T) {
listener, err := net.ListenPacket("udp", "127.0.0.1:0")
require.NoError(t, err)
s := newSyslog()
s.Address = "udp://" + listener.LocalAddr().String()
err = s.Connect()
require.NoError(t, err)
testSyslogWriteWithPacket(t, s, listener)
}
func testSyslogWriteWithStream(t *testing.T, s *Syslog, lconn net.Conn) {
metrics := []telegraf.Metric{}
m1, _ := metric.New(
"testmetric",
map[string]string{},
map[string]interface{}{},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC))
metrics = append(metrics, m1)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(metrics[0])
require.NoError(t, err)
messageBytesWithFraming, err := s.getSyslogMessageBytesWithFraming(syslogMessage)
require.NoError(t, err)
err = s.Write(metrics)
require.NoError(t, err)
buf := make([]byte, 256)
n, err := lconn.Read(buf)
require.NoError(t, err)
assert.Equal(t, string(messageBytesWithFraming), string(buf[:n]))
}
func testSyslogWriteWithPacket(t *testing.T, s *Syslog, lconn net.PacketConn) {
s.Framing = framing.NonTransparent
metrics := []telegraf.Metric{}
m1, _ := metric.New(
"testmetric",
map[string]string{},
map[string]interface{}{},
time.Date(2010, time.November, 10, 23, 0, 0, 0, time.UTC))
metrics = append(metrics, m1)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(metrics[0])
require.NoError(t, err)
messageBytesWithFraming, err := s.getSyslogMessageBytesWithFraming(syslogMessage)
require.NoError(t, err)
err = s.Write(metrics)
require.NoError(t, err)
buf := make([]byte, 256)
n, _, err := lconn.ReadFrom(buf)
require.NoError(t, err)
assert.Equal(t, string(messageBytesWithFraming), string(buf[:n]))
}
func TestSyslogWriteErr(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
s := newSyslog()
s.Address = "tcp://" + listener.Addr().String()
err = s.Connect()
require.NoError(t, err)
s.Conn.(*net.TCPConn).SetReadBuffer(256)
lconn, err := listener.Accept()
require.NoError(t, err)
lconn.(*net.TCPConn).SetWriteBuffer(256)
metrics := []telegraf.Metric{testutil.TestMetric(1, "testerr")}
// close the socket to generate an error
lconn.Close()
s.Conn.Close()
err = s.Write(metrics)
require.Error(t, err)
assert.Nil(t, s.Conn)
}
func TestSyslogWriteReconnect(t *testing.T) {
listener, err := net.Listen("tcp", "127.0.0.1:0")
require.NoError(t, err)
s := newSyslog()
s.Address = "tcp://" + listener.Addr().String()
err = s.Connect()
require.NoError(t, err)
s.Conn.(*net.TCPConn).SetReadBuffer(256)
lconn, err := listener.Accept()
require.NoError(t, err)
lconn.(*net.TCPConn).SetWriteBuffer(256)
lconn.Close()
s.Conn = nil
wg := sync.WaitGroup{}
wg.Add(1)
var lerr error
go func() {
lconn, lerr = listener.Accept()
wg.Done()
}()
metrics := []telegraf.Metric{testutil.TestMetric(1, "testerr")}
err = s.Write(metrics)
require.NoError(t, err)
wg.Wait()
assert.NoError(t, lerr)
syslogMessage, err := s.mapper.MapMetricToSyslogMessage(metrics[0])
require.NoError(t, err)
messageBytesWithFraming, err := s.getSyslogMessageBytesWithFraming(syslogMessage)
require.NoError(t, err)
buf := make([]byte, 256)
n, err := lconn.Read(buf)
require.NoError(t, err)
assert.Equal(t, string(messageBytesWithFraming), string(buf[:n]))
}