forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterdate_test.go
283 lines (250 loc) · 6.28 KB
/
filterdate_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
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
package filterdate
import (
"context"
"fmt"
filterjson "github.com/tsaikd/gogstash/filter/json"
"strings"
"testing"
"time"
"github.com/sirupsen/logrus"
"github.com/stretchr/testify/assert"
"github.com/stretchr/testify/require"
"github.com/tsaikd/gogstash/config"
"github.com/tsaikd/gogstash/config/goglog"
"github.com/tsaikd/gogstash/config/logevent"
)
func init() {
goglog.Logger.SetLevel(logrus.DebugLevel)
config.RegistFilterHandler(ModuleName, InitHandler)
config.RegistFilterHandler(filterjson.ModuleName, filterjson.InitHandler)
}
func Test_filter_date_module(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
filter:
- type: date
format: ["02/Jan/2006:15:04:05 -0700"]
source: time_local
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
timestamp, err := time.Parse("2006-01-02T15:04:05Z", "2017-03-20T00:42:51Z")
require.NoError(err)
expectedEvent := logevent.LogEvent{
Timestamp: timestamp,
Extra: map[string]interface{}{
"time_local": "20/Mar/2017:00:42:51 +0000",
},
}
conf.TestInputEvent(logevent.LogEvent{
Extra: map[string]interface{}{
"time_local": "20/Mar/2017:00:42:51 +0000",
},
})
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal(expectedEvent, event)
}
}
func Test_filter_date_module_UNIX(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
filter:
- type: date
format: ["UNIX"]
source: time_local
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
timestamp, err := time.Parse("2006-01-02T15:04:05Z", "2019-01-23T09:57:51.471Z")
require.NoError(err)
expectedEvent := logevent.LogEvent{
Timestamp: timestamp,
Extra: map[string]interface{}{
"time_local": "1548237471.471",
},
}
conf.TestInputEvent(logevent.LogEvent{
Extra: map[string]interface{}{
"time_local": "1548237471.471",
},
})
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal(expectedEvent, event)
}
}
func Test_filter_date_module_UNIX_target(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
filter:
- type: date
format: ["UNIX"]
source: time_local
target: mytimestamp
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
timestamp, err := time.Parse("2006-01-02T15:04:05Z", "2019-01-23T09:57:51.471Z")
require.NoError(err)
eventIn := logevent.LogEvent{
Extra: map[string]interface{}{
"time_local": "1548237471.471",
},
}
expectedEvent := logevent.LogEvent{
Timestamp: eventIn.Timestamp,
Extra: map[string]interface{}{
"time_local": "1548237471.471",
"mytimestamp": timestamp.UTC(),
},
}
conf.TestInputEvent(eventIn)
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal(expectedEvent, event)
}
}
func Test_filter_date_module_joda(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
filter:
- type: date
format: ["YYYY-MM-dd HH:mm:ss,SSS"]
source: time_local
joda: true
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
timestamp, err := time.Parse("2006-01-02T15:04:05.000Z", "2018-09-19T19:50:26.208Z")
require.NoError(err)
expectedEvent := logevent.LogEvent{
Timestamp: timestamp,
Extra: map[string]interface{}{
"time_local": "2018-09-19 19:50:26,208",
},
}
conf.TestInputEvent(logevent.LogEvent{
Extra: map[string]interface{}{
"time_local": "2018-09-19 19:50:26,208",
},
})
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal(expectedEvent, event)
}
}
func Test_filter_date_module_float(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
require := require.New(t)
require.NotNil(require)
ctx := context.Background()
conf, err := config.LoadFromYAML([]byte(strings.TrimSpace(`
debugch: true
filter:
- type: json
source: json
- type: date
format: ["UNIX"]
source: time_local
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
conf.TestInputEvent(logevent.LogEvent{
Extra: map[string]interface{}{
"json": `{"time_local": 1558348989869}`,
},
})
// this should NOT result in gogstash_filter_date_error tag
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal(0, len(event.Tags))
}
}
func Test_filter_date_convert(t *testing.T) {
assert := assert.New(t)
assert.NotNil(assert)
type inputOutput struct {
name string
input interface{}
sec int64
nsec int64
}
tests := []inputOutput{
{
name: "whole float",
input: float64(1558348989869),
sec: int64(1558348989869),
nsec: int64(0),
},
{
name: "decimal float",
input: float64(15583.48989869),
sec: int64(15583),
nsec: int64(489898690),
},
{
name: "whole string",
input: "1558348989869",
sec: int64(1558348989869),
nsec: int64(0),
},
{
name: "fraction string",
input: "1548237471.471",
sec: int64(1548237471),
nsec: int64(471000000),
},
{
name: "exp string",
input: "1.558349e+12",
sec: int64(1558349000000),
nsec: int64(0),
},
{
name: "exp string 2",
input: "1.558348989869e+12",
sec: int64(1558348989869),
nsec: int64(0),
},
{
name: "int",
input: int64(1558348989869),
sec: int64(1558348989869),
nsec: int64(0),
},
}
for _, test := range tests {
t.Run(test.name, func(t *testing.T) {
r := require.New(t)
r.NotNil(r)
var sec, nsec int64
var err error
switch input := test.input.(type) {
case float64:
sec, nsec = convertFloat(input)
default:
sec, nsec, err = convert(fmt.Sprintf("%v", input))
}
r.NoError(err)
r.Equal(test.sec, sec)
r.Equal(test.nsec, nsec)
})
}
}