containerapitest

package
v1.4.2 Latest Latest
Warning

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

Go to latest
Published: Nov 10, 2026 License: MIT Imports: 25 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 containerapitest is the test side of containerapi (SPEC §10.20, step 18.1): a fake engine that speaks both the Docker Engine API and Podman's libpod API, for the default tests of the container extensions, and a harness for the tests against the user's real Docker and Podman (build tags emetos_docker and emetos_podman), which touches only the things it made itself: containers, networks, volumes and pods named emetos-test-<random> that carry the label dev.emetos.test=1, with ports published on 127.0.0.1 only, removed when the test ends (also when it fails). The images they may pull are listed in one place, images.go. TestEngine is the one suite that runs against the fake and, tagged, against the real engines.

Index

Constants

View Source
const (
	// LabelKey and LabelValue are the label every test container, network, volume and pod carries.
	LabelKey   = "dev.emetos.test"
	LabelValue = "1"
	// NamePrefix starts the name of everything a test makes.
	NamePrefix = "emetos-test-"
	// Loopback is the only address a test publishes ports on.
	Loopback = "127.0.0.1"
)

What marks a thing as made by a test (CLAUDE.md "Testing notes"): the label dev.emetos.test=1 and a name that starts with emetos-test-. scripts/container-test-cleanup.sh removes only things with the label.

View Source
const (
	// ImageBusyBox is the small image the real-engine tests run containers from (a few megabytes). The tag is BusyBox 1.37.0.
	ImageBusyBox = "docker.io/library/busybox:1.37.0"

	// ImagePostgres is PostgreSQL 17.6 on Alpine.
	ImagePostgres = "docker.io/library/postgres:17.6-alpine"
	// ImageMySQL is MySQL 8.4.6 (a long-term support release).
	ImageMySQL = "docker.io/library/mysql:8.4.6"
	// ImageMariaDB is MariaDB 11.4.8 (a long-term support release).
	ImageMariaDB = "docker.io/library/mariadb:11.4.8"

	// ImageMongoDB is MongoDB 7.0.14, the server of the tests of the MongoDB extension (extensions/mongodb, SPEC §10.22): 7.0 has
	// time series collections and the $sample stage, and its image starts a server with a root user from the environment.
	ImageMongoDB = "docker.io/library/mongo:7.0.14"
	// ImageRedis is Redis 8.2.2, the server of the tests of the Redis extension (extensions/redis, SPEC §10.23). Redis 8 has the JSON
	// module in its image.
	ImageRedis = "docker.io/library/redis:8.2.2"
)

The images the tests against the real engines may pull, in one place (CLAUDE.md "Testing notes": only pinned image tags listed in one place). Each is a full reference with a version tag that does not move in practice, never "latest". The harness refuses to pull anything that is not listed here.

Variables

PinnedImages lists every image the real-engine tests may pull.

Functions

func Certificates

func Certificates(t testing.TB) (caPEM, certPEM, keyPEM []byte)

Certificates makes a CA and a certificate for 127.0.0.1 signed by it, as PEM, for the tests of tcp:// with TLS. The certificate is good for a server and for a client, so one set serves both ends.

func IsPinned

func IsPinned(ref string) bool

IsPinned reports whether ref is one of PinnedImages.

func LabelFilter

func LabelFilter() containerapi.Filters

LabelFilter is the filter that lists only the things made by tests.

func RandomName

func RandomName(t testing.TB) string

RandomName returns emetos-test-<random>.

func TestEngine

func TestEngine(t *testing.T, h *Harness)

TestEngine runs the suite of the engine client against h: the same calls against the fake engine (in the default tests) and against the user's real Docker and Podman (build tags emetos_docker and emetos_podman). It runs a labelled container of the pinned BusyBox image and exercises the list, inspect, logs, exec, events, stop, start, restart, kill, rename and remove calls, a network, a volume, an image tag, "system df" and, for Podman, a pod. Everything it makes carries the label dev.emetos.test=1 and is removed when the test ends; the harness fails the test when something labelled is left. Prune calls are not part of it: against a real engine they act on whatever the engine decides is unused.

Types

type Fake

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

Fake is an in-memory engine. It speaks the Docker Engine API (Kind Docker) or the libpod API (Kind Podman) over a Unix socket, with state: containers, images, networks, volumes, pods (Podman) and the event stream that follows their changes.

func NewFake

func NewFake(t testing.TB, kind containerapi.Kind, opts FakeOptions) *Fake

NewFake starts a fake engine of the given kind on a Unix socket and stops it when the test ends.

func ServeUnix

func ServeUnix(kind containerapi.Kind, opts FakeOptions, sock string) (*Fake, error)

ServeUnix starts a fake engine of the given kind on the Unix socket at sock, for a program that is not a test (the fake podman of the tests of containerapi.StartService). Close stops it.

func (*Fake) AddContainer

func (f *Fake) AddContainer(name, image string, labels map[string]string, state string) string

AddContainer puts a container straight into the Fake, as if it had been made before the test started, and returns its id. The state is "created", "running" or "exited".

func (*Fake) AddImage

func (f *Fake) AddImage(size int64, tags ...string) string

AddImage puts an image into the Fake under the given names and returns its id.

func (*Fake) AddLabelledImage

func (f *Fake) AddLabelledImage(size int64, labels map[string]string, tags ...string) string

AddLabelledImage is AddImage for an image with labels (a test image has the label of the tests).

func (*Fake) Client

func (f *Fake) Client() *containerapi.Client

Client returns a Client of the Fake's kind connected to it.

func (*Fake) Close

func (f *Fake) Close()

Close stops the engine and its streams. The test cleanup calls it; calling it again is harmless.

func (*Fake) Emit

func (f *Fake) Emit(ev containerapi.Event)

Emit sends an event to the event streams, as if the engine had made it.

func (*Fake) Endpoint

func (f *Fake) Endpoint() containerapi.Endpoint

Endpoint returns the endpoint of the socket.

func (*Fake) FailNext

func (f *Fake) FailNext(method, pathContains string, status int, message string)

FailNext makes the next request with that method and a path that contains pathContains fail once with the given status and message, in the engine's error format.

func (*Fake) FailPull

func (f *Fake) FailPull(ref, message string)

FailPull makes pulling ref fail with message in the progress stream, as a registry error does.

func (*Fake) Handler

func (f *Fake) Handler() http.Handler

Handler returns the http.Handler of the engine, for a test that serves it on a listener of its own (TLS).

func (*Fake) Kind

func (f *Fake) Kind() containerapi.Kind

Kind returns the engine the Fake speaks.

func (*Fake) Log

func (f *Fake) Log(id string, stream containerapi.Stream, text string)

Log appends a line to a container's output.

func (*Fake) Override

func (f *Fake) Override(method, pathContains string, h http.HandlerFunc)

Override answers the requests whose method is method and whose path (without the version prefix) contains pathContains with h, from now on; the engine's own handling is skipped for them.

func (*Fake) Requests

func (f *Fake) Requests() []Request

Requests returns the requests received so far, oldest first.

func (*Fake) ResetRequests

func (f *Fake) ResetRequests()

ResetRequests forgets the requests received so far.

func (*Fake) SetExec

func (f *Fake) SetExec(fn func(cmd []string) (stdout, stderr string, exit int))

SetExec sets the function that runs a command for exec; nil goes back to the little shell.

func (*Fake) SetVolumeSize

func (f *Fake) SetVolumeSize(name string, size int64)

SetVolumeSize sets the size "system df" reports for a volume.

func (*Fake) Socket

func (f *Fake) Socket() string

Socket returns the path of the Unix socket.

type FakeOptions

type FakeOptions struct {
	// APIVersion is the newest API version a Docker fake reports; "" is "1.52" (newer than the Client's 1.47). MinAPIVersion
	// is the oldest; "" is "1.24".
	APIVersion, MinAPIVersion string
	// EngineVersion is the engine's own version; "" is "29.0.0" for Docker and "5.8.0" for Podman.
	EngineVersion string
}

FakeOptions tune a Fake.

type Harness

type Harness struct {
	// T is the test.
	T testing.TB
	// Client talks to the engine.
	Client *containerapi.Client
	// Kind is the engine's kind.
	Kind containerapi.Kind
	// Real reports an engine of the user's, not a Fake.
	Real bool
	// Fake is the fake engine; nil for a real one.
	Fake *Fake
}

Harness runs a test against an engine, real or fake, and makes sure it touches only what it made itself.

func NewFakeHarness

func NewFakeHarness(t testing.TB, kind containerapi.Kind) *Harness

NewFakeHarness makes a Harness on a fake engine.

func RealDocker

func RealDocker(t testing.TB) *Harness

RealDocker makes a Harness on the user's Docker, found as the Docker CLI would (the setting is none; DOCKER_HOST and the Docker context are honored). It skips the test when no engine answers, or when the engine is not on this computer (a tcp:// host): the tests create containers, and only a local engine is meant for that.

func RealPodman

func RealPodman(t testing.TB) *Harness

RealPodman makes a Harness on the user's Podman: its socket, or a podman system service of its own on a private socket when none answers (stopped when the test ends). It skips the test when there is no Podman.

func (*Harness) Container

func (h *Harness) Container(ctx context.Context, spec containerapi.ContainerSpec) string

Container makes a container from spec, without starting it, and removes it when the test ends. The spec is made safe: the name starts with emetos-test- (a random one when none is given), the label is added, ports are published on 127.0.0.1 only, and host paths are not mounted.

func (*Harness) Context

func (h *Harness) Context(d time.Duration) context.Context

Context returns a context that ends after d or when the test ends.

func (*Harness) EnsureImage

func (h *Harness) EnsureImage(ctx context.Context, ref string)

EnsureImage makes sure an image is there, pulling it when it is not. It refuses an image that PinnedImages does not list.

func (*Harness) ImageName

func (h *Harness) ImageName() string

ImageName returns emetos-test-<random>:latest, the name of an image a test builds with the CLI. The image has to carry the label in its Dockerfile (LABEL dev.emetos.test=1); RemoveImageAtEnd removes it by that name when the test ends, after looking at the label.

func (*Harness) Leftovers

func (h *Harness) Leftovers(ctx context.Context) ([]string, error)

Leftovers lists the labelled containers, networks, volumes, images and pods that exist now, as "container emetos-test-ab12". Only the label filter is used to list, so nothing of the user's is looked at.

func (*Harness) Network

func (h *Harness) Network(ctx context.Context) string

Network makes a labelled network and removes it when the test ends. It returns its name.

func (*Harness) Pod

func (h *Harness) Pod(ctx context.Context) string

Pod makes a labelled pod (Podman) and removes it, with its containers, when the test ends. It returns its name.

func (*Harness) RemoveImageAtEnd

func (h *Harness) RemoveImageAtEnd(name string)

RemoveImageAtEnd registers the removal of an image the test builds, by its name, for when the test ends (also when it fails). The image is looked at first and is removed only when it carries the label: a name that is the user's own is refused.

func (*Harness) StartedContainer

func (h *Harness) StartedContainer(ctx context.Context, spec containerapi.ContainerSpec) string

StartedContainer is Container followed by the start.

func (*Harness) Volume

func (h *Harness) Volume(ctx context.Context) string

Volume makes a labelled volume and removes it when the test ends. It returns its name.

type Request

type Request struct {
	// Method and Path are the request line with the version prefix cut off ("/containers/json", "/libpod/pods/json").
	Method, Path string
	// Prefix is the version part that was cut off ("/v1.47", "/v4.0.0"); empty when the path had none.
	Prefix string
	// Query is the raw query.
	Query string
	// Body is the request body (JSON of a create call).
	Body []byte
}

Request is a request the Fake received.

Directories

Path Synopsis
Command fakeengine serves the fake Docker engine of containerapitest on a Unix socket, with a scenario of containers, images, networks and volumes, until it gets SIGINT or SIGTERM.
Command fakeengine serves the fake Docker engine of containerapitest on a Unix socket, with a scenario of containers, images, networks and volumes, until it gets SIGINT or SIGTERM.

Jump to

Keyboard shortcuts

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