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)
|
|
|
|
}
|
|
|
|
|
|
|
|
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
|
|
|
}
|