Documentation ¶
Overview ¶
Package testing provides a fake implementation of the Docker API, useful for testing purpose.
Index ¶
- type DockerServer
- func (s *DockerServer) CustomHandler(path string, handler http.Handler)
- func (s *DockerServer) DefaultHandler() http.Handler
- func (s *DockerServer) MutateContainer(id string, state docker.State) error
- func (s *DockerServer) PrepareExec(id string, callback func())
- func (s *DockerServer) PrepareFailure(id string, urlRegexp string)
- func (s *DockerServer) PrepareMultiFailures(id string, urlRegexp string)
- func (s *DockerServer) ResetFailure(id string)
- func (s *DockerServer) ResetMultiFailures()
- func (s *DockerServer) ServeHTTP(w http.ResponseWriter, r *http.Request)
- func (s *DockerServer) SetHook(hook func(*http.Request))
- func (s *DockerServer) Stop()
- func (s *DockerServer) URL() string
Constants ¶
This section is empty.
Variables ¶
This section is empty.
Functions ¶
This section is empty.
Types ¶
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 127.0.0.1:0 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 (*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) PrepareExec ¶ added in v0.16.0
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 ¶ added in v0.16.0
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) ResetFailure ¶
func (s *DockerServer) ResetFailure(id string)
ResetFailure removes an expected failure identified by the given id.
func (*DockerServer) ResetMultiFailures ¶ added in v0.16.0
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 ¶ added in v0.16.0
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) URL ¶
func (s *DockerServer) URL() string
URL returns the HTTP URL of the server.