2015-11-14 04:37:24 +08:00
|
|
|
package mqtt
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-24 23:30:02 +08:00
|
|
|
"fmt"
|
2023-01-18 02:45:45 +08:00
|
|
|
"github.com/influxdata/telegraf/metric"
|
2022-07-27 23:36:57 +08:00
|
|
|
"path/filepath"
|
2015-11-14 04:37:24 +08:00
|
|
|
"testing"
|
2023-01-18 02:45:45 +08:00
|
|
|
"time"
|
2015-11-14 04:37:24 +08:00
|
|
|
|
2022-11-09 03:04:12 +08:00
|
|
|
"github.com/stretchr/testify/require"
|
2022-05-24 23:30:02 +08:00
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
2016-02-11 06:50:07 +08:00
|
|
|
|
2022-11-09 03:04:12 +08:00
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2015-11-14 04:37:24 +08:00
|
|
|
)
|
|
|
|
|
|
2022-07-27 23:36:57 +08:00
|
|
|
const servicePort = "1883"
|
|
|
|
|
|
|
|
|
|
func launchTestContainer(t *testing.T) *testutil.Container {
|
|
|
|
|
conf, err := filepath.Abs(filepath.Join("testdata", "mosquitto.conf"))
|
|
|
|
|
require.NoError(t, err, "missing file mosquitto.conf")
|
|
|
|
|
|
|
|
|
|
container := testutil.Container{
|
|
|
|
|
Image: "eclipse-mosquitto:2",
|
|
|
|
|
ExposedPorts: []string{servicePort},
|
|
|
|
|
WaitingFor: wait.ForListeningPort(servicePort),
|
|
|
|
|
BindMounts: map[string]string{
|
|
|
|
|
"/mosquitto/config/mosquitto.conf": conf,
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
err = container.Start()
|
|
|
|
|
require.NoError(t, err, "failed to start container")
|
|
|
|
|
|
|
|
|
|
return &container
|
|
|
|
|
}
|
|
|
|
|
|
2021-01-27 02:06:12 +08:00
|
|
|
func TestConnectAndWriteIntegration(t *testing.T) {
|
2015-11-14 04:37:24 +08:00
|
|
|
if testing.Short() {
|
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
|
}
|
|
|
|
|
|
2022-07-27 23:36:57 +08:00
|
|
|
container := launchTestContainer(t)
|
2022-11-09 03:04:12 +08:00
|
|
|
defer container.Terminate()
|
2022-07-27 23:36:57 +08:00
|
|
|
var url = fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort])
|
2022-11-09 03:04:12 +08:00
|
|
|
s := serializers.NewInfluxSerializer()
|
2022-07-27 23:36:57 +08:00
|
|
|
m := &MQTT{
|
|
|
|
|
Servers: []string{url},
|
|
|
|
|
serializer: s,
|
|
|
|
|
KeepAlive: 30,
|
|
|
|
|
Log: testutil.Logger{Name: "mqtt-default-integration-test"},
|
2022-05-24 23:30:02 +08:00
|
|
|
}
|
2022-07-27 23:36:57 +08:00
|
|
|
|
|
|
|
|
// Verify that we can connect to the MQTT broker
|
2023-01-18 02:45:45 +08:00
|
|
|
require.NoError(t, m.Init())
|
|
|
|
|
|
|
|
|
|
// Verify that we can connect to the MQTT broker
|
|
|
|
|
require.NoError(t, m.Connect())
|
2022-07-27 23:36:57 +08:00
|
|
|
|
|
|
|
|
// Verify that we can successfully write data to the mqtt broker
|
2023-01-18 02:45:45 +08:00
|
|
|
require.NoError(t, m.Write(testutil.MockMetrics()))
|
2022-07-27 23:36:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestConnectAndWriteIntegrationMQTTv3(t *testing.T) {
|
|
|
|
|
if testing.Short() {
|
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container := launchTestContainer(t)
|
2022-11-09 03:04:12 +08:00
|
|
|
defer container.Terminate()
|
2022-07-27 23:36:57 +08:00
|
|
|
|
|
|
|
|
var url = fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort])
|
2022-11-09 03:04:12 +08:00
|
|
|
s := serializers.NewInfluxSerializer()
|
2022-07-27 23:36:57 +08:00
|
|
|
m := &MQTT{
|
|
|
|
|
Servers: []string{url},
|
|
|
|
|
Protocol: "3.1.1",
|
|
|
|
|
serializer: s,
|
|
|
|
|
KeepAlive: 30,
|
|
|
|
|
Log: testutil.Logger{Name: "mqttv311-integration-test"},
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify that we can connect to the MQTT broker
|
2023-01-18 02:45:45 +08:00
|
|
|
require.NoError(t, m.Init())
|
|
|
|
|
|
|
|
|
|
// Verify that we can connect to the MQTT broker
|
|
|
|
|
require.NoError(t, m.Connect())
|
2022-07-27 23:36:57 +08:00
|
|
|
|
|
|
|
|
// Verify that we can successfully write data to the mqtt broker
|
2023-01-18 02:45:45 +08:00
|
|
|
require.NoError(t, m.Write(testutil.MockMetrics()))
|
2022-07-27 23:36:57 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestConnectAndWriteIntegrationMQTTv5(t *testing.T) {
|
|
|
|
|
if testing.Short() {
|
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
container := launchTestContainer(t)
|
2022-11-09 03:04:12 +08:00
|
|
|
defer container.Terminate()
|
2022-05-24 23:30:02 +08:00
|
|
|
|
2022-05-28 00:24:31 +08:00
|
|
|
var url = fmt.Sprintf("%s:%s", container.Address, container.Ports[servicePort])
|
2022-11-09 03:04:12 +08:00
|
|
|
s := serializers.NewInfluxSerializer()
|
2015-11-14 04:37:24 +08:00
|
|
|
m := &MQTT{
|
2016-02-11 06:50:07 +08:00
|
|
|
Servers: []string{url},
|
2022-07-27 23:36:57 +08:00
|
|
|
Protocol: "5",
|
2016-02-11 06:50:07 +08:00
|
|
|
serializer: s,
|
2021-10-01 23:10:30 +08:00
|
|
|
KeepAlive: 30,
|
2022-07-27 23:36:57 +08:00
|
|
|
Log: testutil.Logger{Name: "mqttv5-integration-test"},
|
2015-11-14 04:37:24 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify that we can connect to the MQTT broker
|
2023-01-18 02:45:45 +08:00
|
|
|
require.NoError(t, m.Init())
|
|
|
|
|
require.NoError(t, m.Connect())
|
2015-11-14 04:37:24 +08:00
|
|
|
|
|
|
|
|
// Verify that we can successfully write data to the mqtt broker
|
2023-01-18 02:45:45 +08:00
|
|
|
require.NoError(t, m.Write(testutil.MockMetrics()))
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestMQTTTopicGenerationTemplateIsValid(t *testing.T) {
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
topic string
|
|
|
|
|
expectedError string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "a valid pattern is accepted",
|
|
|
|
|
topic: "this/is/valid",
|
|
|
|
|
expectedError: "",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "an invalid pattern is rejected",
|
|
|
|
|
topic: "this/is/#/invalid",
|
|
|
|
|
expectedError: "found forbidden character # in the topic name this/is/#/invalid",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "an invalid pattern is rejected",
|
|
|
|
|
topic: "this/is/+/invalid",
|
|
|
|
|
expectedError: "found forbidden character + in the topic name this/is/+/invalid",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
m := &MQTT{
|
|
|
|
|
Topic: tt.topic,
|
|
|
|
|
}
|
|
|
|
|
err := m.Init()
|
|
|
|
|
if tt.expectedError != "" {
|
|
|
|
|
require.ErrorContains(t, err, tt.expectedError)
|
|
|
|
|
} else {
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|
|
|
|
|
})
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
func TestGenerateTopicName(t *testing.T) {
|
|
|
|
|
s := serializers.NewInfluxSerializer()
|
|
|
|
|
m := &MQTT{
|
|
|
|
|
Servers: []string{"tcp://localhost:502"},
|
|
|
|
|
serializer: s,
|
|
|
|
|
KeepAlive: 30,
|
|
|
|
|
Log: testutil.Logger{},
|
|
|
|
|
}
|
|
|
|
|
tests := []struct {
|
|
|
|
|
name string
|
|
|
|
|
pattern string
|
|
|
|
|
want string
|
|
|
|
|
}{
|
|
|
|
|
{
|
|
|
|
|
name: "matches default legacy format",
|
|
|
|
|
pattern: "telegraf/{{ .Hostname }}/{{ .PluginName }}",
|
|
|
|
|
want: "telegraf/hostname/metric-name",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "respect hardcoded strings",
|
|
|
|
|
pattern: "this/is/a/topic",
|
|
|
|
|
want: "this/is/a/topic",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "allows the use of tags",
|
|
|
|
|
pattern: "{{ .TopicPrefix }}/{{ .Tag \"tag1\" }}",
|
|
|
|
|
want: "prefix/value1",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "uses the plugin name when no pattern is provided",
|
|
|
|
|
pattern: "",
|
|
|
|
|
want: "metric-name",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "ignores tag when tag does not exists",
|
|
|
|
|
pattern: "{{ .TopicPrefix }}/{{ .Tag \"not-a-tag\" }}",
|
|
|
|
|
want: "prefix",
|
|
|
|
|
},
|
|
|
|
|
{
|
|
|
|
|
name: "ignores empty forward slashes",
|
|
|
|
|
pattern: "double//slashes//are//ignored",
|
|
|
|
|
want: "double/slashes/are/ignored",
|
|
|
|
|
},
|
|
|
|
|
}
|
|
|
|
|
for _, tt := range tests {
|
|
|
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
|
|
|
m.Topic = tt.pattern
|
|
|
|
|
m.TopicPrefix = "prefix"
|
|
|
|
|
met := metric.New(
|
|
|
|
|
"metric-name",
|
|
|
|
|
map[string]string{"tag1": "value1"},
|
|
|
|
|
map[string]interface{}{"value": 123},
|
|
|
|
|
time.Date(2022, time.November, 10, 23, 0, 0, 0, time.UTC),
|
|
|
|
|
)
|
|
|
|
|
require.NoError(t, m.Init())
|
|
|
|
|
actual, err := m.generator.Generate("hostname", met)
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
require.Equal(t, tt.want, actual)
|
|
|
|
|
})
|
|
|
|
|
}
|
2015-11-14 04:37:24 +08:00
|
|
|
}
|