harness

package
v1.0.10 Latest Latest
Warning

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

Go to latest
Published: Nov 12, 2026 License: GPL-3.0 Imports: 10 Imported by: 0

Documentation

Overview

Package harness holds the fakes the end-to-end tests drive the real app against: a stateful Docker Engine API, canned earning providers and a canned exchange-rate feed.

Everything here listens on 127.0.0.1 over TCP, never a unix socket and never a named pipe, so the same tests run unchanged on linux, macOS and windows with no container runtime installed, no provider accounts and no internet.

Index

Constants

This section is empty.

Variables

This section is empty.

Functions

This section is empty.

Types

type Container

type Container struct {
	ID     string
	Name   string
	Image  string
	State  string
	Labels map[string]string
	Env    []string
	Cmd    []string
	// Entrypoint and StopTimeout are what the deploy asked Docker for; nil means
	// it asked for neither (the image's entrypoint, the daemon's 10 seconds).
	Entrypoint  []string
	StopTimeout *int
	Mounts      []Mount
	// Runtime, NetworkMode and PortBindings are the HostConfig fields of the same
	// names, as the deploy sent them.
	Runtime      string
	NetworkMode  string
	PortBindings map[string][]PortBinding
	// RestartPolicy is the HostConfig restart policy's name, as the deploy sent it or
	// a later container update set it.
	RestartPolicy string
	// ExtraNetworks are networks a test attached it to beside its network mode.
	ExtraNetworks []string
	// StateError is inspect's State.Error: why Docker's last start of it failed.
	// A start that works clears it.
	StateError string
	// contains filtered or unexported fields
}

Container is one container the fake daemon is holding, as the tests want to read it: the state a user would see plus the pieces a deploy is supposed to carry across (image, labels, environment, command, named volumes).

func (Container) VolumeNames

func (c Container) VolumeNames() []string

VolumeNames lists the names of the container's named volumes, in the order the deploy asked for them.

type Docker

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

Docker is a fake Docker Engine API with state. It answers the endpoints the app actually calls, keeps containers, images and volumes between calls, and records every request so a test can assert on the ORDER of what happened — which is the part a return value cannot show (a deploy that fails and a deploy that fails after destroying the running container both just return an error).

func NewDocker

func NewDocker() *Docker

NewDocker starts the fake daemon on a loopback TCP port.

func (*Docker) AttachNetwork added in v1.0.5

func (d *Docker) AttachNetwork(name, network string)

AttachNetwork reports the container attached to one more network.

func (*Docker) BeforeCall added in v1.0.9

func (d *Docker) BeforeCall(call string, hook func())

BeforeCall makes the next request seen as call ("METHOD /path") run hook before it is answered, outside the fake's lock, so the hook can call the fake. A container's path may name it by name or by id: "POST /containers/cashpilot-x/start" matches a start of that container under its id too. A test uses it to act at an exact point in the middle of an operation.

func (*Docker) Calls

func (d *Docker) Calls() []string

Calls returns the requests seen so far as "METHOD /path", in order.

func (*Docker) CallsWithQuery

func (d *Docker) CallsWithQuery() []string

CallsWithQuery is Calls with the query string kept, as "METHOD /path?query". The query is where the Engine API carries the options that change what a call destroys — removing a container with v=1 takes its anonymous volumes with it, v=0 leaves them — so a test about what survived needs to see it.

func (*Docker) ClearFailures added in v1.0.9

func (d *Docker) ClearFailures()

ClearFailures drops every inspect, stop, start, rename and remove failure set so far, the way a daemon that was struggling recovers.

func (*Docker) Close

func (d *Docker) Close()

Close shuts the fake down.

func (*Docker) Container

func (d *Docker) Container(nameOrID string) (Container, bool)

Container returns the container with this name (or id).

func (*Docker) FailAfterCreate added in v1.0.9

func (d *Docker) FailAfterCreate(name, message string)

FailAfterCreate makes the next create under this name make the container and still answer an error with message, as a create whose reply never arrived.

func (*Docker) FailCreate added in v1.0.9

func (d *Docker) FailCreate(name, message string)

FailCreate makes the next create of a container under this name fail with message, the way Docker refuses a create.

func (*Docker) FailInspect added in v1.0.5

func (d *Docker) FailInspect(name string)

FailInspect makes inspecting this container fail with a server error.

func (*Docker) FailInspectFrom added in v1.0.9

func (d *Docker) FailInspectFrom(nameOrID string, n int)

FailInspectFrom makes inspecting by exactly this name or id fail from the nth such inspect after this call on (n counts from 1). Unlike FailInspect it matches the request, not the container: an inspect of the same container under its other name or its id still works.

func (*Docker) FailRemove added in v1.0.9

func (d *Docker) FailRemove(name string)

FailRemove makes removing this container fail with a server error.

func (*Docker) FailRename added in v1.0.9

func (d *Docker) FailRename(name string)

FailRename makes renaming this container fail with a server error. The name is the one the container has when the rename is asked for.

func (*Docker) FailStart added in v1.0.8

func (d *Docker) FailStart(name string, fail bool)

FailStart makes starting this container fail with a server error (fail true), or work again (fail false).

func (*Docker) FailStartOfNext added in v1.0.9

func (d *Docker) FailStartOfNext(name, message string)

FailStartOfNext makes the next container created under this name fail every start with message, the way Docker refuses a container whose published port a host process holds. Containers that already exist are not affected.

func (*Docker) FailStop added in v1.0.5

func (d *Docker) FailStop(name string)

FailStop makes stopping this container fail with a server error.

func (*Docker) Host

func (d *Docker) Host() string

Host is the value to put in DOCKER_HOST so the app's own client finds this fake with no seam added to production code.

func (*Docker) Images

func (d *Docker) Images() []string

Images returns every image reference that was pulled, in order.

func (*Docker) Names

func (d *Docker) Names() []string

Names returns every container the fake holds, sorted.

func (*Docker) RemoveNetwork added in v1.0.5

func (d *Docker) RemoveNetwork(name string)

RemoveNetwork deletes a network behind the app's back, the way a user's `docker network rm` or a daemon reset would.

func (*Docker) Rename added in v1.0.9

func (d *Docker) Rename(name, to string)

Rename renames a container behind the app's back, the way a deploy that died midway leaves one under another name.

func (*Docker) ResetCalls

func (d *Docker) ResetCalls()

ResetCalls clears the recorded calls so a test can assert on one phase alone.

func (*Docker) SetDown

func (d *Docker) SetDown(down bool)

SetDown makes the daemon go away (every request has its connection dropped, the way a stopped daemon refuses one) or come back.

func (*Docker) SetDownAfter

func (d *Docker) SetDownAfter(call string)

SetDownAfter makes the daemon go away as soon as it has answered this call ("METHOD /path"), so a test can cut it off in the middle of an operation at a chosen point rather than hoping to hit the right moment.

func (*Docker) SetLabel added in v1.0.5

func (d *Docker) SetLabel(name, key, value string)

SetLabel sets (or, with value "", removes) one label of a container, the way a container made by an older version lacks a label a newer one sets.

func (*Docker) SetLogs

func (d *Docker) SetLogs(text string)

SetLogs replaces the log text the fake streams back.

func (*Docker) SetPullError

func (d *Docker) SetPullError(message string)

SetPullError makes the next image pull fail mid-stream with this message, the way a registry outage or a mistyped image does.

func (*Docker) SetRestartPolicy added in v1.0.8

func (d *Docker) SetRestartPolicy(name, policy string)

SetRestartPolicy sets a container's restart policy, the way a container made by an older version keeps the policy it was created with.

func (*Docker) SetState added in v1.0.5

func (d *Docker) SetState(name, state string)

SetState sets a container's state as Docker would report it ("restarting").

func (*Docker) SetStateError added in v1.0.8

func (d *Docker) SetStateError(name, message string)

SetStateError sets a container's State.Error, as Docker records a start that failed ("network ... not found").

func (*Docker) Volumes

func (d *Docker) Volumes() []string

Volumes returns every named volume the fake holds, sorted.

type Mount

type Mount struct {
	Source string
	Target string
}

Mount is one named volume attached to a container: the volume's name on the host and the path it appears at inside the container. Both halves are kept because both matter — the name is what an explicit volume delete addresses, and the path is how you tell an earner's identity data apart from a scratch directory, which is the difference between a safe delete and a device that has to be registered again.

type PortBinding added in v1.0.5

type PortBinding struct {
	HostIP   string `json:"HostIp"`
	HostPort string `json:"HostPort"`
}

PortBinding is one host binding of a published port.

type Providers

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

Providers is a stand-in for the earning platforms and the exchange-rate feeds. One loopback server answers for every hostname the collectors call, so a test can drive the real collectors — real request building, real auth round-trip, real JSON decode, real HTTP over TCP — with no accounts and no internet.

Requests reach it because Client() hands back an http.Client whose transport sends every request to this server while leaving the Host header alone; the handler routes on that Host, the way a real host would.

func NewProviders

func NewProviders() *Providers

NewProviders starts the fake platform and rate server on a loopback TCP port with a usable set of balances and rates already in place.

func (*Providers) Client

func (p *Providers) Client() *http.Client

Client is the http.Client to hand the collectors: it keeps their real, hardcoded URLs and simply delivers the requests here.

func (*Providers) Close

func (p *Providers) Close()

Close shuts the fake down.

func (*Providers) Hits

func (p *Providers) Hits(platform string) int

Hits reports how many requests a platform has had, so a test can prove a collector really went out to the network rather than reading a cache.

func (*Providers) SetBalance

func (p *Providers) SetBalance(platform string, amount float64)

SetBalance changes what a platform reports next time it is asked.

func (*Providers) SetCryptoUSD

func (p *Providers) SetCryptoUSD(coingeckoID string, usd float64)

SetCryptoUSD sets the dollar price of a token, keyed by its CoinGecko id.

func (*Providers) SetFailing

func (p *Providers) SetFailing(platform string, failing bool)

SetFailing makes a platform answer HTTP 500, the way an outage does.

func (*Providers) SetFiat

func (p *Providers) SetFiat(code string, rate float64)

SetFiat sets a USD -> fiat rate.

func (*Providers) URL

func (p *Providers) URL() string

URL is the base URL of the fake, for the rate feeds which take one directly.

Jump to

Keyboard shortcuts

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