test: migrate crate to test-containers code (#11165)

This commit is contained in:
Joshua Powers 2022-05-24 09:28:40 -06:00 committed by GitHub
parent 56eb914998
commit 94ad7f1bfa
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
3 changed files with 37 additions and 38 deletions

View File

@ -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

View File

@ -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
}

View File

@ -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,
}