mockingmoby

package
v0.8.1 Latest Latest
Warning

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

Go to latest
Published: Mar 19, 2023 License: Apache-2.0 Imports: 14 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 MockedStates = map[MockedContainerStatus]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.

View Source
var MockedStatus = map[MockedContainerStatus]string{
	MockedCreated: "created",
	MockedRunning: "running",
	MockedPaused:  "paused",
	MockedDead:    "dead",
	MockedExited:  "exited",
}

MockedStatus 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).

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 MockedContainerStatus // 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 MockedContainerStatus

type MockedContainerStatus int

MockedContainerStatus is a compressed, only-essentials, no-bulls version of Docker's types.ContainerStatus.

const (
	MockedCreated MockedContainerStatus = 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(ctx context.Context, container string, options types.ContainerAttachOptions) (types.HijackedResponse, error)

ContainerAttach is not implemented.

func (*MockingMoby) ContainerCommit

func (mm *MockingMoby) ContainerCommit(ctx context.Context, container string, options types.ContainerCommitOptions) (types.IDResponse, error)

ContainerCommit is not implemented.

func (*MockingMoby) ContainerCreate

func (mm *MockingMoby) ContainerCreate(ctx context.Context, config *containertypes.Config, hostConfig *containertypes.HostConfig, networkingConfig *networktypes.NetworkingConfig, platform *specs.Platform, containerName string) (containertypes.ContainerCreateCreatedBody, error)

ContainerCreate is not implemented.

func (*MockingMoby) ContainerDiff

func (mm *MockingMoby) ContainerDiff(ctx context.Context, container string) ([]containertypes.ContainerChangeResponseItem, error)

ContainerDiff is not implemented.

func (*MockingMoby) ContainerExecAttach

func (mm *MockingMoby) ContainerExecAttach(ctx context.Context, execID string, config types.ExecStartCheck) (types.HijackedResponse, error)

ContainerExecAttach is not implemented.

func (*MockingMoby) ContainerExecCreate

func (mm *MockingMoby) ContainerExecCreate(ctx context.Context, container string, config types.ExecConfig) (types.IDResponse, error)

ContainerExecCreate is not implemented.

func (*MockingMoby) ContainerExecInspect

func (mm *MockingMoby) ContainerExecInspect(ctx context.Context, execID string) (types.ContainerExecInspect, error)

ContainerExecInspect is not implemented.

func (*MockingMoby) ContainerExecResize

func (mm *MockingMoby) ContainerExecResize(ctx context.Context, execID string, options types.ResizeOptions) error

ContainerExecResize is not implemented.

func (*MockingMoby) ContainerExecStart

func (mm *MockingMoby) ContainerExecStart(ctx context.Context, execID string, config types.ExecStartCheck) error

ContainerExecStart is not implemented.

func (*MockingMoby) ContainerExport

func (mm *MockingMoby) ContainerExport(ctx context.Context, container string) (io.ReadCloser, error)

ContainerExport is not implemented.

func (*MockingMoby) ContainerInspect

func (mm *MockingMoby) ContainerInspect(ctx context.Context, nameorid string) (types.ContainerJSON, error)

ContainerInspect returns details about a particular mocked container.

func (*MockingMoby) ContainerInspectWithRaw

func (mm *MockingMoby) ContainerInspectWithRaw(ctx context.Context, container string, getSize bool) (types.ContainerJSON, []byte, error)

ContainerInspectWithRaw is not implemented.

func (*MockingMoby) ContainerKill

func (mm *MockingMoby) ContainerKill(ctx context.Context, container, signal string) error

ContainerKill is not implemented.

func (*MockingMoby) ContainerList

func (mm *MockingMoby) ContainerList(ctx context.Context, options types.ContainerListOptions) ([]types.Container, error)

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

func (*MockingMoby) ContainerLogs

func (mm *MockingMoby) ContainerLogs(ctx context.Context, container string, options types.ContainerLogsOptions) (io.ReadCloser, error)

ContainerLogs is not implemented.

func (*MockingMoby) ContainerPause

func (mm *MockingMoby) ContainerPause(ctx context.Context, container string) error

ContainerPause is not implemented.

func (*MockingMoby) ContainerRemove

func (mm *MockingMoby) ContainerRemove(ctx context.Context, container string, options types.ContainerRemoveOptions) error

ContainerRemove is not implemented.

func (*MockingMoby) ContainerRename

func (mm *MockingMoby) ContainerRename(ctx context.Context, container, newContainerName string) error

ContainerRename is not implemented.

func (*MockingMoby) ContainerResize

func (mm *MockingMoby) ContainerResize(ctx context.Context, container string, options types.ResizeOptions) error

ContainerResize is not implemented.

func (*MockingMoby) ContainerRestart

func (mm *MockingMoby) ContainerRestart(ctx context.Context, container string, options container.StopOptions) error

ContainerRestart is not implemented.

func (*MockingMoby) ContainerStart

func (mm *MockingMoby) ContainerStart(ctx context.Context, container string, options types.ContainerStartOptions) error

ContainerStart is not implemented.

func (*MockingMoby) ContainerStatPath

func (mm *MockingMoby) ContainerStatPath(ctx context.Context, container, path string) (types.ContainerPathStat, error)

ContainerStatPath is not implemented.

func (*MockingMoby) ContainerStats

func (mm *MockingMoby) ContainerStats(ctx context.Context, container string, stream bool) (types.ContainerStats, error)

ContainerStats is not implemented.

func (*MockingMoby) ContainerStatsOneShot

func (mm *MockingMoby) ContainerStatsOneShot(ctx context.Context, container string) (types.ContainerStats, error)

ContainerStatsOneShot is not implemented.

func (*MockingMoby) ContainerStop

func (mm *MockingMoby) ContainerStop(ctx context.Context, container string, options container.StopOptions) error

ContainerStop is not implemented.

func (*MockingMoby) ContainerTop

func (mm *MockingMoby) ContainerTop(ctx context.Context, container string, arguments []string) (containertypes.ContainerTopOKBody, error)

ContainerTop is not implemented.

func (*MockingMoby) ContainerUnpause

func (mm *MockingMoby) ContainerUnpause(ctx context.Context, container string) error

ContainerUnpause is not implemented.

func (*MockingMoby) ContainerUpdate

func (mm *MockingMoby) ContainerUpdate(ctx context.Context, container string, updateConfig containertypes.UpdateConfig) (containertypes.ContainerUpdateOKBody, error)

ContainerUpdate is not implemented.

func (*MockingMoby) ContainerWait

func (mm *MockingMoby) ContainerWait(ctx context.Context, container string, condition containertypes.WaitCondition) (<-chan containertypes.ContainerWaitOKBody, <-chan error)

ContainerWait is not implemented.

func (*MockingMoby) ContainersPrune

func (mm *MockingMoby) ContainersPrune(ctx context.Context, pruneFilters filters.Args) (types.ContainersPruneReport, error)

ContainersPrune is not implemented.

func (*MockingMoby) CopyFromContainer

func (mm *MockingMoby) CopyFromContainer(ctx context.Context, container, srcPath string) (io.ReadCloser, types.ContainerPathStat, error)

CopyFromContainer is not implemented.

func (*MockingMoby) CopyToContainer

func (mm *MockingMoby) CopyToContainer(ctx context.Context, container, path string, content io.Reader, options types.CopyToContainerOptions) error

CopyToContainer is not implemented.

func (*MockingMoby) DaemonHost added in v0.4.0

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 types.DiskUsageOptions) (types.DiskUsage, error)

DiskUsage is not implemented.

func (*MockingMoby) Events

func (mm *MockingMoby) Events(ctx context.Context, options types.EventsOptions) (<-chan events.Message, <-chan error)

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) (types.Info, error)

Info returns engine information, consisting only of a fake engine ID, but nothing else.

func (*MockingMoby) NegotiateAPIVersion added in v0.7.1

func (mm *MockingMoby) NegotiateAPIVersion(ctx context.Context)

NegotiateAPIVersion is a mock no-op.

func (*MockingMoby) PauseContainer added in v0.3.0

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) (types.Ping, error)

Ping is not implemented.

func (*MockingMoby) RegistryLogin

func (mm *MockingMoby) RegistryLogin(ctx context.Context, auth types.AuthConfig) (registry.AuthenticateOKBody, 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 added in v0.3.0

func (mm *MockingMoby) UnpauseContainer(nameorid string)

UnpauseContainer unpauses a container, if currently paused, and emits a container unpause event.

Jump to

Keyboard shortcuts

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