From e8a2173b8e34235d7aa411ca6c8116676b089b9b Mon Sep 17 00:00:00 2001 From: Joshua Powers Date: Tue, 24 May 2022 09:28:40 -0600 Subject: [PATCH] test: migrate crate to test-containers code (#11165) --- docker-compose.yml | 12 ----- plugins/outputs/cratedb/cratedb_test.go | 61 ++++++++++++++----------- testutil/container.go | 2 + 3 files changed, 37 insertions(+), 38 deletions(-) diff --git a/docker-compose.yml b/docker-compose.yml index fb162f5df25f5..571e0529c64fc 100644 --- a/docker-compose.yml +++ b/docker-compose.yml @@ -85,15 +85,3 @@ services: ports: - "389:389" - "636:636" - crate: - image: crate/crate - ports: - - "4200:4200" - - "4230:4230" - - "6543:5432" - command: - - crate - - -Cnetwork.host=0.0.0.0 - - -Ctransport.host=localhost - environment: - - CRATE_HEAP_SIZE=128m diff --git a/plugins/outputs/cratedb/cratedb_test.go b/plugins/outputs/cratedb/cratedb_test.go index 0bdfd8d3e2652..6aa9ab5d044a2 100644 --- a/plugins/outputs/cratedb/cratedb_test.go +++ b/plugins/outputs/cratedb/cratedb_test.go @@ -2,7 +2,7 @@ package cratedb import ( "database/sql" - "os" + "fmt" "strings" "testing" "time" @@ -12,25 +12,39 @@ import ( "github.com/influxdata/telegraf/metric" "github.com/influxdata/telegraf/testutil" "github.com/stretchr/testify/require" + "github.com/testcontainers/testcontainers-go/wait" ) -func TestConnectAndWriteIntegration(t *testing.T) { - t.Skip("Skipping due to trust authentication failure") +func createTestContainer(t *testing.T) testutil.Container { + container := testutil.Container{ + Image: "crate", + ExposedPorts: []string{"5432"}, + Entrypoint: []string{ + "/docker-entrypoint.sh", + "-Cdiscovery.type=single-node", + }, + WaitingFor: wait.ForListeningPort("5432/tcp"), + } + err := container.Start() + require.NoError(t, err, "failed to start container") - if os.Getenv("CIRCLE_PROJECT_REPONAME") != "" { - t.Skip("Skipping test on CircleCI due to docker failures") + return container +} + +func TestConnectAndWriteIntegration(t *testing.T) { + if testing.Short() { + t.Skip("Skipping integration test in short mode") } - url := testURL() - table := "test-1" + container := createTestContainer(t) + defer func() { + require.NoError(t, container.Terminate(), "terminating container failed") + }() + url := fmt.Sprintf("postgres://crate@%s:%s/test", container.Address, container.Port) - // dropSQL drops our table before each test. This simplifies changing the - // schema during development :). - dropSQL := "DROP TABLE IF EXISTS " + escapeString(table, `"`) + table := "testing" db, err := sql.Open("pgx", url) require.NoError(t, err) - _, err = db.Exec(dropSQL) - require.NoError(t, err) defer db.Close() c := &CrateDB{ @@ -129,13 +143,17 @@ func escapeValueTests() []escapeValueTest { } func Test_escapeValueIntegration(t *testing.T) { - t.Skip("Skipping due to trust authentication failure") - - if os.Getenv("CIRCLE_PROJECT_REPONAME") != "" { - t.Skip("Skipping test on CircleCI due to docker failures") + if testing.Short() { + t.Skip("Skipping integration test in short mode") } - db, err := sql.Open("pgx", testURL()) + container := createTestContainer(t) + defer func() { + require.NoError(t, container.Terminate(), "terminating container failed") + }() + url := fmt.Sprintf("postgres://crate@%s:%s/test", container.Address, container.Port) + + db, err := sql.Open("pgx", url) require.NoError(t, err) defer db.Close() @@ -228,12 +246,3 @@ func Test_hashID(t *testing.T) { } } } - -//nolint:unused // Used in skipped tests -func testURL() string { - url := os.Getenv("CRATE_URL") - if url == "" { - return "postgres://" + testutil.GetLocalHost() + ":6543/test?sslmode=disable" - } - return url -} diff --git a/testutil/container.go b/testutil/container.go index 448637c29ebc3..45ffe8757ef77 100644 --- a/testutil/container.go +++ b/testutil/container.go @@ -14,6 +14,7 @@ import ( type Container struct { Image string + Entrypoint []string Env map[string]string ExposedPorts []string WaitingFor wait.Strategy @@ -34,6 +35,7 @@ func (c *Container) Start() error { Env: c.Env, ExposedPorts: c.ExposedPorts, WaitingFor: c.WaitingFor, + Entrypoint: c.Entrypoint, }, Started: true, }