stdtestcontainer

package
v0.0.266 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Nov 25, 2026 License: MIT Imports: 22 Imported by: 0 modules, 0 packages

The first number is the count of modules that contain a package that imports this one. The module count is a better indicator of popularity than the package count. A module with 100 importing packages is counted only once instead of 100 times, reflecting the fact that the choice to use the package was likely made only once, not 100 times.
A '?' means that the value hasn't been computed yet.

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

View Source
const (
	S3AccessKey = "stdgo"
	S3SecretKey = "stdgo-secret"
)

Credentials of the root user of every S3 server started here; not a secret.

View Source
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

View Source
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

func Network(tb testing.TB) string

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 Bifrost added in v0.0.265

func Bifrost(tb testing.TB, image, config string, env map[string]string) Server

Bifrost returns a Bifrost LLM gateway (https://docs.getbifrost.ai) that is shared by every test, and every test package, of the same `go test` run, the way S3 is. It serves the configuration in the file at config, the gateway's config.json, which may reference the variables of env as env.<NAME>, such as the keys of its providers.

Calls with another image, configuration, or env get another gateway. The gateway calls the real providers, so tests share it only to read its answers; they must not change its configuration. It requires a running Docker daemon.

func Gotenberg added in v0.0.265

func Gotenberg(tb testing.TB, image string) Server

Gotenberg returns a Gotenberg server (https://gotenberg.dev) that is shared by every test, and every test package, of the same `go test` run, the way S3 is. It runs with WEBHOOK_ENABLE_SYNC_MODE, as stdgotenbergfx requires, and joins the Network, so it reaches the servers there that the requests it is handed address, such as S3's NetworkEndpoint.

Gotenberg keeps no state between requests, so tests share it safely. It requires a running Docker daemon.

func Postgres added in v0.0.244

func Postgres(tb testing.TB, image string) Server

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

func S3(tb testing.TB, image string) Server

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.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL