Skip to content

Commit

Permalink
test: migrate crate to test-containers code (#11165)
Browse files Browse the repository at this point in the history
  • Loading branch information
powersj committed May 24, 2022
1 parent 56eb914 commit 94ad7f1
Show file tree
Hide file tree
Showing 3 changed files with 37 additions and 38 deletions.
12 changes: 0 additions & 12 deletions docker-compose.yml
Original file line number Diff line number Diff line change
Expand Up @@ -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
61 changes: 35 additions & 26 deletions plugins/outputs/cratedb/cratedb_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ package cratedb

import (
"database/sql"
"os"
"fmt"
"strings"
"testing"
"time"
Expand All @@ -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{
Expand Down Expand Up @@ -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()

Expand Down Expand Up @@ -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
}
2 changes: 2 additions & 0 deletions testutil/container.go
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ import (

type Container struct {
Image string
Entrypoint []string
Env map[string]string
ExposedPorts []string
WaitingFor wait.Strategy
Expand All @@ -34,6 +35,7 @@ func (c *Container) Start() error {
Env: c.Env,
ExposedPorts: c.ExposedPorts,
WaitingFor: c.WaitingFor,
Entrypoint: c.Entrypoint,
},
Started: true,
}
Expand Down

0 comments on commit 94ad7f1

Please sign in to comment.