-
Notifications
You must be signed in to change notification settings - Fork 40
/
Copy pathcsv_test.go
214 lines (198 loc) · 4.85 KB
/
csv_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
package csv
import (
"reflect"
"regexp"
"testing"
"time"
"github.com/stretchr/testify/assert"
"github.com/honeycombio/honeytail/event"
"github.com/honeycombio/honeytail/parsers"
)
const (
commonLogFormatTimeLayout = "02/Jan/2006:15:04:05 -0700"
iso8601TimeLayout = "2006-01-02T15:04:05-07:00"
)
// Test Init(...) success/fail
type testInitMap struct {
options *Options
expectedPass bool
}
var testInitCases = []testInitMap{
{
expectedPass: true,
options: &Options{
NumParsers: 5,
TimeFieldName: "local_time",
TimeFieldFormat: "%d/%b/%Y:%H:%M:%S %z",
Fields: "one, two, three",
},
},
{
expectedPass: false,
options: &Options{
NumParsers: 5,
TimeFieldName: "local_time",
TimeFieldFormat: "%d/%b/%Y:%H:%M:%S %z",
Fields: "", // No fields specified should cause a failure
},
},
}
func TestInit(t *testing.T) {
for _, testCase := range testInitCases {
p := &Parser{}
err := p.Init(testCase.options)
if (err == nil) != testCase.expectedPass {
if err == nil {
t.Error("Parser Init(...) passed; expected it to fail.")
} else {
t.Error("Parser Init(...) failed; expected it to pass. Error:", err)
}
} else {
t.Logf("Init pass status is %t as expected", (err == nil))
}
}
}
type testLineMap struct {
fields string
input string
expected map[string]interface{}
err bool
}
var tlms = []testLineMap{
{
fields: "one,two,three",
input: "exx, why, zee",
expected: map[string]interface{}{
"one": "exx",
"two": " why",
"three": " zee",
},
},
{
// No data should return of error (field num mismatch)
fields: "one,two,three",
input: "",
expected: nil,
err: true,
},
{
// Too few fields should return an error
fields: "one,two,three",
input: "foo,bar",
expected: nil,
err: true,
},
{
// Too many fields should return an error
fields: "one, two",
input: "foo,bar,xyz",
expected: nil,
err: true,
},
{
// Test that int and float are converted successfully
fields: "one,two,three",
input: "1,2.4,xyz",
expected: map[string]interface{}{
"one": 1,
"two": 2.4,
"three": "xyz",
},
},
}
func TestParseLine(t *testing.T) {
for _, tlm := range tlms {
p := &Parser{}
err := p.Init(&Options{
Fields: tlm.fields,
})
assert.NoError(t, err, "could not instantiate parser with fields: %s", tlm.fields)
resp, err := p.lineParser.ParseLine(tlm.input)
t.Logf("%+v", resp)
if tlm.err {
assert.Error(t, err, "p.ParseLine did not return error as expected")
} else {
assert.NoError(t, err, "p.ParseLine unexpectedly returned error %v", err)
}
if !reflect.DeepEqual(resp, tlm.expected) {
t.Errorf("response %+v didn't match expected %+v", resp, tlm.expected)
}
}
}
func TestParseLineTrimWhitespace(t *testing.T) {
tlm := testLineMap{
fields: "one, two, three",
input: "exx, why, zee",
expected: map[string]interface{}{
"one": "exx",
"two": "why",
"three": "zee",
},
}
p := &Parser{}
err := p.Init(&Options{
Fields: tlm.fields,
TrimLeadingSpace: true,
})
assert.NoError(t, err, "could not instantiate parser with fields: %s", tlm.fields)
resp, err := p.lineParser.ParseLine(tlm.input)
t.Logf("%+v", resp)
if tlm.err {
assert.Error(t, err, "p.ParseLine did not return error as expected")
} else {
assert.NoError(t, err, "p.ParseLine unexpectedly returned error %v", err)
}
if !reflect.DeepEqual(resp, tlm.expected) {
t.Errorf("response %+v didn't match expected %+v", resp, tlm.expected)
}
}
type testLineMaps struct {
line string
trimmedLine string
resp map[string]interface{}
typedResp map[string]interface{}
ev event.Event
}
// Test event emitted from ProcessLines
func TestProcessLines(t *testing.T) {
t1, _ := time.ParseInLocation(commonLogFormatTimeLayout, "08/Oct/2015:00:26:26 -0000", time.UTC)
preReg := &parsers.ExtRegexp{regexp.MustCompile("^(?P<pre_hostname>[a-zA-Z-.]+): ")}
tlm := []testLineMaps{
{
line: "somehost: 08/Oct/2015:00:26:26 +0000,123,xyz,:::",
ev: event.Event{
Timestamp: t1,
Data: map[string]interface{}{
"pre_hostname": "somehost",
"one": 123,
"two": "xyz",
"three": ":::",
},
},
},
}
p := &Parser{}
err := p.Init(&Options{
NumParsers: 5,
TimeFieldName: "local_time",
TimeFieldFormat: "%d/%b/%Y:%H:%M:%S %z",
Fields: "local_time,one,two,three",
})
assert.NoError(t, err, "Couldn't instantiate Parser")
lines := make(chan string)
send := make(chan event.Event)
go func() {
for _, pair := range tlm {
lines <- pair.line
}
close(lines)
}()
go p.ProcessLines(lines, send, preReg)
for _, pair := range tlm {
resp := <-send
if !reflect.DeepEqual(resp, pair.ev) {
t.Fatalf("line resp didn't match up for %s. Expected: %+v, actual: %+v",
pair.line, pair.ev, resp)
}
}
}