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 crate to test-containers code #11165

Merged
merged 1 commit into from
May 24, 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
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