forked from influxdata/telegraf
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmultiline_test.go
235 lines (186 loc) · 5.24 KB
/
multiline_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
package tail
import (
"bytes"
"testing"
"time"
"github.com/influxdata/telegraf/internal"
"github.com/stretchr/testify/assert"
)
func TestMultilineConfigOK(t *testing.T) {
c := &MultilineConfig{
Pattern: ".*",
MatchWhichLine: Previous,
}
_, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
}
func TestMultilineConfigError(t *testing.T) {
c := &MultilineConfig{
Pattern: "\xA0",
MatchWhichLine: Previous,
}
_, err := c.NewMultiline()
assert.Error(t, err, "The pattern was invalid")
}
func TestMultilineConfigTimeoutSpecified(t *testing.T) {
duration, _ := time.ParseDuration("10s")
c := &MultilineConfig{
Pattern: ".*",
MatchWhichLine: Previous,
Timeout: &internal.Duration{Duration: duration},
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
assert.Equal(t, duration, m.config.Timeout.Duration)
}
func TestMultilineConfigDefaultTimeout(t *testing.T) {
duration, _ := time.ParseDuration("5s")
c := &MultilineConfig{
Pattern: ".*",
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
assert.Equal(t, duration, m.config.Timeout.Duration)
}
func TestMultilineIsEnabled(t *testing.T) {
c := &MultilineConfig{
Pattern: ".*",
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
isEnabled := m.IsEnabled()
assert.True(t, isEnabled, "Should have been enabled")
}
func TestMultilineIsDisabled(t *testing.T) {
c := &MultilineConfig{
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
isEnabled := m.IsEnabled()
assert.False(t, isEnabled, "Should have been disabled")
}
func TestMultilineFlushEmpty(t *testing.T) {
c := &MultilineConfig{
Pattern: "^=>",
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
var buffer bytes.Buffer
text := m.Flush(&buffer)
assert.Empty(t, text)
}
func TestMultilineFlush(t *testing.T) {
c := &MultilineConfig{
Pattern: "^=>",
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
var buffer bytes.Buffer
buffer.WriteString("foo")
text := m.Flush(&buffer)
assert.Equal(t, "foo", text)
assert.Zero(t, buffer.Len())
}
func TestMultiLineProcessLinePrevious(t *testing.T) {
c := &MultilineConfig{
Pattern: "^=>",
MatchWhichLine: Previous,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
var buffer bytes.Buffer
text := m.ProcessLine("1", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
text = m.ProcessLine("=>2", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
text = m.ProcessLine("=>3", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
text = m.ProcessLine("4", &buffer)
assert.Equal(t, "1=>2=>3", text)
assert.NotZero(t, buffer.Len())
text = m.ProcessLine("5", &buffer)
assert.Equal(t, "4", text)
assert.Equal(t, "5", buffer.String())
}
func TestMultiLineProcessLineNext(t *testing.T) {
c := &MultilineConfig{
Pattern: "=>$",
MatchWhichLine: Next,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
var buffer bytes.Buffer
text := m.ProcessLine("1=>", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
text = m.ProcessLine("2=>", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
text = m.ProcessLine("3=>", &buffer)
assert.Empty(t, text)
assert.NotZero(t, buffer.Len())
text = m.ProcessLine("4", &buffer)
assert.Equal(t, "1=>2=>3=>4", text)
assert.Zero(t, buffer.Len())
text = m.ProcessLine("5", &buffer)
assert.Equal(t, "5", text)
assert.Zero(t, buffer.Len())
}
func TestMultiLineMatchStringWithInvertMatchFalse(t *testing.T) {
c := &MultilineConfig{
Pattern: "=>$",
MatchWhichLine: Next,
InvertMatch: false,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
matches1 := m.matchString("t=>")
matches2 := m.matchString("t")
assert.True(t, matches1)
assert.False(t, matches2)
}
func TestMultiLineMatchStringWithInvertTrue(t *testing.T) {
c := &MultilineConfig{
Pattern: "=>$",
MatchWhichLine: Next,
InvertMatch: true,
}
m, err := c.NewMultiline()
assert.NoError(t, err, "Configuration was OK.")
matches1 := m.matchString("t=>")
matches2 := m.matchString("t")
assert.False(t, matches1)
assert.True(t, matches2)
}
func TestMultilineWhat(t *testing.T) {
var w1 MultilineMatchWhichLine
w1.UnmarshalTOML([]byte(`"previous"`))
assert.Equal(t, Previous, w1)
var w2 MultilineMatchWhichLine
w2.UnmarshalTOML([]byte(`previous`))
assert.Equal(t, Previous, w2)
var w3 MultilineMatchWhichLine
w3.UnmarshalTOML([]byte(`'previous'`))
assert.Equal(t, Previous, w3)
var w4 MultilineMatchWhichLine
w4.UnmarshalTOML([]byte(`"next"`))
assert.Equal(t, Next, w4)
var w5 MultilineMatchWhichLine
w5.UnmarshalTOML([]byte(`next`))
assert.Equal(t, Next, w5)
var w6 MultilineMatchWhichLine
w6.UnmarshalTOML([]byte(`'next'`))
assert.Equal(t, Next, w6)
var w7 MultilineMatchWhichLine
err := w7.UnmarshalTOML([]byte(`nope`))
assert.Equal(t, MultilineMatchWhichLine(-1), w7)
assert.Error(t, err)
}