Skip to content

Commit

Permalink
Switch from ory/dockertest to testcontainers
Browse files Browse the repository at this point in the history
  • Loading branch information
twpayne committed Mar 12, 2024
1 parent c437742 commit 8d0ca94
Show file tree
Hide file tree
Showing 3 changed files with 230 additions and 114 deletions.
57 changes: 33 additions & 24 deletions examples/postgis/main_test.go
Original file line number Diff line number Diff line change
@@ -1,51 +1,60 @@
//go:build docker
// +build docker

package main

import (
"bytes"
"context"
"database/sql"
"fmt"
"os/exec"
"strings"
"testing"
"time"

"github.com/alecthomas/assert/v2"
_ "github.com/lib/pq"
"github.com/ory/dockertest/v3"
"github.com/testcontainers/testcontainers-go"
"github.com/testcontainers/testcontainers-go/modules/postgres"
"github.com/testcontainers/testcontainers-go/wait"
)

func TestMain(t *testing.T) {
func TestIntegration(t *testing.T) {
ctx := context.Background()

if _, err := exec.LookPath("docker"); err != nil {
t.Skip("docker not found in $PATH")
}

var (
dbName = "testdb"
database = "testdb"
user = "testuser"
password = "testpassword"
)

pool, err := dockertest.NewPool("")
pgContainer, err := postgres.RunContainer(ctx,
testcontainers.WithImage("docker.io/postgis/postgis:16-3.4"),
postgres.WithDatabase(database),
postgres.WithUsername(user),
postgres.WithPassword(password),
testcontainers.WithWaitStrategy(
wait.ForLog("database system is ready to accept connections").
WithOccurrence(2).
WithStartupTimeout(5*time.Second),
),
)
assert.NoError(t, err)

resource, err := pool.Run("mdillon/postgis", "latest", []string{
"POSTGRES_DB=" + dbName,
"POSTGRES_PASSWORD=" + password,
"POSTGRES_USER=" + user,
t.Cleanup(func() {
assert.NoError(t, pgContainer.Terminate(ctx))
})

connStr, err := pgContainer.ConnectionString(ctx, "binary_parameters=yes", "sslmode=disable")
assert.NoError(t, err)

db, err := sql.Open("postgres", connStr)
assert.NoError(t, err)
defer func() {
assert.NoError(t, pool.Purge(resource))
assert.NoError(t, db.Close())
}()

var db *sql.DB
assert.NoError(t, pool.Retry(func() error {
dsn := fmt.Sprintf("postgres://%s:%s@localhost:%s/%s?binary_parameters=yes&sslmode=disable", user, password, resource.GetPort("5432/tcp"), dbName)
var err error
db, err = sql.Open("postgres", dsn)
if err != nil {
return err
}
return db.Ping()
}))

assert.NoError(t, createDB(db))

assert.NoError(t, populateDB(db))
Expand Down
74 changes: 49 additions & 25 deletions go.mod
Original file line number Diff line number Diff line change
@@ -1,43 +1,67 @@
module github.com/twpayne/go-geom

go 1.19
go 1.21

toolchain go1.22.1

require (
github.com/DATA-DOG/go-sqlmock v1.5.0
github.com/alecthomas/assert/v2 v2.4.0
github.com/lib/pq v1.10.9
github.com/ory/dockertest/v3 v3.9.1
github.com/testcontainers/testcontainers-go v0.29.1
github.com/testcontainers/testcontainers-go/modules/postgres v0.29.1
github.com/twpayne/go-kml/v3 v3.1.0
)

require (
dario.cat/mergo v1.0.0 // indirect
github.com/Azure/go-ansiterm v0.0.0-20210617225240-d185dfc1b5a1 // indirect
github.com/Microsoft/go-winio v0.5.2 // indirect
github.com/Nvveen/Gotty v0.0.0-20120604004816-cd527374f1e5 // indirect
github.com/Microsoft/go-winio v0.6.1 // indirect
github.com/Microsoft/hcsshim v0.11.4 // indirect
github.com/alecthomas/repr v0.3.0 // indirect
github.com/cenkalti/backoff/v4 v4.1.3 // indirect
github.com/containerd/continuity v0.3.0 // indirect
github.com/docker/cli v20.10.17+incompatible // indirect
github.com/docker/docker v24.0.7+incompatible // indirect
github.com/docker/go-connections v0.4.0 // indirect
github.com/docker/go-units v0.4.0 // indirect
github.com/cenkalti/backoff/v4 v4.2.1 // indirect
github.com/containerd/containerd v1.7.12 // indirect
github.com/containerd/log v0.1.0 // indirect
github.com/cpuguy83/dockercfg v0.3.1 // indirect
github.com/distribution/reference v0.5.0 // indirect
github.com/docker/docker v25.0.3+incompatible // indirect
github.com/docker/go-connections v0.5.0 // indirect
github.com/docker/go-units v0.5.0 // indirect
github.com/felixge/httpsnoop v1.0.3 // indirect
github.com/go-logr/logr v1.2.4 // indirect
github.com/go-logr/stdr v1.2.2 // indirect
github.com/go-ole/go-ole v1.2.6 // indirect
github.com/gogo/protobuf v1.3.2 // indirect
github.com/google/shlex v0.0.0-20191202100458-e7afc7fbc510 // indirect
github.com/golang/protobuf v1.5.3 // indirect
github.com/google/uuid v1.6.0 // indirect
github.com/hexops/gotextdiff v1.0.3 // indirect
github.com/imdario/mergo v0.3.13 // indirect
github.com/mitchellh/mapstructure v1.5.0 // indirect
github.com/moby/term v0.0.0-20210619224110-3f7ff695adc6 // indirect
github.com/klauspost/compress v1.16.0 // indirect
github.com/lufia/plan9stats v0.0.0-20211012122336-39d0f177ccd0 // indirect
github.com/magiconair/properties v1.8.7 // indirect
github.com/moby/patternmatcher v0.6.0 // indirect
github.com/moby/sys/sequential v0.5.0 // indirect
github.com/moby/sys/user v0.1.0 // indirect
github.com/moby/term v0.5.0 // indirect
github.com/morikuni/aec v1.0.0 // indirect
github.com/opencontainers/go-digest v1.0.0 // indirect
github.com/opencontainers/image-spec v1.0.2 // indirect
github.com/opencontainers/runc v1.1.12 // indirect
github.com/opencontainers/image-spec v1.1.0 // indirect
github.com/pkg/errors v0.9.1 // indirect
github.com/sirupsen/logrus v1.9.0 // indirect
github.com/stretchr/testify v1.8.1 // indirect
github.com/xeipuuv/gojsonpointer v0.0.0-20190905194746-02993c407bfb // indirect
github.com/xeipuuv/gojsonreference v0.0.0-20180127040603-bd5ef7bd5415 // indirect
github.com/xeipuuv/gojsonschema v1.2.0 // indirect
golang.org/x/net v0.17.0 // indirect
golang.org/x/sys v0.13.0 // indirect
gopkg.in/yaml.v2 v2.4.0 // indirect
gopkg.in/yaml.v3 v3.0.1 // indirect
github.com/power-devops/perfstat v0.0.0-20210106213030-5aafc221ea8c // indirect
github.com/shirou/gopsutil/v3 v3.23.12 // indirect
github.com/shoenig/go-m1cpu v0.1.6 // indirect
github.com/sirupsen/logrus v1.9.3 // indirect
github.com/tklauser/go-sysconf v0.3.12 // indirect
github.com/tklauser/numcpus v0.6.1 // indirect
github.com/yusufpapurcu/wmi v1.2.3 // indirect
go.opentelemetry.io/contrib/instrumentation/net/http/otelhttp v0.45.0 // indirect
go.opentelemetry.io/otel v1.19.0 // indirect
go.opentelemetry.io/otel/metric v1.19.0 // indirect
go.opentelemetry.io/otel/trace v1.19.0 // indirect
golang.org/x/exp v0.0.0-20230510235704-dd950f8aeaea // indirect
golang.org/x/mod v0.16.0 // indirect
golang.org/x/sys v0.16.0 // indirect
golang.org/x/tools v0.13.0 // indirect
google.golang.org/genproto/googleapis/rpc v0.0.0-20230711160842-782d3b101e98 // indirect
google.golang.org/grpc v1.58.3 // indirect
google.golang.org/protobuf v1.31.0 // indirect
)
Loading

0 comments on commit 8d0ca94

Please sign in to comment.