2015-06-27 03:56:14 +00:00
|
|
|
package kafka_consumer
|
|
|
|
|
|
|
|
import (
|
2018-11-05 21:34:28 +00:00
|
|
|
"context"
|
2015-06-27 03:56:14 +00:00
|
|
|
"testing"
|
2019-07-30 03:41:12 +00:00
|
|
|
"time"
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
"github.com/influxdata/telegraf"
|
2020-01-03 00:27:26 +00:00
|
|
|
"github.com/influxdata/telegraf/internal/tls"
|
2019-07-30 03:41:12 +00:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/value"
|
2016-01-20 18:57:35 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2019-07-30 03:41:12 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2015-06-27 03:56:14 +00:00
|
|
|
)
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
type FakeConsumerGroup struct {
|
|
|
|
brokers []string
|
|
|
|
group string
|
|
|
|
config *sarama.Config
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
handler sarama.ConsumerGroupHandler
|
|
|
|
errors chan error
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (g *FakeConsumerGroup) Consume(ctx context.Context, topics []string, handler sarama.ConsumerGroupHandler) error {
|
|
|
|
g.handler = handler
|
|
|
|
g.handler.Setup(nil)
|
|
|
|
return nil
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (g *FakeConsumerGroup) Errors() <-chan error {
|
|
|
|
return g.errors
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (g *FakeConsumerGroup) Close() error {
|
|
|
|
close(g.errors)
|
|
|
|
return nil
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
type FakeCreator struct {
|
|
|
|
ConsumerGroup *FakeConsumerGroup
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (c *FakeCreator) Create(brokers []string, group string, config *sarama.Config) (ConsumerGroup, error) {
|
|
|
|
c.ConsumerGroup.brokers = brokers
|
|
|
|
c.ConsumerGroup.group = group
|
|
|
|
c.ConsumerGroup.config = config
|
|
|
|
return c.ConsumerGroup, nil
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func TestInit(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
plugin *KafkaConsumer
|
|
|
|
initError bool
|
|
|
|
check func(t *testing.T, plugin *KafkaConsumer)
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default config",
|
|
|
|
plugin: &KafkaConsumer{},
|
|
|
|
check: func(t *testing.T, plugin *KafkaConsumer) {
|
|
|
|
require.Equal(t, plugin.ConsumerGroup, defaultConsumerGroup)
|
|
|
|
require.Equal(t, plugin.MaxUndeliveredMessages, defaultMaxUndeliveredMessages)
|
|
|
|
require.Equal(t, plugin.config.ClientID, "Telegraf")
|
|
|
|
require.Equal(t, plugin.config.Consumer.Offsets.Initial, sarama.OffsetOldest)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "parses valid version string",
|
|
|
|
plugin: &KafkaConsumer{
|
|
|
|
Version: "1.0.0",
|
2020-01-03 00:27:26 +00:00
|
|
|
Log: testutil.Logger{},
|
2019-07-30 03:41:12 +00:00
|
|
|
},
|
|
|
|
check: func(t *testing.T, plugin *KafkaConsumer) {
|
|
|
|
require.Equal(t, plugin.config.Version, sarama.V1_0_0_0)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid version string",
|
|
|
|
plugin: &KafkaConsumer{
|
|
|
|
Version: "100",
|
2020-01-03 00:27:26 +00:00
|
|
|
Log: testutil.Logger{},
|
2019-07-30 03:41:12 +00:00
|
|
|
},
|
|
|
|
initError: true,
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "custom client_id",
|
|
|
|
plugin: &KafkaConsumer{
|
|
|
|
ClientID: "custom",
|
2020-01-03 00:27:26 +00:00
|
|
|
Log: testutil.Logger{},
|
2019-07-30 03:41:12 +00:00
|
|
|
},
|
|
|
|
check: func(t *testing.T, plugin *KafkaConsumer) {
|
|
|
|
require.Equal(t, plugin.config.ClientID, "custom")
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "custom offset",
|
|
|
|
plugin: &KafkaConsumer{
|
|
|
|
Offset: "newest",
|
2020-01-03 00:27:26 +00:00
|
|
|
Log: testutil.Logger{},
|
2019-07-30 03:41:12 +00:00
|
|
|
},
|
|
|
|
check: func(t *testing.T, plugin *KafkaConsumer) {
|
|
|
|
require.Equal(t, plugin.config.Consumer.Offsets.Initial, sarama.OffsetNewest)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "invalid offset",
|
|
|
|
plugin: &KafkaConsumer{
|
|
|
|
Offset: "middle",
|
2020-01-03 00:27:26 +00:00
|
|
|
Log: testutil.Logger{},
|
2019-07-30 03:41:12 +00:00
|
|
|
},
|
|
|
|
initError: true,
|
|
|
|
},
|
2020-01-03 00:27:26 +00:00
|
|
|
{
|
|
|
|
name: "default tls without tls config",
|
|
|
|
plugin: &KafkaConsumer{
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
},
|
|
|
|
check: func(t *testing.T, plugin *KafkaConsumer) {
|
|
|
|
require.False(t, plugin.config.Net.TLS.Enable)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "default tls with a tls config",
|
|
|
|
plugin: &KafkaConsumer{
|
|
|
|
ClientConfig: tls.ClientConfig{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
},
|
|
|
|
check: func(t *testing.T, plugin *KafkaConsumer) {
|
|
|
|
require.True(t, plugin.config.Net.TLS.Enable)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "disable tls",
|
|
|
|
plugin: &KafkaConsumer{
|
|
|
|
EnableTLS: func() *bool { v := false; return &v }(),
|
|
|
|
ClientConfig: tls.ClientConfig{
|
|
|
|
InsecureSkipVerify: true,
|
|
|
|
},
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
},
|
|
|
|
check: func(t *testing.T, plugin *KafkaConsumer) {
|
|
|
|
require.False(t, plugin.config.Net.TLS.Enable)
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "enable tls",
|
|
|
|
plugin: &KafkaConsumer{
|
|
|
|
EnableTLS: func() *bool { v := true; return &v }(),
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
},
|
|
|
|
check: func(t *testing.T, plugin *KafkaConsumer) {
|
|
|
|
require.True(t, plugin.config.Net.TLS.Enable)
|
|
|
|
},
|
|
|
|
},
|
2018-11-05 21:34:28 +00:00
|
|
|
}
|
2019-07-30 03:41:12 +00:00
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
cg := &FakeConsumerGroup{}
|
|
|
|
tt.plugin.ConsumerCreator = &FakeCreator{ConsumerGroup: cg}
|
|
|
|
err := tt.plugin.Init()
|
|
|
|
if tt.initError {
|
|
|
|
require.Error(t, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
tt.check(t, tt.plugin)
|
|
|
|
})
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
2015-11-16 20:12:45 +00:00
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func TestStartStop(t *testing.T) {
|
|
|
|
cg := &FakeConsumerGroup{errors: make(chan error)}
|
|
|
|
plugin := &KafkaConsumer{
|
|
|
|
ConsumerCreator: &FakeCreator{ConsumerGroup: cg},
|
2020-01-03 00:27:26 +00:00
|
|
|
Log: testutil.Logger{},
|
2018-11-29 00:29:26 +00:00
|
|
|
}
|
2019-07-30 03:41:12 +00:00
|
|
|
err := plugin.Init()
|
|
|
|
require.NoError(t, err)
|
2018-11-29 00:29:26 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
var acc testutil.Accumulator
|
|
|
|
err = plugin.Start(&acc)
|
|
|
|
require.NoError(t, err)
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
plugin.Stop()
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
type FakeConsumerGroupSession struct {
|
|
|
|
ctx context.Context
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (s *FakeConsumerGroupSession) Claims() map[string][]int32 {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2018-11-29 00:29:26 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (s *FakeConsumerGroupSession) MemberID() string {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2018-11-29 00:29:26 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (s *FakeConsumerGroupSession) GenerationID() int32 {
|
|
|
|
panic("not implemented")
|
2018-11-29 00:29:26 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (s *FakeConsumerGroupSession) MarkOffset(topic string, partition int32, offset int64, metadata string) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (s *FakeConsumerGroupSession) ResetOffset(topic string, partition int32, offset int64, metadata string) {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (s *FakeConsumerGroupSession) MarkMessage(msg *sarama.ConsumerMessage, metadata string) {
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (s *FakeConsumerGroupSession) Context() context.Context {
|
|
|
|
return s.ctx
|
|
|
|
}
|
2017-04-11 19:05:39 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
type FakeConsumerGroupClaim struct {
|
|
|
|
messages chan *sarama.ConsumerMessage
|
|
|
|
}
|
2017-04-11 19:05:39 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (c *FakeConsumerGroupClaim) Topic() string {
|
|
|
|
panic("not implemented")
|
2017-04-11 19:05:39 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (c *FakeConsumerGroupClaim) Partition() int32 {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2015-11-16 20:12:45 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (c *FakeConsumerGroupClaim) InitialOffset() int64 {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (c *FakeConsumerGroupClaim) HighWaterMarkOffset() int64 {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func (c *FakeConsumerGroupClaim) Messages() <-chan *sarama.ConsumerMessage {
|
|
|
|
return c.messages
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func TestConsumerGroupHandler_Lifecycle(t *testing.T) {
|
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
parser := &value.ValueParser{MetricName: "cpu", DataType: "int"}
|
|
|
|
cg := NewConsumerGroupHandler(acc, 1, parser)
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
|
|
|
|
|
|
|
session := &FakeConsumerGroupSession{
|
|
|
|
ctx: ctx,
|
|
|
|
}
|
|
|
|
var claim FakeConsumerGroupClaim
|
|
|
|
var err error
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
err = cg.Setup(session)
|
|
|
|
require.NoError(t, err)
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
cancel()
|
|
|
|
err = cg.ConsumeClaim(session, &claim)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = cg.Cleanup(session)
|
|
|
|
require.NoError(t, err)
|
2016-02-06 00:36:35 +00:00
|
|
|
}
|
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func TestConsumerGroupHandler_ConsumeClaim(t *testing.T) {
|
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
parser := &value.ValueParser{MetricName: "cpu", DataType: "int"}
|
|
|
|
cg := NewConsumerGroupHandler(acc, 1, parser)
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
ctx, cancel := context.WithCancel(context.Background())
|
|
|
|
defer cancel()
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
session := &FakeConsumerGroupSession{ctx: ctx}
|
|
|
|
claim := &FakeConsumerGroupClaim{
|
|
|
|
messages: make(chan *sarama.ConsumerMessage, 1),
|
|
|
|
}
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
err := cg.Setup(session)
|
|
|
|
require.NoError(t, err)
|
2016-02-06 00:36:35 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
claim.messages <- &sarama.ConsumerMessage{
|
|
|
|
Topic: "telegraf",
|
|
|
|
Value: []byte("42"),
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
2019-07-30 03:41:12 +00:00
|
|
|
|
|
|
|
go func() {
|
|
|
|
err = cg.ConsumeClaim(session, claim)
|
|
|
|
require.NoError(t, err)
|
|
|
|
}()
|
|
|
|
|
|
|
|
acc.Wait(1)
|
|
|
|
cancel()
|
|
|
|
|
|
|
|
err = cg.Cleanup(session)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
expected := []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42,
|
|
|
|
},
|
|
|
|
time.Now(),
|
|
|
|
),
|
|
|
|
}
|
|
|
|
|
|
|
|
testutil.RequireMetricsEqual(t, expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|
2018-11-29 00:29:26 +00:00
|
|
|
|
2019-07-30 03:41:12 +00:00
|
|
|
func TestConsumerGroupHandler_Handle(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
maxMessageLen int
|
|
|
|
topicTag string
|
|
|
|
msg *sarama.ConsumerMessage
|
|
|
|
expected []telegraf.Metric
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "happy path",
|
|
|
|
msg: &sarama.ConsumerMessage{
|
|
|
|
Topic: "telegraf",
|
|
|
|
Value: []byte("42"),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42,
|
|
|
|
},
|
|
|
|
time.Now(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "message to long",
|
|
|
|
maxMessageLen: 4,
|
|
|
|
msg: &sarama.ConsumerMessage{
|
|
|
|
Topic: "telegraf",
|
|
|
|
Value: []byte("12345"),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "parse error",
|
|
|
|
msg: &sarama.ConsumerMessage{
|
|
|
|
Topic: "telegraf",
|
|
|
|
Value: []byte("not an integer"),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "add topic tag",
|
|
|
|
topicTag: "topic",
|
|
|
|
msg: &sarama.ConsumerMessage{
|
|
|
|
Topic: "telegraf",
|
|
|
|
Value: []byte("42"),
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{
|
|
|
|
"topic": "telegraf",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"value": 42,
|
|
|
|
},
|
|
|
|
time.Now(),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
acc := &testutil.Accumulator{}
|
|
|
|
parser := &value.ValueParser{MetricName: "cpu", DataType: "int"}
|
|
|
|
cg := NewConsumerGroupHandler(acc, 1, parser)
|
|
|
|
cg.MaxMessageLen = tt.maxMessageLen
|
|
|
|
cg.TopicTag = tt.topicTag
|
|
|
|
|
|
|
|
ctx := context.Background()
|
|
|
|
session := &FakeConsumerGroupSession{ctx: ctx}
|
|
|
|
|
|
|
|
cg.Reserve(ctx)
|
|
|
|
cg.Handle(session, tt.msg)
|
|
|
|
|
|
|
|
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(), testutil.IgnoreTime())
|
|
|
|
})
|
2018-11-29 00:29:26 +00:00
|
|
|
}
|
|
|
|
}
|