-
Notifications
You must be signed in to change notification settings - Fork 239
/
gohangout_test.go
229 lines (192 loc) · 7.73 KB
/
gohangout_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
package main
import (
"fmt"
"testing"
"github.com/bytedance/mockey"
"github.com/childe/gohangout/filter"
"github.com/childe/gohangout/input"
"github.com/childe/gohangout/internal/config"
"github.com/childe/gohangout/output"
"github.com/smartystreets/goconvey/convey"
yaml "gopkg.in/yaml.v2"
)
type MockInput struct{}
func (m *MockInput) ReadOneEvent() map[string]interface{} {
return nil
}
func (m *MockInput) Shutdown() {}
func TestBuildPluginLink(t *testing.T) {
mockey.PatchConvey("test buildPluginLink", t, func() {
c := `inputs:
- Stdin:
codec: json
- Kafka:
topic:
test-none: 1
codec: json
consumer_settings:
bootstrap.servers: "127.0.0.1:9094"
group.id: gohangout-test-none
retry.backoff.ms: 1000
sasl:
mechanism: PLAIN
user: admin
password: admin-secret
filters:
- Date:
src: '@timestamp'
target: pt
formats:
- '2006-01-02T15:04:05.000-07:00'
outputs:
- Stdout: {}`
config := make(map[string]interface{})
yaml.Unmarshal([]byte(c), &config)
mockey.Mock(input.GetInput).Return(&MockInput{}).Build()
boxes, err := buildPluginLink(config)
convey.So(err, convey.ShouldBeNil)
convey.So(len(boxes), convey.ShouldEqual, 2)
})
}
func TestEndToEndProcessNilExist(t *testing.T) {
mockey.PatchConvey("end-to-end process testing, read 10 messages and then nil and exist", t, func() {
_c := `
inputs:
- Stdin:
codec: json
filters:
- Date:
src: '@timestamp'
target: pt
formats:
- '2006-01-02T15:04:05.000-07:00'
outputs:
- Stdout: {}`
c := make(map[string]interface{})
yaml.Unmarshal([]byte(_c), &c) //
mockey.Mock(config.ParseConfig).Return(c, nil).Build()
emit := mockey.Mock((*output.StdoutOutput).Emit).Return().Build()
msgCount := 10
mockey.Mock((*input.StdinInput).ReadOneEvent).To(func(*input.StdinInput) map[string]interface{} {
if msgCount > 0 {
msgCount--
return map[string]interface{}{"msg": fmt.Sprintf("msg-%d", msgCount)}
} else {
return nil
}
}).Build()
options.exitWhenNil = true
_main()
convey.So(emit.Times(), convey.ShouldEqual, 10)
})
}
func TestEndToEndProcess(t *testing.T) {
mockey.PatchConvey("end-to-end process testing, read 10 messages and then cancel", t, func() {
_c := `
inputs:
- Stdin:
codec: json
filters:
- Date:
src: '@timestamp'
target: pt
formats:
- '2006-01-02T15:04:05.000-07:00'
outputs:
- Stdout: {}`
c := make(map[string]interface{})
yaml.Unmarshal([]byte(_c), &c) //
mockey.Mock(config.ParseConfig).Return(c, nil).Build()
emit := mockey.Mock((*output.StdoutOutput).Emit).Return().Build()
dateFilterOrigin := (*filter.DateFilter).Filter
dateFilter := mockey.Mock((*filter.DateFilter).Filter).To(func(f *filter.DateFilter, event map[string]interface{}) (map[string]interface{}, bool) {
return dateFilterOrigin(f, event)
}).Origin(&dateFilterOrigin).Build()
readOneEvent := mockey.Mock((*input.StdinInput).ReadOneEvent).To(func(*input.StdinInput) map[string]interface{} {
return map[string]interface{}{"msg": "mock message"}
}).Build()
ch := make(chan struct{})
go func() {
_main()
}()
for {
if emit.Times() == 10 {
close(ch)
break
}
}
<-ch
cancel()
convey.So(readOneEvent.Times(), convey.ShouldBeGreaterThanOrEqualTo, 10)
convey.So(emit.Times(), convey.ShouldBeGreaterThanOrEqualTo, 10)
convey.So(dateFilter.Times(), convey.ShouldBeGreaterThanOrEqualTo, 10)
convey.So(readOneEvent.Times(), convey.ShouldBeGreaterThanOrEqualTo, dateFilter.Times())
convey.So(dateFilter.Times(), convey.ShouldBeGreaterThanOrEqualTo, emit.Times())
})
}
func TestEndToEndAllFilters(t *testing.T) {
mockey.PatchConvey("end-to-end process testing, test all filtes", t, func() {
_c := `
inputs:
- Stdin:
codec: json
filters:
- Grok:
failTag: grokfail
match:
- '^(?P<clientip>(?:(\b(?:[0-9A-Za-z][0-9A-Za-z-]{0,62})(?:\.(?:[0-9A-Za-z][0-9A-Za-z-]{0,62}))*(\.?|\b))|((?:(((([0-9A-Fa-f]{1,4}:){7}([0-9A-Fa-f]{1,4}|:))|(([0-9A-Fa-f]{1,4}:){6}(:[0-9A-Fa-f]{1,4}|((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){5}(((:[0-9A-Fa-f]{1,4}){1,2})|:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3})|:))|(([0-9A-Fa-f]{1,4}:){4}(((:[0-9A-Fa-f]{1,4}){1,3})|((:[0-9A-Fa-f]{1,4})?:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){3}(((:[0-9A-Fa-f]{1,4}){1,4})|((:[0-9A-Fa-f]{1,4}){0,2}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){2}(((:[0-9A-Fa-f]{1,4}){1,5})|((:[0-9A-Fa-f]{1,4}){0,3}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(([0-9A-Fa-f]{1,4}:){1}(((:[0-9A-Fa-f]{1,4}){1,6})|((:[0-9A-Fa-f]{1,4}){0,4}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:))|(:(((:[0-9A-Fa-f]{1,4}){1,7})|((:[0-9A-Fa-f]{1,4}){0,5}:((25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)(\.(25[0-5]|2[0-4]\d|1\d\d|[1-9]?\d)){3}))|:)))(%.+)?)|((?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)))))) (?P<user>([a-zA-Z0-9._-]+)) \[(?P<ts>((?:(?:0[1-9])|(?:[12][0-9])|(?:3[01])|[1-9]))/(\b(?:Jan(?:uary)?|Feb(?:ruary)?|Mar(?:ch)?|Apr(?:il)?|May|Jun(?:e)?|Jul(?:y)?|Aug(?:ust)?|Sep(?:tember)?|Oct(?:ober)?|Nov(?:ember)?|Dec(?:ember)?)\b)/((\d\d){1,2}):(([^0-9]?)((?:2[0123]|[01]?[0-9])):((?:[0-5][0-9]))(?::((?:(?:[0-5][0-9]|60)(?:[:.,][0-9]+)?)))([^0-9]?)) ((?:[+-]?(?:[0-9]+))))\] "(?:(?P<verb>\b\w+\b) (?P<request>\S+)(?: HTTP/(?P<httpversion>(?:(([+-]?(?:[0-9]+(?:\.[0-9]+)?)|\.[0-9]+)))))?|(?P<rawrequest>.*?))" (?P<status>(?:(([+-]?(?:[0-9]+(?:\.[0-9]+)?)|\.[0-9]+)))) (?:(?P<bytes>(?:(([+-]?(?:[0-9]+(?:\.[0-9]+)?)|\.[0-9]+))))|-) "(?P<referrer>(.*))" (?P<ua>(.*))" "(?P<x_forwarded_for>(.*))" (?P<body>(.*))$'
- Filters:
if:
- '!EQ($.tags,"grokfail")'
filters:
- Date:
formats:
- '02/Jan/2006:15:04:05 -0700'
src: 'ts'
target: timestamp
remove_fields: [ts]
- Convert:
fields:
status:
remove_if_fail: false
setto_if_fail: 0
to: int
- Drop:
if:
- 'EQ($.tags,"grokfail")'
outputs:
- Stdout: {}
`
c := make(map[string]interface{})
yaml.Unmarshal([]byte(_c), &c) //
mockey.Mock(config.ParseConfig).Return(c, nil).Build()
rst := make([]map[string]interface{}, 0)
emit := mockey.Mock((*output.StdoutOutput).Emit).To(func(o *output.StdoutOutput, e map[string]interface{}) {
rst = append(rst, e)
}).Build()
eventCount := 0
readOneEvent := mockey.Mock((*input.StdinInput).ReadOneEvent).To(func(*input.StdinInput) map[string]interface{} {
switch eventCount {
case 0:
eventCount++
return map[string]interface{}{"message": `10.0.0.100 - [09/Sep/2024:18:40:55 +0800] "GET /assets/loading.gif HTTP/1.1" 200 838555 "http://kafka-admin.corp.com/" "Mozilla/5.0 (Macintosh; Intel Mac OS X 10_15_7) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/128.0.0.0 Safari/537.36 Edg/128.0.0.0" "10.10.10.100" "-"`}
case 1:
eventCount++
return map[string]interface{}{"message": "mock message"}
default:
return nil
}
}).Build()
options.exitWhenNil = true
_main()
convey.So(readOneEvent.Times(), convey.ShouldEqual, 3)
convey.So(emit.Times(), convey.ShouldEqual, 1)
convey.So(len(rst), convey.ShouldEqual, 1)
event := rst[0]
convey.So(event["x_forwarded_for"], convey.ShouldEqual, "10.10.10.100")
convey.So(event["body"], convey.ShouldEqual, `"-"`)
convey.So(event["status"], convey.ShouldEqual, 200)
convey.So(event["timestamp"], convey.ShouldNotBeNil)
convey.So(event["ts"], convey.ShouldBeNil)
})
}