$ show ory/dockertest
ory/dockertest v4 is a Go library that provides a high-level API for managing Docker containers during integration tests. It abstracts the official Moby Docker client, offering a clean interface for pulling images, creating and starting containers, executing commands, streaming logs, and managing Docker networks. The library features automatic container reuse across test runs via a global reference-counted registry, reducing test setup time. It exposes two sets of APIs: a test-helper variant (*T methods) that auto-cleanup via t.Cleanup, and a context-aware variant for non-test code. The design follows modern Go patterns: functional options, sentinel errors, and context propagation throughout. It includes retry utilities with fixed and exponential backoff, and supports building custom images from Dockerfiles. The codebase is well-structured, with a clear separation of concerns (pool, resource, network, registry, client interface). Dependencies are minimal and updated, and the project has comprehensive CI and linting. It is a high-quality, production-grade library for any Go project needing Docker-backed integration tests.
The LLM identified 3 files worth highlighting for reuse:
Defines a `DockerClient` interface that abstracts all Docker operations needed by dockertest. This interface is a self-contained, clean abstraction of the Moby client, covering container lifecycle, image operations, exec, logs, and network management. It is not coupled to any dockertest-specific types and can be extracted into a separate package for mocking or alternative Docker client implementations in other projects. Demonstrates good interface design with complete but minimal method set.
Provides package-level functions `Retry` (fixed interval) and `RetryWithBackoff` (exponential backoff) for retrying operations with a timeout and context support. Uses the `cenkalti/backoff` library. These functions are entirely standalone, depending only on `context` and `time`. They can be extracted and reused in any Go project needing generic retry logic. The pool method `Retry` is a thin wrapper around the package function.
Implements a global, concurrent, reference-counted container registry using `sync.Map` and `atomic.Int32`. The `register`, `acquire`, `release` functions manage entries with thread-safe ref counting. The public `Register`, `Get`, `GetAll`, `ResetRegistry` provide a clean API for container reuse. The core ref-counting logic is reusable as a generic reference-counted map, though currently tied to the `resource` type. Demonstrates careful handling of concurrent race conditions (e.g., checking map consistency after load).