Skip to content

Commit

Permalink
fix(inputs.mongodb): update version check for newer versions (#11635)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj authored Aug 8, 2022
1 parent ce2053d commit 75e8640
Show file tree
Hide file tree
Showing 3 changed files with 69 additions and 61 deletions.
2 changes: 1 addition & 1 deletion plugins/inputs/mongodb/mongodb_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -134,7 +134,7 @@ func poolStatsCommand(version string) (string, error) {
return "", err
}

if major == 5 {
if major >= 5 {
return "connPoolStats", nil
}
return "shardConnPoolStats", nil
Expand Down
75 changes: 68 additions & 7 deletions plugins/inputs/mongodb/mongodb_server_test.go
Original file line number Diff line number Diff line change
@@ -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
Expand All @@ -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
Expand Down Expand Up @@ -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",
Expand Down
53 changes: 0 additions & 53 deletions plugins/inputs/mongodb/mongodb_test.go

This file was deleted.

0 comments on commit 75e8640

Please sign in to comment.