Skip to content

Commit 9fbcdde

Browse files
committed
[WIP] Integration Test
1 parent 3ff429b commit 9fbcdde

File tree

1 file changed

+62
-0
lines changed

1 file changed

+62
-0
lines changed
+62
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
package kafka
2+
3+
import (
4+
"fmt"
5+
"os"
6+
"strings"
7+
"testing"
8+
"time"
9+
10+
"github.com/Shopify/sarama"
11+
"github.com/influxdb/telegraf/testutil"
12+
"github.com/stretchr/testify/assert"
13+
"github.com/stretchr/testify/require"
14+
)
15+
16+
func TestReadsMetricsFromKafka(t *testing.T) {
17+
var zkPeers, brokerPeers []string
18+
19+
if len(os.Getenv("ZOOKEEPER_PEERS")) == 0 {
20+
zkPeers = []string{"localhost:2181"}
21+
} else {
22+
zkPeers = strings.Split(os.Getenv("ZOOKEEPER_PEERS"), ",")
23+
}
24+
25+
if len(os.Getenv("KAFKA_PEERS")) == 0 {
26+
brokerPeers = []string{"localhost:9092"}
27+
} else {
28+
brokerPeers = strings.Split(os.Getenv("KAFKA_PEERS"), ",")
29+
}
30+
31+
k := &Kafka{
32+
ConsumerGroupName: "telegraf_test_consumers",
33+
Topic: fmt.Sprintf("telegraf_test_topic_%d", time.Now().Unix()),
34+
ZookeeperPeers: zkPeers,
35+
}
36+
37+
msg := "cpu_load_short,direction=in,host=server01,region=us-west value=23422.0 1422568543702900257"
38+
producer, err := sarama.NewSyncProducer(brokerPeers, nil)
39+
require.NoError(t, err)
40+
_, _, err = producer.SendMessage(&sarama.ProducerMessage{Topic: k.Topic, Value: sarama.StringEncoder(msg)})
41+
producer.Close()
42+
43+
var acc testutil.Accumulator
44+
45+
// Sanity check
46+
assert.Equal(t, 0, len(acc.Points), "there should not be any points")
47+
48+
err = k.Gather(&acc)
49+
require.NoError(t, err)
50+
51+
assert.Equal(t, 1, len(acc.Points), "there should be a single point")
52+
53+
point := acc.Points[0]
54+
assert.Equal(t, "cpu_load_short", point.Measurement)
55+
assert.Equal(t, map[string]interface{}{"value": 23422.0}, point.Values)
56+
assert.Equal(t, map[string]string{
57+
"host": "server01",
58+
"direction": "in",
59+
"region": "us-west",
60+
}, point.Tags)
61+
assert.Equal(t, time.Unix(0, 1422568543702900257), point.Time)
62+
}

0 commit comments

Comments
 (0)