2016-02-10 06:03:46 +08:00
|
|
|
package mqtt_consumer
|
|
|
|
|
|
|
|
|
|
import (
|
2021-11-23 23:20:39 +08:00
|
|
|
"fmt"
|
2016-02-10 06:03:46 +08:00
|
|
|
"testing"
|
2019-08-15 08:05:34 +08:00
|
|
|
"time"
|
2016-02-10 06:03:46 +08:00
|
|
|
|
2020-12-17 04:38:33 +08:00
|
|
|
mqtt "github.com/eclipse/paho.mqtt.golang"
|
2019-08-15 08:05:34 +08:00
|
|
|
"github.com/influxdata/telegraf"
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/parsers"
|
2022-07-07 04:23:13 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/parsers/influx"
|
2016-02-10 06:03:46 +08:00
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2019-08-15 08:05:34 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2016-02-10 06:03:46 +08:00
|
|
|
)
|
|
|
|
|
|
2019-08-15 08:05:34 +08: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-10 06:03:46 +08:00
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
connectCallCount int
|
|
|
|
|
subscribeCallCount int
|
|
|
|
|
addRouteCallCount int
|
|
|
|
|
disconnectCallCount int
|
|
|
|
|
}
|
2017-09-12 03:24:51 +08:00
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
func (c *FakeClient) Connect() mqtt.Token {
|
|
|
|
|
c.connectCallCount++
|
|
|
|
|
return c.ConnectF()
|
2016-02-10 06:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
func (c *FakeClient) SubscribeMultiple(filters map[string]byte, callback mqtt.MessageHandler) mqtt.Token {
|
|
|
|
|
c.subscribeCallCount++
|
|
|
|
|
return c.SubscribeMultipleF(filters, callback)
|
|
|
|
|
}
|
2016-03-07 20:56:10 +08:00
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
func (c *FakeClient) AddRoute(topic string, callback mqtt.MessageHandler) {
|
|
|
|
|
c.addRouteCallCount++
|
|
|
|
|
c.AddRouteF(topic, callback)
|
|
|
|
|
}
|
2016-03-07 20:56:10 +08:00
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
func (c *FakeClient) Disconnect(quiesce uint) {
|
|
|
|
|
c.disconnectCallCount++
|
|
|
|
|
c.DisconnectF(quiesce)
|
2016-03-07 20:56:10 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
type FakeParser struct {
|
|
|
|
|
}
|
2016-03-07 20:56:10 +08:00
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
// FakeParser satisfies parsers.Parser
|
|
|
|
|
var _ parsers.Parser = &FakeParser{}
|
2016-03-07 20:56:10 +08:00
|
|
|
|
2021-03-23 01:21:36 +08:00
|
|
|
func (p *FakeParser) Parse(_ []byte) ([]telegraf.Metric, error) {
|
2019-08-15 08:05:34 +08:00
|
|
|
panic("not implemented")
|
2016-03-07 20:56:10 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 01:21:36 +08:00
|
|
|
func (p *FakeParser) ParseLine(_ string) (telegraf.Metric, error) {
|
2019-08-15 08:05:34 +08:00
|
|
|
panic("not implemented")
|
2016-03-07 20:56:10 +08:00
|
|
|
}
|
|
|
|
|
|
2021-03-23 01:21:36 +08:00
|
|
|
func (p *FakeParser) SetDefaultTags(_ map[string]string) {
|
2019-08-15 08:05:34 +08:00
|
|
|
panic("not implemented")
|
2016-02-10 06:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
type FakeToken struct {
|
|
|
|
|
sessionPresent bool
|
2021-02-12 00:45:13 +08:00
|
|
|
complete chan struct{}
|
2016-02-10 06:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
// FakeToken satisfies mqtt.Token
|
|
|
|
|
var _ mqtt.Token = &FakeToken{}
|
|
|
|
|
|
|
|
|
|
func (t *FakeToken) Wait() bool {
|
|
|
|
|
return true
|
2016-02-10 06:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
func (t *FakeToken) WaitTimeout(time.Duration) bool {
|
|
|
|
|
return true
|
2019-07-23 05:14:23 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
func (t *FakeToken) Error() error {
|
|
|
|
|
return nil
|
2016-02-10 06:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
func (t *FakeToken) SessionPresent() bool {
|
|
|
|
|
return t.sessionPresent
|
2016-02-10 06:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2021-02-12 00:45:13 +08:00
|
|
|
func (t *FakeToken) Done() <-chan struct{} {
|
|
|
|
|
return t.complete
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08: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) {
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
})
|
2019-09-24 06:39:50 +08:00
|
|
|
plugin.Log = testutil.Logger{}
|
2019-08-15 08:05:34 +08:00
|
|
|
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-10 06:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
// Test that default client has random ID
|
|
|
|
|
func TestRandomClientID(t *testing.T) {
|
|
|
|
|
var err error
|
|
|
|
|
|
|
|
|
|
m1 := New(nil)
|
2019-09-24 06:39:50 +08:00
|
|
|
m1.Log = testutil.Logger{}
|
2019-08-15 08:05:34 +08:00
|
|
|
err = m1.Init()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
m2 := New(nil)
|
2019-09-24 06:39:50 +08:00
|
|
|
m2.Log = testutil.Logger{}
|
2019-08-15 08:05:34 +08:00
|
|
|
err = m2.Init()
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
require.NotEqual(t, m1.opts.ClientID, m2.opts.ClientID)
|
2016-02-10 06:03:46 +08:00
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08:00
|
|
|
// PersistentSession requires ClientID
|
|
|
|
|
func TestPersistentClientIDFail(t *testing.T) {
|
|
|
|
|
plugin := New(nil)
|
2019-09-24 06:39:50 +08:00
|
|
|
plugin.Log = testutil.Logger{}
|
2019-08-15 08:05:34 +08:00
|
|
|
plugin.PersistentSession = true
|
|
|
|
|
|
|
|
|
|
err := plugin.Init()
|
|
|
|
|
require.Error(t, err)
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-20 10:05:22 +08:00
|
|
|
type Message struct {
|
2021-11-23 23:20:39 +08:00
|
|
|
topic string
|
2022-08-23 02:54:38 +08:00
|
|
|
qos byte
|
2019-08-20 10:05:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Message) Duplicate() bool {
|
|
|
|
|
panic("not implemented")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Message) Qos() byte {
|
2022-08-23 02:54:38 +08:00
|
|
|
return m.qos
|
2019-08-20 10:05:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Message) Retained() bool {
|
|
|
|
|
panic("not implemented")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func (m *Message) Topic() string {
|
2021-11-23 23:20:39 +08:00
|
|
|
return m.topic
|
2019-08-20 10:05:22 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
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 {
|
2021-11-23 23:20:39 +08:00
|
|
|
name string
|
|
|
|
|
topic string
|
|
|
|
|
topicTag func() *string
|
|
|
|
|
expectedError error
|
|
|
|
|
topicParsing []TopicParsingConfig
|
|
|
|
|
expected []telegraf.Metric
|
2019-08-20 10:05:22 +08:00
|
|
|
}{
|
|
|
|
|
{
|
2021-11-23 23:20:39 +08:00
|
|
|
name: "default topic when topic tag is unset for backwards compatibility",
|
|
|
|
|
topic: "telegraf",
|
2019-08-20 10:05:22 +08:00
|
|
|
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),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-11-23 23:20:39 +08:00
|
|
|
name: "use topic tag when set",
|
|
|
|
|
topic: "telegraf",
|
2019-08-20 10:05:22 +08:00
|
|
|
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),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
2021-11-23 23:20:39 +08:00
|
|
|
name: "no topic tag is added when topic tag is set to the empty string",
|
|
|
|
|
topic: "telegraf",
|
2019-08-20 10:05:22 +08:00
|
|
|
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),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-11-23 23:20:39 +08:00
|
|
|
{
|
|
|
|
|
name: "topic parsing configured",
|
|
|
|
|
topic: "telegraf/123/test",
|
|
|
|
|
topicTag: func() *string {
|
|
|
|
|
tag := ""
|
|
|
|
|
return &tag
|
|
|
|
|
},
|
|
|
|
|
topicParsing: []TopicParsingConfig{
|
|
|
|
|
{
|
|
|
|
|
Topic: "telegraf/123/test",
|
|
|
|
|
Measurement: "_/_/measurement",
|
|
|
|
|
Tags: "testTag/_/_",
|
|
|
|
|
Fields: "_/testNumber/_",
|
|
|
|
|
FieldTypes: map[string]string{
|
|
|
|
|
"testNumber": "int",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"test",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"testTag": "telegraf",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"testNumber": 123,
|
|
|
|
|
"time_idle": 42,
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "topic parsing configured with a mqtt wild card `+`",
|
|
|
|
|
topic: "telegraf/123/test/hello",
|
|
|
|
|
topicTag: func() *string {
|
|
|
|
|
tag := ""
|
|
|
|
|
return &tag
|
|
|
|
|
},
|
|
|
|
|
topicParsing: []TopicParsingConfig{
|
|
|
|
|
{
|
|
|
|
|
Topic: "telegraf/+/test/hello",
|
|
|
|
|
Measurement: "_/_/measurement/_",
|
|
|
|
|
Tags: "testTag/_/_/_",
|
|
|
|
|
Fields: "_/testNumber/_/testString",
|
|
|
|
|
FieldTypes: map[string]string{
|
|
|
|
|
"testNumber": "int",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"test",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"testTag": "telegraf",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"testNumber": 123,
|
|
|
|
|
"testString": "hello",
|
|
|
|
|
"time_idle": 42,
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "topic parsing configured incorrectly",
|
|
|
|
|
topic: "telegraf/123/test/hello",
|
|
|
|
|
topicTag: func() *string {
|
|
|
|
|
tag := ""
|
|
|
|
|
return &tag
|
|
|
|
|
},
|
|
|
|
|
expectedError: fmt.Errorf("config error topic parsing: fields length does not equal topic length"),
|
|
|
|
|
topicParsing: []TopicParsingConfig{
|
|
|
|
|
{
|
|
|
|
|
Topic: "telegraf/+/test/hello",
|
|
|
|
|
Measurement: "_/_/measurement/_",
|
|
|
|
|
Tags: "testTag/_/_/_",
|
|
|
|
|
Fields: "_/_/testNumber:int/_/testString:string",
|
|
|
|
|
FieldTypes: map[string]string{
|
|
|
|
|
"testNumber": "int",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"test",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"testTag": "telegraf",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"testNumber": 123,
|
|
|
|
|
"testString": "hello",
|
|
|
|
|
"time_idle": 42,
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
2021-12-03 07:55:16 +08:00
|
|
|
{
|
|
|
|
|
name: "topic parsing configured without fields",
|
|
|
|
|
topic: "telegraf/123/test/hello",
|
|
|
|
|
topicTag: func() *string {
|
|
|
|
|
tag := ""
|
|
|
|
|
return &tag
|
|
|
|
|
},
|
|
|
|
|
topicParsing: []TopicParsingConfig{
|
|
|
|
|
{
|
|
|
|
|
Topic: "telegraf/+/test/hello",
|
|
|
|
|
Measurement: "_/_/measurement/_",
|
|
|
|
|
Tags: "testTag/_/_/_",
|
|
|
|
|
FieldTypes: map[string]string{
|
|
|
|
|
"testNumber": "int",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"test",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"testTag": "telegraf",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"time_idle": 42,
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "topic parsing configured without measurement",
|
|
|
|
|
topic: "telegraf/123/test/hello",
|
|
|
|
|
topicTag: func() *string {
|
|
|
|
|
tag := ""
|
|
|
|
|
return &tag
|
|
|
|
|
},
|
|
|
|
|
topicParsing: []TopicParsingConfig{
|
|
|
|
|
{
|
|
|
|
|
Topic: "telegraf/+/test/hello",
|
|
|
|
|
Tags: "testTag/_/_/_",
|
|
|
|
|
Fields: "_/testNumber/_/testString",
|
|
|
|
|
FieldTypes: map[string]string{
|
|
|
|
|
"testNumber": "int",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"cpu",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"testTag": "telegraf",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"testNumber": 123,
|
|
|
|
|
"testString": "hello",
|
|
|
|
|
"time_idle": 42,
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
2022-07-20 21:28:20 +08:00
|
|
|
{
|
|
|
|
|
name: "topic parsing configured topic with a prefix `/`",
|
|
|
|
|
topic: "/telegraf/123/test/hello",
|
|
|
|
|
topicTag: func() *string {
|
|
|
|
|
tag := ""
|
|
|
|
|
return &tag
|
|
|
|
|
},
|
|
|
|
|
topicParsing: []TopicParsingConfig{
|
|
|
|
|
{
|
|
|
|
|
Topic: "/telegraf/+/test/hello",
|
|
|
|
|
Measurement: "/_/_/measurement/_",
|
|
|
|
|
Tags: "/testTag/_/_/_",
|
|
|
|
|
Fields: "/_/testNumber/_/testString",
|
|
|
|
|
FieldTypes: map[string]string{
|
|
|
|
|
"testNumber": "int",
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
},
|
|
|
|
|
expected: []telegraf.Metric{
|
|
|
|
|
testutil.MustMetric(
|
|
|
|
|
"test",
|
|
|
|
|
map[string]string{
|
|
|
|
|
"testTag": "telegraf",
|
|
|
|
|
},
|
|
|
|
|
map[string]interface{}{
|
|
|
|
|
"testNumber": 123,
|
|
|
|
|
"testString": "hello",
|
|
|
|
|
"time_idle": 42,
|
|
|
|
|
},
|
|
|
|
|
time.Unix(0, 0),
|
|
|
|
|
),
|
|
|
|
|
},
|
|
|
|
|
},
|
2019-08-20 10:05:22 +08:00
|
|
|
}
|
|
|
|
|
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
|
|
|
|
|
})
|
2019-09-24 06:39:50 +08:00
|
|
|
plugin.Log = testutil.Logger{}
|
2021-11-23 23:20:39 +08:00
|
|
|
plugin.Topics = []string{tt.topic}
|
2019-08-20 10:05:22 +08:00
|
|
|
plugin.TopicTag = tt.topicTag()
|
2021-11-23 23:20:39 +08:00
|
|
|
plugin.TopicParsing = tt.topicParsing
|
2019-08-20 10:05:22 +08:00
|
|
|
|
2022-07-07 04:23:13 +08:00
|
|
|
parser := &influx.Parser{}
|
|
|
|
|
require.NoError(t, parser.Init())
|
2019-08-20 10:05:22 +08:00
|
|
|
plugin.SetParser(parser)
|
|
|
|
|
|
2022-07-07 04:23:13 +08:00
|
|
|
err := plugin.Init()
|
2021-11-23 23:20:39 +08:00
|
|
|
require.Equal(t, tt.expectedError, err)
|
|
|
|
|
if tt.expectedError != nil {
|
|
|
|
|
return
|
|
|
|
|
}
|
2019-08-20 10:05:22 +08:00
|
|
|
|
|
|
|
|
var acc testutil.Accumulator
|
|
|
|
|
err = plugin.Start(&acc)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
2021-11-23 23:20:39 +08:00
|
|
|
var m Message
|
|
|
|
|
m.topic = tt.topic
|
|
|
|
|
|
|
|
|
|
handler(nil, &m)
|
2019-08-20 10:05:22 +08:00
|
|
|
|
|
|
|
|
plugin.Stop()
|
|
|
|
|
|
|
|
|
|
testutil.RequireMetricsEqual(t, tt.expected, acc.GetTelegrafMetrics(),
|
|
|
|
|
testutil.IgnoreTime())
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2019-08-15 08:05:34 +08: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
|
|
|
|
|
})
|
2019-09-24 06:39:50 +08:00
|
|
|
plugin.Log = testutil.Logger{}
|
2019-08-15 08:05:34 +08:00
|
|
|
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
|
|
|
|
|
})
|
2019-09-24 06:39:50 +08:00
|
|
|
plugin.Log = testutil.Logger{}
|
2019-08-15 08:05:34 +08:00
|
|
|
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
|
|
|
|
|
})
|
2019-09-24 06:39:50 +08:00
|
|
|
plugin.Log = testutil.Logger{}
|
2019-08-15 08:05:34 +08:00
|
|
|
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-10 06:03:46 +08:00
|
|
|
}
|