2015-06-27 03:56:14 +00:00
|
|
|
package kafka_consumer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"testing"
|
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/Shopify/sarama"
|
|
|
|
"github.com/influxdb/telegraf/testutil"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
|
|
"github.com/stretchr/testify/require"
|
|
|
|
)
|
|
|
|
|
|
|
|
func TestReadsMetricsFromKafka(t *testing.T) {
|
2015-07-06 04:46:43 +00:00
|
|
|
if testing.Short() {
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
}
|
2015-06-27 03:56:14 +00:00
|
|
|
var zkPeers, brokerPeers []string
|
|
|
|
|
2015-08-04 14:58:32 +00:00
|
|
|
zkPeers = []string{testutil.GetLocalHost() + ":2181"}
|
|
|
|
brokerPeers = []string{testutil.GetLocalHost() + ":9092"}
|
2015-06-27 03:56:14 +00:00
|
|
|
|
|
|
|
k := &Kafka{
|
|
|
|
ConsumerGroupName: "telegraf_test_consumers",
|
|
|
|
Topic: fmt.Sprintf("telegraf_test_topic_%d", time.Now().Unix()),
|
|
|
|
ZookeeperPeers: zkPeers,
|
|
|
|
}
|
|
|
|
|
|
|
|
msg := "cpu_load_short,direction=in,host=server01,region=us-west value=23422.0 1422568543702900257"
|
|
|
|
producer, err := sarama.NewSyncProducer(brokerPeers, nil)
|
|
|
|
require.NoError(t, err)
|
2015-10-23 15:02:51 +00:00
|
|
|
|
2015-06-27 03:56:14 +00:00
|
|
|
_, _, err = producer.SendMessage(&sarama.ProducerMessage{Topic: k.Topic, Value: sarama.StringEncoder(msg)})
|
2015-10-23 15:02:51 +00:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
2015-06-27 03:56:14 +00:00
|
|
|
producer.Close()
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
// Sanity check
|
|
|
|
assert.Equal(t, 0, len(acc.Points), "there should not be any points")
|
|
|
|
|
|
|
|
err = k.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
assert.Equal(t, 1, len(acc.Points), "there should be a single point")
|
|
|
|
|
|
|
|
point := acc.Points[0]
|
|
|
|
assert.Equal(t, "cpu_load_short", point.Measurement)
|
|
|
|
assert.Equal(t, map[string]interface{}{"value": 23422.0}, point.Values)
|
|
|
|
assert.Equal(t, map[string]string{
|
|
|
|
"host": "server01",
|
|
|
|
"direction": "in",
|
|
|
|
"region": "us-west",
|
|
|
|
}, point.Tags)
|
2015-10-16 22:13:32 +00:00
|
|
|
assert.Equal(t, time.Unix(0, 1422568543702900257).Unix(), point.Time.Unix())
|
2015-06-27 03:56:14 +00:00
|
|
|
}
|