From 45b9bf7a5fef387b72cfd1a1ffd07bf298cfda0d Mon Sep 17 00:00:00 2001 From: Josh Powers Date: Fri, 27 May 2022 10:38:55 -0600 Subject: [PATCH] test: migrate postgres to testcontainers --- plugins/inputs/postgresql/postgresql_test.go | 83 ++++++++++++++++--- .../postgresql_extensible_test.go | 27 +++++- 2 files changed, 96 insertions(+), 14 deletions(-) diff --git a/plugins/inputs/postgresql/postgresql_test.go b/plugins/inputs/postgresql/postgresql_test.go index 30cf776eb0e0a..0ae557a8eb672 100644 --- a/plugins/inputs/postgresql/postgresql_test.go +++ b/plugins/inputs/postgresql/postgresql_test.go @@ -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, }, @@ -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"}, @@ -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], ), }, } @@ -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], ), }, } @@ -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"}, @@ -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"}, diff --git a/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go b/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go index fbcc7e1e8a7e2..9550a52dd31a8 100644 --- a/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go +++ b/plugins/inputs/postgresql_extensible/postgresql_extensible_test.go @@ -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))