2016-09-01 20:11:42 +08:00
|
|
|
package nats
|
|
|
|
|
|
|
|
|
|
import (
|
2022-05-24 23:29:15 +08:00
|
|
|
"fmt"
|
2016-09-01 20:11:42 +08:00
|
|
|
"testing"
|
|
|
|
|
|
|
|
|
|
"github.com/stretchr/testify/require"
|
2022-05-24 23:29:15 +08:00
|
|
|
"github.com/testcontainers/testcontainers-go/wait"
|
2022-11-09 03:04:12 +08:00
|
|
|
|
|
|
|
|
"github.com/influxdata/telegraf/plugins/serializers"
|
|
|
|
|
"github.com/influxdata/telegraf/testutil"
|
2016-09-01 20:11:42 +08:00
|
|
|
)
|
|
|
|
|
|
2021-01-27 02:06:12 +08:00
|
|
|
func TestConnectAndWriteIntegration(t *testing.T) {
|
2016-09-01 20:11:42 +08:00
|
|
|
if testing.Short() {
|
|
|
|
|
t.Skip("Skipping integration test in short mode")
|
|
|
|
|
}
|
|
|
|
|
|
2022-05-28 00:24:31 +08:00
|
|
|
servicePort := "4222"
|
2022-05-24 23:29:15 +08:00
|
|
|
container := testutil.Container{
|
|
|
|
|
Image: "nats",
|
2022-05-28 00:24:31 +08:00
|
|
|
ExposedPorts: []string{servicePort},
|
2022-05-24 23:29:15 +08:00
|
|
|
WaitingFor: wait.ForLog("Server is ready"),
|
|
|
|
|
}
|
|
|
|
|
err := container.Start()
|
|
|
|
|
require.NoError(t, err, "failed to start container")
|
2022-11-09 03:04:12 +08:00
|
|
|
defer container.Terminate()
|
2022-05-24 23:29:15 +08:00
|
|
|
|
2022-05-28 00:24:31 +08:00
|
|
|
server := []string{fmt.Sprintf("nats://%s:%s", container.Address, container.Ports[servicePort])}
|
2022-11-09 03:04:12 +08:00
|
|
|
s := serializers.NewInfluxSerializer()
|
2016-09-01 20:11:42 +08:00
|
|
|
n := &NATS{
|
|
|
|
|
Servers: server,
|
2020-11-28 00:24:26 +08:00
|
|
|
Name: "telegraf",
|
2016-09-01 20:11:42 +08:00
|
|
|
Subject: "telegraf",
|
|
|
|
|
serializer: s,
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Verify that we can connect to the NATS daemon
|
2022-05-24 23:29:15 +08:00
|
|
|
err = n.Connect()
|
2016-09-01 20:11:42 +08:00
|
|
|
require.NoError(t, err)
|
|
|
|
|
|
|
|
|
|
// Verify that we can successfully write data to the NATS daemon
|
|
|
|
|
err = n.Write(testutil.MockMetrics())
|
|
|
|
|
require.NoError(t, err)
|
|
|
|
|
}
|