-
Notifications
You must be signed in to change notification settings - Fork 6
/
xport_kafka_test.go
469 lines (386 loc) · 9.83 KB
/
xport_kafka_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
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
//
// January 2016, cisco
//
// Copyright (c) 2016 by cisco Systems, Inc.
// All rights reserved.
//
// +build integration
package main
import (
"encoding/json"
"fmt"
"strings"
"testing"
"time"
"github.com/dlintw/goconf"
)
var kmod kafkaOutputModule
type dataMsgKafkaTest struct {
content []byte
}
func (m *dataMsgKafkaTest) getDataMsgDescription() string {
return "kafka test msg"
}
func (m *dataMsgKafkaTest) getDataMsgStreamType() dataMsgStreamType {
// doesn't matter
return dMStreamGPB
}
func (m *dataMsgKafkaTest) produceByteStream(*dataMsgStreamSpec) (
error, []byte) {
return nil, m.content
}
func (m *dataMsgKafkaTest) getMetaDataPath() (error, string) {
return nil, "RootOperKafkaTest"
}
func (m *dataMsgKafkaTest) getMetaDataIdentifier() (error, string) {
return nil, "RouterInSpace"
}
func (m *dataMsgKafkaTest) getMetaDataCollectionID() (error, uint64) {
return nil, 42
}
func (m *dataMsgKafkaTest) getMetaData() *dataMsgMetaData {
return &dataMsgMetaData{
Path: "RootOperKafkaTest",
Identifier: "RouterInSpace",
}
}
func (m *dataMsgKafkaTest) produceMetrics(
spec *metricsSpec,
outputHandler metricsOutputHandler,
buf metricsOutputContext) error {
return fmt.Errorf("GPB CODEC: metric extraction unsupported")
}
func TestKafkaNegative(t *testing.T) {
var nc nodeConfig
cfg, err := goconf.ReadConfigFile("pipeline_test_bad.conf")
nc.config = cfg
err, _, _ = kmod.configure("mykafka", nc)
if err == nil {
t.Error("setup function did not fail on bad broker list")
}
cfg, err = goconf.ReadConfigFile("pipeline_test_bad.conf")
nc.config = cfg
err, _, _ = kmod.configure("myotherkafka", nc)
if err == nil {
t.Error("setup function did not fail on bad key config")
}
}
func TestKafkaConnect(t *testing.T) {
var nc nodeConfig
cfg, err := goconf.ReadConfigFile("pipeline_test.conf")
nc.config = cfg
err, _, ctrl := kmod.configure("mykafka", nc)
if err != nil {
t.Errorf("setup function fail to connect to kafka. Is kafka up?")
return
}
//
// Test shutdown
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
//
// Send shutdown message
ctrl <- request
// Wait for ACK
ack := <-respChan
if ack.id != ACK {
t.Error("failed to recieve acknowledgement indicating shutdown complete")
}
}
func TestKafkaClosedDataFeed(t *testing.T) {
var nc nodeConfig
cfg, err := goconf.ReadConfigFile("pipeline_test.conf")
nc.config = cfg
err, data, ctrl := kmod.configure("mykafka", nc)
if err != nil {
t.Errorf("setup function fail to connect to kafka. Is kafka up?")
return
}
//
// Test shutdown
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
// close data connection and wait to make sure select will
// have the time to mull over it.
close(data)
time.Sleep(1)
//
// Send shutdown message
ctrl <- request
// Wait for ACK
ack := <-respChan
if ack.id != ACK {
t.Error("failed to recieve acknowledgement indicating shutdown complete")
}
}
func TestKafkaDataFlow(t *testing.T) {
var msgcontent string
var stats msgStats
var msgcount, i uint64
var nc nodeConfig
// number of messages to send and how long to give up waiting
// for book keeping from sending routing to match up
msgcount = 1000
giveup := 60
cfg, err := goconf.ReadConfigFile("pipeline_test.conf")
nc.config = cfg
err, data, ctrl := kmod.configure("mykafka", nc)
if err != nil {
t.Errorf("setup function fail to connect to kafka. Is kafka up?")
return
}
//
// Test data...
for i = 0; i < msgcount; i++ {
msgcontent = fmt.Sprintf("message number %d", i)
msg := &dataMsgKafkaTest{
content: []byte(msgcontent),
}
data <- msg
}
iterations := 0
for {
time.Sleep(1 * time.Second)
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: REPORT,
respChan: respChan,
}
ctrl <- request
// Wait for report
reply := <-respChan
err = json.Unmarshal(reply.content, &stats)
if (stats.MsgsOK+stats.MsgsNOK) == msgcount ||
iterations > giveup {
//
// We either got them all, or we've given up
break
}
iterations++
}
if err != nil {
t.Error("failed to recieve msg stats")
}
if stats.MsgsOK != msgcount {
t.Errorf("msgs fed %d, send %d, errored %d",
msgcount, stats.MsgsOK, stats.MsgsNOK)
}
// close data connection and wait to make sure select will
// have the time to mull over it.
close(data)
time.Sleep(1 * time.Second)
//
// Send shutdown message
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
ctrl <- request
// Wait for ACK
ack := <-respChan
if ack.id != ACK {
t.Error("failed to recieve acknowledgement indicating shutdown complete")
}
}
var kConsumer kafkaInputModule
var tap tapOutputModule
func TestKafkaConsumer(t *testing.T) {
var msgcontent string
var nc nodeConfig
var ctrl chan<- *ctrlMsg
var msgcount, i uint64
msgcount = 20
dataChan := make(chan dataMsg, 2)
dataChans := []chan<- dataMsg{dataChan}
cfg, err := goconf.ReadConfigFile("pipeline_test_bad.conf")
nc.config = cfg
badsections := []string{
"kafkaconsumernoconsumergroup",
"kafkaconsumerbadkey",
"kafkaconsumerbadencoding",
"kafkaconsumernobroker",
"kafkaconsumernokafkaversion",
"kafkaconsumerbadkafkaversion",
"kafkaBADrequiredAcks",
"kafkaBADTopicTemplace",
"kafkaBADkafkaversion",
}
for _, section := range badsections {
err, ctrl = kConsumer.configure(section, nc, dataChans)
if err == nil {
t.Errorf("kafka consumer section [%v] should fail\n", section)
}
}
cfg, err = goconf.ReadConfigFile("pipeline_test.conf")
nc.config = cfg
//
// Setup tap so we can watch the content collected too
err, tapDataOut, tapCtrlOut := tap.configure("inspector", nc)
if err != nil {
t.Fatalf("setup function failed to setup tap [%v]", err)
}
dataChans = append(dataChans, tapDataOut)
//
// Setup consumer
err, ctrl = kConsumer.configure("kafkaconsumer", nc, dataChans)
if err != nil {
t.Fatalf("setup function failed to setup consumer")
}
//
// Setup to publish messages, and make sure we receive them.
err, dataOut, ctrlOut := kmod.configure("mykafka2", nc)
if err != nil {
t.Fatalf("setup function fail to setup producer. Is kafka up?")
}
//
// Iterate until connection is confirmed...
outer:
for {
dataOut <- &dataMsgKafkaTest{
content: []byte(`["Testing testing testing, 1, 2, 3]`),
}
select {
case <-dataChan:
break outer
case <-time.After(1 * time.Second):
continue
}
}
// t.Logf("Producing %v message\n", msgcount)
for i = 0; i < msgcount; i++ {
msgcontent = fmt.Sprintf(`{"list":[` + strings.Repeat(`"ABC",`, 3000) + `"DEF"]}`)
msg := &dataMsgKafkaTest{
content: []byte(msgcontent),
}
dataOut <- msg
}
//
// Read all the messages.
// t.Logf("Consuming %v message\n", msgcount)
for i = 0; i < msgcount; i++ {
<-dataChan
}
time.Sleep(1 * time.Second)
//t.Logf("Producing %v message\n", msgcount)
for i = 0; i < msgcount; i++ {
msgcontent = fmt.Sprintf(`{"list":[` + strings.Repeat(`"ABC",`, 3000) + `"DEF"]}`)
msg := &dataMsgKafkaTest{
content: []byte(msgcontent),
}
dataOut <- msg
}
//
// Read all the messages.
//t.Logf("Consuming %v message\n", msgcount)
for i = 0; i < msgcount; i++ {
<-dataChan
}
time.Sleep(2 * time.Second)
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
ctrlOut <- request
// Wait for ACK
ack := <-respChan
if ack.id != ACK {
t.Error("failed to recieve ack for producer shutdown complete")
}
respChan = make(chan *ctrlMsg)
request.respChan = respChan
ctrl <- request
ack = <-respChan
if ack.id != ACK {
t.Error("failed to recieve ack for consumer shutdown complete")
}
respChan = make(chan *ctrlMsg)
request.respChan = respChan
tapCtrlOut <- request
ack = <-respChan
if ack.id != ACK {
t.Error("failed to recieve ack for tap shutdown complete")
}
}
func TestKafkaDynamicTopic(t *testing.T) {
var msgcontent string
var nc nodeConfig
var msgcount, i uint64
msgcount = 2
cfg, err := goconf.ReadConfigFile("pipeline_test.conf")
if err != nil {
t.Errorf("kafka read pipeline test config fail [%v]\n", err)
}
nc.config = cfg
pairs := [][]string{
{"kafkaAout", "kafkaAin"},
{"kafkaBout", "kafkaBin"},
{"kafkaCout", "kafkaCin"},
{"kafkaDout", "kafkaDin"},
}
for _, section := range pairs {
dataChan := make(chan dataMsg, 2)
dataChans := []chan<- dataMsg{dataChan}
//fmt.Printf("Configured [%v]\n", section[1])
err, ctrlIn := kConsumer.configure(section[1], nc, dataChans)
if err != nil {
t.Errorf("kafka consumer section [%v] fail [%v]\n", section[1], err)
}
//fmt.Printf("Configured [%v]\n", section[0])
err, dataOut, ctrlOut := kmod.configure(section[0], nc)
if err != nil {
t.Fatalf("setup function fail to setup producer. Is kafka up [%v]?", err)
}
//
// Iterate until connection is confirmed...
outer:
for {
dataOut <- &dataMsgKafkaTest{
content: []byte(`["Testing testing testing, 1, 2, 3]`),
}
select {
case <-dataChan:
break outer
case <-time.After(1 * time.Second):
continue
}
}
//fmt.Printf("Producing %v message %v\n", msgcount, section[0])
for i = 0; i < msgcount; i++ {
msgcontent = fmt.Sprintf(`{"list":[` + strings.Repeat(`"ABC",`, 10) + `"DEF"]}`)
msg := &dataMsgKafkaTest{
content: []byte(msgcontent),
}
dataOut <- msg
}
//
// Read all the messages.
//fmt.Printf("Consuming %v message %v\n", msgcount, section[1])
for i = 0; i < msgcount; i++ {
<-dataChan
}
time.Sleep(2 * time.Second)
respChan := make(chan *ctrlMsg)
request := &ctrlMsg{
id: SHUTDOWN,
respChan: respChan,
}
ctrlOut <- request
ack := <-respChan
if ack.id != ACK {
t.Error("failed to receive ack for producer shutdown complete")
}
ctrlIn <- request
ack = <-respChan
if ack.id != ACK {
t.Error("failed to receive ack for consumer shutdown complete")
}
}
}