Skip to content

Commit

Permalink
test: migrate postgres to testcontainers (#11209)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj committed Jun 2, 2022
1 parent 43c9e05 commit 936b908
Show file tree
Hide file tree
Showing 2 changed files with 96 additions and 14 deletions.
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

0 comments on commit 936b908

Please sign in to comment.