Documentation
¶
Overview ¶
Package mockingmoby is a very minimalist Docker mock client designed for simple unit tests in the whalewatcher package. Only limited listing and inspecting containers is supported, as well as streaming container start and die events. Moreover, only very few container properties are mocked, just to the extend needed in the whalewatcher package.
But in contrast to using a real Docker client in unit tests mockingmoby offers service API hooks which get called at the beginning and end of service API calls. This can be used to synchronize certain "asynchronous" events with exact logical timing without the need to instrument production code under test with hooks. The service API hooks are passed in via (service) context values.
The mocked containers are not created and destroyed using the standard Docker client service API but instead using AddContainer and RemoveContainer. In addition, a mock container can be "stopped" using StopContainer, so it gets into the "exited" state but still exists.
Index ¶
- Constants
- Variables
- func MockAttributes(c MockedContainer) map[string]string
- func WithHook(ctx context.Context, key HookKey, hook Hook) context.Context
- type Hook
- type HookKey
- type MockedContainer
- type MockedContainerState
- type MockingMoby
- func (mm *MockingMoby) AddContainer(c MockedContainer)
- func (mm *MockingMoby) Close() error
- func (mm *MockingMoby) ContainerAttach(context.Context, string, client.ContainerAttachOptions) (client.ContainerAttachResult, error)
- func (mm *MockingMoby) ContainerCommit(context.Context, string, client.ContainerCommitOptions) (client.ContainerCommitResult, error)
- func (mm *MockingMoby) ContainerCreate(context.Context, client.ContainerCreateOptions) (client.ContainerCreateResult, error)
- func (mm *MockingMoby) ContainerDiff(context.Context, string, client.ContainerDiffOptions) (client.ContainerDiffResult, error)
- func (mm *MockingMoby) ContainerExport(context.Context, string, client.ContainerExportOptions) (client.ContainerExportResult, error)
- func (mm *MockingMoby) ContainerInspect(ctx context.Context, nameorid string, options client.ContainerInspectOptions) (client.ContainerInspectResult, error)
- func (mm *MockingMoby) ContainerKill(context.Context, string, client.ContainerKillOptions) (client.ContainerKillResult, error)
- func (mm *MockingMoby) ContainerList(ctx context.Context, options client.ContainerListOptions) (client.ContainerListResult, error)
- func (mm *MockingMoby) ContainerLogs(context.Context, string, client.ContainerLogsOptions) (client.ContainerLogsResult, error)
- func (mm *MockingMoby) ContainerPause(context.Context, string, client.ContainerPauseOptions) (client.ContainerPauseResult, error)
- func (mm *MockingMoby) ContainerRemove(context.Context, string, client.ContainerRemoveOptions) (client.ContainerRemoveResult, error)
- func (mm *MockingMoby) ContainerRename(context.Context, string, client.ContainerRenameOptions) (client.ContainerRenameResult, error)
- func (mm *MockingMoby) ContainerResize(context.Context, string, client.ContainerResizeOptions) (client.ContainerResizeResult, error)
- func (mm *MockingMoby) ContainerRestart(context.Context, string, client.ContainerRestartOptions) (client.ContainerRestartResult, error)
- func (mm *MockingMoby) ContainerStart(context.Context, string, client.ContainerStartOptions) (client.ContainerStartResult, error)
- func (mm *MockingMoby) ContainerStatPath(context.Context, string, client.ContainerStatPathOptions) (client.ContainerStatPathResult, error)
- func (mm *MockingMoby) ContainerStats(context.Context, string, client.ContainerStatsOptions) (client.ContainerStatsResult, error)
- func (mm *MockingMoby) ContainerStop(context.Context, string, client.ContainerStopOptions) (client.ContainerStopResult, error)
- func (mm *MockingMoby) ContainerTop(context.Context, string, client.ContainerTopOptions) (client.ContainerTopResult, error)
- func (mm *MockingMoby) ContainerUnpause(context.Context, string, client.ContainerUnpauseOptions) (client.ContainerUnpauseResult, error)
- func (mm *MockingMoby) ContainerUpdate(context.Context, string, client.ContainerUpdateOptions) (client.ContainerUpdateResult, error)
- func (mm *MockingMoby) ContainerWait(context.Context, string, client.ContainerWaitOptions) client.ContainerWaitResult
- func (mm *MockingMoby) ContainersPrune(context.Context, client.ContainerPruneOptions) (client.ContainerPruneResult, error)
- func (mm *MockingMoby) CopyFromContainer(context.Context, string, client.CopyFromContainerOptions) (client.CopyFromContainerResult, error)
- func (mm *MockingMoby) CopyToContainer(context.Context, string, client.CopyToContainerOptions) (client.CopyToContainerResult, error)
- func (mm *MockingMoby) DaemonHost() string
- func (mm *MockingMoby) DiskUsage(ctx context.Context, options client.DiskUsageOptions) (client.DiskUsageResult, error)
- func (mm *MockingMoby) Events(ctx context.Context, options client.EventsListOptions) client.EventsResult
- func (mm *MockingMoby) Info(ctx context.Context, options client.InfoOptions) (client.SystemInfoResult, error)
- func (mm *MockingMoby) PauseContainer(nameorid string)
- func (mm *MockingMoby) Ping(ctx context.Context, options client.PingOptions) (client.PingResult, error)
- func (mm *MockingMoby) RegistryLogin(ctx context.Context, auth client.RegistryLoginOptions) (client.RegistryLoginResult, error)
- func (mm *MockingMoby) RemoveContainer(nameorid string)
- func (mm *MockingMoby) StopContainer(nameorid string)
- func (mm *MockingMoby) StopEvents()
- func (mm *MockingMoby) UnpauseContainer(nameorid string)
- type OpaqueWrappingError
Constants ¶
const ( ContainerListPre = HookKey("containerlistpre") ContainerListPost = HookKey("containerlistpost") ContainerInspectPre = HookKey("containerinspectpre") ContainerInspectPost = HookKey("containerinspectpost") )
The available API pre/post hook keys.
Variables ¶
var ErrEventStreamStopped = errors.New("event stream stopped")
ErrEventStreamStopped is the error send via the error stream after invoking StopEvents on a MockingMoby.
var MockedContainerStates = map[MockedContainerState]container.ContainerState{ MockedCreated: "created", MockedRunning: "running", MockedPaused: "paused", MockedDead: "dead", MockedExited: "exited", }
MockedContainerStates maps the states of a mocked container to Docker's container status strings that is better suited for code checks (no chatty additions and content variations).
var MockedStates = map[MockedContainerState]string{ MockedCreated: "", MockedRunning: "up for ages", MockedPaused: "pausing a moment", MockedDead: "just sleeping", MockedExited: "exit 42", }
MockedStates maps the states of a mocked container to Docker's textual descriptive (and slightly chatty) container states, suitable for display to hoomans.
Functions ¶
func MockAttributes ¶
func MockAttributes(c MockedContainer) map[string]string
MockAttributes returns a mocked attributes map for the specified mock container, based on the container's labels and additional attributes (namely, the container name as opposed to its ID). The attributes map is suitable for direct emission in the Actor fields of Docker events.
Types ¶
type Hook ¶
Hook is a hook function called in the processing of a service API request. Hook functions get passed HookKeys for the specific types of API requests. Hooks might return errors in order to either early abort an API request or make it override the API error return value (where applicable).
Please note: hooks never get called for API requests that were already called with a "Done" context (cancelled or timed out).
type MockedContainer ¶
type MockedContainer struct {
ID string // unique identifier of container
Name string // name of container without any prefixing "/"
Status MockedContainerState // container status (without any thrills)
PID int // PID of initial container process if container is "alive"
Labels map[string]string // container labels
}
MockedContainer is our very, very limited knowledge about a mocked container; it just stores the minimum of information we need in mocking our own unit tests.
type MockedContainerState ¶
type MockedContainerState int
MockedContainerState is a compressed, only-essentials, no-bulls version of Docker's types.ContainerStatus.
const ( MockedCreated MockedContainerState = iota MockedRunning MockedPaused MockedDead MockedExited )
The available states of a mocked container.
type MockingMoby ¶
type MockingMoby struct {
client.ContainerAPIClient
client.SystemAPIClient
// contains filtered or unexported fields
}
MockingMoby is a mock Docker client implementing only listing all containers, inspecting them (limited information only) and receiving container-related events. All other service API methods will return a not-implemented error when tried.
Please note that only a single call to the Events API method is supported per mock client instance.
func NewMockingMoby ¶
func NewMockingMoby() *MockingMoby
NewMockingMoby returns a new instance of a mock Docker client.
func (*MockingMoby) AddContainer ¶
func (mm *MockingMoby) AddContainer(c MockedContainer)
AddContainer adds a mocked container and optionally emits a container event if the container is in running or paused states.
func (*MockingMoby) Close ¶
func (mm *MockingMoby) Close() error
Close closes the mock client, releasing its internal resources.
func (*MockingMoby) ContainerAttach ¶
func (mm *MockingMoby) ContainerAttach(context.Context, string, client.ContainerAttachOptions) (client.ContainerAttachResult, error)
ContainerAttach is not implemented.
func (*MockingMoby) ContainerCommit ¶
func (mm *MockingMoby) ContainerCommit(context.Context, string, client.ContainerCommitOptions) (client.ContainerCommitResult, error)
ContainerCommit is not implemented.
func (*MockingMoby) ContainerCreate ¶
func (mm *MockingMoby) ContainerCreate(context.Context, client.ContainerCreateOptions) (client.ContainerCreateResult, error)
ContainerCreate is not implemented.
func (*MockingMoby) ContainerDiff ¶
func (mm *MockingMoby) ContainerDiff(context.Context, string, client.ContainerDiffOptions) (client.ContainerDiffResult, error)
ContainerDiff is not implemented.
func (*MockingMoby) ContainerExport ¶
func (mm *MockingMoby) ContainerExport(context.Context, string, client.ContainerExportOptions) (client.ContainerExportResult, error)
ContainerExport is not implemented.
func (*MockingMoby) ContainerInspect ¶
func (mm *MockingMoby) ContainerInspect(ctx context.Context, nameorid string, options client.ContainerInspectOptions) (client.ContainerInspectResult, error)
ContainerInspect returns details about a particular mocked container.
func (*MockingMoby) ContainerKill ¶
func (mm *MockingMoby) ContainerKill(context.Context, string, client.ContainerKillOptions) (client.ContainerKillResult, error)
ContainerKill is not implemented.
func (*MockingMoby) ContainerList ¶
func (mm *MockingMoby) ContainerList(ctx context.Context, options client.ContainerListOptions) (client.ContainerListResult, error)
ContainerList returns the list of currently known containers, ignoring any list options.
func (*MockingMoby) ContainerLogs ¶
func (mm *MockingMoby) ContainerLogs(context.Context, string, client.ContainerLogsOptions) (client.ContainerLogsResult, error)
ContainerLogs is not implemented.
func (*MockingMoby) ContainerPause ¶
func (mm *MockingMoby) ContainerPause(context.Context, string, client.ContainerPauseOptions) (client.ContainerPauseResult, error)
ContainerPause is not implemented.
func (*MockingMoby) ContainerRemove ¶
func (mm *MockingMoby) ContainerRemove(context.Context, string, client.ContainerRemoveOptions) (client.ContainerRemoveResult, error)
ContainerRemove is not implemented.
func (*MockingMoby) ContainerRename ¶
func (mm *MockingMoby) ContainerRename(context.Context, string, client.ContainerRenameOptions) (client.ContainerRenameResult, error)
ContainerRename is not implemented.
func (*MockingMoby) ContainerResize ¶
func (mm *MockingMoby) ContainerResize(context.Context, string, client.ContainerResizeOptions) (client.ContainerResizeResult, error)
ContainerResize is not implemented.
func (*MockingMoby) ContainerRestart ¶
func (mm *MockingMoby) ContainerRestart(context.Context, string, client.ContainerRestartOptions) (client.ContainerRestartResult, error)
ContainerRestart is not implemented.
func (*MockingMoby) ContainerStart ¶
func (mm *MockingMoby) ContainerStart(context.Context, string, client.ContainerStartOptions) (client.ContainerStartResult, error)
ContainerStart is not implemented.
func (*MockingMoby) ContainerStatPath ¶
func (mm *MockingMoby) ContainerStatPath(context.Context, string, client.ContainerStatPathOptions) (client.ContainerStatPathResult, error)
ContainerStatPath is not implemented.
func (*MockingMoby) ContainerStats ¶
func (mm *MockingMoby) ContainerStats(context.Context, string, client.ContainerStatsOptions) (client.ContainerStatsResult, error)
ContainerStats is not implemented.
func (*MockingMoby) ContainerStop ¶
func (mm *MockingMoby) ContainerStop(context.Context, string, client.ContainerStopOptions) (client.ContainerStopResult, error)
ContainerStop is not implemented.
func (*MockingMoby) ContainerTop ¶
func (mm *MockingMoby) ContainerTop(context.Context, string, client.ContainerTopOptions) (client.ContainerTopResult, error)
ContainerTop is not implemented.
func (*MockingMoby) ContainerUnpause ¶
func (mm *MockingMoby) ContainerUnpause(context.Context, string, client.ContainerUnpauseOptions) (client.ContainerUnpauseResult, error)
ContainerUnpause is not implemented.
func (*MockingMoby) ContainerUpdate ¶
func (mm *MockingMoby) ContainerUpdate(context.Context, string, client.ContainerUpdateOptions) (client.ContainerUpdateResult, error)
ContainerUpdate is not implemented.
func (*MockingMoby) ContainerWait ¶
func (mm *MockingMoby) ContainerWait(context.Context, string, client.ContainerWaitOptions) client.ContainerWaitResult
ContainerWait is not implemented.
func (*MockingMoby) ContainersPrune ¶
func (mm *MockingMoby) ContainersPrune(context.Context, client.ContainerPruneOptions) (client.ContainerPruneResult, error)
ContainersPrune is not implemented.
func (*MockingMoby) CopyFromContainer ¶
func (mm *MockingMoby) CopyFromContainer(context.Context, string, client.CopyFromContainerOptions) (client.CopyFromContainerResult, error)
CopyFromContainer is not implemented.
func (*MockingMoby) CopyToContainer ¶
func (mm *MockingMoby) CopyToContainer(context.Context, string, client.CopyToContainerOptions) (client.CopyToContainerResult, error)
CopyToContainer is not implemented.
func (*MockingMoby) DaemonHost ¶
func (mm *MockingMoby) DaemonHost() string
DaemonHost returns the host address used by the client
func (*MockingMoby) DiskUsage ¶
func (mm *MockingMoby) DiskUsage(ctx context.Context, options client.DiskUsageOptions) (client.DiskUsageResult, error)
DiskUsage is not implemented.
func (*MockingMoby) Events ¶
func (mm *MockingMoby) Events(ctx context.Context, options client.EventsListOptions) client.EventsResult
Events returns a stream of fake events. It ignores all options, but checks ctx for being Done (with or without any error) and then mirrors the context error to the (events) error channel returned by Events. After an error the event channel will be closed automatically.
Please note that only a single call to the Events API method is supported per mock client instance.
func (*MockingMoby) Info ¶
func (mm *MockingMoby) Info(ctx context.Context, options client.InfoOptions) (client.SystemInfoResult, error)
Info returns engine information, consisting only of a fake engine ID, but nothing else.
func (*MockingMoby) PauseContainer ¶
func (mm *MockingMoby) PauseContainer(nameorid string)
PauseContainer pauses a container, if currently running, and emits a container pause event.
func (*MockingMoby) Ping ¶
func (mm *MockingMoby) Ping(ctx context.Context, options client.PingOptions) (client.PingResult, error)
Ping is not implemented.
func (*MockingMoby) RegistryLogin ¶
func (mm *MockingMoby) RegistryLogin(ctx context.Context, auth client.RegistryLoginOptions) (client.RegistryLoginResult, error)
RegistryLogin is not implemented.
func (*MockingMoby) RemoveContainer ¶
func (mm *MockingMoby) RemoveContainer(nameorid string)
RemoveContainer removes a mocked container and emits a container event if the container was in running or paused states.
func (*MockingMoby) StopContainer ¶
func (mm *MockingMoby) StopContainer(nameorid string)
StopContainer stops a mocked container, but does not remove it yet. It emits a container event if the container was in running or paused state.
func (*MockingMoby) StopEvents ¶
func (mm *MockingMoby) StopEvents()
StopEvents closes down streaming events with an error on the error channel; it is used in unit tests to simulate event stream errors other than a cancelled context.
func (*MockingMoby) UnpauseContainer ¶
func (mm *MockingMoby) UnpauseContainer(nameorid string)
UnpauseContainer unpauses a container, if currently paused, and emits a container unpause event.
type OpaqueWrappingError ¶
type OpaqueWrappingError struct {
// contains filtered or unexported fields
}
OpaqueWrappingError wraps a containerd error with a custom error message, where the wrapped container error can be unwrapped, but unlike fmt.Errorf the wrapped error's Error message is never used.
func (*OpaqueWrappingError) Error ¶
func (e *OpaqueWrappingError) Error() string
func (*OpaqueWrappingError) Unwrap ¶
func (e *OpaqueWrappingError) Unwrap() error