fix(inputs.mongodb): update version check for newer versions (#11635)

This commit is contained in:
Joshua Powers 2022-08-08 09:37:52 -06:00 committed by GitHub
parent ce2053d26d
commit 75e8640a26
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 69 additions and 61 deletions

View File

@ -134,7 +134,7 @@ func poolStatsCommand(version string) (string, error) {
return "", err
}
if major == 5 {
if major >= 5 {
return "connPoolStats", nil
}
return "shardConnPoolStats", nil

View File

@ -1,17 +1,54 @@
//go:build integration
// +build integration
package mongodb
import (
"fmt"
"testing"
"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"
"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 {
in string
out string
@ -26,10 +63,29 @@ func TestGetDefaultTags(t *testing.T) {
}
}
func TestAddDefaultStats(t *testing.T) {
var acc testutil.Accumulator
func TestAddDefaultStatsIntegration(t *testing.T) {
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)
// need to call this twice so it can perform the diff
@ -63,6 +119,11 @@ func TestPoolStatsVersionCompatibility(t *testing.T) {
version: "5.0.0",
expectedCommand: "connPoolStats",
},
{
name: "mongodb v6",
version: "6.0.0",
expectedCommand: "connPoolStats",
},
{
name: "invalid version",
version: "v4",

View File

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