dockertest

package
v0.3.0 Latest Latest
Warning

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

Go to latest
Published: Aug 16, 2026 License: Apache-2.0 Imports: 7 Imported by: 0

Documentation

Overview

Package dockertest is the shared half of this repo's Dockerized test fixtures: it labels the container a fixture starts, and reaps the ones a killed run left behind. pgtest, blobtest, secretstest and gcstest each start one container per test binary and remove it in a deferred call — and a defer does not run when the process is killed. Ctrl-C, an IDE's stop button, go test's own timeout panic, a laptop suspending mid-run: any of them leaves the container running, and its anonymous volume growing, forever. One machine accumulated twenty stray Postgres containers holding 12.6 GB that way (#346). Nothing else reaps them — the fixtures shell out to docker rather than going through testcontainers, so there is no ryuk sidecar on the machine to notice.

This is not github.com/ory/dockertest. Production code must never import it.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

func RunArgs

func RunArgs(harness string, rest ...string) []string

RunArgs builds the `docker run` argument list for a detached fixture container of the named harness, labelled so SweepStrays can find it once the process that started it is gone:

exec.Command("docker", dockertest.RunArgs("pgtest",
	"-e", "POSTGRES_PASSWORD=test", "-p", "127.0.0.1:0:5432", image)...)

Every fixture starts its container through this, so none of them can start an unlabelled one — the invariant the sweep's completeness rests on.

func SweepStrays

func SweepStrays(harness string)

SweepStrays force-removes every fixture container older than strayAge, with -v so the anonymous volume the image declares goes with it — the reason the leak cost gigabytes rather than megabytes. Call it at the top of a fixture's TestMain, before that run starts its own container: the next run is what cleans up after the killed one.

harness only names the caller in the messages. The sweep reaps every fixture's strays whichever suite left them, since the age rule is the same for all of them and the label is this repo's alone.

Listing is best effort and silent: a daemon that will not answer at all is about to be reported far better by the caller's own `docker run`, and a stray this run never sees is one the next run gets. Removal is not silent — see removeContainer.

The whole sweep shares one deadline, rather than each docker call carrying its own. This runs inside TestMain, before m.Run, where `go test -timeout`'s alarm is not yet armed and nothing will interrupt a wedged daemon: with a per-call bound only, the twenty strays #346 reported could hold a test binary for a quarter of an hour and look like a hang rather than a leak. Giving up with strays left costs nothing, because every later run sweeps again.

Types

This section is empty.

Jump to

Keyboard shortcuts

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