2016-02-09 22:03:46 +00:00
|
|
|
package mqtt_consumer
|
|
|
|
|
|
|
|
import (
|
|
|
|
"testing"
|
2019-08-15 00:05:34 +00:00
|
|
|
"time"
|
2016-02-09 22:03:46 +00:00
|
|
|
|
2018-11-05 21:34:28 +00:00
|
|
|
"github.com/eclipse/paho.mqtt.golang"
|
2019-08-15 00:05:34 +00:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2016-02-09 22:03:46 +00:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2019-08-15 00:05:34 +00:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-09 22:03:46 +00:00
|
|
|
)
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
type FakeClient struct {
|
|
|
|
ConnectF func() mqtt.Token
|
|
|
|
SubscribeMultipleF func(filters map[string]byte, callback mqtt.MessageHandler) mqtt.Token
|
|
|
|
AddRouteF func(topic string, callback mqtt.MessageHandler)
|
|
|
|
DisconnectF func(quiesce uint)
|
2016-02-09 22:03:46 +00:00
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
connectCallCount int
|
|
|
|
subscribeCallCount int
|
|
|
|
addRouteCallCount int
|
|
|
|
disconnectCallCount int
|
|
|
|
}
|
2017-09-11 19:24:51 +00:00
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (c *FakeClient) Connect() mqtt.Token {
|
|
|
|
c.connectCallCount++
|
|
|
|
return c.ConnectF()
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (c *FakeClient) SubscribeMultiple(filters map[string]byte, callback mqtt.MessageHandler) mqtt.Token {
|
|
|
|
c.subscribeCallCount++
|
|
|
|
return c.SubscribeMultipleF(filters, callback)
|
|
|
|
}
|
2016-03-07 12:56:10 +00:00
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (c *FakeClient) AddRoute(topic string, callback mqtt.MessageHandler) {
|
|
|
|
c.addRouteCallCount++
|
|
|
|
c.AddRouteF(topic, callback)
|
|
|
|
}
|
2016-03-07 12:56:10 +00:00
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (c *FakeClient) Disconnect(quiesce uint) {
|
|
|
|
c.disconnectCallCount++
|
|
|
|
c.DisconnectF(quiesce)
|
2016-03-07 12:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
type FakeParser struct {
|
|
|
|
}
|
2016-03-07 12:56:10 +00:00
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
// FakeParser satisfies parsers.Parser
|
|
|
|
var _ parsers.Parser = &FakeParser{}
|
2016-03-07 12:56:10 +00:00
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (p *FakeParser) Parse(buf []byte) ([]telegraf.Metric, error) {
|
|
|
|
panic("not implemented")
|
2016-03-07 12:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (p *FakeParser) ParseLine(line string) (telegraf.Metric, error) {
|
|
|
|
panic("not implemented")
|
2016-03-07 12:56:10 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (p *FakeParser) SetDefaultTags(tags map[string]string) {
|
|
|
|
panic("not implemented")
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
type FakeToken struct {
|
|
|
|
sessionPresent bool
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
// FakeToken satisfies mqtt.Token
|
|
|
|
var _ mqtt.Token = &FakeToken{}
|
|
|
|
|
|
|
|
func (t *FakeToken) Wait() bool {
|
|
|
|
return true
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (t *FakeToken) WaitTimeout(time.Duration) bool {
|
|
|
|
return true
|
2019-07-22 21:14:23 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (t *FakeToken) Error() error {
|
|
|
|
return nil
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func (t *FakeToken) SessionPresent() bool {
|
|
|
|
return t.sessionPresent
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
// Test the basic lifecycle transitions of the plugin.
|
|
|
|
func TestLifecycleSanity(t *testing.T) {
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
|
|
|
plugin := New(func(o *mqtt.ClientOptions) Client {
|
|
|
|
return &FakeClient{
|
|
|
|
ConnectF: func() mqtt.Token {
|
|
|
|
return &FakeToken{}
|
|
|
|
},
|
|
|
|
AddRouteF: func(topic string, callback mqtt.MessageHandler) {
|
|
|
|
},
|
|
|
|
SubscribeMultipleF: func(filters map[string]byte, callback mqtt.MessageHandler) mqtt.Token {
|
|
|
|
return &FakeToken{}
|
|
|
|
},
|
|
|
|
DisconnectF: func(quiesce uint) {
|
|
|
|
},
|
|
|
|
}
|
|
|
|
})
|
|
|
|
plugin.Servers = []string{"tcp://127.0.0.1"}
|
|
|
|
|
|
|
|
parser := &FakeParser{}
|
|
|
|
plugin.SetParser(parser)
|
|
|
|
|
|
|
|
err := plugin.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = plugin.Start(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
err = plugin.Gather(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
plugin.Stop()
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
// Test that default client has random ID
|
|
|
|
func TestRandomClientID(t *testing.T) {
|
|
|
|
var err error
|
|
|
|
|
|
|
|
m1 := New(nil)
|
|
|
|
err = m1.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
m2 := New(nil)
|
|
|
|
err = m2.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
require.NotEqual(t, m1.opts.ClientID, m2.opts.ClientID)
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
// PersistentSession requires ClientID
|
|
|
|
func TestPersistentClientIDFail(t *testing.T) {
|
|
|
|
plugin := New(nil)
|
|
|
|
plugin.PersistentSession = true
|
|
|
|
|
|
|
|
err := plugin.Init()
|
|
|
|
require.Error(t, err)
|
|
|
|
}
|
|
|
|
|
2019-08-20 02:05:22 +00:00
|
|
|
type Message struct {
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Duplicate() bool {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Qos() byte {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Retained() bool {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Topic() string {
|
|
|
|
return "telegraf"
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) MessageID() uint16 {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Payload() []byte {
|
|
|
|
return []byte("cpu time_idle=42i")
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *Message) Ack() {
|
|
|
|
panic("not implemented")
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestTopicTag(t *testing.T) {
|
|
|
|
tests := []struct {
|
|
|
|
name string
|
|
|
|
topicTag func() *string
|
|
|
|
expected []telegraf.Metric
|
|
|
|
}{
|
|
|
|
{
|
|
|
|
name: "default topic when topic tag is unset for backwards compatibility",
|
|
|
|
topicTag: func() *string {
|
|
|
|
return nil
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{
|
|
|
|
"topic": "telegraf",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"time_idle": 42,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "use topic tag when set",
|
|
|
|
topicTag: func() *string {
|
|
|
|
tag := "topic_tag"
|
|
|
|
return &tag
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{
|
|
|
|
"topic_tag": "telegraf",
|
|
|
|
},
|
|
|
|
map[string]interface{}{
|
|
|
|
"time_idle": 42,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
{
|
|
|
|
name: "no topic tag is added when topic tag is set to the empty string",
|
|
|
|
topicTag: func() *string {
|
|
|
|
tag := ""
|
|
|
|
return &tag
|
|
|
|
},
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
testutil.MustMetric(
|
|
|
|
"cpu",
|
|
|
|
map[string]string{},
|
|
|
|
map[string]interface{}{
|
|
|
|
"time_idle": 42,
|
|
|
|
},
|
|
|
|
time.Unix(0, 0),
|
|
|
|
),
|
|
|
|
},
|
|
|
|
},
|
|
|
|
}
|
|
|
|
for _, tt := range tests {
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
var handler mqtt.MessageHandler
|
|
|
|
client := &FakeClient{
|
|
|
|
ConnectF: func() mqtt.Token {
|
|
|
|
return &FakeToken{}
|
|
|
|
},
|
|
|
|
AddRouteF: func(topic string, callback mqtt.MessageHandler) {
|
|
|
|
handler = callback
|
|
|
|
},
|
|
|
|
SubscribeMultipleF: func(filters map[string]byte, callback mqtt.MessageHandler) mqtt.Token {
|
|
|
|
return &FakeToken{}
|
|
|
|
},
|
|
|
|
DisconnectF: func(quiesce uint) {
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
plugin := New(func(o *mqtt.ClientOptions) Client {
|
|
|
|
return client
|
|
|
|
})
|
|
|
|
plugin.Topics = []string{"telegraf"}
|
|
|
|
plugin.TopicTag = tt.topicTag()
|
|
|
|
|
|
|
|
parser, err := parsers.NewInfluxParser()
|
|
|
|
require.NoError(t, err)
|
|
|
|
plugin.SetParser(parser)
|
|
|
|
|
|
|
|
err = plugin.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err = plugin.Start(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
handler(nil, &Message{})
|
|
|
|
|
|
|
|
plugin.Stop()
|
|
|
|
|
|
|
|
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(),
|
|
|
|
testutil.IgnoreTime())
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2019-08-15 00:05:34 +00:00
|
|
|
func TestAddRouteCalledForEachTopic(t *testing.T) {
|
|
|
|
client := &FakeClient{
|
|
|
|
ConnectF: func() mqtt.Token {
|
|
|
|
return &FakeToken{}
|
|
|
|
},
|
|
|
|
AddRouteF: func(topic string, callback mqtt.MessageHandler) {
|
|
|
|
},
|
|
|
|
SubscribeMultipleF: func(filters map[string]byte, callback mqtt.MessageHandler) mqtt.Token {
|
|
|
|
return &FakeToken{}
|
|
|
|
},
|
|
|
|
DisconnectF: func(quiesce uint) {
|
|
|
|
},
|
|
|
|
}
|
|
|
|
plugin := New(func(o *mqtt.ClientOptions) Client {
|
|
|
|
return client
|
|
|
|
})
|
|
|
|
plugin.Topics = []string{"a", "b"}
|
|
|
|
|
|
|
|
err := plugin.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err = plugin.Start(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
plugin.Stop()
|
|
|
|
|
|
|
|
require.Equal(t, client.addRouteCallCount, 2)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubscribeCalledIfNoSession(t *testing.T) {
|
|
|
|
client := &FakeClient{
|
|
|
|
ConnectF: func() mqtt.Token {
|
|
|
|
return &FakeToken{}
|
|
|
|
},
|
|
|
|
AddRouteF: func(topic string, callback mqtt.MessageHandler) {
|
|
|
|
},
|
|
|
|
SubscribeMultipleF: func(filters map[string]byte, callback mqtt.MessageHandler) mqtt.Token {
|
|
|
|
return &FakeToken{}
|
|
|
|
},
|
|
|
|
DisconnectF: func(quiesce uint) {
|
|
|
|
},
|
|
|
|
}
|
|
|
|
plugin := New(func(o *mqtt.ClientOptions) Client {
|
|
|
|
return client
|
|
|
|
})
|
|
|
|
plugin.Topics = []string{"b"}
|
|
|
|
|
|
|
|
err := plugin.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err = plugin.Start(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
plugin.Stop()
|
|
|
|
|
|
|
|
require.Equal(t, client.subscribeCallCount, 1)
|
|
|
|
}
|
|
|
|
|
|
|
|
func TestSubscribeNotCalledIfSession(t *testing.T) {
|
|
|
|
client := &FakeClient{
|
|
|
|
ConnectF: func() mqtt.Token {
|
|
|
|
return &FakeToken{sessionPresent: true}
|
|
|
|
},
|
|
|
|
AddRouteF: func(topic string, callback mqtt.MessageHandler) {
|
|
|
|
},
|
|
|
|
SubscribeMultipleF: func(filters map[string]byte, callback mqtt.MessageHandler) mqtt.Token {
|
|
|
|
return &FakeToken{}
|
|
|
|
},
|
|
|
|
DisconnectF: func(quiesce uint) {
|
|
|
|
},
|
|
|
|
}
|
|
|
|
plugin := New(func(o *mqtt.ClientOptions) Client {
|
|
|
|
return client
|
|
|
|
})
|
|
|
|
plugin.Topics = []string{"b"}
|
|
|
|
|
|
|
|
err := plugin.Init()
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
err = plugin.Start(&acc)
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
plugin.Stop()
|
|
|
|
|
|
|
|
require.Equal(t, client.subscribeCallCount, 0)
|
2016-02-09 22:03:46 +00:00
|
|
|
}
|