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
- Variables
- func Certificates(t testing.TB) (caPEM, certPEM, keyPEM []byte)
- func IsPinned(ref string) bool
- func LabelFilter() containerapi.Filters
- func RandomName(t testing.TB) string
- func TestEngine(t *testing.T, h *Harness)
- type Fake
- func (f *Fake) AddContainer(name, image string, labels map[string]string, state string) string
- func (f *Fake) AddImage(size int64, tags ...string) string
- func (f *Fake) AddLabelledImage(size int64, labels map[string]string, tags ...string) string
- func (f *Fake) Client() *containerapi.Client
- func (f *Fake) Close()
- func (f *Fake) Emit(ev containerapi.Event)
- func (f *Fake) Endpoint() containerapi.Endpoint
- func (f *Fake) FailNext(method, pathContains string, status int, message string)
- func (f *Fake) FailPull(ref, message string)
- func (f *Fake) Handler() http.Handler
- func (f *Fake) Kind() containerapi.Kind
- func (f *Fake) Log(id string, stream containerapi.Stream, text string)
- func (f *Fake) Override(method, pathContains string, h http.HandlerFunc)
- func (f *Fake) Requests() []Request
- func (f *Fake) ResetRequests()
- func (f *Fake) SetExec(fn func(cmd []string) (stdout, stderr string, exit int))
- func (f *Fake) SetVolumeSize(name string, size int64)
- func (f *Fake) Socket() string
- type FakeOptions
- type Harness
- func (h *Harness) Container(ctx context.Context, spec containerapi.ContainerSpec) string
- func (h *Harness) Context(d time.Duration) context.Context
- func (h *Harness) EnsureImage(ctx context.Context, ref string)
- func (h *Harness) ImageName() string
- func (h *Harness) Leftovers(ctx context.Context) ([]string, error)
- func (h *Harness) Network(ctx context.Context) string
- func (h *Harness) Pod(ctx context.Context) string
- func (h *Harness) RemoveImageAtEnd(name string)
- func (h *Harness) StartedContainer(ctx context.Context, spec containerapi.ContainerSpec) string
- func (h *Harness) Volume(ctx context.Context) string
- type Request
Constants ¶
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.
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 ¶
var PinnedImages = []string{ImageBusyBox, ImagePostgres, ImageMySQL, ImageMariaDB, ImageMongoDB, ImageRedis}
PinnedImages lists every image the real-engine tests may pull.
Functions ¶
func Certificates ¶
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 LabelFilter ¶
func LabelFilter() containerapi.Filters
LabelFilter is the filter that lists only the things made by tests.
func TestEngine ¶
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 ¶
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 ¶
AddImage puts an image into the Fake under the given names and returns its id.
func (*Fake) AddLabelledImage ¶
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 ¶
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 ¶
FailPull makes pulling ref fail with message in the progress stream, as a registry error does.
func (*Fake) 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) ResetRequests ¶
func (f *Fake) ResetRequests()
ResetRequests forgets the requests received so far.
func (*Fake) SetExec ¶
SetExec sets the function that runs a command for exec; nil goes back to the little shell.
func (*Fake) SetVolumeSize ¶
SetVolumeSize sets the size "system df" reports for a volume.
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 ¶
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 ¶
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) EnsureImage ¶
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 ¶
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 ¶
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 ¶
Network makes a labelled network and removes it when the test ends. It returns its name.
func (*Harness) Pod ¶
Pod makes a labelled pod (Podman) and removes it, with its containers, when the test ends. It returns its name.
func (*Harness) RemoveImageAtEnd ¶
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.
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.
Source Files
¶
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. |