forked from dfuentes/haproxyparse
-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsing_test.go
185 lines (172 loc) · 5.45 KB
/
parsing_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
package main
import (
"regexp"
"testing"
"time"
"github.com/stretchr/testify/assert"
)
// TODO more tests
type valOrEmptyTest struct {
from map[string]string
val string
expected string
}
var valOrEmptyTests = []valOrEmptyTest{
{
from: map[string]string{"a": "b"},
val: "a",
expected: "b",
},
{
from: map[string]string{"a": "b"},
val: "c",
expected: "",
},
}
func TestValOrEmpty(t *testing.T) {
for _, testCase := range valOrEmptyTests {
actual := valOrEmpty(testCase.from, testCase.val)
assert.Equal(t, testCase.expected, actual)
}
}
type getMatchedSubexprTest struct {
regex *regexp.Regexp
toMatch string
expected map[string]string
}
var getMatchedSubexprTests = []getMatchedSubexprTest{
{
regex: regexp.MustCompile(`^(?P<first>\w+)\s(?P<second>\w+)$`),
toMatch: "first second",
expected: map[string]string{"first": "first", "second": "second"},
},
}
func TestGetMatchedSubexpr(t *testing.T) {
for _, testCase := range getMatchedSubexprTests {
actual := getMatchedSubexpr(testCase.regex, testCase.toMatch)
assert.Equal(t, testCase.expected, actual)
}
}
type parseLineTest struct {
line string
expected HAProxyLogLine
}
func parseTimeIgnoreError(s string) time.Time {
t, _ := time.Parse(DateFormat, s)
return t
}
var parseLineTests = []parseLineTest{
// {
// line: `Jan 1 00:00:00 local0 haproxy[2072]: 1.2.3.4:5555 [02/Dec/2014:16:41:06.226] front back/server 0/0/0/7/7 200 359 - - ---- 72/72/1/1/0 0/0 {|ELB-HealthChecker/1.0|Basic something} {|} "GET /elb/check HTTP/1.1"`,
// expected: HAProxyLogLine{
// ProcessId: "2072",
// ClientIp: "1.2.3.4",
// ClientPort: "5555",
// Date: "02/Dec/2014:16:41:06.226",
// FrontendName: "front",
// BackendName: "back",
// ServerName: "server",
// SendTime: "0",
// WaitTime: "0",
// ConnectionTime: "0",
// ResponseTime: "7",
// TotalTime: "7",
// StatusCode: "200",
// BytesRead: "359",
// RequestCookie: "-",
// ResponseCookie: "-",
// TerminationState: "----",
// ActiveConnections: "72",
// FrontendConnections: "72",
// BackendConnections: "1",
// ServerConnections: "1",
// Retries: "0",
// ServerQueue: "0",
// BackendQueue: "0",
// AuthHeader: "Basic something",
// ResponseHeaders: "|",
// HttpMethod: "GET",
// HttpUri: "/elb/check",
// UriComplete: true,
// Time: parseTimeIgnoreError("02/Dec/2014:16:41:06.226"),
// },
// },
// {
// line: `Dec 2 16:41:06 local0 haproxy[2072]: 1.2.3.4:5555 [02/Dec/2014:16:41:06.226] front back/server 0/0/0/7/7 200 359 - - ---- 72/72/1/1/0 0/0 {|ELB-HealthChecker/1.0|} {|} "GET /elb/check`,
// expected: HAProxyLogLine{
// ProcessId: "2072",
// ClientIp: "1.2.3.4",
// ClientPort: "5555",
// Date: "02/Dec/2014:16:41:06.226",
// FrontendName: "front",
// BackendName: "back",
// ServerName: "server",
// SendTime: "0",
// WaitTime: "0",
// ConnectionTime: "0",
// ResponseTime: "7",
// TotalTime: "7",
// StatusCode: "200",
// BytesRead: "359",
// RequestCookie: "-",
// ResponseCookie: "-",
// TerminationState: "----",
// ActiveConnections: "72",
// FrontendConnections: "72",
// BackendConnections: "1",
// ServerConnections: "1",
// Retries: "0",
// ServerQueue: "0",
// BackendQueue: "0",
// AuthHeader: "",
// ResponseHeaders: "|",
// HttpMethod: "GET",
// HttpUri: "/elb/check",
// UriComplete: false,
// Time: parseTimeIgnoreError("02/Dec/2014:16:41:06.226"),
// },
// },
{
line: `Jan 1 00:00:00 local0 haproxy[2344]: 1.2.3.4 - - [02/Dec/2014:16:41:32 +0000] "GET /elb/check HTTP/1.1" 200 359 "" "" 5555 073 "front" "back" "server" 4995 0 0 5 5000 ---- 72 72 1 1 0 0 0 "" "" "" "ELB-HealthChecker/1.0" "Basic something"`,
expected: HAProxyLogLine{
ProcessId: "2344",
ClientIp: "1.2.3.4",
ClientPort: "5555",
Date: "02/Dec/2014:16:41:32 +0000",
FrontendName: "front",
BackendName: "back",
ServerName: "server",
SendTime: "4995",
WaitTime: "0",
ConnectionTime: "0",
ResponseTime: "5",
TotalTime: "5000",
StatusCode: "200",
BytesRead: "359",
RequestCookie: "",
ResponseCookie: "",
TerminationState: "----",
ActiveConnections: "72",
FrontendConnections: "72",
BackendConnections: "1",
ServerConnections: "1",
Retries: "0",
ServerQueue: "0",
BackendQueue: "0",
XForward: "",
UserAgent: "ELB-HealthChecker/1.0",
AuthHeader: "Basic something",
HttpMethod: "GET",
HttpUri: "/elb/check",
UriComplete: true,
Time: parseTimeIgnoreError("02/Dec/2014:16:41:32 +0000"),
},
},
}
func TestParseLine(t *testing.T) {
for _, testCase := range parseLineTests {
actual, err := parseLine(testCase.line)
assert.Nil(t, err)
assert.Equal(t, testCase.expected, actual)
}
}