fix(inputs.mongodb): update version check for newer versions (#11635)
This commit is contained in:
parent
ce2053d26d
commit
75e8640a26
|
|
@ -134,7 +134,7 @@ func poolStatsCommand(version string) (string, error) {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
||||||
if major == 5 {
|
if major >= 5 {
|
||||||
return "connPoolStats", nil
|
return "connPoolStats", nil
|
||||||
}
|
}
|
||||||
return "shardConnPoolStats", nil
|
return "shardConnPoolStats", nil
|
||||||
|
|
|
||||||
|
|
@ -1,17 +1,54 @@
|
||||||
//go:build integration
|
|
||||||
// +build integration
|
|
||||||
|
|
||||||
package mongodb
|
package mongodb
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"fmt"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/docker/go-connections/nat"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"github.com/testcontainers/testcontainers-go/wait"
|
||||||
|
|
||||||
"github.com/influxdata/telegraf/testutil"
|
"github.com/influxdata/telegraf/testutil"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetDefaultTags(t *testing.T) {
|
var ServicePort = "27017"
|
||||||
|
|
||||||
|
func createTestServer(t *testing.T) *testutil.Container {
|
||||||
|
container := testutil.Container{
|
||||||
|
Image: "mongo",
|
||||||
|
ExposedPorts: []string{ServicePort},
|
||||||
|
WaitingFor: wait.ForAll(
|
||||||
|
wait.NewHTTPStrategy("/").WithPort(nat.Port(ServicePort)),
|
||||||
|
wait.ForLog("Waiting for connections"),
|
||||||
|
),
|
||||||
|
}
|
||||||
|
err := container.Start()
|
||||||
|
require.NoError(t, err, "failed to start container")
|
||||||
|
|
||||||
|
return &container
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestGetDefaultTagsIntegration(t *testing.T) {
|
||||||
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
|
container := createTestServer(t)
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, container.Terminate(), "terminating container failed")
|
||||||
|
}()
|
||||||
|
|
||||||
|
m := &MongoDB{
|
||||||
|
Log: testutil.Logger{},
|
||||||
|
Servers: []string{
|
||||||
|
fmt.Sprintf("mongodb://%s:%s", container.Address, container.Ports[ServicePort]),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := m.Init()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
server := m.clients[0]
|
||||||
|
|
||||||
var tagTests = []struct {
|
var tagTests = []struct {
|
||||||
in string
|
in string
|
||||||
out string
|
out string
|
||||||
|
|
@ -26,10 +63,29 @@ func TestGetDefaultTags(t *testing.T) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestAddDefaultStats(t *testing.T) {
|
func TestAddDefaultStatsIntegration(t *testing.T) {
|
||||||
var acc testutil.Accumulator
|
if testing.Short() {
|
||||||
|
t.Skip("Skipping integration test in short mode")
|
||||||
|
}
|
||||||
|
|
||||||
err := server.gatherData(&acc, false, true, true, true, []string{"local"})
|
container := createTestServer(t)
|
||||||
|
defer func() {
|
||||||
|
require.NoError(t, container.Terminate(), "terminating container failed")
|
||||||
|
}()
|
||||||
|
|
||||||
|
m := &MongoDB{
|
||||||
|
Log: testutil.Logger{},
|
||||||
|
Servers: []string{
|
||||||
|
fmt.Sprintf("mongodb://%s:%s", container.Address, container.Ports[ServicePort]),
|
||||||
|
},
|
||||||
|
}
|
||||||
|
err := m.Init()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
server := m.clients[0]
|
||||||
|
|
||||||
|
var acc testutil.Accumulator
|
||||||
|
err = server.gatherData(&acc, false, true, true, true, []string{"local"})
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
|
|
||||||
// need to call this twice so it can perform the diff
|
// need to call this twice so it can perform the diff
|
||||||
|
|
@ -63,6 +119,11 @@ func TestPoolStatsVersionCompatibility(t *testing.T) {
|
||||||
version: "5.0.0",
|
version: "5.0.0",
|
||||||
expectedCommand: "connPoolStats",
|
expectedCommand: "connPoolStats",
|
||||||
},
|
},
|
||||||
|
{
|
||||||
|
name: "mongodb v6",
|
||||||
|
version: "6.0.0",
|
||||||
|
expectedCommand: "connPoolStats",
|
||||||
|
},
|
||||||
{
|
{
|
||||||
name: "invalid version",
|
name: "invalid version",
|
||||||
version: "v4",
|
version: "v4",
|
||||||
|
|
|
||||||
|
|
@ -1,53 +0,0 @@
|
||||||
//go:build integration
|
|
||||||
// +build integration
|
|
||||||
|
|
||||||
package mongodb
|
|
||||||
|
|
||||||
import (
|
|
||||||
"context"
|
|
||||||
"log"
|
|
||||||
"math/rand"
|
|
||||||
"os"
|
|
||||||
"testing"
|
|
||||||
"time"
|
|
||||||
|
|
||||||
"github.com/influxdata/telegraf/testutil"
|
|
||||||
)
|
|
||||||
|
|
||||||
var server *Server
|
|
||||||
|
|
||||||
func testSetup(_ *testing.M) {
|
|
||||||
connectionString := os.Getenv("MONGODB_URL")
|
|
||||||
if connectionString == "" {
|
|
||||||
connectionString = "mongodb://127.0.0.1:27017"
|
|
||||||
}
|
|
||||||
|
|
||||||
m := &MongoDB{
|
|
||||||
Log: testutil.Logger{},
|
|
||||||
Servers: []string{connectionString},
|
|
||||||
}
|
|
||||||
err := m.Init()
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("Failed to connect to MongoDB: %v", err)
|
|
||||||
}
|
|
||||||
|
|
||||||
server = m.clients[0]
|
|
||||||
}
|
|
||||||
|
|
||||||
func testTeardown(_ *testing.M) {
|
|
||||||
err := server.client.Disconnect(context.Background())
|
|
||||||
if err != nil {
|
|
||||||
log.Fatalf("failed to disconnect: %v", err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func TestMain(m *testing.M) {
|
|
||||||
// seed randomness for use with tests
|
|
||||||
rand.Seed(time.Now().UTC().UnixNano())
|
|
||||||
|
|
||||||
testSetup(m)
|
|
||||||
res := m.Run()
|
|
||||||
testTeardown(m)
|
|
||||||
|
|
||||||
os.Exit(res)
|
|
||||||
}
|
|
||||||
Loading…
Reference in New Issue