Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test: migrate postgres to testcontainers #11209

Merged
merged 1 commit into from
Jun 2, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
83 changes: 71 additions & 12 deletions plugins/inputs/postgresql/postgresql_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,21 +4,50 @@ 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"
)

const servicePort = "5432"

func launchTestContainer(t *testing.T) *testutil.Container {
container := testutil.Container{
Image: "postgres:alpine",
ExposedPorts: []string{servicePort},
Env: map[string]string{
"POSTGRES_HOST_AUTH_METHOD": "trust",
},
WaitingFor: wait.ForAll(
wait.ForLog("database system is ready to accept connections"),
wait.ForListeningPort(nat.Port(servicePort)),
),
}

err := container.Start()
require.NoError(t, err, "failed to start container")

return &container
}

func TestPostgresqlGeneratesMetricsIntegration(t *testing.T) {
if testing.Short() {
t.Skip("Skipping integration test in short mode")
}

container := launchTestContainer(t)
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

p := &Postgresql{
Service: Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
),
IsPgBouncer: false,
},
Expand Down Expand Up @@ -99,11 +128,17 @@ func TestPostgresqlTagsMetricsWithDatabaseNameIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}

container := launchTestContainer(t)
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

p := &Postgresql{
Service: Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
),
},
Databases: []string{"postgres"},
Expand All @@ -125,11 +160,17 @@ func TestPostgresqlDefaultsToAllDatabasesIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}

container := launchTestContainer(t)
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

p := &Postgresql{
Service: Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
),
},
}
Expand Down Expand Up @@ -158,11 +199,17 @@ func TestPostgresqlIgnoresUnwantedColumnsIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}

container := launchTestContainer(t)
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

p := &Postgresql{
Service: Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
),
},
}
Expand All @@ -181,11 +228,17 @@ func TestPostgresqlDatabaseWhitelistTestIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}

container := launchTestContainer(t)
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

p := &Postgresql{
Service: Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
),
},
Databases: []string{"template0"},
Expand Down Expand Up @@ -221,11 +274,17 @@ func TestPostgresqlDatabaseBlacklistTestIntegration(t *testing.T) {
t.Skip("Skipping integration test in short mode")
}

container := launchTestContainer(t)
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

p := &Postgresql{
Service: Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
),
},
IgnoredDatabases: []string{"template0"},
Expand Down
27 changes: 25 additions & 2 deletions plugins/inputs/postgresql_extensible/postgresql_extensible_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,25 +6,48 @@ import (
"testing"
"time"

"github.com/docker/go-connections/nat"
"github.com/stretchr/testify/require"
"github.com/testcontainers/testcontainers-go/wait"

"github.com/influxdata/telegraf/plugins/inputs/postgresql"
"github.com/influxdata/telegraf/testutil"
)

func queryRunner(t *testing.T, q query) *testutil.Accumulator {
servicePort := "5432"
container := testutil.Container{
Image: "postgres:alpine",
ExposedPorts: []string{servicePort},
Env: map[string]string{
"POSTGRES_HOST_AUTH_METHOD": "trust",
},
WaitingFor: wait.ForAll(
wait.ForLog("database system is ready to accept connections"),
wait.ForListeningPort(nat.Port(servicePort)),
),
}

err := container.Start()
require.NoError(t, err, "failed to start container")
defer func() {
require.NoError(t, container.Terminate(), "terminating container failed")
}()

p := &Postgresql{
Log: testutil.Logger{},
Service: postgresql.Service{
Address: fmt.Sprintf(
"host=%s user=postgres sslmode=disable",
testutil.GetLocalHost(),
"host=%s port=%s user=postgres sslmode=disable",
container.Address,
container.Ports[servicePort],
),
IsPgBouncer: false,
},
Databases: []string{"postgres"},
Query: q,
}

var acc testutil.Accumulator
require.NoError(t, p.Init())
require.NoError(t, p.Start(&acc))
Expand Down