mockingmoby

package
v2.0.0 Latest Latest
Warning

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

Go to latest
Published: Apr 24, 2026 License: Apache-2.0 Imports: 10 Imported by: 0

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

View Source
const (
	ContainerListPre     = HookKey("containerlistpre")
	ContainerListPost    = HookKey("containerlistpost")
	ContainerInspectPre  = HookKey("containerinspectpre")
	ContainerInspectPost = HookKey("containerinspectpost")
)

The available API pre/post hook keys.

Variables

View Source
var ErrEventStreamStopped = errors.New("event stream stopped")

ErrEventStreamStopped is the error send via the error stream after invoking StopEvents on a MockingMoby.

View Source
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).

View Source
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.

func WithHook

func WithHook(ctx context.Context, key HookKey, hook Hook) context.Context

WithHook returns a new context with the specific Hook added.

Types

type Hook

type Hook func(HookKey) error

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 HookKey

type HookKey string

HookKey identifies a particular API pre or post hook.

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

ContainerAttach is not implemented.

func (*MockingMoby) ContainerCommit

ContainerCommit is not implemented.

func (*MockingMoby) ContainerCreate

ContainerCreate is not implemented.

func (*MockingMoby) ContainerDiff

ContainerDiff is not implemented.

func (*MockingMoby) ContainerExport

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

ContainerKill is not implemented.

func (*MockingMoby) ContainerList

ContainerList returns the list of currently known containers, ignoring any list options.

func (*MockingMoby) ContainerLogs

ContainerLogs is not implemented.

func (*MockingMoby) ContainerPause

ContainerPause is not implemented.

func (*MockingMoby) ContainerRemove

ContainerRemove is not implemented.

func (*MockingMoby) ContainerRename

ContainerRename is not implemented.

func (*MockingMoby) ContainerResize

ContainerResize is not implemented.

func (*MockingMoby) ContainerRestart

ContainerRestart is not implemented.

func (*MockingMoby) ContainerStart

ContainerStart is not implemented.

func (*MockingMoby) ContainerStatPath

ContainerStatPath is not implemented.

func (*MockingMoby) ContainerStats

ContainerStats is not implemented.

func (*MockingMoby) ContainerStop

ContainerStop is not implemented.

func (*MockingMoby) ContainerTop

ContainerTop is not implemented.

func (*MockingMoby) ContainerUnpause

ContainerUnpause is not implemented.

func (*MockingMoby) ContainerUpdate

ContainerUpdate is not implemented.

func (*MockingMoby) ContainerWait

ContainerWait is not implemented.

func (*MockingMoby) ContainersPrune

ContainersPrune is not implemented.

func (*MockingMoby) CopyFromContainer

CopyFromContainer is not implemented.

func (*MockingMoby) CopyToContainer

CopyToContainer is not implemented.

func (*MockingMoby) DaemonHost

func (mm *MockingMoby) DaemonHost() string

DaemonHost returns the host address used by the client

func (*MockingMoby) DiskUsage

DiskUsage is not implemented.

func (*MockingMoby) Events

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

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

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

Jump to

Keyboard shortcuts

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