forked from tsaikd/gogstash
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfilterjson_test.go
131 lines (113 loc) · 3.07 KB
/
filterjson_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
package filterjson
import (
"context"
"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)
}
func Test_filter_json_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: json
message: message
timestamp: time
timeformat: "2006-01-02T15:04:05Z"
remove_field: ["myfield"]
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
timestamp, err := time.Parse("2006-01-02T15:04:05Z", "2016-12-04T09:09:41.193Z")
require.NoError(err)
expectedEvent := logevent.LogEvent{
Timestamp: timestamp,
Message: "Test",
Extra: map[string]interface{}{
"host": "Hostname",
},
Tags: []string{"foo"},
}
conf.TestInputEvent(logevent.LogEvent{
Timestamp: time.Now(),
Message: "{ \"message\": \"Test\", \"host\": \"Hostname\", \"time\":\"2016-12-04T09:09:41.193Z\", \"tags\": [ \"foo\" ] }",
Extra: map[string]interface{}{
"myfield": "test",
},
})
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal(expectedEvent, event)
}
// not ok
expectedEvent = logevent.LogEvent{
Timestamp: timestamp,
Message: "Test",
Extra: map[string]interface{}{
"myfield": "test",
},
Tags: []string{"gogstash_filter_json_error"},
}
conf.TestInputEvent(logevent.LogEvent{
Timestamp: timestamp,
Message: "Test",
Extra: map[string]interface{}{
"myfield": "test",
},
})
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal(expectedEvent, event)
}
}
func Test_filter_json_module_source(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
message: message
timestamp: time
source: myfield
timeformat: "2006-01-02T15:04:05Z"
`)))
require.NoError(err)
require.NoError(conf.Start(ctx))
timestamp, err := time.Parse("2006-01-02T15:04:05Z", "2016-12-04T09:09:41.193Z")
require.NoError(err)
fieldvalue := `{ "message": "Test", "host":"Hostname", "time":"2016-12-04T09:09:41.193Z", "tags": [ "foo" ] }`
expectedEvent := logevent.LogEvent{
Timestamp: timestamp,
Message: "Test",
Extra: map[string]interface{}{
"host": "Hostname",
"myfield": fieldvalue,
},
Tags: []string{"foo"},
}
conf.TestInputEvent(logevent.LogEvent{
Timestamp: time.Now(),
Extra: map[string]interface{}{
"myfield": fieldvalue,
},
})
if event, err := conf.TestGetOutputEvent(300 * time.Millisecond); assert.NoError(err) {
require.Equal(expectedEvent, event)
}
}