v0.0.0-...-7c75973 Latest Latest

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

Go to latest
Published: Nov 8, 2020 License: BSD-2-Clause Imports: 23 Imported by: 0



Package testing provides a fake implementation of the Docker API, useful for testing purpose.



This section is empty.


This section is empty.


This section is empty.


type DockerServer

type DockerServer struct {
	// contains filtered or unexported fields

DockerServer represents a programmable, concurrent (not much), HTTP server implementing a fake version of the Docker remote API.

It can used in standalone mode, listening for connections or as an arbitrary HTTP handler.

For more details on the remote API, check http://goo.gl/G3plxW.

func NewServer

func NewServer(bind string, containerChan chan<- *docker.Container, hook func(*http.Request)) (*DockerServer, error)

NewServer returns a new instance of the fake server, in standalone mode. Use the method URL to get the URL of the server.

It receives the bind address (use for getting an available port on the host), a channel of containers and a hook function, that will be called on every request.

The fake server will send containers in the channel whenever the container changes its state, via the HTTP API (i.e.: create, start and stop). This channel may be nil, which means that the server won't notify on state changes.

func NewTLSServer

func NewTLSServer(bind string, containerChan chan<- *docker.Container, hook func(*http.Request), tlsConfig TLSConfig) (*DockerServer, error)

NewTLSServer creates and starts a TLS-enabled testing server.

func (*DockerServer) CustomHandler

func (s *DockerServer) CustomHandler(path string, handler http.Handler)

CustomHandler registers a custom handler for a specific path.

For example:

server.CustomHandler("/containers/json", http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
    http.Error(w, "Something wrong is not right", http.StatusInternalServerError)

func (*DockerServer) DefaultHandler

func (s *DockerServer) DefaultHandler() http.Handler

DefaultHandler returns default http.Handler mux, it allows customHandlers to call the default behavior if wanted.

func (*DockerServer) MutateContainer

func (s *DockerServer) MutateContainer(id string, state docker.State) error

MutateContainer changes the state of a container, returning an error if the given id does not match to any container "running" in the server.

func (*DockerServer) MutateTask

func (s *DockerServer) MutateTask(id string, newTask swarm.Task) error

MutateTask changes a task, returning an error if the given id does not match to any task in the server.

func (*DockerServer) PrepareExec

func (s *DockerServer) PrepareExec(id string, callback func())

PrepareExec adds a callback to a container exec in the fake server.

This function will be called whenever the given exec id is started, and the given exec id will remain in the "Running" start while the function is running, so it's useful for emulating an exec that runs for two seconds, for example:

opts := docker.CreateExecOptions{
    AttachStdin:  true,
    AttachStdout: true,
    AttachStderr: true,
    Tty:          true,
    Cmd:          []string{"/bin/bash", "-l"},
// Client points to a fake server.
exec, err := client.CreateExec(opts)
// handle error
server.PrepareExec(exec.ID, func() {time.Sleep(2 * time.Second)})
err = client.StartExec(exec.ID, docker.StartExecOptions{Tty: true}) // will block for 2 seconds
// handle error

func (*DockerServer) PrepareFailure

func (s *DockerServer) PrepareFailure(id string, urlRegexp string)

PrepareFailure adds a new expected failure based on a URL regexp it receives an id for the failure.

func (*DockerServer) PrepareMultiFailures

func (s *DockerServer) PrepareMultiFailures(id string, urlRegexp string)

PrepareMultiFailures enqueues a new expected failure based on a URL regexp it receives an id for the failure.

func (*DockerServer) PrepareStats

func (s *DockerServer) PrepareStats(id string, callback func(string) docker.Stats)

PrepareStats adds a callback that will be called for each container stats call.

This callback function will be called multiple times if stream is set to true when stats is called.

func (*DockerServer) ResetFailure

func (s *DockerServer) ResetFailure(id string)

ResetFailure removes an expected failure identified by the given id.

func (*DockerServer) ResetMultiFailures

func (s *DockerServer) ResetMultiFailures()

ResetMultiFailures removes all enqueued failures.

func (*DockerServer) ServeHTTP

func (s *DockerServer) ServeHTTP(w http.ResponseWriter, r *http.Request)

ServeHTTP handles HTTP requests sent to the server.

func (*DockerServer) SetHook

func (s *DockerServer) SetHook(hook func(*http.Request))

SetHook changes the hook function used by the server.

The hook function is a function called on every request.

func (*DockerServer) Stop

func (s *DockerServer) Stop()

Stop stops the server.

func (*DockerServer) SwarmAddress

func (s *DockerServer) SwarmAddress() string

SwarmAddress returns the address if there's a fake swarm server enabled.

func (*DockerServer) URL

func (s *DockerServer) URL() string

URL returns the HTTP URL of the server.

type TLSConfig

type TLSConfig struct {
	CertPath    string
	CertKeyPath string
	RootCAPath  string

TLSConfig is the set of options to start the TLS-enabled testing server.

Jump to

Keyboard shortcuts

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