40 lines
976 B
Go
40 lines
976 B
Go
|
package dbtest
|
||
|
|
||
|
import (
|
||
|
"context"
|
||
|
|
||
|
"github.com/testcontainers/testcontainers-go"
|
||
|
tcpostgres "github.com/testcontainers/testcontainers-go/modules/postgres"
|
||
|
"github.com/testcontainers/testcontainers-go/wait"
|
||
|
)
|
||
|
|
||
|
func TestPostgresDatabase(ctx context.Context) (string, DbCloser, error) {
|
||
|
container, err := tcpostgres.RunContainer(
|
||
|
ctx,
|
||
|
withImage("docker.io/postgres:16-alpine"),
|
||
|
tcpostgres.WithDatabase("gophershare"),
|
||
|
tcpostgres.WithUsername("postgres"),
|
||
|
tcpostgres.WithPassword("postgres"),
|
||
|
testcontainers.WithWaitStrategy(
|
||
|
wait.ForListeningPort("5432/tcp"),
|
||
|
),
|
||
|
)
|
||
|
if err != nil {
|
||
|
return "", nil, err
|
||
|
}
|
||
|
|
||
|
connString, err := container.ConnectionString(ctx, "sslmode=disable")
|
||
|
if err != nil {
|
||
|
return "", nil, err
|
||
|
}
|
||
|
|
||
|
return connString, CloserFunc(container.Terminate), nil
|
||
|
}
|
||
|
|
||
|
func withImage(image string) testcontainers.CustomizeRequestOption {
|
||
|
return func(req *testcontainers.GenericContainerRequest) error {
|
||
|
req.Image = image
|
||
|
return nil
|
||
|
}
|
||
|
}
|