2016-02-10 23:28:52 +00:00
|
|
|
package natsconsumer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2018-06-11 23:13:59 +00:00
|
|
|
nats "github.com/nats-io/go-nats"
|
2017-03-24 19:03:36 +00:00
|
|
|
"github.com/stretchr/testify/assert"
|
2016-02-10 23:28:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2016-11-28 18:19:35 +00:00
|
|
|
testMsg = "cpu_load_short,host=server01 value=23422.0 1422568543702900257\n"
|
2016-02-10 23:28:52 +00:00
|
|
|
testMsgGraphite = "cpu.load.short.graphite 23422 1454780029"
|
|
|
|
testMsgJSON = "{\"a\": 5, \"b\": {\"c\": 6}}\n"
|
2016-11-28 18:19:35 +00:00
|
|
|
invalidMsg = "cpu_load_short,host=server01 1422568543702900257\n"
|
2016-02-12 10:05:33 +00:00
|
|
|
metricBuffer = 5
|
2016-02-10 23:28:52 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func newTestNatsConsumer() (*natsConsumer, chan *nats.Msg) {
|
2016-02-12 10:05:33 +00:00
|
|
|
in := make(chan *nats.Msg, metricBuffer)
|
2016-02-10 23:28:52 +00:00
|
|
|
n := &natsConsumer{
|
2016-02-16 00:21:38 +00:00
|
|
|
QueueGroup: "test",
|
|
|
|
Subjects: []string{"telegraf"},
|
|
|
|
Servers: []string{"nats://localhost:4222"},
|
|
|
|
Secure: false,
|
|
|
|
in: in,
|
|
|
|
errs: make(chan error, metricBuffer),
|
|
|
|
done: make(chan struct{}),
|
2016-02-10 23:28:52 +00:00
|
|
|
}
|
|
|
|
return n, in
|
|
|
|
}
|
|
|
|
|
2016-02-12 10:05:33 +00:00
|
|
|
// Test that the parser parses NATS messages into metrics
|
2016-02-10 23:28:52 +00:00
|
|
|
func TestRunParser(t *testing.T) {
|
|
|
|
n, in := newTestNatsConsumer()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
n.acc = &acc
|
2016-02-10 23:28:52 +00:00
|
|
|
defer close(n.done)
|
|
|
|
|
|
|
|
n.parser, _ = parsers.NewInfluxParser()
|
2016-10-26 15:38:56 +00:00
|
|
|
n.wg.Add(1)
|
2016-02-10 23:28:52 +00:00
|
|
|
go n.receiver()
|
|
|
|
in <- natsMsg(testMsg)
|
|
|
|
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2016-02-10 23:28:52 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// Test that the parser ignores invalid messages
|
|
|
|
func TestRunParserInvalidMsg(t *testing.T) {
|
|
|
|
n, in := newTestNatsConsumer()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
n.acc = &acc
|
2016-02-10 23:28:52 +00:00
|
|
|
defer close(n.done)
|
|
|
|
|
|
|
|
n.parser, _ = parsers.NewInfluxParser()
|
2016-10-26 15:38:56 +00:00
|
|
|
n.wg.Add(1)
|
2016-02-10 23:28:52 +00:00
|
|
|
go n.receiver()
|
|
|
|
in <- natsMsg(invalidMsg)
|
|
|
|
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.WaitError(1)
|
2018-03-28 00:30:51 +00:00
|
|
|
assert.Contains(t, acc.Errors[0].Error(), "E! subject: telegraf, error: metric parse error")
|
2017-03-24 19:03:36 +00:00
|
|
|
assert.EqualValues(t, 0, acc.NMetrics())
|
2016-02-10 23:28:52 +00:00
|
|
|
}
|
|
|
|
|
2016-02-12 10:05:33 +00:00
|
|
|
// Test that the parser parses line format messages into metrics
|
2016-02-10 23:28:52 +00:00
|
|
|
func TestRunParserAndGather(t *testing.T) {
|
|
|
|
n, in := newTestNatsConsumer()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
n.acc = &acc
|
2016-02-10 23:28:52 +00:00
|
|
|
defer close(n.done)
|
|
|
|
|
|
|
|
n.parser, _ = parsers.NewInfluxParser()
|
2016-10-26 15:38:56 +00:00
|
|
|
n.wg.Add(1)
|
2016-02-10 23:28:52 +00:00
|
|
|
go n.receiver()
|
|
|
|
in <- natsMsg(testMsg)
|
|
|
|
|
|
|
|
n.Gather(&acc)
|
|
|
|
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2016-02-10 23:28:52 +00:00
|
|
|
acc.AssertContainsFields(t, "cpu_load_short",
|
|
|
|
map[string]interface{}{"value": float64(23422)})
|
|
|
|
}
|
|
|
|
|
2016-02-12 10:05:33 +00:00
|
|
|
// Test that the parser parses graphite format messages into metrics
|
2016-02-10 23:28:52 +00:00
|
|
|
func TestRunParserAndGatherGraphite(t *testing.T) {
|
|
|
|
n, in := newTestNatsConsumer()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
n.acc = &acc
|
2016-02-10 23:28:52 +00:00
|
|
|
defer close(n.done)
|
|
|
|
|
|
|
|
n.parser, _ = parsers.NewGraphiteParser("_", []string{}, nil)
|
2016-10-26 15:38:56 +00:00
|
|
|
n.wg.Add(1)
|
2016-02-10 23:28:52 +00:00
|
|
|
go n.receiver()
|
|
|
|
in <- natsMsg(testMsgGraphite)
|
|
|
|
|
|
|
|
n.Gather(&acc)
|
|
|
|
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2016-02-10 23:28:52 +00:00
|
|
|
acc.AssertContainsFields(t, "cpu_load_short_graphite",
|
|
|
|
map[string]interface{}{"value": float64(23422)})
|
|
|
|
}
|
|
|
|
|
2016-02-12 10:05:33 +00:00
|
|
|
// Test that the parser parses json format messages into metrics
|
2016-02-10 23:28:52 +00:00
|
|
|
func TestRunParserAndGatherJSON(t *testing.T) {
|
|
|
|
n, in := newTestNatsConsumer()
|
2016-02-16 00:21:38 +00:00
|
|
|
acc := testutil.Accumulator{}
|
|
|
|
n.acc = &acc
|
2016-02-10 23:28:52 +00:00
|
|
|
defer close(n.done)
|
|
|
|
|
2018-06-27 22:53:52 +00:00
|
|
|
n.parser, _ = parsers.NewJSONParser("nats_json_test", []string{}, nil, nil)
|
2016-10-26 15:38:56 +00:00
|
|
|
n.wg.Add(1)
|
2016-02-10 23:28:52 +00:00
|
|
|
go n.receiver()
|
|
|
|
in <- natsMsg(testMsgJSON)
|
|
|
|
|
|
|
|
n.Gather(&acc)
|
|
|
|
|
2017-03-24 19:03:36 +00:00
|
|
|
acc.Wait(1)
|
2016-02-10 23:28:52 +00:00
|
|
|
acc.AssertContainsFields(t, "nats_json_test",
|
|
|
|
map[string]interface{}{
|
|
|
|
"a": float64(5),
|
|
|
|
"b_c": float64(6),
|
|
|
|
})
|
|
|
|
}
|
|
|
|
|
|
|
|
func natsMsg(val string) *nats.Msg {
|
|
|
|
return &nats.Msg{
|
|
|
|
Subject: "telegraf",
|
|
|
|
Data: []byte(val),
|
|
|
|
}
|
|
|
|
}
|