forked from digitalocean/captainslog
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsyslogmsg_test.go
More file actions
212 lines (188 loc) · 6.45 KB
/
syslogmsg_test.go
File metadata and controls
212 lines (188 loc) · 6.45 KB
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
package captainslog_test
import (
"strings"
"testing"
"time"
"github.com/digitalocean/captainslog"
)
func TestSyslogMsgToStringWithPid(t *testing.T) {
input := []byte("<4>2016-03-08T14:59:36.293816+00:00 host.example.com kernel[12]: test\n")
msg, err := captainslog.NewSyslogMsgFromBytes(input)
if err != nil {
t.Error(err)
}
wanted := "<4>2016-03-08T14:59:36.293816+00:00 host.example.com kernel[12]: test\n"
if want, got := wanted, msg.String(); want != got {
t.Errorf("want %q, got %q", want, got)
}
}
func TestSyslogMsgPlainWithAddedKeys(t *testing.T) {
input := []byte("<4>2016-03-08T14:59:36.293816+00:00 host.example.com kernel: [15803005.789011] ------------[ cut here ]------------\n")
msg := captainslog.NewSyslogMsg()
msg, err := captainslog.NewSyslogMsgFromBytes(input)
if err != nil {
t.Error(err)
}
msg.JSONValues["tags"] = []string{"trace"}
rfc3164 := msg.String()
if want, got := true, strings.Contains(rfc3164, "tags"); want != got {
t.Errorf("want '%v', got '%v'", want, got)
}
if want, got := true, strings.Contains(rfc3164, "msg"); want != got {
t.Errorf("want '%v', got '%v'", want, got)
}
}
func TestSyslogMsgJSONFromPlain(t *testing.T) {
input := []byte("<4>2016-03-08T14:59:36.293816+00:00 host.example.com kernel: test\n")
msg, err := captainslog.NewSyslogMsgFromBytes(input)
if err != nil {
t.Error(err)
}
output, err := msg.JSON()
if err != nil {
t.Error(err)
}
wanted := `{"syslog_content":" test","syslog_facilitytext":"kern","syslog_host":"host.example.com","syslog_pid":"","syslog_programname":"kernel","syslog_severitytext":"warning","syslog_tag":"kernel:","syslog_time":"2016-03-08T14:59:36.293816Z"}`
if want, got := wanted, string(output); want != got {
t.Errorf("want %q, got %q", want, got)
}
}
func TestSyslogMsgJSONFromCEE(t *testing.T) {
input := []byte("<4>2016-03-08T14:59:36.293816+00:00 host.example.com test[12]: @cee:{\"a\":1}\n")
msg, err := captainslog.NewSyslogMsgFromBytes(input)
if err != nil {
t.Error(err)
}
output, err := msg.JSON()
if err != nil {
t.Error(err)
}
wanted := `{"a":1,"syslog_facilitytext":"kern","syslog_host":"host.example.com","syslog_pid":"12","syslog_programname":"test","syslog_severitytext":"warning","syslog_tag":"test[12]:","syslog_time":"2016-03-08T14:59:36.293816Z"}`
if want, got := wanted, string(output); want != got {
t.Errorf("want %q, got %q", want, got)
}
}
func TestSyslogMsgJSONFromCEEWithDontParseJSON(t *testing.T) {
input := []byte("<4>2016-03-08T14:59:36.293816+00:00 host.example.com kernel: @cee:{\"a\":1}\n")
msg, err := captainslog.NewSyslogMsgFromBytes(input, captainslog.OptionDontParseJSON)
if err != nil {
t.Error(err)
}
output, err := msg.JSON()
if err != nil {
t.Error(err)
}
wanted := `{"a":1,"syslog_facilitytext":"kern","syslog_host":"host.example.com","syslog_pid":"","syslog_programname":"kernel","syslog_severitytext":"warning","syslog_tag":"kernel:","syslog_time":"2016-03-08T14:59:36.293816Z"}`
if want, got := wanted, string(output); want != got {
t.Errorf("want %q, got %q", want, got)
}
}
func TestNginxToSyslogMsgBackToString(t *testing.T) {
input := []byte("<174>2017-04-12T13:31:11.918068+00:00 www.example.com nginx 192.168.1.1 - - [12/Apr/2017:13:31:11 +0000] \"GET /hello?from=world HTTP/1.1\" 200 18 \"https://something.example.com\" \"Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/57.0.2987.133 Safari/537.36\"")
msg, err := captainslog.NewSyslogMsgFromBytes(input)
if err != nil {
t.Error(err)
}
wanted := string(input) + "\n"
if want, got := wanted, msg.String(); want != got {
t.Errorf("want %q, got %q", want, got)
}
}
func TestNewSyslogMsg(t *testing.T) {
type tag struct {
key string
value string
}
testCases := []struct {
name string
options []captainslog.SyslogMsgOption
content string
facility captainslog.Facility
severity captainslog.Severity
timeString string
timeFormat string
program string
pid string
host string
tags []tag
jsonKeys []string
want string
}{
{
name: "constructing plain syslog message",
options: []captainslog.SyslogMsgOption{},
content: "this is a non json message",
facility: captainslog.Local7,
severity: captainslog.Err,
timeString: "2017 Aug 15 16:18:34",
timeFormat: "2006 Jan 02 15:04:05",
program: "test",
pid: "12",
host: "host.example.com",
want: "<187>2017-08-15T16:18:34+00:00 host.example.com test[12]:this is a non json message\n",
},
{
name: "emitting a message in local syslog format",
options: []captainslog.SyslogMsgOption{captainslog.OptionUseLocalFormat},
content: "this is a non json message",
facility: captainslog.Local7,
severity: captainslog.Err,
timeString: "2017 Aug 15 16:18:34",
timeFormat: "2006 Jan 02 15:04:05",
program: "test",
pid: "12",
host: "host.example.com",
want: "<187>Aug 15 16:18:34 test[12]:this is a non json message\n",
},
{
name: "emitting a JSON message in local syslog format",
options: []captainslog.SyslogMsgOption{captainslog.OptionUseLocalFormat},
content: "{\"level\":\"info\",\"msg\":\"test message\"}",
facility: captainslog.Local6,
severity: captainslog.Info,
timeString: "2017 Aug 15 16:18:34",
timeFormat: "2006 Jan 02 15:04:05",
program: "test",
pid: "12",
host: "host.example.com",
tags: []tag{
tag{
key: "sometag",
value: "somevalue",
},
},
jsonKeys: []string{"level", "msg"},
want: "<182>Aug 15 16:18:34 test[12]: {\"level\":\"info\",\"msg\":\"test message\",\"sometag\":\"somevalue\"}\n",
},
}
for _, tc := range testCases {
t.Run(tc.name, func(t *testing.T) {
msg := captainslog.NewSyslogMsg(tc.options...)
msg.SetFacility(tc.facility)
msg.SetSeverity(tc.severity)
msgTime, err := time.Parse(tc.timeFormat, tc.timeString)
if err != nil {
t.Error(err)
}
msg.SetTime(msgTime)
msg.SetProgram(tc.program)
msg.SetPid(tc.pid)
msg.SetHost(tc.host)
err = msg.SetContent(tc.content)
if err != nil {
t.Error(err)
}
for _, t := range tc.tags {
msg.AddTag(t.key, t.value)
}
for _, v := range tc.jsonKeys {
if _, ok := msg.JSONValues[v]; !ok {
t.Errorf("could not find expected key %q in msg.JSONValues", v)
}
}
if want, got := tc.want, msg.String(); want != got {
t.Errorf("want %q, got %q", want, got)
}
})
}
}