forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathrfc3164_test.go
123 lines (111 loc) · 2.74 KB
/
rfc3164_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
package syslog
import (
"fmt"
"net"
"testing"
"time"
"github.com/influxdata/telegraf"
"github.com/influxdata/telegraf/testutil"
"github.com/stretchr/testify/require"
)
func timeMustParse(value string) time.Time {
format := "Jan 2 15:04:05 2006"
t, err := time.Parse(format, value)
if err != nil {
panic(fmt.Sprintf("couldn't parse time: %v", value))
}
return t
}
func getTestCasesForRFC3164() []testCasePacket {
currentYear := time.Now().Year()
ts := timeMustParse(fmt.Sprintf("Dec 2 16:31:03 %d", currentYear)).UnixNano()
testCases := []testCasePacket{
{
name: "complete",
data: []byte("<13>Dec 2 16:31:03 host app: Test"),
wantBestEffort: testutil.MustMetric(
"syslog",
map[string]string{
"appname": "app",
"severity": "notice",
"hostname": "host",
"facility": "user",
},
map[string]interface{}{
"timestamp": ts,
"message": "Test",
"facility_code": 1,
"severity_code": 5,
},
defaultTime,
),
wantStrict: testutil.MustMetric(
"syslog",
map[string]string{
"appname": "app",
"severity": "notice",
"hostname": "host",
"facility": "user",
},
map[string]interface{}{
"timestamp": ts,
"message": "Test",
"facility_code": 1,
"severity_code": 5,
},
defaultTime,
),
},
}
return testCases
}
func testRFC3164(t *testing.T, protocol string, address string, bestEffort bool) {
for _, tc := range getTestCasesForRFC3164() {
t.Run(tc.name, func(t *testing.T) {
// Create receiver
receiver := newUDPSyslogReceiver(protocol+"://"+address, bestEffort, syslogRFC3164)
acc := &testutil.Accumulator{}
require.NoError(t, receiver.Start(acc))
defer receiver.Stop()
// Connect
conn, err := net.Dial(protocol, address)
require.NotNil(t, conn)
require.NoError(t, err)
// Write
_, err = conn.Write(tc.data)
conn.Close()
if err != nil {
if err, ok := err.(*net.OpError); ok {
if err.Err.Error() == "write: message too long" {
return
}
}
}
// Waiting ...
if tc.wantStrict == nil && tc.werr || bestEffort && tc.werr {
acc.WaitError(1)
}
if tc.wantBestEffort != nil && bestEffort || tc.wantStrict != nil && !bestEffort {
acc.Wait(1) // RFC3164 mandates a syslog message per UDP packet
}
// Compare
var got telegraf.Metric
var want telegraf.Metric
if len(acc.Metrics) > 0 {
got = acc.GetTelegrafMetrics()[0]
}
if bestEffort {
want = tc.wantBestEffort
} else {
want = tc.wantStrict
}
testutil.RequireMetricEqual(t, want, got)
})
}
}
func TestRFC3164BestEffort_udp(t *testing.T) {
testRFC3164(t, "udp", address, true)
}
func TestRFC3164Strict_udp(t *testing.T) {
testRFC3164(t, "udp", address, false)
}