Documentation
¶
Overview ¶
Package stdtestcontainer provides re-usable code for using testcontainers-go.
Shared servers ¶
Some servers, such as Postgres and S3, are started once per `go test` run and shared by every test, and every test package, of it, because starting one per test would make the tests too slow. They return a Server with the addresses to reach them at, and join the Network, so containers that tests start reach them too.
Share a server only when tests can isolate their state on it in another way, so parallel tests and test packages never see each other's data: each test gets its own database cloned from a migrated template (stdpgxtestfx), or its own randomly named buckets (stds3testfx). A server whose state tests cannot partition like that, such as one whose global configuration tests change, must not be shared: start one per test instead, and stop it when the test ends.
Index ¶
Constants ¶
const ( S3AccessKey = "stdgo" S3SecretKey = "stdgo-secret" )
Credentials of the root user of every S3 server started here; not a secret.
const PostgresMaxConnections = 2000
PostgresMaxConnections is the max_connections of the server that Postgres starts. It leaves room for test packages that run in parallel, each with parallel tests whose pools stdpgxfx.TestProvide caps (STDPGX_POOL_MAX_CONNS). It is one value for the whole run, because every test package shares the server, and the first to start it sets its flags.
Variables ¶
var ( // CreateGenericContainer sets the function used for creating the generic container, can be replaced for testing. CreateGenericContainer = testcontainers.GenericContainer // CleanupContainer sets the function used for cleaning up the container, can be replaced for testing. CleanupContainer = testcontainers.CleanupContainer )
Functions ¶
func Network ¶ added in v0.0.263
Network returns the name of a Docker network that is shared by every test, and every test package, of the same `go test` run. The servers started here, such as S3, join it, so containers that tests start on it reach them by name instead of through the host, which a container cannot reach on every platform: attach them with WithNetwork. The first call creates the network; later calls, including those from other test packages (which Go runs in other processes), reuse it. Testcontainers' reaper (Ryuk) removes it after the run. It requires a running Docker daemon.
func SetupLambdaRIEContainer ¶
func SetupLambdaRIEContainer( t *testing.T, ctx context.Context, imageName string, entrypoint []string, platform string, env map[string]string, hostRieFilePath string, hostAWSCredentialsFilePath string, ) *requests.Builder
SetupLambdaRIEContainer will use testcontainers-go to setup a container for e2e testing of a lambda handler.
func WithNetwork ¶ added in v0.0.263
func WithNetwork(tb testing.TB, aliases ...string) testcontainers.CustomizeRequestOption
WithNetwork attaches a container to the Network under aliases, the host names other containers on it reach it at.
Types ¶
type Server ¶ added in v0.0.263
type Server struct {
// Endpoint is where the tests reach the server, through a port that its
// container publishes on the host.
Endpoint string
// NetworkEndpoint is where containers on the [Network] reach the server,
// by its container's name, such as services that the code under test
// hands requests for the server to.
NetworkEndpoint string
}
Server is a server that is shared by every test, and every test package, of a `go test` run; see the package documentation for when to share one.
func Postgres ¶ added in v0.0.244
Postgres returns a PostgreSQL server that is shared by every test, and every test package, of the same `go test` run, with the connection URLs to reach it at. The first call starts the server in a container; later calls, including those from other test packages (which Go runs in other processes), reuse it.
The image, e.g. "postgres:18", is required so the server matches the version the code runs against in production; callers asking for different images get different servers.
The server holds no data that tests can rely on: create a separate database per test on top of it, e.g. with pgtestdb or stdpgxtestfx. It requires a running Docker daemon.
func S3 ¶ added in v0.0.255
S3 returns an S3-compatible server that is shared by every test, and every test package, of the same `go test` run, with the endpoint URLs to reach it at. Sign requests with S3AccessKey and S3SecretKey. The first call starts the server in a container; later calls, including those from other test packages (which Go runs in other processes), reuse it.
The image is required so the server matches the one the code runs against elsewhere, such as in Docker Compose; callers asking for different images get different servers. The image must take MinIO's "server" command and MINIO_ROOT_USER and MINIO_ROOT_PASSWORD variables, as MinIO's own images and forks such as pgsty/silo do.
The server holds no data that tests can rely on: create separate buckets per test on top of it, e.g. with stds3testfx. It requires a running Docker daemon.