dockertestutil

package
v0.29.2 Latest Latest
Warning

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

Go to latest
Published: Jul 1, 2026 License: BSD-3-Clause Imports: 20 Imported by: 0

Documentation

Index

Constants

View Source
const (
	DockerOpInitialInterval = 1 * time.Second
	DockerOpMaxInterval     = 10 * time.Second
	DockerOpMaxElapsedTime  = 90 * time.Second
)

Docker control-plane retry policy. MaxElapsedTime sits above the worst observed libnetwork bridge reprogramming time (~60 s on contended GHA runners).

View Source
const (
	// TimestampFormatRunID is used for generating unique run identifiers
	// Format: "20060102-150405" provides compact date-time for file/directory names.
	TimestampFormatRunID = "20060102-150405"
)

Variables

View Source
var (
	ErrDockertestCommandFailed  = errors.New("dockertest command failed")
	ErrDockertestCommandTimeout = errors.New("dockertest command timed out")
)
View Source
var (
	ErrContainerNotFound = errors.New("container not found")
	ErrConditionTimeout  = errors.New("condition not met within timeout")
)

Functions

func AddContainerToNetwork

func AddContainerToNetwork(
	pool *dockertest.Pool,
	network *dockertest.Network,
	testContainer string,
) error

func AuthConfiguration added in v0.29.0

func AuthConfiguration() docker.AuthConfiguration

AuthConfiguration returns Docker Hub auth for the dockertest pool.

func CleanImagesInCI added in v0.26.0

func CleanImagesInCI(pool *dockertest.Pool) error

CleanImagesInCI removes images if running in CI. It only removes dangling (untagged) images to avoid forcing rebuilds. Tagged images (golang:*, tailscale/tailscale:*, etc.) are automatically preserved.

func CleanUnreferencedNetworks added in v0.26.0

func CleanUnreferencedNetworks(pool *dockertest.Pool) error

CleanUnreferencedNetworks removes networks that are not referenced by any containers.

func DisconnectAndReconnect added in v0.29.0

func DisconnectAndReconnect(
	pool *dockertest.Pool,
	network *dockertest.Network,
	testContainer string,
) error

DisconnectAndReconnect calls Disconnect followed by Reconnect; both primitives drive their own libnetwork settle waits.

func DisconnectContainerFromNetwork added in v0.29.0

func DisconnectContainerFromNetwork(
	pool *dockertest.Pool,
	network *dockertest.Network,
	testContainer string,
) error

DisconnectContainerFromNetwork detaches the container at the docker daemon level (cable-pull semantics) and waits for libnetwork to drop the endpoint before returning — re-attaching during the reprogramming window otherwise fails with "network is unreachable".

func DockerAddIntegrationLabels added in v0.27.0

func DockerAddIntegrationLabels(opts *dockertest.RunOptions, testType string)

DockerAddIntegrationLabels adds integration test labels to Docker dockertest.RunOptions. This allows the hi tool to identify containers belonging to specific test runs. This function should be called before passing dockertest.RunOptions to dockertest functions.

func DockerAllowLocalIPv6

func DockerAllowLocalIPv6(config *docker.HostConfig)

DockerAllowLocalIPv6 allows IPv6 traffic within the container.

func DockerAllowNetworkAdministration

func DockerAllowNetworkAdministration(config *docker.HostConfig)

DockerAllowNetworkAdministration gives the container network administration capabilities.

func DockerMemoryLimit added in v0.27.0

func DockerMemoryLimit(config *docker.HostConfig)

DockerMemoryLimit sets memory limit and disables OOM kill for containers.

func DockerRestartPolicy

func DockerRestartPolicy(config *docker.HostConfig)

DockerRestartPolicy sets the restart policy for containers.

func ExecuteCommand

func ExecuteCommand(
	resource *dockertest.Resource,
	cmd []string,
	env []string,
	options ...ExecuteCommandOption,
) (string, string, error)

func ExtractRunIDFromContainerName added in v0.27.0

func ExtractRunIDFromContainerName(containerName string) string

ExtractRunIDFromContainerName extracts the run ID from container name. Expects format: "prefix-YYYYMMDD-HHMMSS-HASH".

func GenerateRunID added in v0.27.0

func GenerateRunID() string

GenerateRunID creates a unique run identifier with timestamp and random hash. Format: YYYYMMDD-HHMMSS-HASH (e.g., 20250619-143052-a1b2c3).

func GetFirstOrCreateNetwork

func GetFirstOrCreateNetwork(pool *dockertest.Pool, name string) (*dockertest.Network, error)

func GetFirstOrCreateNetworkWithSubnet added in v0.29.0

func GetFirstOrCreateNetworkWithSubnet(pool *dockertest.Pool, name, subnet string) (*dockertest.Network, error)

GetFirstOrCreateNetworkWithSubnet creates a Docker network with an optional custom subnet. When subnet is empty, Docker auto-assigns from its default pool. Use RFC 5737 TEST-NET ranges (e.g. "198.51.100.0/24") for networks that need to be reachable through Tailscale exit nodes, since Tailscale's shrinkDefaultRoute strips RFC1918 ranges from exit node forwarding filters.

func GetIntegrationRunID added in v0.27.0

func GetIntegrationRunID() string

GetIntegrationRunID returns the run ID for the current integration test session. This is set by the hi tool and passed through environment variables.

func IsRunningInContainer

func IsRunningInContainer() bool

IsRunningInContainer checks if the current process is running inside a Docker container. This is used by tests to determine if they should run integration tests.

func PullWithAuth added in v0.29.0

func PullWithAuth(pool *dockertest.Pool, imageRef string) error

PullWithAuth ensures imageRef is local, pulling with auth and retrying transient errors when it is not.

func RandomFreeHostPort added in v0.18.0

func RandomFreeHostPort() (int, error)

RandomFreeHostPort asks the kernel for a free open port that is ready to use. (from https://github.com/phayes/freeport)

func ReconnectContainerToNetwork added in v0.29.0

func ReconnectContainerToNetwork(
	pool *dockertest.Pool,
	network *dockertest.Network,
	testContainer string,
) error

ReconnectContainerToNetwork is the inverse of DisconnectContainerFromNetwork — re-attaches the container to the network so traffic can flow again.

func RegistryAuth added in v0.29.0

func RegistryAuth() (string, error)

RegistryAuth returns base64-encoded credentials for the modern Docker SDK's image.PullOptions{RegistryAuth: ...}, or "" when none.

func RunDockerBuildForDiagnostics added in v0.27.0

func RunDockerBuildForDiagnostics(contextDir, dockerfile string) (string, error)

RunDockerBuildForDiagnostics runs docker build manually to get detailed error output. This is used when a docker build fails to provide more detailed diagnostic information than what dockertest typically provides.

Returns the build output regardless of success/failure, and an error if the build failed.

func SaveLog added in v0.20.0

func SaveLog(
	pool *dockertest.Pool,
	resource *dockertest.Resource,
	basePath string,
) (string, string, error)

func WriteLog added in v0.24.0

func WriteLog(
	pool *dockertest.Pool,
	resource *dockertest.Resource,
	stdout io.Writer,
	stderr io.Writer,
) error

Types

type CredentialSource added in v0.29.0

type CredentialSource string
const (
	CredentialSourceEnv       CredentialSource = "env"
	CredentialSourceConfig    CredentialSource = "config"
	CredentialSourceAnonymous CredentialSource = "anonymous"
)

func Credentials added in v0.29.0

func Credentials() (string, string, CredentialSource)

Credentials resolves Docker Hub credentials from DOCKERHUB_USERNAME/DOCKERHUB_TOKEN, then ~/.docker/config.json, then anonymous. The Docker Go SDKs do not read config.json on their own.

type ExecuteCommandConfig

type ExecuteCommandConfig struct {
	// contains filtered or unexported fields
}

type ExecuteCommandOption

type ExecuteCommandOption func(*ExecuteCommandConfig) error

func ExecuteCommandTimeout

func ExecuteCommandTimeout(timeout time.Duration) ExecuteCommandOption

Jump to

Keyboard shortcuts

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