Skip to content
This repository has been archived by the owner on Apr 2, 2024. It is now read-only.

Commit

Permalink
Make sure db is accepting connections in container for itests (#71)
Browse files Browse the repository at this point in the history
testcontainers.wait.ForSQL strategy is used to wait for the
success of `SELECT 1` on the database container.
  • Loading branch information
atanasovskib committed May 1, 2020
1 parent a72f7f4 commit 52a9bd8
Showing 1 changed file with 8 additions and 5 deletions.
13 changes: 8 additions & 5 deletions pkg/internal/testhelpers/containers.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,16 +8,17 @@ import (
"context"
"fmt"
"io/ioutil"
"os"
"path/filepath"
"runtime"
"testing"

"github.com/docker/go-connections/nat"
"github.com/jackc/pgx/v4"
"github.com/jackc/pgx/v4/pgxpool"
_ "github.com/jackc/pgx/v4/stdlib"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/wait"
"os"
"path/filepath"
"testing"
)

const (
Expand Down Expand Up @@ -104,7 +105,9 @@ func StartPGContainer(ctx context.Context, withExtension bool, testDataDir strin
req := testcontainers.ContainerRequest{
Image: image,
ExposedPorts: []string{string(containerPort)},
WaitingFor: wait.NewHostPortStrategy(containerPort),
WaitingFor: wait.ForSQL(containerPort, "pgx", func(port nat.Port) string {
return "dbname=postgres password=password user=postgres host=127.0.0.1 port=" + port.Port()
}),
Env: map[string]string{
"POSTGRES_PASSWORD": "password",
},
Expand Down Expand Up @@ -154,7 +157,7 @@ func StartPromContainer(storagePath string, ctx context.Context) (testcontainers
req := testcontainers.ContainerRequest{
Image: "prom/prometheus",
ExposedPorts: []string{string(prometheusPort)},
WaitingFor: wait.NewHostPortStrategy(prometheusPort),
WaitingFor: wait.ForListeningPort(prometheusPort),
BindMounts: map[string]string{
storagePath: "/prometheus",
},
Expand Down

0 comments on commit 52a9bd8

Please sign in to comment.