-
-
Notifications
You must be signed in to change notification settings - Fork 4
/
parse_test.go
252 lines (243 loc) · 5.82 KB
/
parse_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
package parsers_test
import (
"testing"
"github.com/tj/assert"
"github.com/apex/parsers"
)
var cases = []struct {
Label string
Input string
Output parsers.Event
}{
{
Label: "Lambda start",
Input: "START RequestId: f7172574-5884-44d9-95f4-7438fb83e9b0 Version: 26",
Output: &parsers.AWSLambdaStart{
RequestID: "f7172574-5884-44d9-95f4-7438fb83e9b0",
Version: "26",
},
},
{
Label: "Lambda start with $LATEST",
Input: "START RequestId: f7172574-5884-44d9-95f4-7438fb83e9b0 Version: $LATEST",
Output: &parsers.AWSLambdaStart{
RequestID: "f7172574-5884-44d9-95f4-7438fb83e9b0",
Version: "$LATEST",
},
},
{
Label: "Lambda end",
Input: "END RequestId: f7172574-5884-44d9-95f4-7438fb83e9b0",
Output: &parsers.AWSLambdaEnd{
RequestID: "f7172574-5884-44d9-95f4-7438fb83e9b0",
},
},
{
Label: "Lambda report",
Input: "REPORT RequestId: 136f2f48-069e-4808-8d73-b31c4d97e146\tDuration: 7.80 ms\tBilled Duration: 100 ms\tMemory Size: 512 MB\tMax Memory Used: 115 MB\t",
Output: &parsers.AWSLambdaReport{
RequestID: "136f2f48-069e-4808-8d73-b31c4d97e146",
Duration: 7.8,
BilledDuration: 100,
MemorySize: 512,
MaxMemoryUsed: 115,
},
},
{
Label: "Lambda report with init duration",
Input: "REPORT RequestId: 136f2f48-069e-4808-8d73-b31c4d97e146\tDuration: 7.80 ms\tBilled Duration: 100 ms\tMemory Size: 512 MB\tMax Memory Used: 115 MB\tInit Duration: 185.62 ms\t",
Output: &parsers.AWSLambdaReportInit{
RequestID: "136f2f48-069e-4808-8d73-b31c4d97e146",
Duration: 7.8,
BilledDuration: 100,
InitDuration: 185.62,
MemorySize: 512,
MaxMemoryUsed: 115,
},
},
{
Label: "Lambda timeout",
Input: "2020-08-19T09:20:47.075Z 8173dbda-4443-4bcd-8d4c-33704efa0f05 Task timed out after 30.03 seconds",
Output: &parsers.AWSLambdaTimeout{
Timestamp: "2020-08-19T09:20:47.075Z",
RequestID: "8173dbda-4443-4bcd-8d4c-33704efa0f05",
Duration: 30.03,
},
},
{
Label: "Heroku syslog",
Input: "<45>1 2020-08-28T10:38:06.285004+00:00 host app api - Some random message here",
Output: &parsers.Syslog{
Priority: 45,
SyslogVersion: 1,
Timestamp: "2020-08-28T10:38:06.285004+00:00",
Hostname: "host",
Appname: "app",
ProcID: "api",
MsgID: "-",
Message: "Some random message here",
},
},
{
Label: "Unmatched",
Input: `{ "some": "json" }`,
Output: nil,
},
{
Label: "Empty",
Input: ``,
Output: nil,
},
}
// Test parsing.
func TestParse(t *testing.T) {
for _, c := range cases {
t.Run(c.Label, func(t *testing.T) {
v, _ := parsers.Parse(c.Input)
assert.Equal(t, c.Output, v)
})
}
}
var herokuCases = []struct {
Label string
Input string
Output parsers.Event
}{
{
Label: "Heroku deployment",
Input: "Deploy 059375fe by user [email protected]",
Output: &parsers.HerokuDeploy{
Commit: "059375fe",
User: "[email protected]",
},
},
{
Label: "Heroku release",
Input: "Release v16 created by user [email protected]",
Output: &parsers.HerokuRelease{
Version: "v16",
User: "[email protected]",
},
},
{
Label: "Heroku rollback",
Input: "Rollback to v11 by user [email protected]",
Output: &parsers.HerokuRollback{
Version: "v11",
User: "[email protected]",
},
},
{
Label: "Heroku build start",
Input: "Build started by user [email protected]",
Output: &parsers.HerokuBuild{
User: "[email protected]",
},
},
{
Label: "Heroku state change",
Input: "State changed from starting to crashed",
Output: &parsers.HerokuStateChange{
From: "starting",
To: "crashed",
},
},
{
Label: "Heroku process exit",
Input: "Process exited with status 143",
Output: &parsers.HerokuProcessExit{
Status: 143,
},
},
{
Label: "Heroku starting process",
Input: "Starting process with command `node index.js`",
Output: &parsers.HerokuProcessStart{
Command: "node index.js",
},
},
{
Label: "Heroku listening",
Input: "Listening on 55766",
Output: &parsers.HerokuProcessListening{
Port: 55766,
},
},
{
Label: "Heroku set env var",
Input: "Set FOO config vars by user [email protected]",
Output: &parsers.HerokuConfigSet{
Variables: "FOO",
User: "[email protected]",
},
},
{
Label: "Heroku set env vars",
Input: "Set FOO, BAR config vars by user [email protected]",
Output: &parsers.HerokuConfigSet{
Variables: "FOO, BAR",
User: "[email protected]",
},
},
{
Label: "Heroku remove env vars",
Input: "Remove FOO config vars by user [email protected]",
Output: &parsers.HerokuConfigRemove{
Variables: "FOO",
User: "[email protected]",
},
},
{
Label: "Heroku scale 0 free",
Input: "Scaled to web@0:Free by user [email protected]",
Output: &parsers.HerokuScale{
Dynos: "web@0:Free",
User: "[email protected]",
},
},
{
Label: "Heroku scale 1 free",
Input: "Scaled to web@1:Free by user [email protected]",
Output: &parsers.HerokuScale{
Dynos: "web@1:Free",
User: "[email protected]",
},
},
{
Label: "Heroku scale multiple",
Input: "Scaled to web@1:Free worker@0:Free by user [email protected]",
Output: &parsers.HerokuScale{
Dynos: "web@1:Free worker@0:Free",
User: "[email protected]",
},
},
}
// Test parsing Heroku messages.
func TestParseHeroku(t *testing.T) {
for _, c := range herokuCases {
t.Run(c.Label, func(t *testing.T) {
v, _ := parsers.ParseHeroku(c.Input)
assert.Equal(t, c.Output, v)
})
}
}
// Benchmark parsing.
func BenchmarkParse(b *testing.B) {
for i := 0; i < b.N; i++ {
s := "REPORT RequestId: 136f2f48-069e-4808-8d73-b31c4d97e146\tDuration: 7.80 ms\tBilled Duration: 100 ms\tMemory Size: 512 MB\tMax Memory Used: 115 MB\t\n"
_, ok := parsers.Parse(s)
if !ok {
b.Fatal("failed parsing")
}
}
}
// Benchmark parsing.
func BenchmarkParseHeroku(b *testing.B) {
for i := 0; i < b.N; i++ {
s := "Set FOO, BAR config vars by user [email protected]"
_, ok := parsers.ParseHeroku(s)
if !ok {
b.Fatal("failed parsing")
}
}
}